1 |
#include "salloc.h" |
2 |
|
3 |
#define SALLOC_BLOCK 65536 // size of basic block to allocate |
4 |
|
5 |
rxvt_salloc::rxvt_salloc (int size) |
6 |
{ |
7 |
this->size = size < sizeof (chain) ? sizeof (chain) : size; |
8 |
firstline = 0; |
9 |
firstblock = 0; |
10 |
firstfree = SALLOC_BLOCK; |
11 |
} |
12 |
|
13 |
rxvt_salloc::~rxvt_salloc () |
14 |
{ |
15 |
while (firstblock) |
16 |
{ |
17 |
chain *next = firstblock->next; |
18 |
::free (firstblock); |
19 |
firstblock = next; |
20 |
} |
21 |
} |
22 |
|
23 |
void * |
24 |
rxvt_salloc::alloc () |
25 |
{ |
26 |
void *r; |
27 |
|
28 |
if (firstline) |
29 |
{ |
30 |
r = (void *)firstline; |
31 |
firstline = firstline->next; |
32 |
} |
33 |
else |
34 |
{ |
35 |
if (firstfree + size > SALLOC_BLOCK) |
36 |
{ |
37 |
chain *next = (chain *)rxvt_malloc ((SALLOC_BLOCK - sizeof (chain)) / size * size + sizeof (chain)); |
38 |
next->next = firstblock; |
39 |
firstblock = next; |
40 |
firstfree = sizeof (chain); |
41 |
} |
42 |
|
43 |
r = (void *)((char *)firstblock + firstfree); |
44 |
|
45 |
firstfree += size; |
46 |
} |
47 |
|
48 |
return r; |
49 |
} |
50 |
|
51 |
void |
52 |
rxvt_salloc::free (void *data) |
53 |
{ |
54 |
chain *line = (chain *)data; |
55 |
line->next = firstline; |
56 |
firstline = line; |
57 |
} |
58 |
|