Add example and readme

This commit is contained in:
2024-05-09 22:55:42 +02:00
parent 532eabb264
commit c6bfc90bcd
6 changed files with 82 additions and 6 deletions
+3 -1
View File
@@ -1,4 +1,6 @@
test
test.exe
test2
test2.exe
test2.exe
example
example.exe
+40
View File
@@ -0,0 +1,40 @@
# A Buddy Allocator
This is a general purpose allocator designed for memory-constrained environments. It uses the Buddy System allocation scheme. Only allocations with a size that is a power of 2 are supported. Any size other that's not a power of 2 will be rounded up.
Here's an example:
```c
#include <stdio.h>
#include <string.h>
#include "buddy.h"
int main(void)
{
#define NUM_PAGES 16
// This is the memory we will allocate from
char memory[NUM_PAGES * BUDDY_ALLOC_MAX_BLOCK_SIZE];
struct page_info info[NUM_PAGES]; // This is required to keep track of allocation state
struct buddy_alloc alloc = buddy_startup(memory, sizeof(memory), info, NUM_PAGES);
// Allocate some memory (NULL is returned on failure)
// If the memory pool passed to the allocator was properly aligned,
// you can count to allocate any and all bytes of that pool.
char *str1 = buddy_malloc(&alloc, 32);
char *str2 = buddy_malloc(&alloc, 32);
strncpy(str1, "Hello", 32);
strncpy(str2, "world", 32);
printf("%s, %s!\n", str1, str2);
buddy_free(&alloc, 32, str1);
buddy_free(&alloc, 32, str2);
buddy_free(&alloc, 32, str2); // Double frees are caught!
buddy_cleanup(&alloc);
return 0;
}
```
+33
View File
@@ -0,0 +1,33 @@
#include <stdio.h>
#include <string.h>
#include "buddy.h"
int main(void)
{
#define NUM_PAGES 16
// This is the memory we will allocate from
char memory[NUM_PAGES * BUDDY_ALLOC_MAX_BLOCK_SIZE];
struct page_info info[NUM_PAGES]; // This is required to keep track of allocation state
struct buddy_alloc alloc = buddy_startup(memory, sizeof(memory), info, NUM_PAGES);
// Allocate some memory (NULL is returned on failure)
// If the memory pool passed to the allocator was properly aligned,
// you can count to allocate any and all bytes of that pool.
char *str1 = buddy_malloc(&alloc, 32);
char *str2 = buddy_malloc(&alloc, 32);
strncpy(str1, "Hello", 32);
strncpy(str2, "world", 32);
printf("%s, %s!\n", str1, str2);
buddy_free(&alloc, 32, str1);
buddy_free(&alloc, 32, str2);
buddy_free(&alloc, 32, str2); // Double frees are caught!
buddy_cleanup(&alloc);
return 0;
}
+3 -2
View File
@@ -1,4 +1,5 @@
all:
gcc test.c buddy.c -o test -Wall -Wextra -ggdb #-fsanitize=address,undefined
gcc test2.c buddy.c -o test2 -Wall -Wextra -ggdb
gcc test.c buddy.c -o test -Wall -Wextra -ggdb
gcc test2.c buddy.c -o test2 -Wall -Wextra -ggdb
gcc example.c buddy.c -o example -Wall -Wextra -ggdb
+2 -2
View File
@@ -46,7 +46,7 @@ int main(void)
int i = rand() % num_current_allocs;
struct alloc_info deallocating = current_allocs[i];
current_allocs[i] = current_allocs[--num_current_allocs];
fprintf(stderr, "buddy_free(%lu, %lu)\n", deallocating.len, (uintptr_t) deallocating.ptr - (uintptr_t) alloc.base);
fprintf(stderr, "buddy_free(%d, %d)\n", (int) deallocating.len, (int) ((uintptr_t) deallocating.ptr - (uintptr_t) alloc.base));
buddy_free(&alloc, deallocating.len, (void*) deallocating.ptr);
}
}
@@ -61,7 +61,7 @@ int main(void)
//fprintf(stderr, "buddy_malloc(%lu) = NULL\n", len);
} else {
//buddy_dump(&alloc, stderr);
fprintf(stderr, "buddy_malloc(%lu) = %lu\n", len, (uintptr_t) ptr - (uintptr_t) alloc.base);
fprintf(stderr, "buddy_malloc(%d) = %d\n", (int) len, (int) ((uintptr_t) ptr - (uintptr_t) alloc.base));
}
if (ptr != NULL) {
+1 -1
View File
@@ -56,7 +56,7 @@ int main(void)
}
}
fprintf(stderr, "performed=%lu, expected=%u\n", performed, MAX_ALLOCS);
fprintf(stderr, "performed=%d, expected=%d\n", (int) performed, (int) MAX_ALLOCS);
buddy_cleanup(&alloc);
return 0;