4 * Ullrich von Bassewitz, 19.06.1998
14 void _hadd (void* mem, size_t size)
15 /* Add an arbitrary memory block to the heap. This function is used by
16 * free(), but it does also allow usage of otherwise unused memory
17 * blocks as heap space. The given block is entered in the free list
18 * without any checks, so beware!
22 struct freeblock* left;
23 struct freeblock* right;
25 if (size >= sizeof (struct freeblock)) {
27 /* Set the admin data */
28 f = (struct freeblock*) mem;
31 /* Check if the freelist is empty */
34 /* The freelist is empty until now, insert the block */
42 /* We have to search the free list. As we are doing so, we check
43 * if it is possible to combine this block with another already
44 * existing block. Beware: The block may be the "missing link"
45 * between *two* other blocks.
49 while (right && f > right) {
55 /* Ok, the current block must be inserted between left and right (but
56 * beware: one of the two may be zero!). Also check for the condition
57 * that we have to merge two or three blocks.
60 /* Check if we must merge the block with the right one */
61 if (((unsigned) f) + size == (unsigned) right) {
62 /* Merge with the right block */
63 f->size += right->size;
64 if (f->next = right->next) {
67 /* This is now the last block */
71 /* No merge, just set the link */
77 /* Special case: This is the new freelist end */
81 /* Check if we must merge the block with the left one */
82 if ((unsigned) f == ((unsigned) left) + left->size) {
83 /* Merge with the left block */
84 left->size += f->size;
85 if (left->next = f->next) {
86 left->next->prev = left;
88 /* This is now the last block */
92 /* No merge, just set the link */
98 /* Special case: This is the new freelist start */