4 * Very simple linked-list based malloc()/free().
10 static struct free_arena_header
*
11 __free_block(struct free_arena_header
*ah
)
13 struct free_arena_header
*pah
, *nah
;
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
;
25 ah
->a
.type
= ARENA_TYPE_DEAD
;
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
;
53 nah
->a
.type
= ARENA_TYPE_DEAD
;
57 /* Return the block that contains the called block */
63 struct free_arena_header
*ah
;
68 ah
= (struct free_arena_header
*)
69 ((struct arena_header
*)ptr
- 1);
72 assert( ah
->a
.type
== ARENA_TYPE_USED
);
77 /* Here we could insert code to return memory to the system. */