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
|
/*
** This is a copyrighted work which is functionally identical to work
** originally published in Micro Cornucopia magazine (issue #52, March-April,
** 1990) and is freely licensed by the author, Walter Bright, for any use.
*/
/*_ mem.h Fri May 26 1989 Modified by: Walter Bright */
/* $Header: /home/cvs/c_cpp_reference/reference/C/CONTRIB/SNIP/mem.h,v 1.1.1.1 2000/02/24 23:01:19 tasin Exp $ */
/* Copyright 1986-1988 by Northwest Software */
/* All Rights Reserved */
/* Written by Walter Bright */
#ifndef MEM_H
#define MEM_H 1
#ifndef TOOLKIT_H
#include "toolkit.h"
#endif
/*
* Memory management routines.
*
* Compiling:
*
* #define MEM_DEBUG 1 when compiling to enable extended debugging
* features.
*
* Features always enabled:
*
* o mem_init() is called at startup, and mem_term() at
* close, which checks to see that the number of alloc's is
* the same as the number of free's.
* o Behavior on out-of-memory conditions can be controlled
* via mem_setexception().
*
* Extended debugging features:
*
* o Enabled by #define MEM_DEBUG 1 when compiling.
* o Check values are inserted before and after the alloc'ed data
* to detect pointer underruns and overruns.
* o Free'd pointers are checked against alloc'ed pointers.
* o Free'd storage is cleared to smoke out references to free'd data.
* o Realloc'd pointers are always changed, and the previous storage
* is cleared, to detect erroneous dependencies on the previous
* pointer.
* o The routine mem_checkptr() is provided to check an alloc'ed
* pointer.
*/
/********************* GLOBAL VARIABLES *************************/
extern int mem_inited; /* != 0 if mem package is initialized. */
/* Test this if you have other packages */
/* that depend on mem being initialized */
/********************* PUBLIC FUNCTIONS *************************/
/***********************************
* Set behavior when mem runs out of memory.
* Input:
* flag = MEM_ABORTMSG: Abort the program with the message
* 'Fatal error: out of memory' sent
* to stdout. This is the default behavior.
* MEM_ABORT: Abort the program with no message.
* MEM_RETNULL: Return NULL back to caller.
* MEM_CALLFP: Call application-specified function.
* fp must be supplied.
* fp Optional function pointer. Supplied if
* (flag == MEM_CALLFP). This function returns
* MEM_XXXXX, indicating what mem should do next.
* The function could do things like swap
* data out to disk to free up more memory.
* fp could also return:
* MEM_RETRY: Try again to allocate the space. Be
* careful not to go into an infinite loop.
*/
#if __cplusplus
enum MEM_E { MEM_ABORTMSG, MEM_ABORT, MEM_RETNULL, MEM_CALLFP, MEM_RETRY };
void mem_setexception P((enum MEM_E, int (*)()));
#else
#define MEM_ABORTMSG 0
#define MEM_ABORT 1
#define MEM_RETNULL 2
#define MEM_CALLFP 3
#define MEM_RETRY 4
void mem_setexception P((int, int(*)()));
#endif
/****************************
* Allocate space for string, copy string into it, and
* return pointer to the new string.
* This routine doesn't really belong here, but it is used so often
* that I gave up and put it here.
* Use:
* char *mem_strdup(const char *s);
* Returns:
* pointer to copied string if succussful.
* else returns NULL (if MEM_RETNULL)
*/
char *mem_strdup P((const char *));
/**************************
* Function so we can have a pointer to function mem_free().
* This is needed since mem_free is sometimes defined as a macro,
* and then the preprocessor screws up.
* The pointer to mem_free() is used frequently with the list package.
* Use:
* void mem_freefp(void *p);
*/
/***************************
* Check for errors. This routine does a consistency check on the
* storage allocator, looking for corrupted data. It should be called
* when the application has CPU cycles to burn.
* Use:
* void mem_check(void);
*/
void mem_check P((void ));
/***************************
* Check ptr to see if it is in the range of allocated data.
* Cause assertion failure if it isn't.
*/
void mem_checkptr P((void *ptr));
/***************************
* Allocate and return a pointer to numbytes of storage.
* Use:
* void *mem_malloc(unsigned numbytes);
* void *mem_calloc(unsigned numbytes); allocated memory is cleared
* Input:
* numbytes Number of bytes to allocate
* Returns:
* if (numbytes > 0)
* pointer to allocated data, NULL if out of memory
* else
* return NULL
*/
void *mem_malloc P((unsigned));
void *mem_calloc P((unsigned));
/*****************************
* Reallocate memory.
* Use:
* void *mem_realloc(void *ptr,unsigned numbytes);
*/
void *mem_realloc P((void *,unsigned));
/*****************************
* Free memory allocated by mem_malloc(), mem_calloc() or mem_realloc().
* Use:
* void mem_free(void *ptr);
*/
void mem_free P((void *));
/***************************
* Initialize memory handler.
* Use:
* void mem_init(void);
* Output:
* mem_inited = 1
*/
void mem_init P((void ));
/***************************
* Terminate memory handler. Useful for checking for errors.
* Use:
* void mem_term(void);
* Output:
* mem_inited = 0
*/
void mem_term P((void ));
/* The following stuff forms the implementation rather than the
* definition, so ignore it.
*/
#if MEM_DEBUG /* if creating debug version */
#define mem_strdup(p) mem_strdup_debug((p),__FILE__,__LINE__)
#define mem_malloc(u) mem_malloc_debug((u),__FILE__,__LINE__)
#define mem_calloc(u) mem_calloc_debug((u),__FILE__,__LINE__)
#define mem_realloc(p,u) mem_realloc_debug((p),(u),__FILE__,__LINE__)
#define mem_free(p) mem_free_debug((p),__FILE__,__LINE__)
char *mem_strdup_debug P((const char *,char *,int));
void *mem_calloc_debug P((unsigned,char *,int));
void *mem_malloc_debug P((unsigned,char *,int));
void *mem_realloc_debug P((void *,unsigned,char *,int));
void mem_free_debug P((void *,char *,int));
void mem_freefp P((void *));
void mem_setnewfileline P((void *,char *,int));
#else
#define mem_freefp mem_free
#define mem_check()
#define mem_checkptr(p)
#endif /* MEM_DEBUG */
#endif /* MEM_H */
|