-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchunk.h
130 lines (76 loc) · 4.24 KB
/
chunk.h
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
/*--------------------------------------------------------------------*/
/* chunk.h */
/* Author: Bob Dondero */
/*--------------------------------------------------------------------*/
#ifndef CHUNK_INCLUDED
#define CHUNK_INCLUDED
#include <stddef.h>
/* A Chunk can be either free or in use. */
enum ChunkStatus {CHUNK_FREE, CHUNK_INUSE};
/* A Chunk is a sequence of Units. The first Unit is a header that
indicates the number of Units in the Chunk, whether the Chunk is
free, and, if the Chunk is free, a pointer to the next Chunk in the
free list. The last Unit is a footer that indicates the number of
Units in the Chunk and, if the Chunk is free, a pointer to the
previous Chunk in the free list. The Units between the header and
footer are the payload. */
typedef struct Chunk *Chunk_T;
/*--------------------------------------------------------------------*/
/* The minimum number of units that a Chunk can contain. */
enum {MIN_UNITS_PER_CHUNK = 3};
/*--------------------------------------------------------------------*/
/* Translate uBytes, a number of bytes, to units. Return the result. */
size_t Chunk_bytesToUnits(size_t uBytes);
/*--------------------------------------------------------------------*/
/* Translate uUnits, a number of units, to bytes. Return the result. */
size_t Chunk_unitsToBytes(size_t uUnits);
/*--------------------------------------------------------------------*/
/* Return the address of the payload of oChunk. */
void *Chunk_toPayload(Chunk_T oChunk);
/*--------------------------------------------------------------------*/
/* Return the Chunk whose payload is pointed to by pv. */
Chunk_T Chunk_fromPayload(void *pv);
/*--------------------------------------------------------------------*/
/* Return the status of oChunk. */
enum ChunkStatus Chunk_getStatus(Chunk_T oChunk);
/*--------------------------------------------------------------------*/
/* Set the status of oChunk to eStatus. */
void Chunk_setStatus(Chunk_T oChunk, enum ChunkStatus eStatus);
/*--------------------------------------------------------------------*/
/* Return oChunk's number of units. */
size_t Chunk_getUnits(Chunk_T oChunk);
/*--------------------------------------------------------------------*/
/* Set oChunk's number of units to uUnits. */
void Chunk_setUnits(Chunk_T oChunk, size_t uUnits);
/*--------------------------------------------------------------------*/
/* Return oChunk's next Chunk in the free list, or NULL if there
is no next Chunk. */
Chunk_T Chunk_getNextInList(Chunk_T oChunk);
/*--------------------------------------------------------------------*/
/* Set oChunk's next Chunk in the free list to oNextChunk. */
void Chunk_setNextInList(Chunk_T oChunk, Chunk_T oNextChunk);
/*--------------------------------------------------------------------*/
/* Return oChunk's previous Chunk in the free list, or NULL if there
is no previous Chunk. */
Chunk_T Chunk_getPrevInList(Chunk_T oChunk);
/*--------------------------------------------------------------------*/
/* Set oChunk's previous Chunk in the free list to oPrevChunk. */
void Chunk_setPrevInList(Chunk_T oChunk, Chunk_T oPrevChunk);
/*--------------------------------------------------------------------*/
/* Return oChunk's next Chunk in memory, or NULL if there is no
next Chunk. Use oHeapEnd to determine if there is no next
Chunk. oChunk's number of units must be set properly for this
function to work. */
Chunk_T Chunk_getNextInMem(Chunk_T oChunk, Chunk_T oHeapEnd);
/*--------------------------------------------------------------------*/
/* Return oChunk's previous Chunk in memory, or NULL if there is no
previous Chunk. Use oHeapStart to determine if there is no
previous Chunk. The previous Chunk's number of units must be set
properly for this function to work. */
Chunk_T Chunk_getPrevInMem(Chunk_T oChunk, Chunk_T oHeapStart);
/*--------------------------------------------------------------------*/
/* Return 1 (TRUE) if oChunk is valid, notably with respect to
oHeapStart and oHeapEnd, or 0 (FALSE) otherwise. */
int Chunk_isValid(Chunk_T oChunk,
Chunk_T oHeapStart, Chunk_T oHeapEnd);
#endif