aboutsummaryrefslogtreecommitdiffstats
path: root/net/core/sock.c
diff options
context:
space:
mode:
authorPavel Emelyanov <xemul@openvz.org>2007-11-01 03:33:50 -0400
committerDavid S. Miller <davem@davemloft.net>2007-11-01 03:33:50 -0400
commitc308c1b20e2eb7b13f200a7c18b3f23561318367 (patch)
tree1e0c1012b6b72d9fb9689f43e50ab8441b9ed4f3 /net/core/sock.c
parent1e2e6b89f1d3152da0606d23e65e8760bf62a4c3 (diff)
[NET]: Cleanup the allocation/freeing of the sock object
The sock object is allocated either from the generic cache with the kmalloc, or from the proc->slab cache. Move this logic into an isolated set of helpers and make the sk_alloc/sk_free look a bit nicer. Signed-off-by: Pavel Emelyanov <xemul@openvz.org> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'net/core/sock.c')
-rw-r--r--net/core/sock.c44
1 files changed, 29 insertions, 15 deletions
diff --git a/net/core/sock.c b/net/core/sock.c
index 9c2dbfaca60d..6ee2ed104a83 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -870,6 +870,31 @@ static void sock_copy(struct sock *nsk, const struct sock *osk)
870#endif 870#endif
871} 871}
872 872
873static struct sock *sk_prot_alloc(struct proto *prot, gfp_t priority)
874{
875 struct sock *sk;
876 struct kmem_cache *slab;
877
878 slab = prot->slab;
879 if (slab != NULL)
880 sk = kmem_cache_alloc(slab, priority);
881 else
882 sk = kmalloc(prot->obj_size, priority);
883
884 return sk;
885}
886
887static void sk_prot_free(struct proto *prot, struct sock *sk)
888{
889 struct kmem_cache *slab;
890
891 slab = prot->slab;
892 if (slab != NULL)
893 kmem_cache_free(slab, sk);
894 else
895 kfree(sk);
896}
897
873/** 898/**
874 * sk_alloc - All socket objects are allocated here 899 * sk_alloc - All socket objects are allocated here
875 * @net: the applicable net namespace 900 * @net: the applicable net namespace
@@ -881,14 +906,9 @@ static void sock_copy(struct sock *nsk, const struct sock *osk)
881struct sock *sk_alloc(struct net *net, int family, gfp_t priority, 906struct sock *sk_alloc(struct net *net, int family, gfp_t priority,
882 struct proto *prot, int zero_it) 907 struct proto *prot, int zero_it)
883{ 908{
884 struct sock *sk = NULL; 909 struct sock *sk;
885 struct kmem_cache *slab = prot->slab;
886
887 if (slab != NULL)
888 sk = kmem_cache_alloc(slab, priority);
889 else
890 sk = kmalloc(prot->obj_size, priority);
891 910
911 sk = sk_prot_alloc(prot, priority);
892 if (sk) { 912 if (sk) {
893 if (zero_it) { 913 if (zero_it) {
894 memset(sk, 0, prot->obj_size); 914 memset(sk, 0, prot->obj_size);
@@ -911,10 +931,7 @@ struct sock *sk_alloc(struct net *net, int family, gfp_t priority,
911 return sk; 931 return sk;
912 932
913out_free: 933out_free:
914 if (slab != NULL) 934 sk_prot_free(prot, sk);
915 kmem_cache_free(slab, sk);
916 else
917 kfree(sk);
918 return NULL; 935 return NULL;
919} 936}
920 937
@@ -940,10 +957,7 @@ void sk_free(struct sock *sk)
940 957
941 security_sk_free(sk); 958 security_sk_free(sk);
942 put_net(sk->sk_net); 959 put_net(sk->sk_net);
943 if (sk->sk_prot_creator->slab != NULL) 960 sk_prot_free(sk->sk_prot_creator, sk);
944 kmem_cache_free(sk->sk_prot_creator->slab, sk);
945 else
946 kfree(sk);
947 module_put(owner); 961 module_put(owner);
948} 962}
949 963