Update README with new example

This commit is contained in:
2024-05-14 13:06:58 +02:00
parent 7790547482
commit 0caf457cc4
+7 -13
View File
@@ -10,31 +10,25 @@ Here's an example:
int main(void) int main(void)
{ {
#define NUM_PAGES 16
// This is the memory we will allocate from // This is the memory we will allocate from
char memory[NUM_PAGES * BUDDY_ALLOC_MAX_BLOCK_SIZE]; char memory[1 << 20];
struct page_info info[NUM_PAGES]; // This is required to keep track of allocation state struct buddy *alloc = buddy_startup(memory, sizeof(memory));
struct buddy_alloc alloc = buddy_startup(memory, sizeof(memory), info, NUM_PAGES);
// Allocate some memory (NULL is returned on failure) // Allocate some memory (NULL is returned on failure)
// If the memory pool passed to the allocator was properly aligned, char *str1 = buddy_malloc(alloc, 32);
// you can count to allocate any and all bytes of that pool. char *str2 = buddy_malloc(alloc, 32);
char *str1 = buddy_malloc(&alloc, 32);
char *str2 = buddy_malloc(&alloc, 32);
strncpy(str1, "Hello", 32); strncpy(str1, "Hello", 32);
strncpy(str2, "world", 32); strncpy(str2, "world", 32);
printf("%s, %s!\n", str1, str2); printf("%s, %s!\n", str1, str2);
buddy_free(&alloc, 32, str1); buddy_free(alloc, 32, str1);
buddy_free(&alloc, 32, str2); buddy_free(alloc, 32, str2);
buddy_free(&alloc, 32, str2); // Double frees are caught! buddy_free(alloc, 32, str2); // Double frees are caught!
buddy_cleanup(&alloc);
return 0; return 0;
} }
``` ```