File: mem.cc

package info (click to toggle)
aegis 4.24.3-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 34,056 kB
  • ctags: 12,500
  • sloc: cpp: 178,528; sh: 79,948; makefile: 34,813; yacc: 4,610; perl: 1,499; ansic: 492; awk: 325
file content (403 lines) | stat: -rw-r--r-- 6,153 bytes parent folder | download | duplicates (3)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//
//	aegis - project change supervisor
//	Copyright (C) 1991-1994, 1999, 2002-2006, 2008 Peter Miller
//
//	This program is free software; you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation; either version 3 of the License, or
//	(at your option) any later version.
//
//	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 General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with this program. If not, see
//	<http://www.gnu.org/licenses/>.
//

#include <common/ac/stddef.h>
#include <common/ac/string.h>
#include <common/ac/stdlib.h>
#include <common/ac/errno.h>
#include <common/ac/new.h>

#include <common/mem.h>
#include <common/error.h>

#ifdef DMALLOC
#undef new
#undef delete
#include <dmalloc.h>
#endif


#ifdef _AIX

//
// Yet another AIX stupidity:
// malloc does not guarantee that the space is available in swap.
//

#include <common/ac/signal.h>
#include <setjmp.h>

static jmp_buf  aix_bungy;


//
// Catch SIGDANGER and longjmp to aix_touch.
//

static void
aix_danger(int n)
{
    longjmp(aix_bungy, 1);
}

//
// Touch the pages that cover [p, p+nbytes-1].
//

static int
aix_touch(void *vp, size_t nbytes)
{
    char            *p;
    char            *endp;
    int             pgsize;
    volatile char   c;
    void            (*oldsig)(int);

    oldsig = signal(SIGDANGER, aix_danger);
    if (setjmp(aix_bungy))
    {
	signal(SIGDANGER, oldsig);
	return -1;
    }

    //
    // A load is enough to cause the
    // allocation of the paging space
    //
    p = vp;
    pgsize = getpagesize();
    endp = p + nbytes;
    while (p < endp)
    {
	c = *(volatile char *)p;
	p += pgsize;
    }

    //
    // restore the signal handler
    //
    signal(SIGDANGER, oldsig);
    return 0;
}

#endif

void *
mem_alloc(size_t n)
{
    if (n < 1)
	n = 1;
    int old_errno = errno;
    errno = 0;
    void *cp = malloc(n);
    if (!cp)
    {
	if (!errno)
	    errno = ENOMEM;
	nfatal("malloc(%ld)", (long)n);
    }
#ifdef _AIX
    //
    // watch out for AIX stupidity
    //
    if (aix_touch(cp, n))
    {
	errno = ENOMEM;
	nfatal("malloc(%ld)", (long)n);
    }
#endif
    errno = old_errno;
    return cp;
}

#ifdef DMALLOC

void *
dmem_alloc(const char* file, int line, size_t n)
{
    if (n < 1)
	n = 1;
    int old_errno = errno;
    errno = 0;
    void *cp = dmalloc_malloc(file, line, n, DMALLOC_FUNC_MALLOC, 0, 0);
    if (!cp)
    {
	if (!errno)
	    errno = ENOMEM;
	nfatal("malloc(%ld)", (long)n);
    }
#ifdef _AIX
    //
    // watch out for AIX stupidity
    //
    if (aix_touch(cp, n))
    {
	errno = ENOMEM;
	nfatal("malloc(%ld)", (long)n);
    }
#endif
    errno = old_errno;
    return cp;
}

#endif // DMALLOC


void *
mem_alloc_clear(size_t n)
{
    void *cp = mem_alloc(n);
    memset(cp, 0, n);
    return cp;
}


#ifdef DMALLOC

void *
dmem_alloc_clear(const char *file, int line, size_t n)
{
    void *cp = dmem_alloc(file, line, n);
    memset(cp, 0, n);
    return cp;
}

#endif // DMALLOC


void
mem_free(void *cp)
{
    free(cp);
}


#ifdef DMALLOC

void
dmem_free(const char *file, int line, void *cp)
{
    dmalloc_free(file, line, cp, DMALLOC_FUNC_FREE);
}

#endif // DMALLOC


char *
mem_copy_string(const char *s, size_t len)
{
    char *cp = (char *)mem_alloc(len + 1);
    if (len)
	memcpy(cp, s, len);
    cp[len] = 0;
    return cp;
}


#ifdef DMALLOC

char *
dmem_copy_string(const char *file, int line, const char *s, size_t len)
{
    char *cp = (char *)dmem_alloc(file, line, len + 1);
    if (len)
	memcpy(cp, s, len);
    cp[len] = 0;
    return cp;
}

#endif // DMALLOC


char *
mem_copy_string(const char *s)
{
    return mem_copy_string(s, (s ? strlen(s) : 0));
}

#ifdef DMALLOC

char *
dmem_copy_string(const char *file, int line, const char *s)
{
    return dmem_copy_string(file, line, s, (s ? strlen(s) : 0));
}

#endif // DMALLOC


void *
operator new(size_t nbytes)
    THROW_BAD_ALLOC
{
    return mem_alloc(nbytes);
}


#ifdef DMALLOC

void *
operator new(size_t nbytes, const char *file, int line)
    THROW_BAD_ALLOC
{
    return dmem_alloc(file, line, nbytes);
}

#endif // DMALLOC


void *
operator new[](size_t nbytes)
    THROW_BAD_ALLOC
{
    return mem_alloc(nbytes);
}


#ifdef DMALLOC

void *
operator new[](size_t nbytes, const char *file, int line)
    THROW_BAD_ALLOC
{
    return dmem_alloc(file, line, nbytes);
}

#endif // DMALLOC


void
operator delete(void *ptr)
    throw()
{
    if (ptr)
        mem_free(ptr);
}


#ifdef DMALLOC

void
operator delete(void *ptr, const char *file, int line)
    throw()
{
    if (ptr)
        dmem_free(file, line, ptr);
}

#endif // DMALLOC


void
operator delete[](void *ptr)
    throw()
{
    if (ptr)
        mem_free(ptr);
}


#ifdef DMALLOC

void
operator delete[](void *ptr, const char *file, int line)
    throw()
{
    if (ptr)
        dmem_free(file, line, ptr);
}

#endif // DMALLOC


#if HAVE_HEADER_NEW || HAVE_NEW_H


void *
operator new(size_t nbytes, std::nothrow_t &)
    throw()
{
    return mem_alloc(nbytes);
}


#ifdef DMALLOC

void *
operator new(size_t nbytes, std::nothrow_t &, const char *file, int line)
    throw()
{
    return dmem_alloc(file, line, nbytes);
}

#endif // DMALLOC


void *
operator new[](size_t nbytes, std::nothrow_t &)
    throw()
{
    return mem_alloc(nbytes);
}


void
operator delete(void *ptr, std::nothrow_t const &)
    throw()
{
    if (ptr)
        mem_free(ptr);
}


#ifdef DMALLOC

void
operator delete(void *ptr, std::nothrow_t const &, const char *file, int line)
    throw()
{
    if (ptr)
        dmem_free(file, line, ptr);
}

#endif // DMALLOC


void
operator delete[](void *ptr, std::nothrow_t const &)
    throw()
{
    if (ptr)
        mem_free(ptr);
}


#ifdef DMALLOC

void
operator delete[](void *ptr, std::nothrow_t const &, const char *file, int line)
    throw()
{
    if (ptr)
        dmem_free(file, line, ptr);
}

#endif // DMALLOC

#endif