af8a65fc2ae17ea1edae0876ddc1214f00633106
[idzebra-moved-to-github.git] / dfa / imalloc.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2009 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20
21 #include <stdio.h>
22 #include <assert.h>
23 #include <stdlib.h>
24
25 #include <idzebra/util.h>
26 #include <yaz/xmalloc.h>
27 #include "imalloc.h"
28
29 #if MEMDEBUG
30 #define MAG1 0x8fe1
31 #define MAG2 0x91
32 #define MAG3 0xee
33
34 long alloc       = 0L;
35 long max_alloc   = 0L;
36 int  alloc_calls = 0;
37 int  free_calls  = 0;
38 #endif
39
40 void *imalloc (size_t size)
41 {
42 #if MEMDEBUG
43     size_t words = (4*sizeof(unsigned) -1 + size)/sizeof(unsigned);
44     char *p = (char *)xmalloc( words*sizeof(unsigned) );
45     if( !p )
46         yaz_log (YLOG_FATAL, "No memory: imalloc(%u); c/f %d/%d; %ld/%ld",
47            size, alloc_calls, free_calls, alloc, max_alloc );
48     *((unsigned *)p) = size;
49     ((unsigned *)p)[1] = MAG1;
50     p += sizeof(unsigned)*2;
51     size[(unsigned char *) p] = MAG2;
52     size[(unsigned char *) p+1] = MAG3;
53     if( (alloc+=size) > max_alloc )
54         max_alloc = alloc;
55     ++alloc_calls;
56     return (void *) p;
57 #else
58     void *p = (void *)xmalloc( size );
59     if( !p )
60         yaz_log (YLOG_FATAL, "Out of memory (imalloc)" );
61     return p;
62 #endif
63 }
64
65 void *icalloc (size_t size)
66 {
67 #if MEMDEBUG
68     unsigned words = (4*sizeof(unsigned) -1 + size)/sizeof(unsigned);
69     char *p = (char *) xcalloc( words*sizeof(unsigned), 1 );
70     if( !p )
71         yaz_log (YLOG_FATAL, "No memory: icalloc(%u); c/f %d/%d; %ld/%ld",
72            size, alloc_calls, free_calls, alloc, max_alloc );
73     ((unsigned *)p)[0] = size;
74     ((unsigned *)p)[1] = MAG1;
75     p += sizeof(unsigned)*2;
76     size[(unsigned char *) p] = MAG2;
77     size[(unsigned char *) p+1] = MAG3;
78     if( (alloc+=size) > max_alloc )
79         max_alloc = alloc;
80     ++alloc_calls;
81     return (void *)p;
82 #else
83     void *p = (void *) xcalloc( size, 1 );
84     if( !p )
85         yaz_log (YLOG_FATAL, "Out of memory (icalloc)" );
86     return p;
87 #endif
88 }
89
90 void ifree (void *p)
91 {
92 #if MEMDEBUG
93     size_t size;
94     if( !p )
95         return;
96     ++free_calls;
97     size = (-2)[(unsigned *) p];
98     if( (-1)[(unsigned *) p] != MAG1 )
99         yaz_log (YLOG_FATAL,"Internal: ifree(%u) magic 1 corrupted", size );
100     if( size[(unsigned char *) p] != MAG2 )
101         yaz_log (YLOG_FATAL,"Internal: ifree(%u) magic 2 corrupted", size );
102     if( (size+1)[(unsigned char *) p] != MAG3 )
103         yaz_log (YLOG_FATAL,"Internal: ifree(%u) magic 3 corrupted", size );
104     alloc -= size;
105     if( alloc < 0L )
106         yaz_log (YLOG_FATAL,"Internal: ifree(%u) negative alloc.", size );
107     xfree( (unsigned *) p-2 );
108 #else
109     xfree (p);
110 #endif
111 }
112
113 #if MEMDEBUG
114 void imemstat (void)
115 {
116     fprintf( stdout, "imalloc: calls malloc/free %d/%d, ",
117                                      alloc_calls, free_calls );
118     if( alloc )
119         fprintf( stdout, "memory cur/max %ld/%ld : unreleased",
120                                                           alloc, max_alloc );
121     else
122         fprintf( stdout, "memory max %ld", max_alloc );
123     fputc( '\n', stdout );
124 }
125 #endif
126 /*
127  * Local variables:
128  * c-basic-offset: 4
129  * c-file-style: "Stroustrup"
130  * indent-tabs-mode: nil
131  * End:
132  * vim: shiftwidth=4 tabstop=8 expandtab
133  */
134