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