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