From 83bbd4bed27b79d9ee66aaf4c7e631e258106559 Mon Sep 17 00:00:00 2001 From: yuchen Date: Sun, 22 Mar 2015 14:49:24 +0800 Subject: [PATCH] add buddy system in related_info of lab2 --- related_info/lab2/buddy_system.c | 198 ++++++++++++++++++++++++++++++++++++++ related_info/lab2/buddy_system.md | 42 ++++++++ 2 files changed, 240 insertions(+) create mode 100644 related_info/lab2/buddy_system.c create mode 100644 related_info/lab2/buddy_system.md diff --git a/related_info/lab2/buddy_system.c b/related_info/lab2/buddy_system.c new file mode 100644 index 0000000..cf29fe8 --- /dev/null +++ b/related_info/lab2/buddy_system.c @@ -0,0 +1,198 @@ +#include +#include +#include +#include + +struct buddy2 { + unsigned size; + unsigned longest[1]; +}; + +struct buddy2* buddy2_new( int size ); +void buddy2_destroy( struct buddy2* self ); + +int buddy2_alloc(struct buddy2* self, int size); +void buddy2_free(struct buddy2* self, int offset); + +int buddy2_size(struct buddy2* self, int offset); +void buddy2_dump(struct buddy2* self); + +#define LEFT_LEAF(index) ((index) * 2 + 1) +#define RIGHT_LEAF(index) ((index) * 2 + 2) +#define PARENT(index) ( ((index) + 1) / 2 - 1) + +#define IS_POWER_OF_2(x) (!((x)&((x)-1))) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +#define ALLOC malloc +#define FREE free + +static unsigned fixsize(unsigned size) { + size |= size >> 1; + size |= size >> 2; + size |= size >> 4; + size |= size >> 8; + size |= size >> 16; + return size+1; +} + +struct buddy2* buddy2_new( int size ) { + struct buddy2* self; + unsigned node_size; + int i; + + if (size < 1 || !IS_POWER_OF_2(size)) + return NULL; + + self = (struct buddy2*)ALLOC( 2 * size * sizeof(unsigned)); + self->size = size; + node_size = size * 2; + + for (i = 0; i < 2 * size - 1; ++i) { + if (IS_POWER_OF_2(i+1)) + node_size /= 2; + self->longest[i] = node_size; + } + return self; +} + +void buddy2_destroy( struct buddy2* self) { + FREE(self); +} + +int buddy2_alloc(struct buddy2* self, int size) { + unsigned index = 0; + unsigned node_size; + unsigned offset = 0; + + if (self==NULL) + return -1; + + if (size <= 0) + size = 1; + else if (!IS_POWER_OF_2(size)) + size = fixsize(size); + + if (self->longest[index] < size) + return -1; + + for(node_size = self->size; node_size != size; node_size /= 2 ) { + if (self->longest[LEFT_LEAF(index)] >= size) + index = LEFT_LEAF(index); + else + index = RIGHT_LEAF(index); + } + + self->longest[index] = 0; + offset = (index + 1) * node_size - self->size; + + while (index) { + index = PARENT(index); + self->longest[index] = + MAX(self->longest[LEFT_LEAF(index)], self->longest[RIGHT_LEAF(index)]); + } + + return offset; +} + +void buddy2_free(struct buddy2* self, int offset) { + unsigned node_size, index = 0; + unsigned left_longest, right_longest; + + assert(self && offset >= 0 && offset < self->size); + + node_size = 1; + index = offset + self->size - 1; + + for (; self->longest[index] ; index = PARENT(index)) { + node_size *= 2; + if (index == 0) + return; + } + + self->longest[index] = node_size; + + while (index) { + index = PARENT(index); + node_size *= 2; + + left_longest = self->longest[LEFT_LEAF(index)]; + right_longest = self->longest[RIGHT_LEAF(index)]; + + if (left_longest + right_longest == node_size) + self->longest[index] = node_size; + else + self->longest[index] = MAX(left_longest, right_longest); + } +} + +int buddy2_size(struct buddy2* self, int offset) { + unsigned node_size, index = 0; + + assert(self && offset >= 0 && offset < self->size); + + node_size = 1; + for (index = offset + self->size - 1; self->longest[index] ; index = PARENT(index)) + node_size *= 2; + + return node_size; +} + +void buddy2_dump(struct buddy2* self) { + char canvas[65]; + int i,j; + unsigned node_size, offset; + + if (self == NULL) { + printf("buddy2_dump: (struct buddy2*)self == NULL"); + return; + } + + if (self->size > 64) { + printf("buddy2_dump: (struct buddy2*)self is too big to dump"); + return; + } + + memset(canvas,'_', sizeof(canvas)); + node_size = self->size * 2; + + for (i = 0; i < 2 * self->size - 1; ++i) { + if ( IS_POWER_OF_2(i+1) ) + node_size /= 2; + + if ( self->longest[i] == 0 ) { + if (i >= self->size - 1) { + canvas[i - self->size + 1] = '*'; + } + else if (self->longest[LEFT_LEAF(i)] && self->longest[RIGHT_LEAF(i)]) { + offset = (i+1) * node_size - self->size; + + for (j = offset; j < offset + node_size; ++j) + canvas[j] = '*'; + } + } + } + canvas[self->size] = '\0'; + puts(canvas); +} + +int main() { + char cmd[80]; + int arg; + struct buddy2* buddy = buddy2_new(32); + buddy2_dump(buddy); + for (;;) { + scanf("%s %d", cmd, &arg); + if (strcmp(cmd, "alloc") == 0) { + printf("allocated@%d\n", buddy2_alloc(buddy, arg)); + buddy2_dump(buddy); + } else if (strcmp(cmd, "free") == 0) { + buddy2_free(buddy, arg); + buddy2_dump(buddy); + } else if (strcmp(cmd, "size") == 0) { + printf("size: %d\n", buddy2_size(buddy, arg)); + buddy2_dump(buddy); + } else + buddy2_dump(buddy); + } +} diff --git a/related_info/lab2/buddy_system.md b/related_info/lab2/buddy_system.md new file mode 100644 index 0000000..b962b6e --- /dev/null +++ b/related_info/lab2/buddy_system.md @@ -0,0 +1,42 @@ +# buddy system example code +From + - https://github.com/wuwenbin/buddy2 + - [伙伴分配器的一个极简实现 by 我的上铺叫路遥 酷 壳 – CoolShell.cn](http://coolshell.cn/articles/10427.html) + +## run & debug +try to +``` +gcc -g -O0 -o buddy_system buddy_system.c +酷 壳 – CoolShell.cn +gdb buddy_system +``` + +## background +> 下述内容直接来源于 [伙伴分配器的一个极简实现 by 我的上铺叫路遥 酷 壳 – CoolShell.cn](http://coolshell.cn/articles/10427.html) + +### 数据结构总体思路 +通过一个数组形式的完全二叉树来监控管理内存,二叉树的节点用于标记相应内存块的使用状态,高层节点对应大的块,低层节点对应小的块,在分配和释放中我们就通过这些节点的标记属性来进行块的分离合并。如图所示,假设总大小为16单位的内存,我们就建立一个深度为5的满二叉树,根节点从数组下标[0]开始,监控大小16的块;它的左右孩子节点下标[1~2],监控大小8的块;第三层节点下标[3~6]监控大小4的块……依此类推。 +``` +struct buddy2 { + unsigned size; + unsigned longest[1]; +}; +``` +这里的成员size表明管理内存的总单元数目(测试用例中是32),成员longest就是二叉树的节点标记,表明所对应的内存块的空闲单位。 + +### 初始化 +整个分配器的大小就是满二叉树节点数目,即所需管理内存单元数目的2倍。一个节点对应4个字节,longest记录了节点所对应的的内存块大小。 + +### 分配阶段 +首先要搜索大小适配的块,假设第一次分配3,转换成2的幂是4,我们先要对整个内存进行对半切割,从16切割到4需要两步,那么从下标[0]节点开始深度搜索到下标[3]的节点并将其标记为已分配。第二次再分配3那么就标记下标[4]的节点。第三次分配6,即大小为8,那么搜索下标[2]的节点,因为下标[1]所对应的块被下标[3~4]占用了。 + +具体而言,内存分配的alloc中,入参是分配器指针和需要分配的大小,返回值是内存块索引。alloc函数首先将size调整到2的幂大小,并检查是否超过最大限度。然后进行适配搜索,深度优先遍历,当找到对应节点后,将其longest标记为0,即分离适配的块出来,并转换为内存块索引offset返回,依据二叉树排列序号,比如内存总体大小32,我们找到节点下标[8],内存块对应大小是4,则offset = (8+1)*4-32 = 4,那么分配内存块就从索引4开始往后4个单位。在函数返回之前需要回溯,因为小块内存被占用,大块就不能分配了,比如下标[8]标记为0分离出来,那么其父节点下标[0]、[1]、[3]也需要相应大小的分离。将它们的longest进行折扣计算,取左右子树较大值,下标[3]取4,下标[1]取8,下标[0]取16,表明其对应的最大空闲值。 + +### 释放阶段 +我们依次释放上述第一次和第二次分配的块,即先释放[3]再释放[4],当释放下标[4]节点后,我们发现之前释放的[3]是相邻的,于是我们立马将这两个节点进行合并,这样一来下次分配大小8的时候,我们就可以搜索到下标[1]适配了。若进一步释放下标[2],同[1]合并后整个内存就回归到初始状态。 + +具体而言,在内存释放的free接口,我们只要传入之前分配的内存地址索引,并确保它是有效值。之后就跟alloc做反向回溯,从最后的节点开始一直往上找到longest为0的节点,即当初分配块所适配的大小和位置。我们将longest恢复到原来满状态的值。继续向上回溯,检查是否存在合并的块,依据就是左右子树longest的值相加是否等于原空闲块满状态的大小,如果能够合并,就将父节点longest标记为相加的和。 + +### 小结 +上面两个成对alloc/free接口的时间复杂度都是O(logN),保证了程序运行性能。然而这段程序设计的独特之处就在于使用加权来标记内存空闲状态,而不是一般的有限状态机,实际上longest既可以表示权重又可以表示状态,状态机就毫无必要了, +