Skip to content

Instantly share code, notes, and snippets.

@muaddib1971
Last active September 15, 2022 01:26
Show Gist options
  • Save muaddib1971/8905b76cf38e2d36aa97a0b7104d2ea4 to your computer and use it in GitHub Desktop.
Save muaddib1971/8905b76cf38e2d36aa97a0b7104d2ea4 to your computer and use it in GitHub Desktop.
beginning of malloc impementation
#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