forked from acsl-language/acsl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
acsl_allocator.c
388 lines (369 loc) · 12.3 KB
/
acsl_allocator.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include <stdlib.h>
#define DEFAULT_BLOCK_SIZE 1000
typedef enum _bool { false = 0, true = 1 } bool;
/*@ predicate finite_list<A>((A* -> A*) next_elem, A* ptr) =
@ ptr == \null ||
@ (\valid(ptr) && finite_list(next_elem,next_elem(ptr))) ;
@
@ logic integer list_length<A>((A* -> A*) next_elem, A* ptr) =
@ (ptr == \null) ? 0 :
@ 1 + list_length(next_elem,next_elem(ptr)) ;
@
@
@ predicate lower_length<A>((A* -> A*) next_elem,
@ A* ptr1, A* ptr2) =
@ finite_list(next_elem, ptr1) && finite_list(next_elem, ptr2)
@ && list_length(next_elem, ptr1) < list_length(next_elem, ptr2) ;
@*/
// forward reference
struct _memory_slice;
/* A memory block holds a pointer to a raw block of memory allocated by
* calling [malloc]. It is sliced into chunks, which are maintained by
* the [slice] structure. It maintains additional information such as
* the [size] of the memory block, the number of bytes [used] and the [next]
* index at which to put a chunk.
*/
typedef struct _memory_block {
//@ ghost boolean packed;
// ghost field [packed] is meant to be used as a guard that tells when
// the invariant of a structure of type [memory_block] holds
unsigned int size;
// size of the array [data]
unsigned int next;
// next index in [data] at which to put a chunk
unsigned int used;
// how many bytes are used in [data], not necessarily contiguous ones
char* data;
// raw memory block allocated by [malloc]
struct _memory_slice* slice;
// structure that describes the slicing of a block into chunks
} memory_block;
/*@ strong type invariant inv_memory_block(memory_block mb) =
@ mb.packed ==>
@ (0 < mb.size && mb.used <= mb.next <= mb.size
@ && \offset(mb.data) == 0
@ && \block_length(mb.data) == mb.size) ;
@
@ predicate valid_memory_block(memory_block* mb) =
@ \valid(mb) && mb->packed ;
@*/
/* A memory chunk holds a pointer [data] to some part of a memory block
* [block]. It maintains the [offset] at which it points in the block,
* as well as the [size] of the block it is allowed to access.
* A field [free] tells whether the chunk is used or not.
*/
typedef struct _memory_chunk {
//@ ghost boolean packed;
// ghost field [packed] is meant to be used as a guard that tells when
// the invariant of a structure of type [memory_chunk] holds
unsigned int offset;
// offset at which [data] points into [block->data]
unsigned int size;
// size of the chunk
bool free;
// true if the chunk is not used, false otherwise
memory_block* block;
// block of memory into which the chunk points
char* data;
// shortcut for [block->data + offset]
} memory_chunk;
/*@ strong type invariant inv_memory_chunk(memory_chunk mc) =
@ mc.packed ==>
@ (0 < mc.size && valid_memory_block(mc.block)
@ && mc.offset + mc.size <= mc.block->next) ;
@
@ predicate valid_memory_chunk(memory_chunk* mc, int s) =
@ \valid(mc) && mc->packed && mc->size == s ;
@
@ predicate used_memory_chunk(memory_chunk mc) =
@ mc.free == false ;
@
@ predicate freed_memory_chunk(memory_chunk mc) =
@ mc.free == true ;
@*/
/* A memory chunk list links memory chunks in the same memory block.
* Newly allocated chunks are put first, so that the offset of chunks
* decreases when following the [next] pointer. Allocated chunks should
* fill the memory block up to its own [next] index.
*/
typedef struct _memory_chunk_list {
memory_chunk* chunk;
// current list element
struct _memory_chunk_list* next;
// tail of the list
} memory_chunk_list;
/*@ logic memory_chunk_list* next_chunk(memory_chunk_list* ptr) =
@ ptr->next ;
@
@ predicate valid_memory_chunk_list
@ (memory_chunk_list* mcl, memory_block* mb) =
@ \valid(mcl) && valid_memory_chunk(mcl->chunk,mcl->chunk->size)
@ && mcl->chunk->block == mb
@ && (mcl->next == \null ||
@ valid_memory_chunk_list(mcl->next, mb))
@ && mcl->offset == mcl->chunk->offset
@ && (
@ // it is the last chunk in the list
@ (mcl->next == \null && mcl->chunk->offset == 0)
@ ||
@ // it is a chunk in the middle of the list
@ (mcl->next != \null
@ && mcl->next->chunk->offset + mcl->next->chunk->size
@ == mcl->chunk->offset)
@ )
@ && finite_list(next_chunk, mcl) ;
@
@ predicate valid_complete_chunk_list
@ (memory_chunk_list* mcl, memory_block* mb) =
@ valid_memory_chunk_list(mcl,mb)
@ && mcl->next->chunk->offset +
@ mcl->next->chunk->size == mb->next ;
@
@ predicate chunk_lower_length(memory_chunk_list* ptr1,
@ memory_chunk_list* ptr2) =
@ lower_length(next_chunk, ptr1, ptr2) ;
@*/
/* A memory slice holds together a memory block [block] and a list of chunks
* [chunks] on this memory block.
*/
typedef struct _memory_slice {
//@ ghost boolean packed;
// ghost field [packed] is meant to be used as a guard that tells when
// the invariant of a structure of type [memory_slice] holds
memory_block* block;
memory_chunk_list* chunks;
} memory_slice;
/*@ strong type invariant inv_memory_slice(memory_slice* ms) =
@ ms.packed ==>
@ (valid_memory_block(ms->block) && ms->block->slice == ms
@ && (ms->chunks == \null
@ || valid_complete_chunk_list(ms->chunks, ms->block))) ;
@
@ predicate valid_memory_slice(memory_slice* ms) =
@ \valid(ms) && ms->packed ;
@*/
/* A memory slice list links memory slices, to form a memory pool.
*/
typedef struct _memory_slice_list {
//@ ghost boolean packed;
// ghost field [packed] is meant to be used as a guard that tells when
// the invariant of a structure of type [memory_slice_list] holds
memory_slice* slice;
// current list element
struct _memory_slice_list* next;
// tail of the list
} memory_slice_list;
/*@ logic memory_slice_list* next_slice(memory_slice_list* ptr) =
@ ptr->next ;
@
@ strong type invariant inv_memory_slice_list(memory_slice_list* msl) =
@ msl.packed ==>
@ (valid_memory_slice(msl->slice)
@ && (msl->next == \null ||
@ valid_memory_slice_list(msl->next))
@ && finite_list(next_slice, msl)) ;
@
@ predicate valid_memory_slice_list(memory_slice_list* msl) =
@ \valid(msl) && msl->packed ;
@
@ predicate slice_lower_length(memory_slice_list* ptr1,
@ memory_slice_list* ptr2) =
@ lower_length(next_slice, ptr1, ptr2)
@ } */
typedef memory_slice_list* memory_pool;
/*@ type invariant valid_memory_pool(memory_pool *mp) =
@ \valid(mp) && valid_memory_slice_list(*mp) ;
@*/
/*@ behavior zero_size:
@ assumes s == 0;
@ assigns \nothing;
@ ensures \result == 0;
@
@ behavior positive_size:
@ assumes s > 0;
@ requires valid_memory_pool(arena);
@ ensures \result == 0
@ || (valid_memory_chunk(\result,s) &&
@ used_memory_chunk(*\result));
@ */
memory_chunk* memory_alloc(memory_pool* arena, unsigned int s) {
memory_slice_list *msl = *arena;
memory_chunk_list *mcl;
memory_slice *ms;
memory_block *mb;
memory_chunk *mc;
unsigned int mb_size;
//@ ghost unsigned int mcl_offset;
char *mb_data;
// guard condition
if (s == 0) return 0;
// iterate through memory blocks (or slices)
/*@
@ loop invariant valid_memory_slice_list(msl);
@ loop variant msl for slice_lower_length;
@ */
while (msl != 0) {
ms = msl->slice;
mb = ms->block;
mcl = ms->chunks;
// does [mb] contain enough free space?
if (s <= mb->size - mb->next) {
//@ ghost ms->ghost = false; // unpack the slice
// allocate a new chunk
mc = (memory_chunk*)malloc(sizeof(memory_chunk));
if (mc == 0) return 0;
mc->offset = mb->next;
mc->size = s;
mc->free = false;
mc->block = mb;
//@ ghost mc->ghost = true; // pack the chunk
// update block accordingly
//@ ghost mb->ghost = false; // unpack the block
mb->next += s;
mb->used += s;
//@ ghost mb->ghost = true; // pack the block
// add the new chunk to the list
mcl = (memory_chunk_list*)malloc(sizeof(memory_chunk_list));
if (mcl == 0) return 0;
mcl->chunk = mc;
mcl->next = ms->chunks;
ms->chunks = mcl;
//@ ghost ms->ghost = true; // pack the slice
return mc;
}
// iterate through memory chunks
/*@
@ loop invariant valid_memory_chunk_list(mcl,mb);
@ loop variant mcl for chunk_lower_length;
@ */
while (mcl != 0) {
mc = mcl->chunk;
// is [mc] free and large enough?
if (mc->free && s <= mc->size) {
mc->free = false;
mb->used += mc->size;
return mc;
}
// try next chunk
mcl = mcl->next;
}
msl = msl->next;
}
// allocate a new block
mb_size = (DEFAULT_BLOCK_SIZE < s) ? s : DEFAULT_BLOCK_SIZE;
mb_data = (char*)malloc(mb_size);
if (mb_data == 0) return 0;
mb = (memory_block*)malloc(sizeof(memory_block));
if (mb == 0) return 0;
mb->size = mb_size;
mb->next = s;
mb->used = s;
mb->data = mb_data;
//@ ghost mb->ghost = true; // pack the block
// allocate a new chunk
mc = (memory_chunk*)malloc(sizeof(memory_chunk));
if (mc == 0) return 0;
mc->offset = 0;
mc->size = s;
mc->free = false;
mc->block = mb;
//@ ghost mc->ghost = true; // pack the chunk
// allocate a new chunk list
mcl = (memory_chunk_list*)malloc(sizeof(memory_chunk_list));
if (mcl == 0) return 0;
//@ ghost mcl->offset = 0;
mcl->chunk = mc;
mcl->next = 0;
// allocate a new slice
ms = (memory_slice*)malloc(sizeof(memory_slice));
if (ms == 0) return 0;
ms->block = mb;
ms->chunks = mcl;
//@ ghost ms->ghost = true; // pack the slice
// update the block accordingly
mb->slice = ms;
// add the new slice to the list
msl = (memory_slice_list*)malloc(sizeof(memory_slice_list));
if (msl == 0) return 0;
msl->slice = ms;
msl->next = *arena;
//@ ghost msl->ghost = true; // pack the slice list
*arena = msl;
return mc;
}
/*@ behavior null_chunk:
@ assumes chunk == \null;
@ assigns \nothing;
@
@ behavior valid_chunk:
@ assumes chunk != \null;
@ requires valid_memory_pool(arena);
@ requires valid_memory_chunk(chunk,chunk->size);
@ requires used_memory_chunk(chunk);
@ ensures
@ // if it is not the last chunk in the block, mark it as free
@ (valid_memory_chunk(chunk,chunk->size)
@ && freed_memory_chunk(chunk))
@ ||
@ // if it is the last chunk in the block, deallocate the block
@ ! \valid(chunk);
@ */
void memory_free(memory_pool* arena, memory_chunk* chunk) {
memory_slice_list *msl = *arena;
memory_block *mb = chunk->block;
memory_slice *ms = mb->slice;
memory_chunk_list *mcl;
memory_chunk *mc;
// is it the last chunk in use in the block?
if (mb->used == chunk->size) {
// remove the corresponding slice from the memory pool
// case it is the first slice
if (msl->slice == ms) {
*arena = msl->next;
//@ ghost msl->ghost = false; // unpack the slice list
free(msl);
}
// case it is not the first slice
while (msl != 0) {
if (msl->next != 0 && msl->next->slice == ms) {
memory_slice_list* msl_next = msl->next;
msl->next = msl->next->next;
// unpack the slice list
//@ ghost msl_next->ghost = false;
free(msl_next);
break;
}
msl = msl->next;
}
//@ ghost ms->ghost = false; // unpack the slice
// deallocate all chunks in the block
mcl = ms->chunks;
// iterate through memory chunks
/*@
@ loop invariant valid_memory_chunk_list(mcl,mb);
@ loop variant mcl for chunk_lower_length;
@ */
while (mcl != 0) {
memory_chunk_list *mcl_next = mcl->next;
mc = mcl->chunk;
//@ ghost mc->ghost = false; // unpack the chunk
free(mc);
free(mcl);
mcl = mcl_next;
}
mb->next = 0;
mb->used = 0;
// deallocate the memory block and its data
//@ ghost mb->ghost = false; // unpack the block
free(mb->data);
free(mb);
// deallocate the corresponding slice
free(ms);
return;
}
// mark the chunk as freed
chunk->free = true;
// update the block accordingly
mb->used -= chunk->size;
return;
}