Exemple #1
0
/**
 *	sk_alloc - All socket objects are allocated here
 *	@family: protocol family
 *	@priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
 *	@prot: struct proto associated with this new sock instance
 *	@zero_it: if we should zero the newly allocated sock
 */
struct sock *sk_alloc(int family, gfp_t priority,
		      struct proto *prot, int zero_it)
{
	struct sock *sk = NULL;
	kmem_cache_t *slab = prot->slab;

	if (slab != NULL)
		sk = kmem_cache_alloc(slab, priority);
	else
		sk = kmalloc(prot->obj_size, priority);

	if (sk) {
		if (zero_it) {
			memset(sk, 0, prot->obj_size);
			sk->sk_family = family;
			/*
			 * See comment in struct sock definition to understand
			 * why we need sk_prot_creator -acme
			 */
			sk->sk_prot = sk->sk_prot_creator = prot;
			sock_lock_init(sk);
		}
		
		if (security_sk_alloc(sk, family, priority))
			goto out_free;

		if (!try_module_get(prot->owner))
			goto out_free;
	}
	return sk;

out_free:
	if (slab != NULL)
		kmem_cache_free(slab, sk);
	else
		kfree(sk);
	return NULL;
}
Exemple #2
0
/**
 *	sk_alloc - All socket objects are allocated here
 *	@family - protocol family
 *	@priority - for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
 *	@zero_it - zeroes the allocated sock
 *	@slab - alternate slab
 *
 *	All socket objects are allocated here. If @zero_it is non-zero
 *	it should have the size of the are to be zeroed, because the
 *	private slabcaches have different sizes of the generic struct sock.
 *	1 has been kept as a way to say sizeof(struct sock).
 */
struct sock *sk_alloc(int family, int priority, int zero_it, kmem_cache_t *slab)
{
	struct sock *sk = NULL;

	if (!slab)
		slab = sk_cachep;
	sk = kmem_cache_alloc(slab, priority);
	if (sk) {
		if (zero_it) {
			memset(sk, 0,
			       zero_it == 1 ? sizeof(struct sock) : zero_it);
			sk->sk_family = family;
			sock_lock_init(sk);
		}
		sk->sk_slab = slab;
		
		if (security_sk_alloc(sk, family, priority)) {
			kmem_cache_free(slab, sk);
			sk = NULL;
		}
	}
	return sk;
}