]> pd.if.org Git - pdclib/blobdiff - functions/stdlib/free.c
Comment cleanups.
[pdclib] / functions / stdlib / free.c
index 150d7b337ae60167941f87315c6d13be28e0d4bd..04217e68e86eb0821331450292853bda3885d9cc 100644 (file)
@@ -1,48 +1,50 @@
-/* ----------------------------------------------------------------------------
- * $Id$
- * ----------------------------------------------------------------------------
- * Public Domain C Library - http://pdclib.sourceforge.net
- * This code is Public Domain. Use, modify, and redistribute at will.
- * --------------------------------------------------------------------------*/
+/* void free( void * )
 
-void free( void * ptr ) { /* TODO */ };
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
 
-/* Thomas "Beyond Infinity" Kreitner's code - unchecked, unoptimized
+#include <stdlib.h>
+
+#ifndef REGTEST
+
+#include <_PDCLIB_int.h>
+
+/* TODO: Primitive placeholder. Much room for improvement. */
+
+/* structure holding first and last element of free node list */
+extern struct _PDCLIB_headnode_t _PDCLIB_memlist;
+
+void free( void * ptr )
 {
-    chunk_t * chunk;
-    chunk_t * prev_chunk;
-    chunk = used_mem.start;
-    do
+    if ( ptr == NULL )
     {
-        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;
+        return;
     }
-    else if ( chunk == used_mem.last )
+    ptr = (void *)( (char *)ptr - sizeof( struct _PDCLIB_memnode_t ) );
+    ( (struct _PDCLIB_memnode_t *)ptr )->next = NULL;
+    if ( _PDCLIB_memlist.last != NULL )
     {
-        /* chunk is the last element of used_mem list */
-        prev_chunk->next = NULL;
-        used_mem.last = prev_chunk;
+        _PDCLIB_memlist.last->next = ptr;
     }
     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;
+        _PDCLIB_memlist.first = ptr;
     }
+    _PDCLIB_memlist.last = ptr;
+}
+
+#endif
+
+#ifdef TEST
+#include <_PDCLIB_test.h>
+#include <stdbool.h>
+
+int main( void )
+{
+    free( NULL );
+    TESTCASE( true );
+    return TEST_RESULTS;
 }
+
+#endif