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