Slab allocators: consistent ZERO_SIZE_PTR support and NULL result semantics

Define ZERO_OR_NULL_PTR macro to be able to remove the checks from the
allocators.  Move ZERO_SIZE_PTR related stuff into slab.h.

Make ZERO_SIZE_PTR work for all slab allocators and get rid of the
WARN_ON_ONCE(size == 0) that is still remaining in SLAB.

Make slub return NULL like the other allocators if a too large memory segment
is requested via __kmalloc.

Signed-off-by: Christoph Lameter <[email protected]>
Acked-by: Pekka Enberg <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
This commit is contained in:
Christoph Lameter 2007-07-17 04:03:22 -07:00 • committed by Linus Torvalds
commit 6cb8f91320
7 changed files with 57 additions and 35 deletions

View file

@ -347,7 +347,7 @@ static void slob_free(void *block, int size)
slobidx_t units;
unsigned long flags;
if (!block)
if (ZERO_OR_NULL_PTR(block))
return;
BUG_ON(!size);
@ -424,10 +424,13 @@ out:
void *__kmalloc_node(size_t size, gfp_t gfp, int node)
{
unsigned int *m;
int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN);
if (size < PAGE_SIZE - align) {
unsigned int *m;
if (!size)
return ZERO_SIZE_PTR;
m = slob_alloc(size + align, gfp, align, node);
if (m)
*m = size;
@ -450,7 +453,7 @@ void kfree(const void *block)
{
struct slob_page *sp;
if (!block)
if (ZERO_OR_NULL_PTR(block))
return;
sp = (struct slob_page *)virt_to_page(block);
@ -468,7 +471,7 @@ size_t ksize(const void *block)
{
struct slob_page *sp;
if (!block)
if (ZERO_OR_NULL_PTR(block))
return 0;
sp = (struct slob_page *)virt_to_page(block);