]> git.cworth.org Git - vogl/blob - src/voglcore/rmalloc.h
Initial vogl checkin
[vogl] / src / voglcore / rmalloc.h
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  *
25  **************************************************************************/
26
27 /* =====================================================================
28    File:        rmalloc.h
29    Author:      Rammi
30    Date:        11/16/1995
31
32    License:     (This is the open source ISC license, see
33              http://en.wikipedia.org/wiki/ISC_license
34              for more info)
35
36     Copyright Â© 2010 Andreas M. Rammelt <rammi@hexco.de>
37
38     Permission to use, copy, modify, and/or distribute this software for any
39     purpose with or without fee is hereby granted, provided that the above
40     copyright notice and this permission notice appear in all copies.
41
42     THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
43     WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
44     MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
45     ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
46     WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
47     ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
48     OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
49
50
51    Content:     Switching on/off of malloc debug library. This is done
52       by preprocessor switch MALLOC_DEBUG.
53
54       If MALLOC_DEBUG is defined, special debug versions for
55       the functions of the standard malloc lib are used.
56       They try to get hold of the following things:
57
58       * Overwrite borders of allocated block
59       * Multiple free of a block
60       * free of an unallocated block
61
62       All sources must include this file. No more changes necessary.
63
64       Special macros:
65       RM_TEST    Force testing of all allocated blocks.
66
67       RM_STAT    Show a statistic of allocated blocks.
68
69       RM_RETAG(p)  change position for allocated block to pos of
70              this macro
71
72       RM_SET(p, f) Set flag f for block p. f can be a combination of
73              the following:
74          RM_STATIC Block is considered static. Is only summarized
75                       in statistics.
76
77          RM_STRING Block is meant to hold a string.
78
79       The global behaviour can be changed by editing, recompiling and
80       linking rmalloc.c. See information there.
81
82    Changes:
83             See rmalloc.c
84
85    ===================================================================== */
86
87 #ifndef R_MALLOC_H
88 #define R_MALLOC_H
89
90 #ifdef __cplusplus
91 extern "C" {
92 #endif
93
94 /* =========
95            INCLUDEs:
96            ========= */
97
98 #include <stdlib.h>
99 #include <string.h>
100
101 /* ========
102            DEFINEs:
103            ======== */
104
105 #define RM_UPPERSTYLE /* this is only used for backward compatibility */
106
107 /* once again useful: INT2STRING(prepro_macro_containing_number)-->"number" */
108 #define FUNCTIONIZE(a, b) a(b)
109 #define STRINGIZE(a) #a
110 #define INT2STRING(i) FUNCTIONIZE(STRINGIZE, i)
111
112 /* Flags in header (with WITH_FLAGS defined, see rmalloc.c) */
113 #define RM_STATIC (0x0001 << 0) /* static memory */
114 #define RM_STRING (0x0001 << 1) /* contains string */
115
116 #ifdef MALLOC_DEBUG
117 /* Useful, to build 1 string from __FILE__ & __LINE__ in compile time: */
118 #define RM_FILE_POS __FILE__ ":" INT2STRING(__LINE__)
119
120 #ifdef RM_UPPERSTYLE
121 /* Deprecated: Only used for backward compatibility: */
122 #define MALLOC(s) Rmalloc((s), RM_FILE_POS)
123 #define CALLOC(n, s) Rcalloc((n), (s), RM_FILE_POS)
124 #define REALLOC(p, s) Rrealloc((p), (s), RM_FILE_POS)
125 #define FREE(p) Rfree((p), RM_FILE_POS)
126 #define FREE0(p) Rfree((p), RM_FILE_POS), (p) = NULL
127 #define STRDUP(s) Rstrdup((s), RM_FILE_POS)
128 #define MALLOC_USABLE_SIZE(s) Rmalloc_usable_size(s, RM_FILE_POS)
129 #else /* !RM_UPPERSTYLE */
130 /* Wrap with our stuff: */
131 #ifdef malloc
132 #undef malloc
133 #endif
134 #define malloc(s) Rmalloc((s), RM_FILE_POS)
135
136 #ifdef calloc
137 #undef calloc
138 #endif
139 #define calloc(n, s) Rcalloc((n), (s), RM_FILE_POS)
140
141 #ifdef realloc
142 #undef realloc
143 #endif
144 #define realloc(p, s) Rrealloc((p), (s), RM_FILE_POS)
145
146 #ifdef free
147 #undef free
148 #endif
149 #define free(p) Rfree((p), RM_FILE_POS)
150
151 #ifdef free0
152 #undef free0
153 #endif
154 #define free0(p) Rfree((p), RM_FILE_POS), (p) = NULL
155
156 #ifdef strdup
157 #undef strdup
158 #endif
159 #define strdup(s) Rstrdup((s), RM_FILE_POS)
160
161 #ifdef getcwd
162 #undef getcwd
163 #endif
164 #define getcwd(b, s) Rgetcwd((b), (s), RM_FILE_POS)
165
166 #ifdef malloc_usable_size
167 #undef malloc_usable_size
168 #endif
169 #define malloc_usable_size(s) Rmalloc_usable_size((s), RM_FILE_POS)
170 #endif /* RM_UPPERSTYLE */
171
172 #define RM_TEST Rmalloc_test(RM_FILE_POS)
173 #define RM_STAT Rmalloc_stat(RM_FILE_POS)
174 #define RM_RETAG(p) Rmalloc_retag((p), RM_FILE_POS)
175 #define RM_SET(p, f) Rmalloc_set_flags((p), (f), RM_FILE_POS)
176
177 #else /* ! MALLOC_DEBUG */
178
179 #ifdef RM_UPPERSTYLE
180 /* The normal stuff (backward compatibility): */
181 #define MALLOC(s) malloc((s))
182 #define CALLOC(n, s) calloc((n), (s))
183 #define REALLOC(p, s) realloc((p), (s))
184 #define FREE(p) free((p))
185 #define FREE0(p) free((p)), (p) = NULL
186 #define STRDUP(s) (char *) strdup((s))
187 #define MALLOC_USABLE_SIZE(s) malloc_usable_size(s)
188 #else /* !RM_UPPERSTYLE */
189 #define free0(p) free((p)), (p) = NULL
190 #endif /* RM_UPPERSTYLE */
191 #define RM_TEST
192 #define RM_STAT
193 #define RM_RETAG(p) (p)
194 #define RM_SET(p, f) (p)
195
196 #endif /* ! MALLOC_DEBUG */
197
198 /* ===========
199            PROTOTYPES:
200            =========== */
201
202 #if defined(MALLOC_DEBUG) || defined(RM_NEED_PROTOTYPES)
203
204 typedef void *(*rmalloc_malloc_func_t)(size_t size, void *pUser);
205 typedef void (*rmalloc_free_func_t)(void *ptr, void *pUser);
206 typedef void *(*rmalloc_realloc_func_t)(void *ptr, size_t size, void *pUser);
207
208 void Rmalloc_set_callbacks(rmalloc_malloc_func_t pMalloc, rmalloc_free_func_t pFree, rmalloc_realloc_func_t pRealloc, void *pUser);
209
210 void *Rmalloc(size_t size, const char *file);
211 void *Rcalloc(size_t nelem, size_t size, const char *file);
212 void *Rrealloc(void *p, size_t size, const char *file);
213 void Rfree(void *p, const char *file);
214 char *Rstrdup(const char *str, const char *file);
215 char *Rgetcwd(char *buffer, size_t size, const char *file);
216 void Rtest_malloc(const char *file);
217 void Rmalloc_test(const char *file);
218 void Rmalloc_stat(const char *file);
219 void *Rmalloc_retag(void *p, const char *file);
220 void *Rmalloc_set_flags(void *p, unsigned flags, const char *file);
221 size_t Rmalloc_usable_size(void *p, const char *file);
222 #endif /* MALLOC_DEBUG || RM_NEED_PROTOTYPES */
223
224 #ifdef __cplusplus
225 }
226 #endif
227
228 #endif /* !R_MALLOC_H */