1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2009 Index Data
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
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
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
25 #include <idzebra/util.h>
26 #include <yaz/xmalloc.h>
40 void *imalloc (size_t size)
43 size_t words = (4*sizeof(unsigned) -1 + size)/sizeof(unsigned);
44 char *p = (char *)xmalloc( words*sizeof(unsigned) );
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 )
58 void *p = (void *)xmalloc( size );
60 yaz_log (YLOG_FATAL, "Out of memory (imalloc)" );
65 void *icalloc (size_t size)
68 unsigned words = (4*sizeof(unsigned) -1 + size)/sizeof(unsigned);
69 char *p = (char *) xcalloc( words*sizeof(unsigned), 1 );
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 )
83 void *p = (void *) xcalloc( size, 1 );
85 yaz_log (YLOG_FATAL, "Out of memory (icalloc)" );
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 );
106 yaz_log (YLOG_FATAL,"Internal: ifree(%u) negative alloc.", size );
107 xfree( (unsigned *) p-2 );
116 fprintf( stdout, "imalloc: calls malloc/free %d/%d, ",
117 alloc_calls, free_calls );
119 fprintf( stdout, "memory cur/max %ld/%ld : unreleased",
122 fprintf( stdout, "memory max %ld", max_alloc );
123 fputc( '\n', stdout );
129 * c-file-style: "Stroustrup"
130 * indent-tabs-mode: nil
132 * vim: shiftwidth=4 tabstop=8 expandtab