Skip to content

Instantly share code, notes, and snippets.

@jack126guy
Last active Aug 29, 2015
Embed
What would you like to do?
"Free list" example
/*
* http://en.wikipedia.org/wiki/Free_list
* This code in the public domain under Creative Commons CC0 1.0 Universal Public Domain Dedication:
* http://creativecommons.org/publicdomain/zero/1.0/
*/
typedef struct free_list_node {
void* memory;
size_t size;
struct free_list_node* next;
} free_list_node;
static free_list_node* free_list = NULL;
void* free_list_alloc(size_t size) {
free_list_node* foundnode;
void* memoryptr;
/* Do stuff
* ...
* Assume that we've found/created a node with a memory block large enough
* and put a pointer to it in "foundnode".
*/
memoryptr = foundnode->memory;
/* Delete the node from the list.
* ...
*/
return memoryptr;
}
void free_list_free(void* memory) {
free_list_node* returnednode;
returnednode = malloc(sizeof(*returnednode));
if(!returnednode) {
/* Handle allocation falure... */
} else {
/* Add the node to the list... */
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment