Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- struct boundary_t
- {
- boundary_t *next;
- boundary_t *previous;
- size_t size;
- };
- boundary_t *head = 0;
- void *malloc(size_t size)
- {
- if (!head) {
- head = (boundary_t *) sbrk(sizeof(boundary_t));
- head->next = 0;
- head->previous = 0;
- head->size = 0;
- };
- boundary_t *b = head;
- size_t needed_size = sizeof(boundary_t) + size;
- for (; b->next; b = b->next) {
- if (!b->size) {
- size_t free_size = ((size_t) b->next) - ((size_t) (b + 1));
- if (free_size >= needed_size) {
- b->size = size;
- boundary_t *next = b->next;
- b->next = (boundary_t *)(((size_t) (b + 1)) + size);
- b->next->next = next;
- b->next->previous = b;
- b->next->size = 0;
- return (void *) (b + 1);
- };
- };
- };
- void *ptr = sbrk(size);
- b->next = (boundary_t *) sbrk(sizeof(boundary_t));
- b->next->next = 0;
- b->next->previous = b;
- b->next->size = 0;
- return ptr;
- };
- void free(void *ptr)
- {
- if (!head)
- return;
- boundary_t *b = head;
- for (; b; b = b->next) {
- if (((void *) (b + 1)) == ptr) {
- if (!b->size)
- return;
- if (b == head) {
- head->size = 0;
- head->next = head->next->next;
- head->next->previous = head;
- return;
- };
- if (b->next->next) {
- head->next->next->previous = head->previous;
- head->previous->next = head->next->next;
- return;
- };
- head->next->previous = head->previous;
- head->previous = head->next;
- return;
- };
- };
- };
- int main()
- {
- return 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement