-
Notifications
You must be signed in to change notification settings - Fork 0
/
chunk.c
226 lines (167 loc) · 5.5 KB
/
chunk.c
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*--------------------------------------------------------------------*/
/* chunk.c */
/* Author: Bob Dondero */
/*--------------------------------------------------------------------*/
#include "chunk.h"
#include <stddef.h>
#include <stdio.h>
#include <assert.h>
/*--------------------------------------------------------------------*/
/* Physically a Chunk is a structure consisting of a number of units
and an address. Logically a Chunk consists of multiple such
structures. */
struct Chunk
{
/* The number of units in the Chunk. The low-order bit
stores the Chunk's status. */
size_t uUnits;
/* The address of an adjacent Chunk. */
Chunk_T oAdjacentChunk;
};
/*--------------------------------------------------------------------*/
size_t Chunk_bytesToUnits(size_t uBytes)
{
size_t uUnits;
uUnits = ((uBytes - 1) / sizeof(struct Chunk)) + 1;
uUnits++; /* Allow room for a header. */
uUnits++; /* Allow room for a footer. */
return uUnits;
}
/*--------------------------------------------------------------------*/
size_t Chunk_unitsToBytes(size_t uUnits)
{
return uUnits * sizeof(struct Chunk);
}
/*--------------------------------------------------------------------*/
void *Chunk_toPayload(Chunk_T oChunk)
{
assert(oChunk != NULL);
return (void*)(oChunk + 1);
}
/*--------------------------------------------------------------------*/
Chunk_T Chunk_fromPayload(void *pv)
{
assert(pv != NULL);
return (Chunk_T)pv - 1;
}
/*--------------------------------------------------------------------*/
enum ChunkStatus Chunk_getStatus(Chunk_T oChunk)
{
assert(oChunk != NULL);
return oChunk->uUnits & 1U;
}
/*--------------------------------------------------------------------*/
void Chunk_setStatus(Chunk_T oChunk, enum ChunkStatus eStatus)
{
assert(oChunk != NULL);
assert((eStatus == CHUNK_FREE) || (eStatus == CHUNK_INUSE));
oChunk->uUnits &= ~1U;
oChunk->uUnits |= eStatus;
}
/*--------------------------------------------------------------------*/
size_t Chunk_getUnits(Chunk_T oChunk)
{
assert(oChunk != NULL);
return oChunk->uUnits >> 1;
}
/*--------------------------------------------------------------------*/
void Chunk_setUnits(Chunk_T oChunk, size_t uUnits)
{
assert(oChunk != NULL);
assert(uUnits >= MIN_UNITS_PER_CHUNK);
/* Set the Units in oChunk's header. */
oChunk->uUnits &= 1U;
oChunk->uUnits |= uUnits << 1U;
/* Set the Units in oChunk's footer. */
(oChunk + uUnits - 1)->uUnits = uUnits;
}
/*--------------------------------------------------------------------*/
Chunk_T Chunk_getNextInList(Chunk_T oChunk)
{
assert(oChunk != NULL);
return oChunk->oAdjacentChunk;
}
/*--------------------------------------------------------------------*/
void Chunk_setNextInList(Chunk_T oChunk, Chunk_T oNextChunk)
{
assert(oChunk != NULL);
oChunk->oAdjacentChunk = oNextChunk;
}
/*--------------------------------------------------------------------*/
Chunk_T Chunk_getPrevInList(Chunk_T oChunk)
{
assert(oChunk != NULL);
return (oChunk + Chunk_getUnits(oChunk) - 1)->oAdjacentChunk;
}
/*--------------------------------------------------------------------*/
void Chunk_setPrevInList(Chunk_T oChunk, Chunk_T oPrevChunk)
{
assert(oChunk != NULL);
(oChunk + Chunk_getUnits(oChunk) - 1)->oAdjacentChunk = oPrevChunk;
}
/*--------------------------------------------------------------------*/
Chunk_T Chunk_getNextInMem(Chunk_T oChunk, Chunk_T oHeapEnd)
{
Chunk_T oNextChunk;
assert(oChunk != NULL);
assert(oHeapEnd != NULL);
assert(oChunk < oHeapEnd);
oNextChunk = oChunk + Chunk_getUnits(oChunk);
assert(oNextChunk <= oHeapEnd);
if (oNextChunk == oHeapEnd)
return NULL;
return oNextChunk;
}
/*--------------------------------------------------------------------*/
Chunk_T Chunk_getPrevInMem(Chunk_T oChunk, Chunk_T oHeapStart)
{
Chunk_T oPrevChunk;
assert(oChunk != NULL);
assert(oHeapStart != NULL);
assert(oChunk >= oHeapStart);
if (oChunk == oHeapStart)
return NULL;
oPrevChunk = oChunk - ((oChunk - 1)->uUnits);
assert(oPrevChunk >= oHeapStart);
return oPrevChunk;
}
/*--------------------------------------------------------------------*/
/* Return the number of units as stored in oChunk's footer. */
static size_t Chunk_getFooterUnits(Chunk_T oChunk)
{
assert(oChunk != NULL);
return (oChunk + Chunk_getUnits(oChunk) - 1)->uUnits;
}
/*--------------------------------------------------------------------*/
int Chunk_isValid(Chunk_T oChunk,
Chunk_T oHeapStart, Chunk_T oHeapEnd)
{
assert(oChunk != NULL);
assert(oHeapStart != NULL);
assert(oHeapEnd != NULL);
if (oChunk < oHeapStart)
{ fprintf(stderr, "A chunk starts before the heap start\n");
return 0;
}
if (oChunk >= oHeapEnd)
{ fprintf(stderr, "A chunk starts after the heap end\n");
return 0;
}
if (oChunk + Chunk_getUnits(oChunk) > oHeapEnd)
{ fprintf(stderr, "A chunk ends after the heap end\n");
return 0;
}
if (Chunk_getUnits(oChunk) == 0)
{ fprintf(stderr, "A chunk has zero units\n");
return 0;
}
if (Chunk_getUnits(oChunk) < (size_t)MIN_UNITS_PER_CHUNK)
{ fprintf(stderr, "A chunk has too few units\n");
return 0;
}
if (Chunk_getUnits(oChunk) != Chunk_getFooterUnits(oChunk))
{ fprintf(stderr, "A chunk has inconsistent header/footer sizes\n");
return 0;
}
return 1;
}