Repository navigation
Expand file tree
/
Copy pathkmalloc.c
More file actions
executable file
·177 lines (143 loc) · 3.98 KB
/
Copy pathkmalloc.c
File metadata and controls
executable file
·177 lines (143 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* malloc(), realloc(), free() - simple memory allocation routines
*
* This is a very small and simple minded malloc Author: Kees J. Bot
* implementation. Ideal for things like a 29 Jan 1994
* bootstrap program, or for debugging. Six times
* slower than any good malloc.
*/
#define nil 0
#define sbrk _sbrk
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#if !DEBUG
#define NDEBUG 1
#define debug(expr) ((void) 0)
#else
#define debug(expr) expr
#endif
#include <assert.h>
typedef struct cell {
size_t size; /* Size of a malloc()'ed object. */
#if DEBUG
unsigned magic; /* To recognize a cell. */
#endif
struct cell *next; /* Next cell on the free list. */
#if DEBUG
unsigned sacred; /* Don't touch while unallocated. */
#endif
} cell_t;
#if UINT_MAX <= 0xFFFF
#define MAGIC 0x537B
#else
#define MAGIC 0x537BC0D8
#endif
/* Size of the header of an object. */
#define HDR_SIZE offsetof(cell_t, next)
/* An offset from a cell pointer to a next cell. */
#define offset(cp, size) ((cell_t *) ((char *) (cp) + (size)))
/* Address of the object in a cell and back. */
#define cell2obj(cp) ((void *) ((char *) (cp) + HDR_SIZE))
#define obj2cell(op) ((cell_t *) ((char *) (op) - HDR_SIZE))
/* The free list. */
static cell_t *freelist;
void *malloc(size_t size)
/* Allocate an object of at least the given size. */
{
cell_t **pcp, *cp;
size += HDR_SIZE;
if (size < sizeof(cell_t)) size= sizeof(cell_t);
/* Align to a word. Use a real malloc if you need better alignment. */
size= (size + sizeof(int) - 1) & ~(sizeof(int) - 1);
/* Space for a magic number at the end of the chunk. */
debug(size += sizeof(unsigned));
for (;;) {
/* Do a first fit search. */
pcp= &freelist;
while ((cp= *pcp) != nil) {
cell_t *next= cp->next;
assert(cp->magic == MAGIC);
assert(cp->sacred == MAGIC);
if (offset(cp, cp->size) == next) {
/* Join adjacent free cells. */
assert(next->magic == MAGIC);
assert(next->sacred == MAGIC);
cp->size+= next->size;
cp->next= next->next;
continue; /* Try again. */
}
if (size <= cp->size) break; /* Big enough. */
/* Next cell. */
pcp= &cp->next;
}
if (cp != nil) break; /* Found a big enough chunk. */
/* Allocate a new chunk at the break. */
if ((cp= (cell_t *) sbrk(size)) == (cell_t *) -1) {
return nil;
}
cp->size= size;
cp->next= nil;
debug(cp->magic= MAGIC);
debug(cp->sacred= MAGIC);
*pcp= cp;
}
/* We've got a cell that is big enough. Can we break it up? */
if (cp->size >= size + sizeof(cell_t)) {
cell_t *next= offset(cp, size);
next->size= cp->size - size;
next->next= cp->next;
debug(next->magic= MAGIC);
debug(next->sacred= MAGIC);
cp->size= size;
cp->next= next;
}
/* Unchain the cell we've found and return an address in it. */
*pcp= cp->next;
debug(memset(cell2obj(cp), 0xAA, cp->size - HDR_SIZE));
debug(((unsigned *) offset(cp, cp->size))[-1]= MAGIC);
return cell2obj(cp);
}
void free(void *op)
/* Deallocate an object. */
{
cell_t **prev, *next, *cp;
if (op == nil) return; /* Aaargh. */
cp= obj2cell(op);
assert(cp->magic == MAGIC);
assert(((unsigned *) offset(cp, cp->size))[-1] == MAGIC);
debug(cp->sacred= MAGIC);
/* Find the spot where the object belongs. */
prev= &freelist;
while ((next= *prev) != nil && next < cp) {
assert(next->magic == MAGIC);
assert(next->sacred == MAGIC);
prev= &next->next;
}
/* Put the new free cell in the list. */
*prev= cp;
cp->next= next;
#if DEBUG
/* Check the rest of the list. */
while (next != nil) {
assert(next->magic == MAGIC);
assert(next->sacred == MAGIC);
next= next->next;
}
#endif
}
void *realloc(void *op, size_t size)
/* Change the size of an object. Don't bother being smart, just copy it. */
{
size_t oldsize;
void *new;
oldsize= op == nil ? 0 : obj2cell(op)->size - HDR_SIZE;
new= malloc(size);
memcpy(new, op, oldsize > size ? size : oldsize);
free(op);
return new;
}
/*
* $PchId: malloc.c,v 1.4 1996/02/22 09:15:56 philip Exp $
*/