https://github.com/jazzfool/buddy
C Buddy Allocator
https://github.com/jazzfool/buddy
Last synced: over 1 year ago
JSON representation
C Buddy Allocator
- Host: GitHub
- URL: https://github.com/jazzfool/buddy
- Owner: jazzfool
- License: mit
- Created: 2020-05-21T01:34:16.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-05-21T01:49:51.000Z (about 6 years ago)
- Last Synced: 2025-01-23T22:14:03.309Z (over 1 year ago)
- Language: C
- Size: 3.91 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Simple buddy allocator.
A buddy allocator allocates based on halved partitions.
The allocator will halve the partitions as much as possible,
then give the final halved partition to the allocatee.
Moreover, allocations within free'd partitions can allow for
coalescing of partitions (i.e. merging partitions).
This leads to minimal external fragmentation, but results in
internal fragmentation.
In order to use this implementation; in a single translation unit,
define BUDDY_ALLOC_IMPLEMENTATION then include "buddy.h".
In all other files, include "buddy.h" normally.
Note: This doesn't actually allocate any memory. The buffer simply
has a specified size and allocations consist of offset + size.
This is to be integrated with actual memory (e.g. a GPU buffer).
Example Usage;
```
BuddyBuffer* buf = buddy_buf_new(1024);
BuddyAllocation alloc;
if (!buddy_buf_alloc(buf, 30, &alloc)) {
printf("allocation failed!");
return;
}
printf("offset: %zu, size: %zu", alloc.offset, alloc.size);
buddy_buf_free(alloc); // this is optional
buddy_buf_destroy(buf);
```
This is licensed under the MIT License.