簡體   English   中英

Buddy 分配器、內存塊和 FreeRTOS

[英]Buddy allocator, blocks of memory and FreeRTOS

我正在嘗試在 C 語言中為 FreeRTOS 實現好友分配器。

我做了一個函數 buddy_free 用於內存管理。 我正在使用 struct _buddy_block 和函數進行分配和內存管理,但事情進展不順利,我需要你的幫助。 以下是我的資料來源和問題:

typedef struct _buddy_block {
    struct _buddy_block *next;
    size_t size;
    bool is_free;
} buddy_block_t;

typedef struct {
    buddy_block_t *freelist;
    size_t total_size;
    size_t min_block_size;
} buddy_allocator_t;

分配:

void *buddy_alloc(buddy_allocator_t *allocator, size_t size) {
    // Find the first free block that is large enough to satisfy the request
    buddy_block_t *block = allocator->freelist;
    while (block != NULL && (block->size < size || !block->is_free)) {
        block = block->next;
    }

    // If no suitable block was found, return NULL
    if (block == NULL) {
        return NULL;
    }

    // Split the block into two blocks if the block is larger than needed
    if (block->size > size) {
        // Create a new block for the remainder
        buddy_block_t *remainder = (buddy_block_t *) ((uint8_t *) block + size);

        remainder->size = block->size - size;
        remainder->is_free = true;
        remainder->next = block->next;

        // Update the current block
        block->size = size;
        block->next = remainder;
    }

    // Mark the block as allocated and return a pointer to the memory
    block->is_free = false;
    return (void *) (block + 1);
}
void buddy_free(buddy_allocator_t *allocator, void *ptr) {
    // Get a pointer to the block header
    buddy_block_t *block = (buddy_block_t *) ptr - 1;

    if (block->is_free) {
        return;
    }


    // Mark the block as free
    block->is_free = true;

    // Try to merge the block with its buddy (if it has one and the buddy is free)
    size_t block_size = block->size;
    buddy_block_t *buddy = (buddy_block_t *) ((uint8_t *) block + block_size);

    // Check if the buddy block is within the memory region managed by the allocator
    if (block < allocator->freelist ||
        block > (buddy_block_t *) ((uint8_t *) allocator->freelist + allocator->total_size) ||
        buddy < allocator->freelist ||
        buddy > (buddy_block_t *) ((uint8_t *) allocator->freelist + allocator->total_size)) {
    // One of the blocks is outside of the memory region managed by the allocator, so they cannot be merged
        return;
    }

    // Check if the buddy block is free and has the same size as the current block
    if (buddy->is_free && buddy->size == block_size) {
        // The buddy is free and has the same size as the current block, so they can be merged
        if (buddy < block) {
            // The buddy comes before the current block in memory, so it should be the new block
            buddy->size *= 2;
            buddy->next = block->next;
            block = buddy;
        } else {
        // The current block comes before the buddy in memory, so it should be the new block
            block->size *= 2;
            block->next = buddy->next;
        }
    }

// Insert the merged block back into the free list
    buddy_block_t *prev = NULL;
    buddy_block_t *curr = allocator->freelist;
    while (curr != NULL && curr < block) {
        prev = curr;
        curr = curr->next;
    }
    block->next = curr;
    if (prev == NULL) {
        allocator->freelist = block;
    } else {
        prev->next = block;
    }
}

第一個問題是在行中:

while (block != NULL && (block->size < size || !block->is_free))

我在 test_buddy_alloc_insufficient_memory 測試中遇到分段錯誤:

// Test the behavior of the buddy_alloc function when it is unable to fulfill an allocation request due to insufficient free memory
void test_buddy_alloc_insufficient_memory() {
    // Allocate all of the available memory
    buddy_allocator_t allocator;
    void *ptr = buddy_alloc(&allocator, allocator.total_size);
    assert(ptr != NULL);

    // Attempt to allocate more memory
    ptr = buddy_alloc(&allocator, 1);
    assert(ptr == NULL);
}
// Test the behavior of the buddy_alloc function when it is called with a size of 0
void test_buddy_alloc_size_zero() {
    buddy_allocator_t allocator;
    // Attempt to allocate a block of size 0
    void *ptr = buddy_alloc(&allocator, 0);
    assert(ptr == NULL);
}

有人可以幫我修復或改進我的代碼嗎?

你有 UB(未定義的行為)。

在你的test_*函數中,你有:

buddy_allocator_t allocator;

這是在堆棧上並且未初始化

你需要:

buddy_allocator_t allocator = { 0 };

使用-Wall ,編譯器會標記:

orig.c: In function ‘test_buddy_alloc_insufficient_memory’:
orig.c:114:14: warning: ‘allocator.total_size’ is used uninitialized in this function [-Wuninitialized]
  void *ptr = buddy_alloc(&allocator, allocator.total_size);
              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

此外,在buddy_alloc中,它做的第一件事是:

buddy_block_t *block = allocator->freelist;

while (block != NULL && (block->size < size || !block->is_free))

