1 /* $Id: imalloc.c,v 1.14 2006-05-10 08:13:18 adam Exp $
2 Copyright (C) 1995-2005
5 This file is part of the Zebra server.
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
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
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
28 #include <idzebra/util.h>
29 #include <yaz/xmalloc.h>
43 void *imalloc (size_t size)
46 size_t words = (4*sizeof(unsigned) -1 + size)/sizeof(unsigned);
47 char *p = (char *)xmalloc( words*sizeof(unsigned) );
49 yaz_log (YLOG_FATAL, "No memory: imalloc(%u); c/f %d/%d; %ld/%ld",
50 size, alloc_calls, free_calls, alloc, max_alloc );
51 *((unsigned *)p) = size;
52 ((unsigned *)p)[1] = MAG1;
53 p += sizeof(unsigned)*2;
54 size[(unsigned char *) p] = MAG2;
55 size[(unsigned char *) p+1] = MAG3;
56 if( (alloc+=size) > max_alloc )
61 void *p = (void *)xmalloc( size );
63 yaz_log (YLOG_FATAL, "Out of memory (imalloc)" );
68 void *icalloc (size_t size)
71 unsigned words = (4*sizeof(unsigned) -1 + size)/sizeof(unsigned);
72 char *p = (char *) xcalloc( words*sizeof(unsigned), 1 );
74 yaz_log (YLOG_FATAL, "No memory: icalloc(%u); c/f %d/%d; %ld/%ld",
75 size, alloc_calls, free_calls, alloc, max_alloc );
76 ((unsigned *)p)[0] = size;
77 ((unsigned *)p)[1] = MAG1;
78 p += sizeof(unsigned)*2;
79 size[(unsigned char *) p] = MAG2;
80 size[(unsigned char *) p+1] = MAG3;
81 if( (alloc+=size) > max_alloc )
86 void *p = (void *) xcalloc( size, 1 );
88 yaz_log (YLOG_FATAL, "Out of memory (icalloc)" );
100 size = (-2)[(unsigned *) p];
101 if( (-1)[(unsigned *) p] != MAG1 )
102 yaz_log (YLOG_FATAL,"Internal: ifree(%u) magic 1 corrupted", size );
103 if( size[(unsigned char *) p] != MAG2 )
104 yaz_log (YLOG_FATAL,"Internal: ifree(%u) magic 2 corrupted", size );
105 if( (size+1)[(unsigned char *) p] != MAG3 )
106 yaz_log (YLOG_FATAL,"Internal: ifree(%u) magic 3 corrupted", size );
109 yaz_log (YLOG_FATAL,"Internal: ifree(%u) negative alloc.", size );
110 xfree( (unsigned *) p-2 );
119 fprintf( stdout, "imalloc: calls malloc/free %d/%d, ",
120 alloc_calls, free_calls );
122 fprintf( stdout, "memory cur/max %ld/%ld : unreleased",
125 fprintf( stdout, "memory max %ld", max_alloc );
126 fputc( '\n', stdout );
132 * indent-tabs-mode: nil
134 * vim: shiftwidth=4 tabstop=8 expandtab