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