简体   繁体   English

在 C 中测试实时系统的伙伴内存分配器

[英]Testing buddy memory allocator for real-time system in C

I am trying to write Buddy memory allocator for FreeRTOS.我正在尝试为 FreeRTOS 编写 Buddy 内存分配器。

Structs:结构:

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;

Here's some of my code:这是我的一些代码:

#include <stdint.h>
#include <cstring>
#include "buddy_alloc.h"

#define BUDDY_MIN_BLOCK_SIZE 32
#define NULL 0


void buddy_init(buddy_allocator_t *allocator, void *memory, size_t *size) {
    // Initialize the allocator structure
    allocator->total_size = *size;
    allocator->min_block_size = BUDDY_MIN_BLOCK_SIZE;
    allocator->freelist = (buddy_block_t *) memory;
    allocator->freelist->next = NULL;
    allocator->freelist->size = *size;
    allocator->freelist->is_free = true;
}

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;

    // 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 (buddy < allocator->freelist ||
        buddy > (buddy_block_t *) ((uint8_t *) allocator->freelist + allocator->total_size)) {
        // The buddy is outside of the memory region managed by the allocator, so it 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;
    }
}

Now I need to test it, I wrote some tests in main.c:现在我需要测试它,我在 main.c 中写了一些测试:

// Test 1: Test with a single block in the free list
void test_buddy_init_1() {
    // Initialize the allocator
    buddy_allocator_t allocator;
    size_t size = 128;
    buddy_init(&allocator, NULL, &size);

    // Check the total size of the memory region
    assert(allocator.total_size == 16);

    // Check the size of the first block in the free list
    assert(allocator.freelist->size == 16);

    // Check the "is_free" flag of the first block
    assert(allocator.freelist->is_free == true);

    // Check the "next" pointer of the first block
    assert(allocator.freelist->next == NULL);
}

// Test 2: Test with a larger memory region
void test_buddy_init_2() {
    // Initialize the allocator
    buddy_allocator_t allocator;
    size_t size = 128;
    buddy_init(&allocator, NULL, &size);

    // Check the total size of the memory region
    assert(allocator.total_size == 128);

    // Check the size of the first block in the free list
    assert(allocator.freelist->size == 128);

    // Check the "is_free" flag of the first block
    assert(allocator.freelist->is_free == true);

    // Check the "next" pointer of the first block
    assert(allocator.freelist->next == NULL);
}

When I run these tests I get Segmentation Fault - Process finished with exit code -1073741819 (0xC0000005)当我运行这些测试时,出现分段错误 - 进程已完成,退出代码为 -1073741819 (0xC0000005)

So, my question is, what's the issue?所以,我的问题是,问题是什么? It's because I run these tests on Windows?是因为我在 Windows 上运行这些测试? Or am I doing something wrong in the code?或者我在代码中做错了什么?

There's a NULL pointer dereference in buddy_init in your tests.在您的测试中,在buddy_init中有一个 NULL 指针取消引用。

void test_buddy_init_2() {
    // Initialize the allocator
    buddy_allocator_t allocator;
    size_t size = 128;
    buddy_init(&allocator, NULL, &size);
    ...

void buddy_init(buddy_allocator_t *allocator, void *memory, size_t *size) {
    // Initialize the allocator structure
    allocator->total_size = *size;
    allocator->min_block_size = BUDDY_MIN_BLOCK_SIZE;
    allocator->freelist = (buddy_block_t *) memory;
    allocator->freelist->next = NULL;
    ...

As can be seen memory is NULL, therefore allocator->freelist is NULL, therefore allocator->freelist->next is a NULL pointer dereference and therefore undefined behaviour.可以看出memory为 NULL,因此allocator->freelist为 NULL,因此allocator->freelist->next是 NULL 指针解引用,因此是未定义的行为。 This is the cause of your crash.这是你崩溃的原因。

I'm guessing that you should pass a suitably large block of memory to buddy_init instead of NULL.我猜你应该将一个适当大的内存块传递给buddy_init而不是 NULL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM