整理了手头上几本书中关于资源和内存管理的章节
<Understanding the linux kenel
第三版> 8.1.7 the buddy system Buddy算法, 解决内存碎片问题. 张贴书本原文如下:
The
technique adopted by Linux to solve the external fragmentation problem
is based on the well-known buddy system
algorithm. All free page frames are grouped into 11 lists of blocks
that contain groups of 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, and 1024
contiguous page frames, respectively. The largest request of 1024 page
frames corresponds to a chunk of 4 MB of contiguous RAM. The physical
address of the first page frame of a block is a multiple of the group
sizefor example, the initial address of a 16-page-frame block is a
multiple of 16 x 212 (212 = 4,096, which is the
regular page size).
We'll
show how the algorithm works through a simple example:
Assume there is a request for a group of 256 contiguous
page frames (i.e., one megabyte). The algorithm checks first to see
whether a free block in the 256-page-frame list exists. If there is no
such block, the algorithm looks for the next larger blocka free block in
the 512-page-frame list. If such a block exists, the kernel allocates
256 of the 512 page frames to satisfy the request and inserts the
remaining 256 page frames into the list of free 256-page-frame blocks.
If there is no free 512-page block, the kernel then looks for the next
larger block (i.e., a free 1024-page-frame block). If such a block
exists, it allocates 256 of the 1024 page frames to satisfy the request,
inserts the first 512 of the remaining 768 page frames into the list of
free 512-page-frame blocks, and inserts the last 256 page frames into
the list of free 256-page-frame blocks. If the list of 1024-page-frame
blocks is empty, the algorithm gives up and signals an error condition.
The reverse operation, releasing blocks of page frames,
gives rise to the name of this algorithm. The kernel attempts to merge
pairs of free buddy blocks of size b
together into a single block of size 2b.
Two blocks are considered buddies if:
-
Both
blocks have the same size, say b.
-
They are located in contiguous physical addresses.
-
The physical address of the first page frame of the
first block is a multiple of 2 x b x 212.
The algorithm is iterative; if it succeeds in merging
released blocks, it doubles b and tries
again so as to create even bigger blocks
|
<Modern
C++ design 泛型编程与设计模式> Chapter 4 small object allocation
本书讨论的是loki库。第4章探讨了小对象的内存分配
<STL源码解析> 2.2 STL空间分配器
翻译by 侯捷
SGI的STL实现中,allocator的实现例子。
二级分配器:大于128byte交给一个分配器,直接分配内存。小数据块交给次级分配器。
次级分配器用一个freelist数组维护可分配的小块内存区域。freelist数组中的项是一个固定内存大小的链表。freelist中的项
这
里用了一个小技巧(union)
union obj { union obj* free_list_link; char
client_data[1]; }
|
这个既可用于空闲列表节点,又能作为数据指针(强制转换), 这样就可以节省信息记录的空间。
详细说明参考原书
<
游戏编程精粹1> 1.6 通用的基于句柄的资源管理器 该文章实现HandleMgr模板类实现不同类型资源的管理器.handle为整数值
基本思路
- vector<DATA>存放实际数据,
vector储存magic number, FreeVector储存数据vector中的空闲索引值
- Acquire根据
handl值返回数据指针(引用计数+1), Release释放data( 引用计数减1 )
技巧1:
利用空结构实现类型匹配(STL内经常用这个技巧)
struct tagTexture {}
typedef Handle<tagTexture> HTexture
技巧2:资源释放时不需要析构,只需要把相应index加入空闲列表。分配时重用该对象,重新初始化值,可以提高效率。
技巧3:一般资源管理器类作为Singleton
扩展1:为标准功能增加自动引用计数(参考智能指针的实现?)
<
游戏编程精粹1> 1.9 基于帧的内存分配 by steven ranck
思路:栈方式(后进先出)的内存分配器。预先分配大块内存,然后按栈顺序分配和释放内存。
仅适用于分配,释放有严格顺序的资源(如关卡资源)
<游戏编程精粹1> 1.1 技巧:所谓的数据继承
对于不变的对象属性,具体类用引用指向这些固定属性,而不是继承。
因为仅通过对象继承,每个对象都有这些固定属性的拷贝,浪费空间。
Sprite(速度,满血值,攻击力) <-- SpriteInstance( 对sprite引用, 位置,当前生命值)