-
Notifications
You must be signed in to change notification settings - Fork 26
/
mempool.c
102 lines (91 loc) · 2.39 KB
/
mempool.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
/*
* Part of DNS zone file validator `validns`.
*
* Copyright 2011-2014 Anton Berezin <[email protected]>
* Modified BSD license.
* (See LICENSE file in the distribution.)
*
*/
#include <stdlib.h>
#include <string.h>
#include "mempool.h"
#include "carp.h"
struct pool
{
struct pool *next;
size_t pool_size;
size_t free_index;
char mem[0];
};
static struct pool *freespace = NULL;
static struct pool *temp_freespace = NULL;
static void new_pool(size_t size)
{
struct pool *pool;
size = (size + sizeof(void *) - 1) / sizeof(void *);
size *= sizeof(void *);
pool = malloc(size + sizeof(struct pool));
if (!pool)
croak(1, "new_pool malloc");
pool->next = freespace;
pool->free_index = 0;
pool->pool_size = size;
freespace = pool;
}
void
mem_requirements_hint(size_t size)
{
if (freespace) return;
new_pool(size);
}
void *getmem(size_t size)
{
void *ret;
size = (size + sizeof(void *) - 1) / sizeof(void *);
size *= sizeof(void *);
if (!freespace) new_pool(size > 256000 ? size : 256000);
if (freespace->pool_size - freespace->free_index < size)
new_pool(size > 256000 ? size : 256000);
ret = freespace->mem + freespace->free_index;
freespace->free_index += size;
return ret;
}
void *getmem_temp(size_t size)
{
void *ret;
size = (size + sizeof(void *) - 1) / sizeof(void *);
size *= sizeof(void *);
if (!temp_freespace) {
size_t pool_size = size > 1024*1024 ? size : 1024*1024;
pool_size = (pool_size + sizeof(void *) - 1) / sizeof(void *);
pool_size *= sizeof(void *);
temp_freespace = malloc(pool_size + sizeof(struct pool));
if (!temp_freespace)
croak(1, "getmem_temp malloc");
temp_freespace->next = NULL;
temp_freespace->free_index = 0;
temp_freespace->pool_size = pool_size;
}
if (temp_freespace->pool_size - temp_freespace->free_index < size)
croak(1, "getmem_temp request too large");
ret = temp_freespace->mem + temp_freespace->free_index;
temp_freespace->free_index += size;
return ret;
}
int freeall_temp(void)
{
if (temp_freespace) {
temp_freespace->free_index = 0;
}
return 1;
}
char *quickstrdup(char *s)
{
char *r = getmem(strlen(s)+1);
return strcpy(r, s);
}
char *quickstrdup_temp(char *s)
{
char *r = getmem_temp(strlen(s)+1);
return strcpy(r, s);
}