我开始实现一个通用内存池。这是出于学习目的,所以肯定有很多错误。但是,我在前进。现在我被困在一个新的部分。一、代码
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
typedef enum { FALSE, TRUE } BOOL;
typedef struct mem_block {
uint8_t* data;
size_t block_size;
size_t pool_position;
BOOL is_freed;
} mem_block;
typedef struct mem_pool {
mem_block* blocks;
size_t index;
size_t pool_size;
} mem_pool;
mem_pool *pool_init() {
mem_pool *pool = (mem_pool *) malloc(sizeof(mem_pool));
pool->pool_size = (size_t) 128;
mem_block* blk = (mem_block *) malloc(pool->pool_size * sizeof (mem_block));
pool->index = 0;
pool->blocks = blk;
return pool;
}
void *pool_allocate(mem_pool **pool, size_t size) {
mem_pool* _pool = *pool;
size_t free_portion = _pool->pool_size - _pool->index;
if(size < free_portion){
mem_block* allocated_blk = _pool->blocks + _pool->index;
uint8_t* data = (uint8_t*) malloc(size * sizeof(uint8_t));
allocated_blk->data = data;
allocated_blk->block_size = size;
allocated_blk->is_freed = FALSE;
allocated_blk->pool_position = _pool->index;
_pool->index += size;
return (void *) allocated_blk->data;
}
else{
printf("Pool is out of memory");
return NULL;
}
}
/*void pool_free(mem_pool **pool, void *block) {
mem_block* cur = (mem_block*) block;
mem_block* next = cur + 1;
// override the unneeded memory
memmove(cur, next, (*pool)->pool_size - next->pool_position);
}*/
typedef struct complex {
double i;
double r;
} complex;
mem_pool *GLOBAL_POOL = pool_init();
int main() {
complex *c1 = (complex *) pool_allocate(&GLOBAL_POOL, sizeof(complex));
c1->r = 1.0;
c1->i = 2.0;
printf("Value is (%f + %fi)\n", c1->r, c1->i);
printf("Remaining free size is %ld\n", GLOBAL_POOL->pool_size - GLOBAL_POOL->index);
complex *c2 = (complex *) pool_allocate(&GLOBAL_POOL, sizeof(complex));
c2->r = 2.0;
c2->i = 3.0;
printf("Value is (%f + %fi)\n", c2->r, c2->i);
printf("Remaining free size is %ld\n", GLOBAL_POOL->pool_size - GLOBAL_POOL->index);
mem_block* cur = (mem_block *) &c2;
printf("Position of c2 is %ld\n", cur->pool_position);
printf("Adress of c2's block is %x\n", cur);
printf("Address of c2 is %x\n", &c2);
printf("c2 points to %x\n", c2);
complex *c3 = (complex *) pool_allocate(&GLOBAL_POOL, sizeof(complex));
c3->r = 3.0;
c3->i = 4.0;
printf("Value is (%f + %fi)\n", c3->r, c3->i);
printf("Remaining free size is %ld\n", GLOBAL_POOL->pool_size - GLOBAL_POOL->index);
cur = (mem_block *) &c3;
printf("Position of c3 is %ld\n", cur->pool_position);
printf("Adress of c3's block is %x\n", cur);
printf("Address of c3 is %x\n", &c3);
printf("c3 points to %x\n", c3);
complex *c4 = (complex *) pool_allocate(&GLOBAL_POOL, sizeof(complex));
c4->r = 4.0;
c4->i = 5.0;
printf("Value is (%f + %fi)\n", c4->r, c4->i);
printf("Remaining free size is %ld\n", GLOBAL_POOL->pool_size - GLOBAL_POOL->index);
complex *c5 = (complex *) pool_allocate(&GLOBAL_POOL, sizeof(complex));
c5->r = 5.0;
c5->i = 6.0;
printf("Value is (%f + %fi)\n", c5->r, c5->i);
printf("Remaining free size is %ld\n", GLOBAL_POOL->pool_size - GLOBAL_POOL->index);
complex *c6 = (complex *) pool_allocate(&GLOBAL_POOL, sizeof(complex));
c6->r = 6.0;
c6->i = 7.0;
printf("Value is (%f + %fi)\n", c6->r, c6->i);
printf("Remaining free size is %ld\n", GLOBAL_POOL->pool_size - GLOBAL_POOL->index);
complex *c7 = (complex *) pool_allocate(&GLOBAL_POOL, sizeof(complex));
c7->r = 7.0;
c7->i = 8.0;
printf("Value is (%f + %fi)\n", c7->r, c7->i);
printf("Remaining free size is %ld\n", GLOBAL_POOL->pool_size - GLOBAL_POOL->index);
complex *c8 = (complex *) pool_allocate(&GLOBAL_POOL, sizeof(complex));
if(c8 != NULL) {
c8->r = 3.0;
c8->i = 4.0;
printf("Value is (%f + %fi)\n", c8->r, c8->i);
}else {
return -1;
}
return 0;
}
目前,它的大小是 128 字节,但是一旦我掌握了基础知识,我就会让它变得无限大。分配可能工作正常,您可以看到输出并看到我可以设置分配的指针并使用该值。最重要的是,我想实现free
. 它在注释掉的函数pool_free
中。要执行memmove
,我需要知道mem_block
要删除的索引。
在 中pool_allocate
,您可以看到我返回了data
分配块的指针,而不是整个内容,因此它可以像常规一样使用malloc
。这意味着,在释放时,我需要mem_block
从数据指针中恢复。为了能够做到这一点,我将data
指针作为 a 的第一个元素mem_block
。
让我进一步解释代码
mem_block* cur = (mem_block *) &c3;
printf("Position of c3 is %ld\n", cur->pool_position);
printf("Adress of c3's block is %x\n", cur);
printf("Address of c3 is %x\n", &c3);
printf("c3 points to %x\n", c3);
c3
这里是分配使用pool_allocate
和结果指向数据的指针,uint8_t*
实际上被强制转换complex *
为使用。所以c3
指向一个complex
对象。取消引用应该提供实际数据,我认为这是可行的。但它也有自己的地址。我认为这个地址与其块的指针相同,data
因为它是 a 中的第一个成员mem_block
。所以我将它自由地转换为mem_block*
. 但这不起作用。
printf("Position of c3 is %ld\n", cur->pool_position);
说Position of c3 is 0
或其他胡言乱语。我期待看到 16、32 等内容,因为每个mem_block
都是 16 个字节。那么,为什么你认为演员
mem_block* cur = (mem_block *) &c3;
不会让我和它一起工作c3
吗mem_block
?也许我无法通过引用正确传递指针,因此对指向的事物的更改在外部不可见?这似乎不太可能,因为我可以按预期使用分配的对象,但谁知道呢?我检查了每个部分,但仍然无法解决问题。