资源简介

仿照linux的buddy+slub内存管理算法,可以在裸机中应用标准内存管理库函数,如malloc free等

资源截图

代码片段和文件信息

/*
 * slab allocator implementation
 *
 * A slab allocator reserves a ZONE for each chunk size then lays the
 * chunks out in an array within the zone.  Allocation and deallocation
 * is nearly instantanious and fragmentation/overhead losses are limited
 * to a fixed worst-case amount.
 *
 * The downside of this slab implementation is in the chunk size
 * multiplied by the number of zones.  ~80 zones * 128K = 10MB of VM per cpu.
 * In a kernel implementation all this memory will be physical so
 * the zone size is adjusted downward on machines with less physical
 * memory.  The upside is that overhead is bounded... this is the *worst*
 * case overhead.
 *
 * Slab management is done on a per-cpu basis and no locking or mutexes
 * are required only a critical section.  When one cpu frees memory
 * belonging to another cpu‘s slab manager an asynchronous IPI message
 * will be queued to execute the operation.   In addition both the
 * high level slab allocator and the low level zone allocator optimize
 * M_ZERO requests and the slab allocator does not have to pre initialize
 * the linked list of chunks.
 *
 * XXX Balancing is needed between cpus.  Balance will be handled through
 * asynchronous IPIs primarily by reassigning the z_Cpu ownership of chunks.
 *
 * XXX If we have to allocate a new zone and M_USE_RESERVE is set use of
 * the new zone should be restricted to M_USE_RESERVE requests only.
 *
 * Alloc Size Chunking        Number of zones
 * 0-127 8 16
 * 128-255 16 8
 * 256-511 32 8
 * 512-1023 64 8
 * 1024-2047 128 8
 * 2048-4095 256 8
 * 4096-8191 512 8
 * 8192-16383 1024 8
 * 16384-32767 2048 8
 * (if PAGE_SIZE is 4K the maximum zone allocation is 16383)
 *
 * Allocations >= zone_limit go directly to kmem.
 *
 * API REQUIREMENTS AND SIDE EFFECTS
 *
 *    To operate as a drop-in replacement to the FreeBSD-4.x malloc() we
 *    have remained compatible with the following API requirements:
 *
 *    + small power-of-2 sized allocations are power-of-2 aligned (kern_tty)
 *    + all power-of-2 sized allocations are power-of-2 aligned (twe)
 *    + malloc(0) is allowed and returns non-0 (ahc driver)
 *    + ability to allocate arbitrarily large chunks of memory
 */

/*
 * Chunk structure for free elements
 */


#define ASSERT(a)
#define ALIGN(size align) (((size) + (align) - 1) & ~((align)-1))
/**
 * @ingroup BasicDef
 *
 * @def ALIGN_DOWN(size align)
 * Return the down number of aligned at specified width. ALIGN_DOWN(13 4)
 * would return 12. 
 */
#define ALIGN_DOWN(size align) ((size) & ~((align) -1)) 

#define PAGE_SIZE 4096
#define PAGE_MASK  (PAGE_SIZE - 1)
#define PAGE_BITS 12
#define SLAB_DEBUG
#define MEM_STATS

/* some statistical variable */
#ifdef MEM_STATS
static unsigned int used_mem max_mem;
#endif
static void (*malloc_hook)(void *ptr unsigned int size)=NULL;
static void (*free_hook)(void *ptr)=NULL;

void malloc_sethook(void (*hook)(void *ptr unsigned int size))
{
malloc_hook 

评论

共有 条评论