Import of old SSLeay release: SSLeay 0.8.1b
[openssl.git] / crypto / mem.c
1 /* crypto/mem.c */
2 /* Copyright (C) 1995-1997 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
66 static int mh_mode=CRYPTO_MEM_CHECK_OFF;
67 static unsigned long order=0;
68
69 static LHASH *mh=NULL;
70
71 typedef struct mem_st
72         {
73         char *addr;
74         int num;
75         char *file;
76         int line;
77         unsigned long order;
78         } MEM;
79
80 int CRYPTO_mem_ctrl(mode)
81 int 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
101 static int mem_cmp(a,b)
102 MEM *a,*b;
103         {
104         return(a->addr - b->addr);
105         }
106
107 static unsigned long mem_hash(a)
108 MEM *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
118 static char *(*malloc_func)()=  (char *(*)())malloc;
119 static char *(*realloc_func)()= (char *(*)())realloc;
120 static void (*free_func)()=     (void (*)())free;
121
122 void CRYPTO_set_mem_functions(m,r,f)
123 char *(*m)();
124 char *(*r)();
125 void (*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
133 void CRYPTO_get_mem_functions(m,r,f)
134 char *(**m)();
135 char *(**r)();
136 void (**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
143 char *CRYPTO_malloc(num)
144 int num;
145         {
146         return(malloc_func(num));
147         }
148
149 char *CRYPTO_realloc(str,num)
150 char *str;
151 int num;
152         {
153         return(realloc_func(str,num));
154         }
155
156 void CRYPTO_free(str)
157 char *str;
158         {
159         free_func(str);
160         }
161
162 char *CRYPTO_dbg_malloc(num,file,line)
163 int num;
164 char *file;
165 int line;
166         {
167         char *ret;
168         MEM *m;
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 (lh_insert(mh,(char *)m) != NULL)
197                         {
198                         free(m);
199                         free(ret);
200                         /* abort(); */
201                         ret=NULL;
202                         }
203                 CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);
204                 }
205         return(ret);
206         }
207
208 void CRYPTO_dbg_free(addr)
209 char *addr;
210         {
211         MEM m,*mp;
212
213         if ((mh_mode & CRYPTO_MEM_CHECK_ON) && (mh != NULL))
214                 {
215                 CRYPTO_w_lock(CRYPTO_LOCK_MALLOC);
216                 m.addr=addr;
217                 mp=(MEM *)lh_delete(mh,(char *)&m);
218                 if (mp != NULL)
219                         free(mp);
220                 CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);
221                 }
222         free_func(addr);
223         }
224
225 char *CRYPTO_dbg_realloc(addr,num,file,line)
226 char *addr;
227 int num;
228 char *file;
229 int line;
230         {
231         char *ret;
232         MEM m,*mp;
233
234         ret=realloc_func(addr,num);
235         if (ret == addr) return(ret);
236
237         if (mh_mode & CRYPTO_MEM_CHECK_ON)
238                 {
239                 if (ret == NULL) return(NULL);
240                 m.addr=addr;
241                 CRYPTO_w_lock(CRYPTO_LOCK_MALLOC);
242                 mp=(MEM *)lh_delete(mh,(char *)&m);
243                 if (mp != NULL)
244                         {
245                         mp->addr=ret;
246                         lh_insert(mh,(char *)mp);
247                         }
248                 CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);
249                 }
250         return(ret);
251         }
252
253 char *CRYPTO_remalloc(a,n)
254 char *a;
255 int n;
256         {
257         if (a != NULL) Free(a);
258         a=(char *)Malloc(n);
259         return(a);
260         }
261
262 char *CRYPTO_dbg_remalloc(a,n,file,line)
263 char *a;
264 int n;
265 char *file;
266 int line;
267         {
268         if (a != NULL) CRYPTO_dbg_free(a);
269         a=(char *)CRYPTO_dbg_malloc(n,file,line);
270         return(a);
271         }
272
273
274 typedef struct mem_leak_st
275         {
276         BIO *bio;
277         int chunks;
278         long bytes;
279         } MEM_LEAK;
280
281 static void print_leak(m,l)
282 MEM *m;
283 MEM_LEAK *l;
284         {
285         char buf[128];
286
287         sprintf(buf,"%5ld file=%s, line=%d, number=%d, address=%08lX\n",
288                 m->order,m->file,m->line,m->num,(long)m->addr);
289         BIO_puts(l->bio,buf);
290         l->chunks++;
291         l->bytes+=m->num;
292         }
293
294 void CRYPTO_mem_leaks(b)
295 BIO *b;
296         {
297         MEM_LEAK ml;
298         char buf[80];
299
300         if (mh == NULL) return;
301         ml.bio=b;
302         ml.bytes=0;
303         ml.chunks=0;
304         CRYPTO_w_lock(CRYPTO_LOCK_MALLOC);
305         lh_doall_arg(mh,(void (*)())print_leak,(char *)&ml);
306         CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);
307         if (ml.chunks != 0)
308                 {
309                 sprintf(buf,"%ld bytes leaked in %d chunks\n",
310                         ml.bytes,ml.chunks);
311                 BIO_puts(b,buf);
312                 }
313         /*
314         lh_stats_bio(mh,b);
315         lh_node_stats_bio(mh,b);
316         lh_node_usage_stats_bio(mh,b);
317         */
318         }
319
320 static void (*mem_cb)()=NULL;
321
322 static void cb_leak(m,cb)
323 MEM *m;
324 char *cb;
325         {
326         void (*mem_callback)()=(void (*)())cb;
327         mem_callback(m->order,m->file,m->line,m->num,m->addr);
328         }
329
330 void CRYPTO_mem_leaks_cb(cb)
331 void (*cb)();
332         {
333         if (mh == NULL) return;
334         CRYPTO_w_lock(CRYPTO_LOCK_MALLOC);
335         mem_cb=cb;
336         lh_doall_arg(mh,(void (*)())cb_leak,(char *)mem_cb);
337         mem_cb=NULL;
338         CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);
339         }
340
341 #ifndef WIN16
342 void CRYPTO_mem_leaks_fp(fp)
343 FILE *fp;
344         {
345         BIO *b;
346
347         if (mh == NULL) return;
348         if ((b=BIO_new(BIO_s_file())) == NULL)
349                 return;
350         BIO_set_fp(b,fp,BIO_NOCLOSE);
351         CRYPTO_mem_leaks(b);
352         BIO_free(b);
353         }
354 #endif
355