Fix for a crash which happened when a document couldn't be opened.
[AROS-Contrib.git] / rexx / src / bmem.c
blobf6d3d9a443ed7b6f9122980c1fd61922545a6fc4
1 /*
2 * $Header$
3 * $Log$
4 * Revision 1.2 2001/04/05 16:59:11 stegerg
5 * AROS does not have an <malloc.h> include. BTW: Should it?
7 * Revision 1.1 2001/04/04 05:43:38 wang
8 * First commit: compiles on Linux, Amiga, Windows, Windows CE, generic gcc
10 * Revision 1.3 1999/11/26 14:30:27 bnv
11 * Added: A dummy int, at Memory structure for 8-byte alignment on 32-bit machines
13 * Revision 1.2 1999/11/26 13:13:47 bnv
14 * Changed: Added MAGIC number also to the end.
15 * Chanted: Modified to work with 64-bit computers.
17 * Revision 1.1 1998/07/02 17:34:50 bnv
18 * Initial revision
22 #define __BMEM_C__
24 #include <stdio.h>
25 #ifndef AROS
26 #include <malloc.h>
27 #endif
28 #include <stdlib.h>
30 #include <bmem.h>
31 #include <lstring.h>
33 #define MAGIC 0x12346789L
36 * This file provides some debugging functions for memory allocation
39 typedef struct tmemory_st {
40 dword magic;
41 char *desc;
42 size_t size;
43 struct tmemory_st *next;
44 struct tmemory_st *prev;
45 #if defined(ALIGN) && !defined(__ALPHA)
46 /* Some machines have problems ithe address is not at 8-bytes aligned */
47 int dummy;
48 #endif
49 byte data[sizeof(void*)];
50 } Memory;
52 static Memory *mem_head = NULL;
53 static long total_mem = 0L;
55 void mem_list(void);
57 /* -------------- mem_malloc ---------------- */
58 void *
59 mem_malloc(size_t size, char *desc)
61 Memory *mem;
63 /* add space for the header */
64 #if defined(__BORLANDC__)&&(defined(__HUGE__)||defined(__LARGE__))
65 mem = (Memory *)farmalloc(sizeof(Memory)+size);
66 #else
67 mem = (Memory *)malloc(sizeof(Memory)+size);
68 #endif
69 if (mem) {
70 /* Create the memory header */
71 mem->magic = MAGIC;
72 mem->desc = desc;
73 mem->size = size;
74 mem->next = NULL;
75 mem->prev = mem_head;
76 if (mem_head)
77 mem_head->next = mem;
79 mem_head = mem;
80 total_mem += size;
82 /* Mark also the END of data */
83 *(dword *)((byte*)mem->data+mem->size) = MAGIC;
85 return (void *)(mem->data);
86 } else
87 return NULL;
88 } /* mem_malloc */
90 /* -------------- mem_realloc ---------------- */
91 void *
92 mem_realloc(void *ptr, size_t size)
94 Memory *mem, *other;
95 int head;
97 /* find our header */
98 mem = (Memory *)((char *)ptr - (sizeof(Memory)-sizeof(void*)));
100 /* check if the memory is valid */
101 if (mem->magic != MAGIC) {
102 fprintf(STDERR,"mem_realloc: object is not allocated size=%d!\n",(int)size);
103 mem_list();
104 exit(99);
107 if (*(dword *)(mem->data+mem->size) != MAGIC) {
108 fprintf(STDERR,"mem_realloc: End magic number doesn't match!\n");
109 mem_list();
110 exit(99);
113 total_mem -= mem->size;
114 head = (mem==mem_head);
116 #if defined(__BORLANDC__)&&(defined(__HUGE__)||defined(__LARGE__))
117 mem = (Memory *)farrealloc(mem,size+sizeof(Memory));
118 #else
119 mem = (Memory *)realloc(mem,size+sizeof(Memory));
120 #endif
122 if (head) mem_head = mem;
123 mem->size = size;
124 total_mem += size;
126 other = mem->prev;
127 if (other) other->next = mem;
128 other = mem->next;
129 if (other) other->prev = mem;
131 /* Mark also the new END of data */
132 *(dword *)(mem->data+mem->size) = MAGIC;
134 return (void *)(mem->data);
135 } /* mem_realloc */
137 /* -------------- mem_free ---------------- */
138 void
139 mem_free(void *ptr)
141 Memory *mem, *mem_prev, *mem_next;
142 int head;
144 /* find our header */
145 mem = (Memory *)((char *)ptr - (sizeof(Memory)-sizeof(void*)));
147 if (mem->magic != MAGIC) {
148 fprintf(STDERR,"mem_free: object %p is not allocated!\n",ptr);
149 mem_list();
150 exit(99);
152 if (*(dword *)(mem->data+mem->size) != MAGIC) {
153 fprintf(STDERR,"mem_realloc: End magic number doesn't match!\n");
154 mem_list();
155 exit(99);
158 /* Remove the MAGIC number, just to catch invalid entries */
159 mem->magic = 0L;
161 mem_prev = mem->prev;
162 mem_next = mem->next;
163 total_mem -= mem->size;
164 head = (mem==mem_head);
166 #if defined(__BORLANDC__)&&(defined(__HUGE__)||defined(__LARGE__))
167 farfree(mem);
168 #else
169 free(mem);
170 #endif
172 if (mem_next) mem_next->prev = mem_prev;
173 if (mem_prev) mem_prev->next = mem_next;
174 if (head) mem_head = mem_prev;
176 } /* mem_free */
178 /* -------------- mem_list ---------------- */
179 void
180 mem_list(void)
182 Memory *mem;
183 int count,i,y;
185 mem = mem_head;
186 count = 0;
187 fprintf(STDERR,"\nMemory list:\n");
188 y = 0;
189 while (mem) {
190 fputs((mem->magic==MAGIC)?" ":"??",STDERR);
192 fprintf(STDERR,"%3d %6d %s\t\"",
193 ++count, (int)mem->size, mem->desc);
194 for (i=0; i<10; i++)
195 fprintf(STDERR,"%c",mem->data[i]);
196 fprintf(STDERR,"\" ");
197 for (i=0; i<10; i++)
198 fprintf(STDERR,"%02X ",mem->data[i]);
199 fprintf(STDERR,"\n");
200 mem = mem->prev;
201 if (++y==15) {
202 if (getchar()=='q') exit(0);
203 y = 0;
206 fprintf(STDERR,"\n");
207 } /* mem_list */
209 /* --------------- memchk_list ------------------- */
211 memchk_list( void )
213 Memory *mem;
214 int i=0;
216 for (mem=mem_head; mem; mem = mem->prev,i++) {
217 if (mem->magic != MAGIC) {
218 fprintf(STDERR,"Magic number destroyed! ID=%d\n",i);
219 mem_list();
221 if (*(dword *)(mem->data+mem->size) != MAGIC) {
222 fprintf(STDERR,"mem_realloc: End magic number doesn't match! ID=%d\n",i);
223 mem_list();
226 return 0;
227 } /* memchk_list */
229 /* -------------- mem_allocated ----------------- */
230 long
231 mem_allocated( void )
233 return total_mem;
234 } /* mem_allocated */