]> pd.if.org Git - pdclib/blob - functions/stdlib/free.c
Fix for #42 - free( NULL ) must not fail.
[pdclib] / functions / stdlib / free.c
1 /* $Id$ */
2
3 /* void free( void * )
4
5    This file is part of the Public Domain C Library (PDCLib).
6    Permission is granted to use, modify, and / or redistribute at will.
7 */
8
9 #include <stdlib.h>
10
11 #ifndef REGTEST
12
13 #ifndef _PDCLIB_INT_H
14 #define _PDCLIB_INT_H _PDCLIB_INT_H
15 #include <_PDCLIB_int.h>
16 #endif
17
18 /* TODO: Primitive placeholder. Much room for improvement. */
19
20 /* structure holding first and last element of free node list */
21 extern struct _PDCLIB_headnode_t _PDCLIB_memlist;
22
23 void free( void * ptr )
24 {
25     if ( ptr == NULL )
26     {
27         return;
28     }
29     ptr = (void *)( (char *)ptr - sizeof( struct _PDCLIB_memnode_t ) );
30     ( (struct _PDCLIB_memnode_t *)ptr )->next = NULL;
31     if ( _PDCLIB_memlist.last != NULL )
32     {
33         _PDCLIB_memlist.last->next = ptr;
34     }
35     else
36     {
37         _PDCLIB_memlist.first = ptr;
38     }
39     _PDCLIB_memlist.last = ptr;
40 }
41
42 #endif
43
44 #ifdef TEST
45 #include <_PDCLIB_test.h>
46 #include <stdbool.h>
47
48 int main( void )
49 {
50     free( NULL );
51     TESTCASE( true );
52     return TEST_RESULTS;
53 }
54
55 #endif