Added facility for open character sets, eg [a-].
[idzebra-moved-to-github.git] / dfa / imalloc.c
1 /*
2  * Copyright (C) 1994-1996, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: imalloc.c,v $
7  * Revision 1.6  1996-10-29 13:57:25  adam
8  * Include of zebrautl.h instead of alexutil.h.
9  *
10  * Revision 1.5  1996/05/14 11:33:41  adam
11  * MEMDEBUG turned off by default.
12  *
13  * Revision 1.4  1995/09/04  12:33:26  adam
14  * Various cleanup. YAZ util used instead.
15  *
16  * Revision 1.3  1994/09/27  16:31:19  adam
17  * First version of grepper: grep with error correction.
18  *
19  * Revision 1.2  1994/09/26  16:30:56  adam
20  * Minor changes. imalloc uses xmalloc now.
21  *
22  * Revision 1.1  1994/09/26  10:16:54  adam
23  * First version of dfa module in alex. This version uses yacc to parse
24  * regular expressions. This should be hand-made instead.
25  *
26  */
27 #include <stdio.h>
28 #include <assert.h>
29 #include <stdlib.h>
30
31 #include <zebrautl.h>
32 #include "imalloc.h"
33
34 #if MEMDEBUG
35 #define MAG1 0x8fe1
36 #define MAG2 0x91
37 #define MAG3 0xee
38
39 long alloc       = 0L;
40 long max_alloc   = 0L;
41 int  alloc_calls = 0;
42 int  free_calls  = 0;
43 #endif
44
45 void *imalloc (size_t size)
46 {
47 #if MEMDEBUG
48     size_t words = (4*sizeof(unsigned) -1 + size)/sizeof(unsigned);
49     char *p = (char *)xmalloc( words*sizeof(unsigned) );
50     if( !p )
51         logf (LOG_FATAL, "No memory: imalloc(%u); c/f %d/%d; %ld/%ld",
52            size, alloc_calls, free_calls, alloc, max_alloc );
53     *((unsigned *)p) = size;
54     ((unsigned *)p)[1] = MAG1;
55     p += sizeof(unsigned)*2;
56     size[(unsigned char *) p] = MAG2;
57     size[(unsigned char *) p+1] = MAG3;
58     if( (alloc+=size) > max_alloc )
59         max_alloc = alloc;
60     ++alloc_calls;
61     return (void *) p;
62 #else
63     void *p = (void *)xmalloc( size );
64     if( !p )
65         logf (LOG_FATAL, "Out of memory (imalloc)" );
66     return p;
67 #endif
68 }
69
70 void *icalloc (size_t size)
71 {
72 #if MEMDEBUG
73     unsigned words = (4*sizeof(unsigned) -1 + size)/sizeof(unsigned);
74     char *p = (char *) xcalloc( words*sizeof(unsigned), 1 );
75     if( !p )
76         logf (LOG_FATAL, "No memory: icalloc(%u); c/f %d/%d; %ld/%ld",
77            size, alloc_calls, free_calls, alloc, max_alloc );
78     ((unsigned *)p)[0] = size;
79     ((unsigned *)p)[1] = MAG1;
80     p += sizeof(unsigned)*2;
81     size[(unsigned char *) p] = MAG2;
82     size[(unsigned char *) p+1] = MAG3;
83     if( (alloc+=size) > max_alloc )
84         max_alloc = alloc;
85     ++alloc_calls;
86     return (void *)p;
87 #else
88     void *p = (void *) xcalloc( size, 1 );
89     if( !p )
90         logf (LOG_FATAL, "Out of memory (icalloc)" );
91     return p;
92 #endif
93 }
94
95 #if MEMDEBUG
96 void i_free (void *p)
97 {
98     size_t size;
99     if( !p )
100         return;
101     ++free_calls;
102     size = (-2)[(unsigned *) p];
103     if( (-1)[(unsigned *) p] != MAG1 )
104         logf (LOG_FATAL,"Internal: ifree(%u) magic 1 corrupted", size );
105     if( size[(unsigned char *) p] != MAG2 )
106         logf (LOG_FATAL,"Internal: ifree(%u) magic 2 corrupted", size );
107     if( (size+1)[(unsigned char *) p] != MAG3 )
108         logf (LOG_FATAL,"Internal: ifree(%u) magic 3 corrupted", size );
109     alloc -= size;
110     if( alloc < 0L )
111         logf (LOG_FATAL,"Internal: ifree(%u) negative alloc.", size );
112     xfree( (unsigned *) p-2 );
113 }
114 #endif
115
116 #if MEMDEBUG
117 void imemstat (void)
118 {
119     fprintf( stdout, "imalloc: calls malloc/free %d/%d, ",
120                                      alloc_calls, free_calls );
121     if( alloc )
122         fprintf( stdout, "memory cur/max %ld/%ld : unreleased",
123                                                           alloc, max_alloc );
124     else
125         fprintf( stdout, "memory max %ld", max_alloc );
126     fputc( '\n', stdout );
127 }
128 #endif