1 /* malloc(), realloc(), free() - simple memory allocation routines
3 * This is a very small and simple minded malloc Author: Kees J. Bot
4 * implementation. Ideal for things like a 29 Jan 1994
5 * bootstrap program, or for debugging. Six times
6 * slower than any good malloc.
18 #define debug(expr) ((void) 0)
20 #define debug(expr) expr
25 size_t size
; /* Size of a malloc()'ed object. */
27 unsigned magic
; /* To recognize a cell. */
29 struct cell
*next
; /* Next cell on the free list. */
31 unsigned sacred
; /* Don't touch while unallocated. */
35 #if UINT_MAX <= 0xFFFF
38 #define MAGIC 0x537BC0D8
41 /* Size of the header of an object. */
42 #define HDR_SIZE offsetof(cell_t, next)
44 /* An offset from a cell pointer to a next cell. */
45 #define offset(cp, size) ((cell_t *) ((char *) (cp) + (size)))
47 /* Address of the object in a cell and back. */
48 #define cell2obj(cp) ((void *) ((char *) (cp) + HDR_SIZE))
49 #define obj2cell(op) ((cell_t *) ((char *) (op) - HDR_SIZE))
52 static cell_t
*freelist
;
54 void *malloc(size_t size
)
55 /* Allocate an object of at least the given size. */
60 if (size
< sizeof(cell_t
)) size
= sizeof(cell_t
);
62 /* Align to a word. Use a real malloc if you need better alignment. */
63 size
= (size
+ sizeof(int) - 1) & ~(sizeof(int) - 1);
65 /* Space for a magic number at the end of the chunk. */
66 debug(size
+= sizeof(unsigned));
69 /* Do a first fit search. */
71 while ((cp
= *pcp
) != nil
) {
72 cell_t
*next
= cp
->next
;
74 assert(cp
->magic
== MAGIC
);
75 assert(cp
->sacred
== MAGIC
);
77 if (offset(cp
, cp
->size
) == next
) {
78 /* Join adjacent free cells. */
79 assert(next
->magic
== MAGIC
);
80 assert(next
->sacred
== MAGIC
);
82 cp
->size
+= next
->size
;
85 continue; /* Try again. */
87 if (size
<= cp
->size
) break; /* Big enough. */
93 if (cp
!= nil
) break; /* Found a big enough chunk. */
95 /* Allocate a new chunk at the break. */
96 if ((cp
= (cell_t
*) sbrk(size
)) == (cell_t
*) -1) {
102 debug(cp
->magic
= MAGIC
);
103 debug(cp
->sacred
= MAGIC
);
107 /* We've got a cell that is big enough. Can we break it up? */
108 if (cp
->size
>= size
+ sizeof(cell_t
)) {
109 cell_t
*next
= offset(cp
, size
);
111 next
->size
= cp
->size
- size
;
112 next
->next
= cp
->next
;
113 debug(next
->magic
= MAGIC
);
114 debug(next
->sacred
= MAGIC
);
119 /* Unchain the cell we've found and return an address in it. */
121 debug(memset(cell2obj(cp
), 0xAA, cp
->size
- HDR_SIZE
));
122 debug(((unsigned *) offset(cp
, cp
->size
))[-1]= MAGIC
);
128 /* Deallocate an object. */
130 cell_t
**prev
, *next
, *cp
;
132 if (op
== nil
) return; /* Aaargh. */
135 assert(cp
->magic
== MAGIC
);
136 assert(((unsigned *) offset(cp
, cp
->size
))[-1] == MAGIC
);
137 debug(cp
->sacred
= MAGIC
);
139 /* Find the spot where the object belongs. */
141 while ((next
= *prev
) != nil
&& next
< cp
) {
142 assert(next
->magic
== MAGIC
);
143 assert(next
->sacred
== MAGIC
);
147 /* Put the new free cell in the list. */
152 /* Check the rest of the list. */
153 while (next
!= nil
) {
154 assert(next
->magic
== MAGIC
);
155 assert(next
->sacred
== MAGIC
);
161 void *realloc(void *op
, size_t size
)
162 /* Change the size of an object. Don't bother being smart, just copy it. */
167 oldsize
= op
== nil
? 0 : obj2cell(op
)->size
- HDR_SIZE
;
170 memcpy(new, op
, oldsize
> size
? size
: oldsize
);
176 * $PchId: malloc.c,v 1.4 1996/02/22 09:15:56 philip Exp $