1 /*  Littlewood-Richardson Calculator
2  *  Copyright (C) 1999- Anders S. Buch (asbuch at math rutgers edu)
3  *  See the file LICENSE for license information.
4  */
5 
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9 
10 #include "alloc.h"
11 jmp_buf lrcalc_panic_frame;
12 
13 #if 0
14 #define DEBUG_MEMORY_PRINT
15 #endif
16 #if 1
17 #define DEBUG_PTR_REF
18 #endif
19 
20 
21 #ifndef DEBUG_MEMORY
22 
amalloc(size_t size)23 void *amalloc(size_t size)
24 {
25   void *p = malloc(size);
26   if (p == NULL)
27     longjmp(lrcalc_panic_frame, 1);
28   return p;
29 }
30 
acalloc(size_t size,size_t num)31 void *acalloc(size_t size, size_t num)
32 {
33   void *p = calloc(size, num);
34   if (p == NULL)
35     longjmp(lrcalc_panic_frame, 1);
36   return p;
37 }
38 
arealloc(void * p,size_t size)39 void *arealloc(void *p, size_t size)
40 {
41   p = realloc(p, size);
42   if (p == NULL)
43     longjmp(lrcalc_panic_frame, 1);
44   return p;
45 }
46 
47 #else
48 /*  DEBUG_MEMORY  */
49 
50 int memory_used = 0;
51 
out_of_memory()52 void out_of_memory()
53 {
54   fprintf(stderr, "out of memory.\n");
55   fprintf(stderr, "Memory balance: %d\n", memory_used);
56   longjmp(lrcalc_panic_frame, 1);
57 }
58 
59 #define ALIGN 16
60 
61 #define HEAD_SPACE	3
62 #define TAIL_SPACE	3
63 
64 #ifdef DEBUG_PTR_REF
65 #define ADD_TO_SIZE	((HEAD_SPACE + TAIL_SPACE) * ALIGN)
66 #define ADD_TO_PTR	(HEAD_SPACE * ALIGN)
67 #else
68 #define ADD_TO_SIZE	ALIGN
69 #define ADD_TO_PTR	ALIGN
70 #endif
71 
72 
73 #ifdef DEBUG_PTR_REF
74 
scramble_storage(void * p)75 static void scramble_storage(void *p)
76 {
77   int size = *((int *) p);
78   memset(p + ADD_TO_PTR, 0x99, size);
79 }
80 
init_storage(void * p)81 static void init_storage(void *p)
82 {
83   int i, size = *((int *) p);
84   unsigned char *s = p;
85   for (i = 4; i < ADD_TO_PTR; i++)
86     s[i] = 0xa5;
87   for (i = 0; i < TAIL_SPACE * ALIGN; i++)
88     s[ADD_TO_PTR + size + i] = 0xa5;
89 }
90 
check_storage(void * p)91 static void check_storage(void *p)
92 {
93   int i, size = *((int *) p);
94   unsigned char *s = p;
95   int dirty = 0, idx = 0;
96   for (i = 4; i < ADD_TO_PTR; i++)
97     if (s[i] != 0xa5)
98       { dirty = 1; idx = i; }
99   for (i = 0; i < TAIL_SPACE * ALIGN; i++)
100     if (s[ADD_TO_PTR + size + i] != 0xa5)
101       { dirty = 1; idx = ADD_TO_PTR + size + i; }
102   if (dirty)
103     {
104       fprintf(stderr, "WARNING: Pointer %p dirty at index %d (%p).\n",
105 	      s, idx, s + idx);
106     }
107 }
108 
109 #endif
110 
111 
amalloc(size_t size)112 void *amalloc(size_t size)
113 {
114   void *p = malloc(size + ADD_TO_SIZE);
115 #ifdef DEBUG_MEMORY_PRINT
116   fprintf(stder, "malloc 0x%08x\n", (int) p);
117 #endif
118   if (p == NULL)
119     out_of_memory();
120   memory_used += size;
121   *((int *) p) = size;
122 #ifdef DEBUG_PTR_REF
123   init_storage(p);
124   scramble_storage(p);
125 #endif
126   return ((char *) p) + ADD_TO_PTR;
127 }
128 
acalloc(size_t num,size_t size)129 void *acalloc(size_t num, size_t size)
130 {
131   void *p = calloc(1, size * num + ADD_TO_SIZE);
132 #ifdef DEBUG_MEMORY_PRINT
133   fprintf(stderr, "calloc 0x%08x\n", (int) p);
134 #endif
135   if (p == NULL)
136     out_of_memory();
137   memory_used += size * num;
138   *((int *) p) = size * num;
139 #ifdef DEBUG_PTR_REF
140   init_storage(p);
141 #endif
142   return ((char *) p) + ADD_TO_PTR;
143 }
144 
arealloc(void * p,size_t size)145 void *arealloc(void *p, size_t size)
146 {
147   p -= ADD_TO_PTR;
148 #ifdef DEBUG_MEMORY_PRINT
149   fprintf(stderr, "realloc 0x%08x -> ", (int) p);
150 #endif
151   p = realloc(p, size + ADD_TO_SIZE);
152 #ifdef DEBUG_MEMORY_PRINT
153   fprintf(stderr, "0x%08x\n", (int) p);
154 #endif
155   if (p == NULL)
156     out_of_memory();
157   memory_used += size - *((int *) p);
158   *((int *) p) = size;
159 #ifdef DEBUG_PTR_REF
160   init_storage(p);
161   /* Could scramble new part here. */
162 #endif
163   return ((char *) p) + ADD_TO_PTR;
164 }
165 
afree(void * p)166 void afree(void *p)
167 {
168   int size;
169   p -= ADD_TO_PTR;
170 #ifdef DEBUG_PTR_REF
171   check_storage(p);
172   scramble_storage(p);
173 #endif
174   size = *((int *) p);
175   memory_used -= size;
176 #ifdef DEBUG_MEMORY_PRINT
177   fprintf(stderr, "free 0x%08x\n", (int) p);
178 #endif
179   free(p);
180 }
181 
mem_report()182 void mem_report()
183 {
184   fprintf(stderr, "Memory balance: %d\n", memory_used);
185 }
186 
187 #endif
188 /* DEBUG_MEMORY */
189