Chinaunix首页 | 论坛 | 博客
  • 博客访问: 714748
  • 博文数量: 165
  • 博客积分: 8218
  • 博客等级: 中将
  • 技术积分: 1749
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-07 19:48
文章分类

全部博文(165)

文章存档

2014年(4)

2011年(3)

2010年(6)

2009年(43)

2008年(109)

分类: LINUX

2008-12-29 20:41:32

The Real Story of kmalloc
The kmalloc allocation engine is a powerful tool and easily learned because of its
similarity to malloc. The function is fast (unless it blocks) and doesn’t clear the mem-
ory it obtains; the allocated region still holds its previous content.* The allocated
region is also contiguous in physical memory. In the next few sections, we talk in
detail about kmalloc, so you can compare it with the memory allocation techniques
that we discuss later.

Per-CPU Variables
Per-CPU variables are an interesting 2.6 kernel feature. When you create a per-CPU
variable, each processor on the system gets its own copy of that variable. This may
seem like a strange thing to want to do, but it has its advantages. Access to per-CPU
variables requires (almost) no locking, because each processor works with its own
copy. Per-CPU variables can also remain in their respective processors’ caches, which
leads to significantly better performance for frequently updated quantities.

Quick Reference
The functions and symbols related to memory allocation are:
#include
void *kmalloc(size_t size, int flags);
void kfree(void *obj);
    The most frequently used interface to memory allocation.
#include
GFP_USER
GFP_KERNEL
GFP_NOFS
GFP_NOIO
GFP_ATOMIC
    Flags that control how memory allocations are performed, from the least restric-
    tive to the most. The GFP_USER and GFP_KERNEL priorities allow the current process
    to be put to sleep to satisfy the request. GFP_NOFS and GFP_NOIO disable filesystem
    operations and all I/O operations, respectively, while GFP_ATOMIC allocations can-
    not sleep at all.
__GFP_DMA
__GFP_HIGHMEM
__GFP_COLD
__GFP_NOWARN
__GFP_HIGH
__GFP_REPEAT
__GFP_NOFAIL
__GFP_NORETRY
    These flags modify the kernel’s behavior when allocating memory.
#include
kmem_cache_t *kmem_cache_create(char *name, size_t size, size_t offset,
  unsigned long flags, constructor( ), destructor( ));
int kmem_cache_destroy(kmem_cache_t *cache);
    Create and destroy a slab cache. The cache can be used to allocate several
    objects of the same size.
SLAB_NO_REAP
SLAB_HWCACHE_ALIGN
SLAB_CACHE_DMA
    Flags that can be specified while creating a cache.
SLAB_CTOR_ATOMIC
SLAB_CTOR_CONSTRUCTOR
    Flags that the allocator can pass to the constructor and the destructor functions.
void *kmem_cache_alloc(kmem_cache_t *cache, int flags);
void kmem_cache_free(kmem_cache_t *cache, const void *obj);
    Allocate and release a single object from the cache.
/proc/slabinfo
    A virtual file containing statistics on slab cache usage.
#include
mempool_t *mempool_create(int min_nr, mempool_alloc_t *alloc_fn, mempool_free_t
  *free_fn, void *data);
void mempool_destroy(mempool_t *pool);
    Functions for the creation of memory pools, which try to avoid memory alloca-
    tion failures by keeping an “emergency list” of allocated items.
void *mempool_alloc(mempool_t *pool, int gfp_mask);
void mempool_free(void *element, mempool_t *pool);
    Functions for allocating items from (and returning them to) memory pools.
unsigned long get_zeroed_page(int flags);
unsigned long __get_free_page(int flags);
unsigned long __get_free_pages(int flags, unsigned long order);
    The page-oriented allocation functions. get_zeroed_page returns a single, zero-
    filled page. All the other versions of the call do not initialize the contents of the
    returned page(s).
int get_order(unsigned long size);
    Returns the allocation order associated to size in the current platform, according
    to PAGE_SIZE. The argument must be a power of two, and the return value is at
    least 0.
void free_page(unsigned long addr);
void free_pages(unsigned long addr, unsigned long order);
    Functions that release page-oriented allocations.
struct page *alloc_pages_node(int nid, unsigned int flags, unsigned int order);
struct page *alloc_pages(unsigned int flags, unsigned int order);
struct page *alloc_page(unsigned int flags);
    All variants of the lowest-level page allocator in the Linux kernel.
void  __free_page(struct page *page);
void  __free_pages(struct page *page, unsigned int order);
void  free_hot_page(struct page *page);
void  free_cold_page(struct page *page);
    Various ways of freeing pages allocated with one of the forms of alloc_page.
#include
void * vmalloc(unsigned long size);
void vfree(void * addr);
#include
void * ioremap(unsigned long offset, unsigned long size);
void iounmap(void *addr);
    Functions that allocate or free a contiguous virtual address space. ioremap
    accesses physical memory through virtual addresses, while vmalloc allocates free
    pages. Regions mapped with ioremap are freed with iounmap, while pages
    obtained from vmalloc are released with vfree.
#include
DEFINE_PER_CPU(type, name);
DECLARE_PER_CPU(type, name);
    Macros that define and declare per-CPU variables.
per_cpu(variable, int cpu_id)
get_cpu_var(variable)
put_cpu_var(variable)
    Macros that provide access to statically declared per-CPU variables.
void *alloc_percpu(type);
void *__alloc_percpu(size_t size, size_t align);
void free_percpu(void *variable);
    Functions that perform runtime allocation and freeing of per-CPU variables.
int get_cpu( );
void put_cpu( );
per_cpu_ptr(void *variable, int cpu_id)
    get_cpu obtains a reference to the current processor (therefore, preventing pre-
    emption and movement to another processor) and returns the ID number of the
    processor; put_cpu returns that reference. To access a dynamically allocated per-
    CPU variable, use per_cpu_ptr with the ID of the CPU whose version should be
    accessed. Manipulations of the current CPU’s version of a dynamic, per-CPU
    variable should probably be surrounded by calls to get_cpu and put_cpu.
#include
void *alloc_bootmem(unsigned long size);
void *alloc_bootmem_low(unsigned long size);
void *alloc_bootmem_pages(unsigned long size);
void *alloc_bootmem_low_pages(unsigned long size);
void free_bootmem(unsigned long addr, unsigned long size);
    Functions (which can be used only by drivers directly linked into the kernel) that
    perform allocation and freeing of memory at system bootstrap time.

阅读(776) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~