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