blob: 0789abc43db161b99f3d3db60f2f76db74a92745 (
plain)
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
|
#include<u.h>
#include<libc.h>
#include<mheap.h>
static void *pool = nil;
static void *ptop;
static ulong psz = 0;
static ulong csz = 0;
static void *stack[128]; // Stack depth is 128 maximum
static void **stop = stack;
int hdebug = 0;
void
hinit(ulong sz)
{
if(pool)
free(pool);
pool = malloc(sz);
if(pool == nil)
sysfatal("hinit: %r");
psz = sz;
csz = 0;
ptop = pool;
}
void *
halloc(ulong sz)
{
void *ptr;
/* Check bounds */
if(sz + csz > psz)
sysfatal("halloc: heap exhausted");
ptr = ptop;
ptop = sz + (uchar*)ptop;
csz += sz;
if(hdebug)
fprint(2, "halloc: %uld\n", csz);
return ptr;
}
/* Stack management */
ulong
hpush(void)
{
*stop = ptop;
return (ulong)*stop++;
}
ulong
hpop(void)
{
ptop = *--stop;
csz = ((uchar *)ptop - (uchar*)pool);
return (ulong)ptop;
}
|