-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchunkbase.c
147 lines (111 loc) · 3.49 KB
/
chunkbase.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
/*--------------------------------------------------------------------*/
/* chunkbase.c */
/* Author: Bob Dondero */
/*--------------------------------------------------------------------*/
#include "chunkbase.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. */
size_t uUnits;
/* The address of the next Chunk. */
Chunk_T oNextChunk;
};
/*--------------------------------------------------------------------*/
size_t Chunk_bytesToUnits(size_t uBytes)
{
size_t uUnits;
uUnits = ((uBytes - 1) / sizeof(struct Chunk)) + 1;
uUnits++; /* Allow room for a header. */
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;
}
/*--------------------------------------------------------------------*/
size_t Chunk_getUnits(Chunk_T oChunk)
{
assert(oChunk != NULL);
return oChunk->uUnits;
}
/*--------------------------------------------------------------------*/
void Chunk_setUnits(Chunk_T oChunk, size_t uUnits)
{
assert(oChunk != NULL);
assert(uUnits >= MIN_UNITS_PER_CHUNK);
oChunk->uUnits = uUnits;
}
/*--------------------------------------------------------------------*/
Chunk_T Chunk_getNextInList(Chunk_T oChunk)
{
assert(oChunk != NULL);
return oChunk->oNextChunk;
}
/*--------------------------------------------------------------------*/
void Chunk_setNextInList(Chunk_T oChunk, Chunk_T oNextChunk)
{
assert(oChunk != NULL);
oChunk->oNextChunk = oNextChunk;
}
/*--------------------------------------------------------------------*/
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;
}
/*--------------------------------------------------------------------*/
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;
}
return 1;
}