对于通用内存池,如何在C中自由地在指针之间进行转换?

时间:2018-05-29 11:42:49

标签: c pointers memory-pool

我开始实现通用内存池。这是出于学习目的,因此肯定存在大量错误。但是,我正在向前迈进。现在我被困在一个新的部分。首先,代码

#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指针作为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指针相同,因为它是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一样?也许我无法正确地通过引用传递指针,以便在外部看不到指向事物的更改?这似乎不太可能,因为我可以按预期使用已分配的对象,但谁知道?我检查了每个部分,但仍无法解决问题。

1 个答案:

答案 0 :(得分:1)

它在malloc中的工作方式是每个块都有一个头(管理结构),在alloc上你返回该结构后的地址。然后在free上你知道你在标题之后有地址,这样你就可以从那里计算出实际标题的地址。

一个简单的技巧是使用

void alloc(size_t size) {
    ...
    struct mem_block *block = <address of block you return>
    return &block[1];
}

void free(void *addr) {
    struct mem_block *block = addr;
    block[-1].is_freed = false;
    ...
}