2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.2 1994-09-26 16:30:56 adam
8 * Minor changes. imalloc uses xmalloc now.
10 * Revision 1.1 1994/09/26 10:16:54 adam
11 * First version of dfa module in alex. This version uses yacc to parse
12 * regular expressions. This should be hand-made instead.
33 void *imalloc (size_t size)
36 size_t words = (4*sizeof(unsigned) -1 + size)/sizeof(unsigned);
37 char *p = (char *)xmalloc( words*sizeof(unsigned) );
39 log (LOG_FATAL, "No memory: imalloc(%u); c/f %d/%d; %ld/%ld",
40 size, alloc_calls, free_calls, alloc, max_alloc );
41 *((unsigned *)p) = size;
42 ((unsigned *)p)[1] = MAG1;
43 p += sizeof(unsigned)*2;
44 size[(unsigned char *) p] = MAG2;
45 size[(unsigned char *) p+1] = MAG3;
46 if( (alloc+=size) > max_alloc )
51 void *p = (void *)xmalloc( size );
53 log (LOG_FATAL, "Out of memory (imalloc)" );
58 void *icalloc (size_t size)
61 unsigned words = (4*sizeof(unsigned) -1 + size)/sizeof(unsigned);
62 char *p = (char *) xcalloc( words*sizeof(unsigned), 1 );
64 log (LOG_FATAL, "No memory: icalloc(%u); c/f %d/%d; %ld/%ld",
65 size, alloc_calls, free_calls, alloc, max_alloc );
66 ((unsigned *)p)[0] = size;
67 ((unsigned *)p)[1] = MAG1;
68 p += sizeof(unsigned)*2;
69 size[(unsigned char *) p] = MAG2;
70 size[(unsigned char *) p+1] = MAG3;
71 if( (alloc+=size) > max_alloc )
76 void p = (void) xcalloc( size, 1 );
78 log (LOG_FATAL, "Out of memory (icalloc)" );
90 size = (-2)[(unsigned *) p];
91 if( (-1)[(unsigned *) p] != MAG1 )
92 log (LOG_FATAL,"Internal: ifree(%u) magic 1 corrupted", size );
93 if( size[(unsigned char *) p] != MAG2 )
94 log (LOG_FATAL,"Internal: ifree(%u) magic 2 corrupted", size );
95 if( (size+1)[(unsigned char *) p] != MAG3 )
96 log (LOG_FATAL,"Internal: ifree(%u) magic 3 corrupted", size );
99 log (LOG_FATAL,"Internal: ifree(%u) negative alloc.", size );
100 xfree( (unsigned *) p-2 );
104 void i_free (void *p)
115 fprintf( stdout, "imalloc: calls malloc/free %d/%d, ",
116 alloc_calls, free_calls );
118 fprintf( stdout, "memory cur/max %ld/%ld : unreleased",
121 fprintf( stdout, "memory max %ld", max_alloc );
122 fputc( '\n', stdout );