1 /* $Id: imalloc.c,v 1.10 2004-11-19 10:26:54 heikki Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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
42 void *imalloc (size_t size)
45 size_t words = (4*sizeof(unsigned) -1 + size)/sizeof(unsigned);
46 char *p = (char *)xmalloc( words*sizeof(unsigned) );
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 )
60 void *p = (void *)xmalloc( size );
62 yaz_log (YLOG_FATAL, "Out of memory (imalloc)" );
67 void *icalloc (size_t size)
70 unsigned words = (4*sizeof(unsigned) -1 + size)/sizeof(unsigned);
71 char *p = (char *) xcalloc( words*sizeof(unsigned), 1 );
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 )
85 void *p = (void *) xcalloc( size, 1 );
87 yaz_log (YLOG_FATAL, "Out of memory (icalloc)" );
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 );
108 yaz_log (YLOG_FATAL,"Internal: ifree(%u) negative alloc.", size );
109 xfree( (unsigned *) p-2 );
118 fprintf( stdout, "imalloc: calls malloc/free %d/%d, ",
119 alloc_calls, free_calls );
121 fprintf( stdout, "memory cur/max %ld/%ld : unreleased",
124 fprintf( stdout, "memory max %ld", max_alloc );
125 fputc( '\n', stdout );