block != NULL是不夠的。 防止取消引用非空但隨機/無效的指針值。


更新:

從討論和進一步審查中,我沒有看到任何代碼來設置具有有效塊的分配器結構。 因此,分配器在沒有任何內存的情況下啟動。

通常,我們會調用brk/sbrk來從系統中獲取更多內存,但是為了測試,我們可以使用靜態/持久固定數組。

這是重組后的代碼:

#include <stddef.h>
#include <stdint.h>
#include <assert.h>
#include <stdbool.h>
#include <string.h>

typedef struct _buddy_block {
    struct _buddy_block *next;
    size_t size;
    bool is_free;
} buddy_block_t;

typedef struct {
    buddy_block_t *freelist;
    size_t total_size;
    size_t min_block_size;
} buddy_allocator_t;

void *
buddy_alloc(buddy_allocator_t * allocator, size_t size)
{
    // Find the first free block that is large enough to satisfy the request
    buddy_block_t *block = allocator->freelist;

    while (block != NULL && (block->size < size || !block->is_free)) {
        block = block->next;
    }

    // If no suitable block was found, return NULL
    if (block == NULL) {
        return NULL;
    }

    // Split the block into two blocks if the block is larger than needed
    if (block->size > size) {
        // Create a new block for the remainder
        buddy_block_t *remainder = (buddy_block_t *) ((uint8_t *) block + size);

        remainder->size = block->size - size;
        remainder->is_free = true;
        remainder->next = block->next;

        // Update the current block
        block->size = size;
        block->next = remainder;
    }

    // Mark the block as allocated and return a pointer to the memory
    block->is_free = false;
    return (void *) (block + 1);
}

void
buddy_free(buddy_allocator_t * allocator, void *ptr)
{
    // Get a pointer to the block header
    buddy_block_t *block = (buddy_block_t *) ptr - 1;

    if (block->is_free) {
        return;
    }

    // Mark the block as free
    block->is_free = true;

    // Try to merge the block with its buddy (if it has one and the buddy is free)
    size_t block_size = block->size;
    buddy_block_t *buddy = (buddy_block_t *) ((uint8_t *) block + block_size);

    // Check if the buddy block is within the memory region managed by the allocator
    if (block < allocator->freelist || block > (buddy_block_t *) ((uint8_t *) allocator->freelist + allocator->total_size) || buddy < allocator->freelist || buddy > (buddy_block_t *) ((uint8_t *) allocator->freelist + allocator->total_size)) {
        // One of the blocks is outside of the memory region managed by the allocator, so they cannot be merged
        return;
    }

    // Check if the buddy block is free and has the same size as the current block
    if (buddy->is_free && buddy->size == block_size) {
        // The buddy is free and has the same size as the current block, so they can be merged
        if (buddy < block) {
            // The buddy comes before the current block in memory, so it should be the new block
            buddy->size *= 2;
            buddy->next = block->next;
            block = buddy;
        }
        else {
            // The current block comes before the buddy in memory, so it should be the new block
            block->size *= 2;
            block->next = buddy->next;
        }
    }

// Insert the merged block back into the free list
    buddy_block_t *prev = NULL;
    buddy_block_t *curr = allocator->freelist;

    while (curr != NULL && curr < block) {
        prev = curr;
        curr = curr->next;
    }
    block->next = curr;
    if (prev == NULL) {
        allocator->freelist = block;
    }
    else {
        prev->next = block;
    }
}

void
initme(buddy_allocator_t *ctl)
{
#if 0
    static buddy_block_t block;
    static char mem[1024 * 1024];

    memset(&block,0,sizeof(block));
    block.size = sizeof(mem);
    block.is_free = 1;

    ctl->total_size = block.size;
    ctl->freelist = &block;
    ctl->min_block_size = 128;
#else
    static char mem[1024 * 1024];
    buddy_block_t *block = (void *) mem;

    memset(block,0,sizeof(*block));
    block->size = sizeof(mem) - sizeof(*block);
    block->is_free = 1;

    ctl->total_size = block->size;
    ctl->freelist = block;
    ctl->min_block_size = 128;
#endif
}

// Test the behavior of the buddy_alloc function when it is unable to fulfill an allocation request due to insufficient free memory
void
test_buddy_alloc_insufficient_memory()
{
    // Allocate all of the available memory
    buddy_allocator_t allocator = { 0 };
    initme(&allocator);

    void *ptr = buddy_alloc(&allocator, allocator.total_size);
    assert(ptr != NULL);

    // Attempt to allocate more memory
    ptr = buddy_alloc(&allocator, 1);
    assert(ptr == NULL);
}

// Test the behavior of the buddy_alloc function when it is called with a size of 0
void
test_buddy_alloc_size_zero()
{
    buddy_allocator_t allocator = { 0 };
    initme(&allocator);

    // Attempt to allocate a block of size 0
    void *ptr = buddy_alloc(&allocator, 0);

    assert(ptr == NULL);
}

int
main(void)
{

    test_buddy_alloc_insufficient_memory();
    test_buddy_alloc_size_zero();

    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM