-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfmem.c
209 lines (160 loc) · 3.97 KB
/
bfmem.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*------------------------------------------------------------------------*/
/* Copyright (c) 2005 - 2010 Armin Biere, Johannes Kepler University. */
/*------------------------------------------------------------------------*/
#include "config.h"
/*------------------------------------------------------------------------*/
#include <assert.h>
#include <stdlib.h>
#include <string.h>
/*------------------------------------------------------------------------*/
#include "bfmem.h"
/*------------------------------------------------------------------------*/
#include "booleforce.h"
/*------------------------------------------------------------------------*/
static size_t current_bytes;
static size_t max_bytes;
/*------------------------------------------------------------------------*/
void
booleforce_reset_mem (void)
{
/* USUALY ALL HEAP MEMORY SHOULD HAVE BEEN FREED */
assert (getenv ("LEAK") || !current_bytes);
current_bytes = 0;
max_bytes = 0;
}
/*------------------------------------------------------------------------*/
void *
booleforce_new (size_t size)
{
size_t real_size, *res;
assert (size);
real_size = size;
#ifndef NDEBUG
real_size += sizeof (size_t);
#endif
res = malloc (real_size);
assert (res);
#ifndef NDEBUG
*res++ = size;
#endif
current_bytes += size;
memset (res, 0, size);
if (current_bytes > max_bytes)
max_bytes = current_bytes;
return res;
}
/*------------------------------------------------------------------------*/
void
booleforce_delete (void *ptr, size_t size)
{
#ifndef NDEBUG
size_t real_size;
#endif
size_t *real_ptr;
if (ptr)
{
real_ptr = ptr;
#ifndef NDEBUG
real_size = size + sizeof (size_t);
real_ptr--;
assert (*real_ptr == size);
#endif
assert (current_bytes >= size);
current_bytes -= size;
#ifndef NDEBUG
memset (real_ptr, 0, real_size);
#endif
free (real_ptr);
}
else
assert (!size);
}
/*------------------------------------------------------------------------*/
void *
booleforce_enlarge (void *ptr, size_t old_size, size_t new_size)
{
size_t real_size, *res;
if (!ptr)
{
assert (!old_size);
return booleforce_new (new_size);
}
res = ptr;
real_size = new_size;
#ifndef NDEBUG
real_size += sizeof (*res);
assert (old_size);
res--;
assert (*res == old_size);
#endif
assert (current_bytes >= old_size);
current_bytes -= old_size;
current_bytes += new_size;
res = realloc (res, real_size);
assert (res);
#ifndef NDEBUG
*res++ = new_size;
#endif
if (new_size > old_size)
memset (((char *) res) + old_size, 0, new_size - old_size);
if (current_bytes > max_bytes)
max_bytes = current_bytes;
return res;
}
/*------------------------------------------------------------------------*/
void
booleforce_delete_ints (int *a)
{
const int *p;
size_t bytes;
for (p = a; *p; p++)
;
bytes = p - a + 1;
bytes *= sizeof (*a);
booleforce_delete (a, bytes);
}
/*------------------------------------------------------------------------*/
size_t
booleforce_max_bytes (void)
{
size_t res;
res = max_bytes;
return res;
}
/*------------------------------------------------------------------------*/
size_t
booleforce_current_bytes (void)
{
size_t res;
res = current_bytes;
return res;
}
/*------------------------------------------------------------------------*/
int *
booleforce_intcpy (const int *src, unsigned count)
{
unsigned i;
int *res;
res = booleforce_new ((count + 1) * sizeof (*res));
for (i = 0; i < count; i++)
res[i] = src[i];
res[count] = 0;
return res;
}
/*------------------------------------------------------------------------*/
char *
booleforce_strdup (const char *str)
{
char *res;
assert (str);
res = booleforce_new (strlen (str) + 1);
strcpy (res, str);
return res;
}
/*------------------------------------------------------------------------*/
void
booleforce_delstr (char *str)
{
assert (str);
booleforce_delete (str, strlen (str) + 1);
}