From: Sepherosa Ziehau Date: Wed, 10 Oct 2012 09:17:12 +0000 (+0800) Subject: kmalloc: Make allocation zone's chunk size align X-Git-Tag: v3.4.0rc~1000^2~12 X-Git-Url: https://gitweb.dragonflybsd.org/dragonfly.git/commitdiff_plain/81dedbc2f49428007630efab98be1f45a9b802da kmalloc: Make allocation zone's chunk size align Remove no longer used macros. Discussed-with: dillon@ Submitted-by: dillon@ --- diff --git a/sys/kern/kern_slaballoc.c b/sys/kern/kern_slaballoc.c index 0edfdc4b10..611f3013f2 100644 --- a/sys/kern/kern_slaballoc.c +++ b/sys/kern/kern_slaballoc.c @@ -171,12 +171,8 @@ static void chunk_mark_free(SLZone *z, void *chunk); /* * Misc constants. Note that allocations that are exact multiples of * PAGE_SIZE, or exceed the zone limit, fall through to the kmem module. - * IN_SAME_PAGE_MASK is used to sanity-check the per-page free lists. */ -#define MIN_CHUNK_SIZE 8 /* in bytes */ -#define MIN_CHUNK_MASK (MIN_CHUNK_SIZE - 1) #define ZONE_RELS_THRESH 32 /* threshold number of zones */ -#define IN_SAME_PAGE_MASK (~(intptr_t)PAGE_MASK | MIN_CHUNK_MASK) /* * The WEIRD_ADDR is used as known text to copy into free objects to @@ -419,46 +415,55 @@ kmalloc_destroy(struct malloc_type **typep) * allocation request size to that particular zone's chunk size. */ static __inline int -zoneindex(unsigned long *bytes) +zoneindex(unsigned long *bytes, unsigned long *align) { unsigned int n = (unsigned int)*bytes; /* unsigned for shift opt */ if (n < 128) { *bytes = n = (n + 7) & ~7; + *align = 8; return(n / 8 - 1); /* 8 byte chunks, 16 zones */ } if (n < 256) { *bytes = n = (n + 15) & ~15; + *align = 16; return(n / 16 + 7); } if (n < 8192) { if (n < 512) { *bytes = n = (n + 31) & ~31; + *align = 32; return(n / 32 + 15); } if (n < 1024) { *bytes = n = (n + 63) & ~63; + *align = 64; return(n / 64 + 23); } if (n < 2048) { *bytes = n = (n + 127) & ~127; + *align = 128; return(n / 128 + 31); } if (n < 4096) { *bytes = n = (n + 255) & ~255; + *align = 256; return(n / 256 + 39); } *bytes = n = (n + 511) & ~511; + *align = 512; return(n / 512 + 47); } #if ZALLOC_ZONE_LIMIT > 8192 if (n < 16384) { *bytes = n = (n + 1023) & ~1023; + *align = 1024; return(n / 1024 + 55); } #endif #if ZALLOC_ZONE_LIMIT > 16384 if (n < 32768) { *bytes = n = (n + 2047) & ~2047; + *align = 2048; return(n / 2048 + 63); } #endif @@ -542,6 +547,7 @@ kmalloc(unsigned long size, struct malloc_type *type, int flags) #endif SLGlobalData *slgd; struct globaldata *gd; + unsigned long align; int zi; #ifdef INVARIANTS int i; @@ -680,7 +686,7 @@ kmalloc(unsigned long size, struct malloc_type *type, int flags) * * Note: zoneindex() will panic of size is too large. */ - zi = zoneindex(&size); + zi = zoneindex(&size, &align); KKASSERT(zi < NZONES); crit_enter(); @@ -824,7 +830,7 @@ kmalloc(unsigned long size, struct malloc_type *type, int flags) if ((size | (size - 1)) + 1 == (size << 1)) off = (off + size - 1) & ~(size - 1); else - off = (off + MIN_CHUNK_MASK) & ~MIN_CHUNK_MASK; + off = (off + align - 1) & ~(align - 1); z->z_Magic = ZALLOC_SLAB_MAGIC; z->z_ZoneIndex = zi; z->z_NMax = (ZoneSize - off) / size; @@ -906,6 +912,7 @@ krealloc(void *ptr, unsigned long size, struct malloc_type *type, int flags) #endif { unsigned long osize; + unsigned long align; SLZone *z; void *nptr; int *kup; @@ -956,7 +963,7 @@ krealloc(void *ptr, unsigned long size, struct malloc_type *type, int flags) * size is not too large. */ if (size < ZoneLimit) { - zoneindex(&size); + zoneindex(&size, &align); if (z->z_ChunkSize == size) return(ptr); }