Be excrutiatingly correct with inline assembly syntax
[syslinux.git] / dos / free.c
blobaa17080d211e86b47f3fab736efc53d9eabf41c5
1 /*
2 * free.c
4 * Very simple linked-list based malloc()/free().
5 */
7 #include <stdlib.h>
8 #include "malloc.h"
10 static struct free_arena_header *
11 __free_block(struct free_arena_header *ah)
13 struct free_arena_header *pah, *nah;
15 pah = ah->a.prev;
16 nah = ah->a.next;
17 if ( pah->a.type == ARENA_TYPE_FREE &&
18 (char *)pah+pah->a.size == (char *)ah ) {
19 /* Coalesce into the previous block */
20 pah->a.size += ah->a.size;
21 pah->a.next = nah;
22 nah->a.prev = pah;
24 #ifdef DEBUG_MALLOC
25 ah->a.type = ARENA_TYPE_DEAD;
26 #endif
28 ah = pah;
29 pah = ah->a.prev;
30 } else {
31 /* Need to add this block to the free chain */
32 ah->a.type = ARENA_TYPE_FREE;
34 ah->next_free = __malloc_head.next_free;
35 ah->prev_free = &__malloc_head;
36 __malloc_head.next_free = ah;
37 ah->next_free->prev_free = ah;
40 /* In either of the previous cases, we might be able to merge
41 with the subsequent block... */
42 if ( nah->a.type == ARENA_TYPE_FREE &&
43 (char *)ah+ah->a.size == (char *)nah ) {
44 ah->a.size += nah->a.size;
46 /* Remove the old block from the chains */
47 nah->next_free->prev_free = nah->prev_free;
48 nah->prev_free->next_free = nah->next_free;
49 ah->a.next = nah->a.next;
50 nah->a.next->a.prev = ah;
52 #ifdef DEBUG_MALLOC
53 nah->a.type = ARENA_TYPE_DEAD;
54 #endif
57 /* Return the block that contains the called block */
58 return ah;
61 void free(void *ptr)
63 struct free_arena_header *ah;
65 if ( !ptr )
66 return;
68 ah = (struct free_arena_header *)
69 ((struct arena_header *)ptr - 1);
71 #ifdef DEBUG_MALLOC
72 assert( ah->a.type == ARENA_TYPE_USED );
73 #endif
75 __free_block(ah);
77 /* Here we could insert code to return memory to the system. */