-
Notifications
You must be signed in to change notification settings - Fork 4
/
region.c
51 lines (40 loc) · 879 Bytes
/
region.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
/* see LICENSE file for copyright and license details */
#include <stdio.h>
#include <stdlib.h>
#include "region.h"
#include "reporting.h"
#include "util.h"
void
region_create(region *r)
{
r->len = 0;
r->alloc_len = 2;
r->pointers = emalloc(sizeof(void*)*r->alloc_len);
}
void *
region_malloc(region *r, size_t size)
{
if (r->len >= r->alloc_len) {
r->alloc_len <<= 1;
r->pointers = erealloc(r->pointers, sizeof(void*)*r->alloc_len);
}
return r->pointers[r->len++] = emalloc(size);
}
void *
region_realloc(region *r, void *v, size_t size)
{
size_t i;
for (i = 0; i < r->len; i++)
if (r->pointers[i] == v)
return r->pointers[i] = erealloc(r->pointers[i], size);
reporterr("unable to realloc region [%p]", v);
}
void
region_free(region *r)
{
size_t i;
for (i = 0; i < r->len; i++)
efree(r->pointers[i]);
efree(r->pointers);
r->pointers = NULL;
}