2 * Copyright (C) 1994-1997, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.7 1997-10-27 14:27:13 adam
10 * Revision 1.6 1996/10/29 13:57:25 adam
11 * Include of zebrautl.h instead of alexutil.h.
13 * Revision 1.5 1996/05/14 11:33:41 adam
14 * MEMDEBUG turned off by default.
16 * Revision 1.4 1995/09/04 12:33:26 adam
17 * Various cleanup. YAZ util used instead.
19 * Revision 1.3 1994/09/27 16:31:19 adam
20 * First version of grepper: grep with error correction.
22 * Revision 1.2 1994/09/26 16:30:56 adam
23 * Minor changes. imalloc uses xmalloc now.
25 * Revision 1.1 1994/09/26 10:16:54 adam
26 * First version of dfa module in alex. This version uses yacc to parse
27 * regular expressions. This should be hand-made instead.
48 void *imalloc (size_t size)
51 size_t words = (4*sizeof(unsigned) -1 + size)/sizeof(unsigned);
52 char *p = (char *)xmalloc( words*sizeof(unsigned) );
54 logf (LOG_FATAL, "No memory: imalloc(%u); c/f %d/%d; %ld/%ld",
55 size, alloc_calls, free_calls, alloc, max_alloc );
56 *((unsigned *)p) = size;
57 ((unsigned *)p)[1] = MAG1;
58 p += sizeof(unsigned)*2;
59 size[(unsigned char *) p] = MAG2;
60 size[(unsigned char *) p+1] = MAG3;
61 if( (alloc+=size) > max_alloc )
66 void *p = (void *)xmalloc( size );
68 logf (LOG_FATAL, "Out of memory (imalloc)" );
73 void *icalloc (size_t size)
76 unsigned words = (4*sizeof(unsigned) -1 + size)/sizeof(unsigned);
77 char *p = (char *) xcalloc( words*sizeof(unsigned), 1 );
79 logf (LOG_FATAL, "No memory: icalloc(%u); c/f %d/%d; %ld/%ld",
80 size, alloc_calls, free_calls, alloc, max_alloc );
81 ((unsigned *)p)[0] = size;
82 ((unsigned *)p)[1] = MAG1;
83 p += sizeof(unsigned)*2;
84 size[(unsigned char *) p] = MAG2;
85 size[(unsigned char *) p+1] = MAG3;
86 if( (alloc+=size) > max_alloc )
91 void *p = (void *) xcalloc( size, 1 );
93 logf (LOG_FATAL, "Out of memory (icalloc)" );
105 size = (-2)[(unsigned *) p];
106 if( (-1)[(unsigned *) p] != MAG1 )
107 logf (LOG_FATAL,"Internal: ifree(%u) magic 1 corrupted", size );
108 if( size[(unsigned char *) p] != MAG2 )
109 logf (LOG_FATAL,"Internal: ifree(%u) magic 2 corrupted", size );
110 if( (size+1)[(unsigned char *) p] != MAG3 )
111 logf (LOG_FATAL,"Internal: ifree(%u) magic 3 corrupted", size );
114 logf (LOG_FATAL,"Internal: ifree(%u) negative alloc.", size );
115 xfree( (unsigned *) p-2 );
124 fprintf( stdout, "imalloc: calls malloc/free %d/%d, ",
125 alloc_calls, free_calls );
127 fprintf( stdout, "memory cur/max %ld/%ld : unreleased",
130 fprintf( stdout, "memory max %ld", max_alloc );
131 fputc( '\n', stdout );