-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem.c
165 lines (131 loc) · 3.73 KB
/
mem.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
This file is part of HPerf.
Copyright (C) 2020 Laurent Poirrier
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "message.h"
#include "mem.h"
// ************************************************************************
//
// ************************************************************************
void buf_init(void **ptr, size_t *size)
{
*ptr = NULL;
*size = 0;
}
void buf_clear(void **ptr, size_t *size)
{
free(*ptr);
*ptr = NULL;
*size = 0;
}
// ************************************************************************
//
// ************************************************************************
static size_t buf_roundup(size_t bytes)
{
size_t mask = ((size_t)1 << BUF_GRAN) - 1;
return (bytes + mask) & (~mask);
}
// ************************************************************************
//
// ************************************************************************
int buf_resize(void **ptr, size_t *size, size_t esz, size_t n)
{
size_t b0 = buf_roundup(esz * (*size));
size_t b1 = buf_roundup(esz * n);
if (b1 <= b0) {
*size = n;
return 0;
}
void *r = realloc(*ptr, b1);
if (!r) {
ERROR("MEM_RESIZE(%zd -> %zd): buf_resize(%zd -> %zd): %s\n",
*size, n, b0, b1, strerror(errno));
return -1;
}
*ptr = r;
*size = n;
return 0;
}
// ************************************************************************
//
// ************************************************************************
void obstack_init(struct obstack *ob)
{
ob->bufs = NULL;
ob->nbufs = 0;
ob->offset = 0;
}
void obstack_clear(struct obstack *ob)
{
for (size_t i = 0; i < ob->nbufs; i++)
free(ob->bufs[i]);
free(ob->bufs);
ob->bufs = NULL;
ob->nbufs = 0;
ob->offset = 0;
}
void obstack_swap(struct obstack *ob0, struct obstack *ob1)
{
struct obstack tmp;
tmp = *ob1;
*ob1 = *ob0;
*ob0 = tmp;
}
// ************************************************************************
//
// ************************************************************************
char *obstack_get(struct obstack *ob, size_t n)
{
// fast path
if ((ob->nbufs > 0) && (ob->offset + n < OBSTACK_SIZE)) {
char *r = ob->bufs[ob->nbufs - 1] + ob->offset;
ob->offset += n;
return r;
}
// enlarge pointer vector
char **bufs = realloc(ob->bufs, (ob->nbufs + 1) * sizeof(char **));
if (bufs == NULL) {
ERROR("obstack_get(%zd): realloc(%zd bufs): %s\n",
n, ob->nbufs + 1, strerror(errno));
return NULL;
}
ob->bufs = bufs;
// allocate new block
size_t a = (n < OBSTACK_SIZE) ? OBSTACK_SIZE : n;
char *block = malloc(a);
if (block == NULL) {
ERROR("obstack_get(%zd): malloc(%zd): %s\n",
n, a, strerror(errno));
return NULL;
}
// register new block and allocation
ob->bufs[ob->nbufs] = block;
ob->nbufs++;
ob->offset = n;
return block;
}
// ************************************************************************
//
// ************************************************************************
char *obstack_dup(struct obstack *ob, const char *src)
{
size_t len = strlen(src);
char *dst = obstack_get(ob, len + 1);
if (dst == NULL)
return NULL;
memcpy(dst, src, len + 1);
return dst;
}