X-Git-Url: https://pd.if.org/git/?p=pdclib;a=blobdiff_plain;f=functions%2Fstdlib%2Ffree.c;h=150d7b337ae60167941f87315c6d13be28e0d4bd;hp=5801248ebb22f10631e397976ac76eedca754873;hb=1d9d92ba957a0b8307c9a65c35867fde68e6533b;hpb=8c8750c2826684c2420571a8007b9606f72c9040 diff --git a/functions/stdlib/free.c b/functions/stdlib/free.c index 5801248..150d7b3 100644 --- a/functions/stdlib/free.c +++ b/functions/stdlib/free.c @@ -1,8 +1,48 @@ -// ---------------------------------------------------------------------------- -// $Id$ -// ---------------------------------------------------------------------------- -// Public Domain C Library - http://pdclib.sourceforge.net -// This code is Public Domain. Use, modify, and redistribute at will. -// ---------------------------------------------------------------------------- +/* ---------------------------------------------------------------------------- + * $Id$ + * ---------------------------------------------------------------------------- + * Public Domain C Library - http://pdclib.sourceforge.net + * This code is Public Domain. Use, modify, and redistribute at will. + * --------------------------------------------------------------------------*/ void free( void * ptr ) { /* TODO */ }; + +/* Thomas "Beyond Infinity" Kreitner's code - unchecked, unoptimized +{ + chunk_t * chunk; + chunk_t * prev_chunk; + chunk = used_mem.start; + do + { + prev_chunk = chunk; + chunk = chunk->next; + } while ( ( chunk != NULL ) && ( chunk->address != ptr ) ); + if ( chunk->address == ptr ) + { + if( used_mem.start->next->next == NULL ) + { + /* used_mem list has only one entry */ + chunk = used_mem.start->next; + used_mem.last = used_mem.start; + } + else if ( chunk == used_mem.last ) + { + /* chunk is the last element of used_mem list */ + prev_chunk->next = NULL; + used_mem.last = prev_chunk; + } + else + { + /* chunk is neither only nor last in the used_mem list */ + prev_chunk->next = prev_chunk->next->next; + } + chunk->next = NULL; + free_mem.last->next = chunk; + free_mem.last = chunk; + return chunk; + } + else + { + return NULL; + } +}