你好,游客 登录 注册 搜索
背景:
阅读新闻

深入理解Linux内核之SLOB分配器

[日期:2012-05-21] 来源:Linux社区  作者:ture010love [字体:大 中 小]

SLOB分配器源码

001 /*

002 * SLOB Allocator: Simple List Of Blocks

003 *

004 * Matt Mackall <mpm@selenic.com> 12/30/03

005 *

006 * NUMA support by Paul Mundt, 2007.

007 *

008 * How SLOB works:

009 *

010 * The core of SLOB is a traditional K&R style heap allocator, with

011 * support for returning aligned objects. The granularity of this

012 * allocator is as little as 2 bytes, however typically most architectures

013 * will require 4 bytes on 32-bit and 8 bytes on 64-bit.

014 *

015 * The slob heap is a set of linked list of pages from alloc_pages(),

016 * and within each page, there is a singly-linked list of free blocks

017 * (slob_t). The heap is grown on demand. To reduce fragmentation,

018 * heap pages are segregated into three lists, with objects less than

019 * 256 bytes, objects less than 1024 bytes, and all other objects.

020 *

021 * Allocation from heap involves first searching for a page with

022 * sufficient free blocks (using a next-fit-like approach) followed by

023 * a first-fit scan of the page. Deallocation inserts objects back

024 * into the free list in address order, so this is effectively an

025 * address-ordered first fit.

026 *

027 * Above this is an implementation of kmalloc/kfree. Blocks returned

028 * from kmalloc are prepended with a 4-byte header with the kmalloc size.

029 * If kmalloc is asked for objects of PAGE_SIZE or larger, it calls

030 * alloc_pages() directly, allocating compound pages so the page order

031 * does not have to be separately tracked, and also stores the exact

032 * allocation size in page->private so that it can be used to accurately

033 * provide ksize(). These objects are detected in kfree() because slob_page()

034 * is false for them.

035 *

036 * SLAB is emulated on top of SLOB by simply calling constructors and

037 * destructors for every SLAB allocation. Objects are returned with the

038 * 4-byte alignment unless the SLAB_HWCACHE_ALIGN flag is set, in which

039 * case the low-level allocator will fragment blocks to create the proper

040 * alignment. Again, objects of page-size or greater are allocated by

041 * calling alloc_pages(). As SLAB objects know their size, no separate

042 * size bookkeeping is necessary and there is essentially no allocation

043 * space overhead, and compound pages aren't needed for multi-page

044 * allocations.

045 *

046 * NUMA support in SLOB is fairly simplistic, pushing most of the real

047 * logic down to the page allocator, and simply doing the node accounting

048 * on the upper levels. In the event that a node id is explicitly

049 * provided, alloc_pages_exact_node() with the specified node id is used

050 * instead. The common case (or when the node id isn't explicitly provided)

051 * will default to the current node, as per numa_node_id().

052 *

053 * Node aware pages are still inserted in to the global freelist, and

054 * these are scanned for by matching against the node id encoded in the

055 * page flags. As a result, block allocations that can be satisfied from

056 * the freelist will only be done so on pages residing on the same node,

057 * in order to prevent random node placement.

058 */

/*

SLOB适用于嵌入式系统,通常也将其归为slab类分配器。它的代码非常小巧、简单,管理也没有LINUX所提供的SLAB分配器复杂,例如没有per cpu cache。SLOB,为减少碎片,按对象大小将pages链接为三种队列:< 256 bytes、< 1024 bytes、其它大小。对于大于PAGE_SIZE的对象,直接调用alloc_pages分配对象,并将size_order在于首page的private字段中。page内部所有空闲对象都被链成一个free list,由slob_t结构描述。

*/

059

060 #include <linux/kernel.h>

061 #include <linux/slab.h>

062 #include <linux/mm.h>

063 #include <linux/swap.h> /* struct reclaim_state */

064 #include <linux/cache.h>

065 #include <linux/init.h>

066 #include <linux/module.h>

067 #include <linux/rcupdate.h>

068 #include <linux/list.h>

069 #include <linux/kmemtrace.h>

070 #include <linux/kmemleak.h>

071 #include <asm/atomic.h>

072

073 /*

074 * slob_block has a field 'units', which indicates size of block if +ve,

075 * or offset of next block if -ve (in SLOB_UNITs).

076 *

077 * Free blocks of size 1 unit simply contain the offset of the next block.

078 * Those with larger size contain their size in the first SLOB_UNIT of

079 * memory, and the offset of the next free block in the second SLOB_UNIT.

080 */

081 #if PAGE_SIZE <= (32767 * 2)

082 typedef s16 slobidx_t;

083 #else

084 typedef s32 slobidx_t;

085 #endif

086

087 struct slob_block {

088 slobidx_t units;

089 };

090 typedef struct slob_block slob_t;

/*
linux
相关资讯       Linux内核编程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款