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