Last active
September 15, 2022 01:26
-
-
Save muaddib1971/8905b76cf38e2d36aa97a0b7104d2ea4 to your computer and use it in GitHub Desktop.
beginning of malloc impementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <unistd.h> | |
struct alloc { | |
void* data; | |
long size; | |
}; | |
struct alloc thememory[BUFSIZ]; | |
struct alloc frees[BUFSIZ] = {0}; | |
long num_allocs = 0; | |
long num_frees = 0; | |
void* allocate(size_t mem) { | |
struct alloc allocation; | |
int count; | |
for (count = 0; count < num_allocs; ++count) { | |
if (frees[count].size >= mem) { | |
/* move the alloc from the free list to the allocated list */ | |
/* return the chunk */ | |
} | |
} | |
allocation.data = sbrk(mem); | |
thememory[num_allocs++] = allocation; | |
return allocation.data; | |
} | |
void dealloc(void* mem) {} | |
int main() { | |
int* array = allocate(sizeof(int) * 20); | |
dealloc(array); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment