内卷地狱

Handwritten Memory Pool (Simple Fixed-Size)

Edit Me

Handwritten Memory Pool (Simple Fixed-Size)

Simple Version (Fixed-Size Blocks)

Design Diagram

Design

Code Structure

typedef struct mempool_s {
    int blocksize;  // size of each memory block
    int freecount;   // number of remaining free blocks
    char *free_ptr;   // pointer to the next free block
    char *mem;   // head pointer of the entire memory pool
} mempool_t;
int memp_create(mempool_t *m, int block_size) {

    if (!m) return -1;

    // 1. initialize these two simple ints
    m->blocksize = block_size;
    m->freecount = MEM_PAGE_SIZE / block_size;

    // 2. allocate space for the entire pool and initialize m->mem
    m->mem = (char *)malloc(MEM_PAGE_SIZE);
    if (!m->mem) {  // allocation failed (not enough free memory)
        return -2;
    }
    // zero-initialize the allocated space
    memset(m->mem, 0, MEM_PAGE_SIZE);

    // 3. initialize free_ptr
    m->free_ptr = m->mem;

    // initialize the "next pointer" inside each block
    int i = 0;
    char *ptr = m->mem;
    for (i = 0;i < m->freecount;i ++) {

        *(char **)ptr = ptr + block_size;
        ptr = ptr + block_size;
    }
    // the last block's "next_ptr" points to NULL
    *(char **)ptr = NULL;
    return 0;
}
void *memp_alloc(mempool_t *m) {
    // pool is full
    if (!m || m->freecount == 0) return NULL;
    // 1. get the next free block as the return value
    void *ptr = m->free_ptr;
    // 2. update free_ptr
    m->free_ptr = *(char **)ptr;
    // 3. update freecount
    m->freecount --;

    return ptr;
}
void memp_free(mempool_t *m, void *ptr) {
    // equivalent to: ptr->next = m->free_ptr
    // insert the block to be freed at the head of the free list (head insertion)
    *(char **)ptr = m->free_ptr;
    // update free_ptr (the head of the free block linked list)
    m->free_ptr = (char *)ptr;
    // update freecount
    m->freecount ++;
}
void memp_destory(mempool_t *m) {
    if (!m) return ;
    // free the entire pool in one call, since the pool was malloc'd as a whole, not block by block
    free(m->mem);
}

Usage Example

int main() {
    mempool_t m;
    memp_create(&m, 32);

    void *p1 = memp_alloc(&m);
    printf("memp_alloc : %p\n", p1);

    void *p2 = memp_alloc(&m);
    printf("memp_alloc : %p\n", p2);

    void *p3 = memp_alloc(&m);
    printf("memp_alloc : %p\n", p3);

    memp_free(&m, p2);
}

贡献者


这篇文章有帮助吗?

最近更新

Involution Hell© 2026 byCommunityunderCC BY-NC-SA 4.0CCBYNCSA