File: memory.c

package info (click to toggle)
grcompiler 4.2-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,076 kB
  • ctags: 5,163
  • sloc: cpp: 45,565; sh: 4,451; ansic: 4,377; makefile: 185; xml: 175; perl: 127
file content (229 lines) | stat: -rw-r--r-- 6,041 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#ifdef RCS
static char rcsid[]="$Id: memory.c,v 1.3 2005/05/27 10:21:14 mhosken Exp $";
#endif
/******************************************************************************
Copyright (c) 1999 Daniel Stenberg

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE
SOFTWARE.
******************************************************************************/
/******************************************************************************
 *                               FREXXWARE
 * ----------------------------------------------------------------------------
 *
 * Project: Frexx C Preprocessor
 * $Source: /cvsroot/silgraphite/silgraphite/src/GrCompiler/GdlPp/memory.c,v $
 * $Revision: 1.3 $
 * $Date: 2005/05/27 10:21:14 $
 * $Author: mhosken $
 * $State: Exp $
 * $Locker:  $
 *
 * ----------------------------------------------------------------------------
 * $Log: memory.c,v $
 * Revision 1.3  2005/05/27 10:21:14  mhosken
 * Get GrCompiler working on Linux
 *
 * Revision 1.2  2004/10/25 19:08:41  danglassey
 * gcc prefers some extra brackets - remove warnings
 *
 * Revision 1.1  2003/04/21 21:24:18  wardak
 * Add files for the GDL pre-processor (gdlpp.exe).
 *
 * Revision 1.2  1994/01/24  09:36:46  start
 * Made it run with OS9 properly.
 *
 * Revision 1.1  1993/11/03  09:13:08  start
 * Initial revision
 *
 *
 *****************************************************************************/

#include <string.h>
#include <stdlib.h>
#if defined(OS9)
#include <types.h>
#elif defined(unix)
#include <sys/types.h>
#elif defined(AMIGA)
#include <exec/types.h>
#include <proto/exec.h>
#endif

#include <stdio.h>

#include "memory.h"
#ifdef DEBUG
int mem;
int maxmem;
int malloc_count=0;
#endif

/*
 * We have two mallockey pointers because we have two different kinds of
 * Malloc()s! One for each execution and one for each fplInit().
 */
static struct MemInfo *MallocKey=NULL;

void *Realloc(void *ptr, int size)
{
  struct MemInfo *point;
  void *new;
  /* `point' points to the MemInfo structure: */
  point=(struct MemInfo *)((char *)ptr-sizeof(struct MemInfo));
 
  if(size<=point->size)
    /*
     * To increase performance, don't care about reallocing
     * to smaller sizes!
     */
    return(ptr);
  new=Malloc(size); /* allocate new storage */
  if(!new)
    return(NULL); /* fail! */
  
  memcpy(new, ptr, point->size); /* copy contents */

  Free(ptr); /* free old area */

  return(new); /* return new pointer */
}


void *Malloc(int size)
{
  char *alloc = NULL;
  struct MemInfo *point;

#ifdef DEBUG
  size+=MEMORY_COOKIE; /* add extra bytes after the block! */
#endif

#ifdef AMIGA
  alloc=(char *)AllocMem(size+sizeof(struct MemInfo), 0L);
#elif defined(unix)
  alloc=(char *)malloc(size+sizeof(struct MemInfo));
#endif

  if(!alloc)
    return(NULL);

  point=(struct MemInfo *)alloc;
  point->prev=MallocKey;	 /* previous */
  point->next=NULL;		 /* next */
  point->size=size;		 /* size */
#ifdef DEBUG
  malloc_count++;
  mem+=size+sizeof(struct MemInfo);
  if(mem>maxmem)
    maxmem=mem;

  memset((void *)((char *)point+sizeof(struct MemInfo)), 0xbb, size);
#endif
  if(MallocKey)
    point->prev->next=point;

  MallocKey = (void *)point;

  alloc = ((char *)point+sizeof(struct MemInfo));
  return ((void *)alloc);
}

void Free(void *ptr)
{
  struct MemInfo *point;
  /* `point' points to the MemInfo structure: */
  point=(struct MemInfo *)((char *)ptr-sizeof(struct MemInfo));

  if(MallocKey==point) {
    /* if this is the last Malloc, set `last' to `prev' */
    MallocKey=point->prev;
    if(MallocKey)
      MallocKey->next=NULL;
  } else {
    /* point the previous' `next' to our `next', and our next `previous'
       to our `previous'. Unlink us from the chain */
    if(point->prev)
      /* only if we aren't the _first_ Malloc() ! */
      point->prev->next=point->next;
    if(point->next)
      /* only if there is a next! */
      point->next->prev=point->prev;
  }
#ifdef DEBUG
  mem-=point->size+sizeof(struct MemInfo);

  CheckMem(ptr);
#endif

#ifdef AMIGA  
  FreeMem(point, point->size+sizeof(struct MemInfo));
#elif unix
  free(point);
#endif
}

#ifdef DEBUG
void CheckMem(void *ptr)
{
  int i;
  int b=0;
  struct MemInfo *point;
  /* `point' points to the MemInfo structure: */
  point=(struct MemInfo *)((char *)ptr-sizeof(struct MemInfo));
  for(i=0;i<MEMORY_COOKIE; i++)
    if(*((unsigned char *)point+sizeof(struct MemInfo)+i+point->size-MEMORY_COOKIE)!= 0xbb)
      b++;

  if(b) {
#if defined(unix)
    fprintf(stderr, "Memory violation: malloc(%d) was abused %d bytes!\n",
	    point->size-MEMORY_COOKIE-sizeof(struct MemInfo), b);
#elif defined(AMIGA)
    /* ERROR */;
#endif
  }
}
#endif

void FreeAll(void)
{
  struct MemInfo *point;
  void *prev;

  if(!MallocKey)
    return;
  do {
    point=MallocKey;
    /* `point' points to the MemInfo structure! */

    prev=(void *)point->prev;

#ifdef DEBUG
    mem-=point->size+sizeof(struct MemInfo);
#endif
#ifdef AMIGA
    FreeMem(MallocKey, point->size+sizeof(struct MemInfo));
#elif unix
    free((char *)MallocKey);
#endif
  } while((MallocKey=prev));
}