1 |
/* |
2 |
$Id: mem.h,v 1.26 2002/05/30 06:11:06 whmoseley Exp $ |
3 |
** |
4 |
** This program and library is free software; you can redistribute it and/or |
5 |
** modify it under the terms of the GNU (Library) General Public License |
6 |
** as published by the Free Software Foundation; either version 2 |
7 |
** of the License, or any later version. |
8 |
** |
9 |
** This program is distributed in the hope that it will be useful, |
10 |
** but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 |
** GNU (Library) General Public License for more details. |
13 |
** |
14 |
** You should have received a copy of the GNU (Library) General Public License |
15 |
** along with this program; if not, write to the Free Software |
16 |
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
17 |
** |
18 |
** Author: Bill Meier, June 2001 |
19 |
** |
20 |
*/ |
21 |
|
22 |
/* |
23 |
** The following settings control the memory allocator. Each setting is independent. |
24 |
** They also affect the actual memory usage of the program, because (currently) |
25 |
** turning on any of these settings increases the size of each allocation. |
26 |
** MEM_STATISTICS allocates the least extra and MEM_DEBUG allocates the most extra per call. |
27 |
** |
28 |
** In addition (currently) turning on any of these settings will map all |
29 |
** realloc calls into a alloc and free for simplier implementation. However, this |
30 |
** should be transparent to all programs! |
31 |
*/ |
32 |
|
33 |
/* |
34 |
** Normal settings (but not required): |
35 |
** If you turn on MEM_DEBUG, turn on MEM_STATISTICS |
36 |
** If you turn on MEM_TRACE, turn on MEM_STATISTICS |
37 |
*/ |
38 |
|
39 |
#include <memory.h> |
40 |
|
41 |
#ifdef __cplusplus |
42 |
extern "C" { |
43 |
#endif |
44 |
|
45 |
/* MEM_DEBUG checks for memory consistency on alloc/free */ |
46 |
#define MEM_DEBUG 0 |
47 |
|
48 |
/* MEM_TRACE checks for unfreed memory, and where it is allocated */ |
49 |
#define MEM_TRACE 0 |
50 |
|
51 |
/* MEM_STATISTICS gives memory statistics (bytes allocated, calls, etc */ |
52 |
#define MEM_STATISTICS 0 |
53 |
|
54 |
|
55 |
typedef struct _mem_zone { |
56 |
struct _zone *next; /* link to free chunk */ |
57 |
char *name; /* name of zone */ |
58 |
size_t size; /* size to grow zone by */ |
59 |
int attributes; /* attributes of zone (not used yet) */ |
60 |
unsigned int allocs; /* count of allocations (for statistics) */ |
61 |
} MEM_ZONE; |
62 |
|
63 |
|
64 |
/* The following are the basic malloc/realloc/free replacements */ |
65 |
#if MEM_TRACE |
66 |
extern size_t memory_trace_counter; |
67 |
void Mem_bp(int n); |
68 |
#endif |
69 |
|
70 |
#if MEM_DEBUG | MEM_TRACE | MEM_STATISTICS |
71 |
|
72 |
#define emalloc(size) Mem_Alloc(size, __FILE__, __LINE__) |
73 |
#define erealloc(ptr, size) Mem_Realloc(ptr, size, __FILE__, __LINE__) |
74 |
#define efree(ptr) Mem_Free(ptr, __FILE__, __LINE__) |
75 |
|
76 |
void *Mem_Alloc(size_t size, char *file, int line); |
77 |
void *Mem_Realloc(void *ptr, size_t size, char *file, int line); |
78 |
void Mem_Free(void *ptr, char *file, int line); |
79 |
|
80 |
#else |
81 |
|
82 |
void *emalloc(size_t size); |
83 |
void *erealloc(void *ptr, size_t size); |
84 |
void efree(void *ptr); |
85 |
|
86 |
#endif |
87 |
|
88 |
/* Hook to print out statistics if enabled */ |
89 |
void Mem_Summary(char *title, int final); |
90 |
|
91 |
/* Memory zone routines */ |
92 |
|
93 |
/* create a zone -- size should be some reasonable number */ |
94 |
MEM_ZONE *Mem_ZoneCreate(char *name, size_t size, int attributes); |
95 |
|
96 |
/* allocate memory from a zone (can use like malloc if you aren't going to realloc) */ |
97 |
void *Mem_ZoneAlloc(MEM_ZONE *head, size_t size); |
98 |
|
99 |
/* free all memory in a zone */ |
100 |
void Mem_ZoneFree(MEM_ZONE **head); |
101 |
|
102 |
/* memory zone statistics */ |
103 |
#if MEM_STATISTICS |
104 |
void Mem_ZoneStatistics(MEM_ZONE *head); |
105 |
#endif |
106 |
|
107 |
/* make all memory in a zone reusable */ |
108 |
void Mem_ZoneReset(MEM_ZONE *head); |
109 |
|
110 |
/* Returns the allocated memory owned by a zone */ |
111 |
int Mem_ZoneSize(MEM_ZONE *head); |
112 |
|
113 |
/* Don't let people use the regular C calls */ |
114 |
#define malloc $Please_use_emalloc |
115 |
#define realloc $Please_use_erealloc |
116 |
#define free $Please_use_efree |
117 |
|
118 |
|
119 |
#ifdef __cplusplus |
120 |
} |
121 |
#endif /* __cplusplus */ |
122 |
|
123 |
|