summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/mem.c
diff options
context:
space:
mode:
authorbeck <>2002-05-15 02:29:21 +0000
committerbeck <>2002-05-15 02:29:21 +0000
commitb64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9 (patch)
treefa27cf82a1250b64ed3bf5f4a18c7354d470bbcc /src/lib/libcrypto/mem.c
parente471e1ea98d673597b182ea85f29e30c97cd08b5 (diff)
downloadopenbsd-b64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9.tar.gz
openbsd-b64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9.tar.bz2
openbsd-b64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9.zip
OpenSSL 0.9.7 stable 2002 05 08 merge
Diffstat (limited to 'src/lib/libcrypto/mem.c')
-rw-r--r--src/lib/libcrypto/mem.c127
1 files changed, 102 insertions, 25 deletions
diff --git a/src/lib/libcrypto/mem.c b/src/lib/libcrypto/mem.c
index 3b5b2bbc68..effec714e8 100644
--- a/src/lib/libcrypto/mem.c
+++ b/src/lib/libcrypto/mem.c
@@ -70,14 +70,36 @@ static int allow_customize_debug = 1;/* exchanging memory-related functions at
70 * problems when malloc/free pairs 70 * problems when malloc/free pairs
71 * don't match etc. */ 71 * don't match etc. */
72 72
73/* may be changed as long as `allow_customize' is set */ 73
74static void *(*malloc_locked_func)(size_t) = malloc; 74
75static void (*free_locked_func)(void *) = free; 75/* the following pointers may be changed as long as 'allow_customize' is set */
76
76static void *(*malloc_func)(size_t) = malloc; 77static void *(*malloc_func)(size_t) = malloc;
78static void *default_malloc_ex(size_t num, const char *file, int line)
79 { return malloc_func(num); }
80static void *(*malloc_ex_func)(size_t, const char *file, int line)
81 = default_malloc_ex;
82
77static void *(*realloc_func)(void *, size_t)= realloc; 83static void *(*realloc_func)(void *, size_t)= realloc;
84static void *default_realloc_ex(void *str, size_t num,
85 const char *file, int line)
86 { return realloc_func(str,num); }
87static void *(*realloc_ex_func)(void *, size_t, const char *file, int line)
88 = default_realloc_ex;
89
78static void (*free_func)(void *) = free; 90static void (*free_func)(void *) = free;
79 91
80/* may be changed as long as `allow_customize_debug' is set */ 92static void *(*malloc_locked_func)(size_t) = malloc;
93static void *default_malloc_locked_ex(size_t num, const char *file, int line)
94 { return malloc_locked_func(num); }
95static void *(*malloc_locked_ex_func)(size_t, const char *file, int line)
96 = default_malloc_locked_ex;
97
98static void (*free_locked_func)(void *) = free;
99
100
101
102/* may be changed as long as 'allow_customize_debug' is set */
81/* XXX use correct function pointer types */ 103/* XXX use correct function pointer types */
82#ifdef CRYPTO_MDEBUG 104#ifdef CRYPTO_MDEBUG
83/* use default functions from mem_dbg.c */ 105/* use default functions from mem_dbg.c */
@@ -105,12 +127,29 @@ int CRYPTO_set_mem_functions(void *(*m)(size_t), void *(*r)(void *, size_t),
105 { 127 {
106 if (!allow_customize) 128 if (!allow_customize)
107 return 0; 129 return 0;
108 if ((m == NULL) || (r == NULL) || (f == NULL)) 130 if ((m == 0) || (r == 0) || (f == 0))
131 return 0;
132 malloc_func=m; malloc_ex_func=default_malloc_ex;
133 realloc_func=r; realloc_ex_func=default_realloc_ex;
134 free_func=f;
135 malloc_locked_func=m; malloc_locked_ex_func=default_malloc_locked_ex;
136 free_locked_func=f;
137 return 1;
138 }
139
140int CRYPTO_set_mem_ex_functions(
141 void *(*m)(size_t,const char *,int),
142 void *(*r)(void *, size_t,const char *,int),
143 void (*f)(void *))
144 {
145 if (!allow_customize)
146 return 0;
147 if ((m == 0) || (r == 0) || (f == 0))
109 return 0; 148 return 0;
110 malloc_func=m; 149 malloc_func=0; malloc_ex_func=m;
111 realloc_func=r; 150 realloc_func=0; realloc_ex_func=r;
112 free_func=f; 151 free_func=f;
113 malloc_locked_func=m; 152 malloc_locked_func=0; malloc_locked_ex_func=m;
114 free_locked_func=f; 153 free_locked_func=f;
115 return 1; 154 return 1;
116 } 155 }
@@ -121,11 +160,24 @@ int CRYPTO_set_locked_mem_functions(void *(*m)(size_t), void (*f)(void *))
121 return 0; 160 return 0;
122 if ((m == NULL) || (f == NULL)) 161 if ((m == NULL) || (f == NULL))
123 return 0; 162 return 0;
124 malloc_locked_func=m; 163 malloc_locked_func=m; malloc_locked_ex_func=default_malloc_locked_ex;
125 free_locked_func=f; 164 free_locked_func=f;
126 return 1; 165 return 1;
127 } 166 }
128 167
168int CRYPTO_set_locked_mem_ex_functions(
169 void *(*m)(size_t,const char *,int),
170 void (*f)(void *))
171 {
172 if (!allow_customize)
173 return 0;
174 if ((m == NULL) || (f == NULL))
175 return 0;
176 malloc_locked_func=0; malloc_locked_ex_func=m;
177 free_func=f;
178 return 1;
179 }
180
129int CRYPTO_set_mem_debug_functions(void (*m)(void *,int,const char *,int,int), 181int CRYPTO_set_mem_debug_functions(void (*m)(void *,int,const char *,int,int),
130 void (*r)(void *,void *,int,const char *,int,int), 182 void (*r)(void *,void *,int,const char *,int,int),
131 void (*f)(void *,int), 183 void (*f)(void *,int),
@@ -142,17 +194,42 @@ int CRYPTO_set_mem_debug_functions(void (*m)(void *,int,const char *,int,int),
142 return 1; 194 return 1;
143 } 195 }
144 196
197
145void CRYPTO_get_mem_functions(void *(**m)(size_t), void *(**r)(void *, size_t), 198void CRYPTO_get_mem_functions(void *(**m)(size_t), void *(**r)(void *, size_t),
146 void (**f)(void *)) 199 void (**f)(void *))
147 { 200 {
148 if (m != NULL) *m=malloc_func; 201 if (m != NULL) *m = (malloc_ex_func == default_malloc_ex) ?
149 if (r != NULL) *r=realloc_func; 202 malloc_func : 0;
203 if (r != NULL) *r = (realloc_ex_func == default_realloc_ex) ?
204 realloc_func : 0;
205 if (f != NULL) *f=free_func;
206 }
207
208void CRYPTO_get_mem_ex_functions(
209 void *(**m)(size_t,const char *,int),
210 void *(**r)(void *, size_t,const char *,int),
211 void (**f)(void *))
212 {
213 if (m != NULL) *m = (malloc_ex_func != default_malloc_ex) ?
214 malloc_ex_func : 0;
215 if (r != NULL) *r = (realloc_ex_func != default_realloc_ex) ?
216 realloc_ex_func : 0;
150 if (f != NULL) *f=free_func; 217 if (f != NULL) *f=free_func;
151 } 218 }
152 219
153void CRYPTO_get_locked_mem_functions(void *(**m)(size_t), void (**f)(void *)) 220void CRYPTO_get_locked_mem_functions(void *(**m)(size_t), void (**f)(void *))
154 { 221 {
155 if (m != NULL) *m=malloc_locked_func; 222 if (m != NULL) *m = (malloc_locked_ex_func == default_malloc_locked_ex) ?
223 malloc_locked_func : 0;
224 if (f != NULL) *f=free_locked_func;
225 }
226
227void CRYPTO_get_locked_mem_ex_functions(
228 void *(**m)(size_t,const char *,int),
229 void (**f)(void *))
230 {
231 if (m != NULL) *m = (malloc_locked_ex_func != default_malloc_locked_ex) ?
232 malloc_locked_ex_func : 0;
156 if (f != NULL) *f=free_locked_func; 233 if (f != NULL) *f=free_locked_func;
157 } 234 }
158 235
@@ -180,9 +257,9 @@ void *CRYPTO_malloc_locked(int num, const char *file, int line)
180 allow_customize_debug = 0; 257 allow_customize_debug = 0;
181 malloc_debug_func(NULL, num, file, line, 0); 258 malloc_debug_func(NULL, num, file, line, 0);
182 } 259 }
183 ret = malloc_locked_func(num); 260 ret = malloc_locked_ex_func(num,file,line);
184#ifdef LEVITTE_DEBUG 261#ifdef LEVITTE_DEBUG_MEM
185 fprintf(stderr, "LEVITTE_DEBUG: > 0x%p (%d)\n", ret, num); 262 fprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\n", ret, num);
186#endif 263#endif
187 if (malloc_debug_func != NULL) 264 if (malloc_debug_func != NULL)
188 malloc_debug_func(ret, num, file, line, 1); 265 malloc_debug_func(ret, num, file, line, 1);
@@ -194,8 +271,8 @@ void CRYPTO_free_locked(void *str)
194 { 271 {
195 if (free_debug_func != NULL) 272 if (free_debug_func != NULL)
196 free_debug_func(str, 0); 273 free_debug_func(str, 0);
197#ifdef LEVITTE_DEBUG 274#ifdef LEVITTE_DEBUG_MEM
198 fprintf(stderr, "LEVITTE_DEBUG: < 0x%p\n", str); 275 fprintf(stderr, "LEVITTE_DEBUG_MEM: < 0x%p\n", str);
199#endif 276#endif
200 free_locked_func(str); 277 free_locked_func(str);
201 if (free_debug_func != NULL) 278 if (free_debug_func != NULL)
@@ -212,9 +289,9 @@ void *CRYPTO_malloc(int num, const char *file, int line)
212 allow_customize_debug = 0; 289 allow_customize_debug = 0;
213 malloc_debug_func(NULL, num, file, line, 0); 290 malloc_debug_func(NULL, num, file, line, 0);
214 } 291 }
215 ret = malloc_func(num); 292 ret = malloc_ex_func(num,file,line);
216#ifdef LEVITTE_DEBUG 293#ifdef LEVITTE_DEBUG_MEM
217 fprintf(stderr, "LEVITTE_DEBUG: > 0x%p (%d)\n", ret, num); 294 fprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\n", ret, num);
218#endif 295#endif
219 if (malloc_debug_func != NULL) 296 if (malloc_debug_func != NULL)
220 malloc_debug_func(ret, num, file, line, 1); 297 malloc_debug_func(ret, num, file, line, 1);
@@ -228,9 +305,9 @@ void *CRYPTO_realloc(void *str, int num, const char *file, int line)
228 305
229 if (realloc_debug_func != NULL) 306 if (realloc_debug_func != NULL)
230 realloc_debug_func(str, NULL, num, file, line, 0); 307 realloc_debug_func(str, NULL, num, file, line, 0);
231 ret = realloc_func(str,num); 308 ret = realloc_ex_func(str,num,file,line);
232#ifdef LEVITTE_DEBUG 309#ifdef LEVITTE_DEBUG_MEM
233 fprintf(stderr, "LEVITTE_DEBUG: | 0x%p -> 0x%p (%d)\n", str, ret, num); 310 fprintf(stderr, "LEVITTE_DEBUG_MEM: | 0x%p -> 0x%p (%d)\n", str, ret, num);
234#endif 311#endif
235 if (realloc_debug_func != NULL) 312 if (realloc_debug_func != NULL)
236 realloc_debug_func(str, ret, num, file, line, 1); 313 realloc_debug_func(str, ret, num, file, line, 1);
@@ -242,8 +319,8 @@ void CRYPTO_free(void *str)
242 { 319 {
243 if (free_debug_func != NULL) 320 if (free_debug_func != NULL)
244 free_debug_func(str, 0); 321 free_debug_func(str, 0);
245#ifdef LEVITTE_DEBUG 322#ifdef LEVITTE_DEBUG_MEM
246 fprintf(stderr, "LEVITTE_DEBUG: < 0x%p\n", str); 323 fprintf(stderr, "LEVITTE_DEBUG_MEM: < 0x%p\n", str);
247#endif 324#endif
248 free_func(str); 325 free_func(str);
249 if (free_debug_func != NULL) 326 if (free_debug_func != NULL)