Last active
August 29, 2015 14:19
-
-
Save jack126guy/ae66d14893f59551ffcf to your computer and use it in GitHub Desktop.
"Free list" example
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
/* | |
* 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