blob: 2f03de4e79e628bdd4656931053bf116be857995 (
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
#include <u.h>
#include <libc.h>
#include <venti.h>
enum {
IdealAlignment = 32,
ChunkSize = 128*1024
};
void
vtfree(void *p)
{
if(p == 0)
return;
free(p);
}
void *
vtmalloc(int size)
{
void *p;
p = malloc(size);
if(p == 0)
sysfatal("vtmalloc: out of memory");
setmalloctag(p, getcallerpc(&size));
return p;
}
void *
vtmallocz(int size)
{
void *p = vtmalloc(size);
memset(p, 0, size);
setmalloctag(p, getcallerpc(&size));
return p;
}
void *
vtrealloc(void *p, int size)
{
if(p == nil)
return vtmalloc(size);
p = realloc(p, size);
if(p == 0)
sysfatal("vtMemRealloc: out of memory");
setrealloctag(p, getcallerpc(&size));
return p;
}
void *
vtbrk(int n)
{
static Lock lk;
static uchar *buf;
static int nbuf, nchunk;
int align, pad;
void *p;
if(n >= IdealAlignment)
align = IdealAlignment;
else if(n > 8)
align = 8;
else
align = 4;
lock(&lk);
pad = (align - (uintptr)buf) & (align-1);
if(n + pad > nbuf) {
buf = vtmallocz(ChunkSize);
nbuf = ChunkSize;
pad = (align - (uintptr)buf) & (align-1);
nchunk++;
}
assert(n + pad <= nbuf);
p = buf + pad;
buf += pad + n;
nbuf -= pad + n;
unlock(&lk);
return p;
}
|