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