File: mem.h

package info (click to toggle)
libconvert-binary-c-perl 0.74-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 9,100 kB
  • ctags: 21,416
  • sloc: ansic: 63,666; perl: 18,582; yacc: 2,143; makefile: 44
file content (175 lines) | stat: -rw-r--r-- 5,392 bytes parent folder | download | duplicates (6)
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
/*
 * (c) Thomas Pornin 1998 - 2002
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 4. The name of the authors may not be used to endorse or promote
 *    products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#ifndef UCPP__MEM__
#define UCPP__MEM__

#include <stdlib.h>
#include "tune.h"

#if !(defined(UCPP_MALLOC) && defined(UCPP_REALLOC) && defined(UCPP_FREE))
#define UCPP_MALLOC   malloc
#define UCPP_REALLOC  realloc
#define UCPP_FREE     free
#endif

#define die		UCPP_PRIVATE(die)
void die(void);

#if defined AUDIT || defined MEM_CHECK || defined MEM_DEBUG
#define getmem		UCPP_PRIVATE(getmem)
void *getmem(size_t);
#else
#define getmem		UCPP_MALLOC
#endif

#if defined MEM_DEBUG
#define getmem_debug	UCPP_PRIVATE(getmem_debug)
void *getmem_debug(size_t, const char *, int);
#undef getmem
#define getmem(x)	getmem_debug(x, __FILE__, __LINE__)
#endif

#if defined AUDIT || defined MEM_DEBUG
#define freemem		UCPP_PRIVATE(freemem)
void freemem(void *);
#else
#define freemem		UCPP_FREE
#endif

#if defined MEM_DEBUG
#define freemem_debug	UCPP_PRIVATE(freemem_debug)
void freemem_debug(void *, const char *, int);
#undef freemem
#define freemem(x)	freemem_debug(x, __FILE__, __LINE__)
#endif

#define incmem		UCPP_PRIVATE(incmem)
#define sdup		UCPP_PRIVATE(sdup)
void *incmem(void *, size_t, size_t);
char *sdup(const char *);

#if defined MEM_DEBUG
#define incmem_debug	UCPP_PRIVATE(incmem_debug)
#define report_leaks	UCPP_PRIVATE(report_leaks)
#define sdup_debug	UCPP_PRIVATE(sdup_debug)
void *incmem_debug(void *, size_t, size_t, const char *, int);
#undef incmem
#define incmem(x, y, z)	incmem_debug(x, y, z, __FILE__, __LINE__)
void report_leaks(void);
char *sdup_debug(const char *, const char *, int);
#undef sdup
#define sdup(x)		sdup_debug(x, __FILE__, __LINE__)
#endif

#ifdef AUDIT
#define mmv		UCPP_PRIVATE(mmv)
#define mmvwo		UCPP_PRIVATE(mmvwo)
void *mmv(void *, const void *, size_t);
void *mmvwo(void *, const void *, size_t);
#else
#define mmv	memcpy
#define mmvwo	memmove
#endif

/*
 * this macro adds the object obj at the end of the array list, handling
 * memory allocation when needed; ptr contains the number of elements in
 * the array, and memg is the granularity of memory allocations (a power
 * of 2 is recommanded, for optimization reasons).
 *
 * list and ptr may be updated, and thus need to be lvalues.
 */
#define aol(list, ptr, obj, memg)	do { \
		if (((ptr) % (memg)) == 0) { \
			if ((ptr) != 0) { \
				(list) = incmem((list), (ptr) * sizeof(obj), \
					((ptr) + (memg)) * sizeof(obj)); \
			} else { \
				(list) = getmem((memg) * sizeof(obj)); \
			} \
		} \
		(list)[(ptr) ++] = (obj); \
	} while (0)

/*
 * bol() does the same as aol(), but adds the new item at the beginning
 * of the list; beware, the computational cost is greater.
 */
#define bol(list, ptr, obj, memg)	do { \
		if (((ptr) % (memg)) == 0) { \
			if ((ptr) != 0) { \
				(list) = incmem((list), (ptr) * sizeof(obj), \
					((ptr) + (memg)) * sizeof(obj)); \
			} else { \
				(list) = getmem((memg) * sizeof(obj)); \
			} \
		} \
		if ((ptr) != 0) \
			mmvwo((list) + 1, (list), (ptr) * sizeof(obj)); \
		(ptr) ++; \
		(list)[0] = (obj); \
	} while (0)

/*
 * mbol() does the same as bol(), but adds the new item at the given
 * emplacement; bol() is equivalent to mbol with 0 as last argument.
 */
#define mbol(list, ptr, obj, memg, n)	do { \
		if (((ptr) % (memg)) == 0) { \
			if ((ptr) != 0) { \
				(list) = incmem((list), (ptr) * sizeof(obj), \
					((ptr) + (memg)) * sizeof(obj)); \
			} else { \
				(list) = getmem((memg) * sizeof(obj)); \
			} \
		} \
		if ((ptr) > n) \
			mmvwo((list) + n + 1, (list) + n, \
				((ptr) - n) * sizeof(obj)); \
		(ptr) ++; \
		(list)[n] = (obj); \
	} while (0)

/*
 * this macro adds the object obj at the end of the array list, doubling
 * the size of list when needed; as for aol(), ptr and list must be
 * lvalues, and so must be llng
 */

#define wan(list, ptr, obj, llng)	do { \
		if ((ptr) == (llng)) { \
			(llng) += (llng); \
			(list) = incmem((list), (ptr) * sizeof(obj), \
				(llng) * sizeof(obj)); \
		} \
		(list)[(ptr) ++] = (obj); \
	} while (0)

#endif