summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/mem.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/mem.c')
-rw-r--r--src/lib/libcrypto/mem.c353
1 files changed, 353 insertions, 0 deletions
diff --git a/src/lib/libcrypto/mem.c b/src/lib/libcrypto/mem.c
new file mode 100644
index 0000000000..72e501ad0f
--- /dev/null
+++ b/src/lib/libcrypto/mem.c
@@ -0,0 +1,353 @@
1/* crypto/mem.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <stdlib.h>
61#include "buffer.h"
62#include "bio.h"
63#include "lhash.h"
64#include "cryptlib.h"
65
66static int mh_mode=CRYPTO_MEM_CHECK_OFF;
67static unsigned long order=0;
68
69static LHASH *mh=NULL;
70
71typedef struct mem_st
72 {
73 char *addr;
74 int num;
75 char *file;
76 int line;
77 unsigned long order;
78 } MEM;
79
80int CRYPTO_mem_ctrl(mode)
81int mode;
82 {
83 int ret=mh_mode;
84
85 CRYPTO_w_lock(CRYPTO_LOCK_MALLOC);
86 switch (mode)
87 {
88 case CRYPTO_MEM_CHECK_ON:
89 mh_mode|=CRYPTO_MEM_CHECK_ON;
90 break;
91 case CRYPTO_MEM_CHECK_OFF:
92 mh_mode&= ~CRYPTO_MEM_CHECK_ON;
93 break;
94 default:
95 break;
96 }
97 CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);
98 return(ret);
99 }
100
101static int mem_cmp(a,b)
102MEM *a,*b;
103 {
104 return(a->addr - b->addr);
105 }
106
107static unsigned long mem_hash(a)
108MEM *a;
109 {
110 unsigned long ret;
111
112 ret=(unsigned long)a->addr;
113
114 ret=ret*17851+(ret>>14)*7+(ret>>4)*251;
115 return(ret);
116 }
117
118static char *(*malloc_func)()= (char *(*)())malloc;
119static char *(*realloc_func)()= (char *(*)())realloc;
120static void (*free_func)()= (void (*)())free;
121
122void CRYPTO_set_mem_functions(m,r,f)
123char *(*m)();
124char *(*r)();
125void (*f)();
126 {
127 if ((m == NULL) || (r == NULL) || (f == NULL)) return;
128 malloc_func=m;
129 realloc_func=r;
130 free_func=f;
131 }
132
133void CRYPTO_get_mem_functions(m,r,f)
134char *(**m)();
135char *(**r)();
136void (**f)();
137 {
138 if (m != NULL) *m=malloc_func;
139 if (r != NULL) *r=realloc_func;
140 if (f != NULL) *f=free_func;
141 }
142
143char *CRYPTO_malloc(num)
144int num;
145 {
146 return(malloc_func(num));
147 }
148
149char *CRYPTO_realloc(str,num)
150char *str;
151int num;
152 {
153 return(realloc_func(str,num));
154 }
155
156void CRYPTO_free(str)
157char *str;
158 {
159 free_func(str);
160 }
161
162char *CRYPTO_dbg_malloc(num,file,line)
163int num;
164char *file;
165int line;
166 {
167 char *ret;
168 MEM *m,*mm;
169
170 if ((ret=malloc_func(num)) == NULL)
171 return(NULL);
172
173 if (mh_mode & CRYPTO_MEM_CHECK_ON)
174 {
175 if ((m=(MEM *)malloc(sizeof(MEM))) == NULL)
176 {
177 free(ret);
178 return(NULL);
179 }
180 CRYPTO_w_lock(CRYPTO_LOCK_MALLOC);
181 if (mh == NULL)
182 {
183 if ((mh=lh_new(mem_hash,mem_cmp)) == NULL)
184 {
185 free(ret);
186 free(m);
187 return(NULL);
188 }
189 }
190
191 m->addr=ret;
192 m->file=file;
193 m->line=line;
194 m->num=num;
195 m->order=order++;
196 if ((mm=(MEM *)lh_insert(mh,(char *)m)) != NULL)
197 {
198 /* Not good, but don't sweat it */
199 free(mm);
200 }
201 CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);
202 }
203 return(ret);
204 }
205
206void CRYPTO_dbg_free(addr)
207char *addr;
208 {
209 MEM m,*mp;
210
211 if ((mh_mode & CRYPTO_MEM_CHECK_ON) && (mh != NULL))
212 {
213 CRYPTO_w_lock(CRYPTO_LOCK_MALLOC);
214 m.addr=addr;
215 mp=(MEM *)lh_delete(mh,(char *)&m);
216 if (mp != NULL)
217 free(mp);
218 CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);
219 }
220 free_func(addr);
221 }
222
223char *CRYPTO_dbg_realloc(addr,num,file,line)
224char *addr;
225int num;
226char *file;
227int line;
228 {
229 char *ret;
230 MEM m,*mp;
231
232 ret=realloc_func(addr,num);
233 if (ret == addr) return(ret);
234
235 if (mh_mode & CRYPTO_MEM_CHECK_ON)
236 {
237 if (ret == NULL) return(NULL);
238 m.addr=addr;
239 CRYPTO_w_lock(CRYPTO_LOCK_MALLOC);
240 mp=(MEM *)lh_delete(mh,(char *)&m);
241 if (mp != NULL)
242 {
243 mp->addr=ret;
244 lh_insert(mh,(char *)mp);
245 }
246 CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);
247 }
248 return(ret);
249 }
250
251char *CRYPTO_remalloc(a,n)
252char *a;
253int n;
254 {
255 if (a != NULL) Free(a);
256 a=(char *)Malloc(n);
257 return(a);
258 }
259
260char *CRYPTO_dbg_remalloc(a,n,file,line)
261char *a;
262int n;
263char *file;
264int line;
265 {
266 if (a != NULL) CRYPTO_dbg_free(a);
267 a=(char *)CRYPTO_dbg_malloc(n,file,line);
268 return(a);
269 }
270
271
272typedef struct mem_leak_st
273 {
274 BIO *bio;
275 int chunks;
276 long bytes;
277 } MEM_LEAK;
278
279static void print_leak(m,l)
280MEM *m;
281MEM_LEAK *l;
282 {
283 char buf[128];
284
285 sprintf(buf,"%5ld file=%s, line=%d, number=%d, address=%08lX\n",
286 m->order,m->file,m->line,m->num,(long)m->addr);
287 BIO_puts(l->bio,buf);
288 l->chunks++;
289 l->bytes+=m->num;
290 }
291
292void CRYPTO_mem_leaks(b)
293BIO *b;
294 {
295 MEM_LEAK ml;
296 char buf[80];
297
298 if (mh == NULL) return;
299 ml.bio=b;
300 ml.bytes=0;
301 ml.chunks=0;
302 CRYPTO_w_lock(CRYPTO_LOCK_MALLOC);
303 lh_doall_arg(mh,(void (*)())print_leak,(char *)&ml);
304 CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);
305 if (ml.chunks != 0)
306 {
307 sprintf(buf,"%ld bytes leaked in %d chunks\n",
308 ml.bytes,ml.chunks);
309 BIO_puts(b,buf);
310 }
311 /*
312 lh_stats_bio(mh,b);
313 lh_node_stats_bio(mh,b);
314 lh_node_usage_stats_bio(mh,b);
315 */
316 }
317
318static void (*mem_cb)()=NULL;
319
320static void cb_leak(m,cb)
321MEM *m;
322char *cb;
323 {
324 void (*mem_callback)()=(void (*)())cb;
325 mem_callback(m->order,m->file,m->line,m->num,m->addr);
326 }
327
328void CRYPTO_mem_leaks_cb(cb)
329void (*cb)();
330 {
331 if (mh == NULL) return;
332 CRYPTO_w_lock(CRYPTO_LOCK_MALLOC);
333 mem_cb=cb;
334 lh_doall_arg(mh,(void (*)())cb_leak,(char *)mem_cb);
335 mem_cb=NULL;
336 CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);
337 }
338
339#ifndef NO_FP_API
340void CRYPTO_mem_leaks_fp(fp)
341FILE *fp;
342 {
343 BIO *b;
344
345 if (mh == NULL) return;
346 if ((b=BIO_new(BIO_s_file())) == NULL)
347 return;
348 BIO_set_fp(b,fp,BIO_NOCLOSE);
349 CRYPTO_mem_leaks(b);
350 BIO_free(b);
351 }
352#endif
353