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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
/*
* tclMacAlloc.c --
*
* This is a very fast storage allocator. It allocates blocks of a
* small number of different sizes, and keeps free lists of each size.
* Blocks that don't exactly fit are passed up to the next larger size.
* Blocks over a certain size are directly allocated by calling NewPtr.
*
* Copyright (c) 1983 Regents of the University of California.
* Copyright (c) 1996-1997 Sun Microsystems, Inc.
*
* Portions contributed by Chris Kingsley, Jack Jansen and Ray Johnson
*.
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* SCCS: @(#) tclMacAlloc.c 1.13 97/07/24 14:42:19
*/
#include "tclMacInt.h"
#include "tclInt.h"
#include <Memory.h>
#include <stdlib.h>
#include <string.h>
/*
* Flags that are used by ConfigureMemory to define how the allocator
* should work. They can be or'd together.
*/
#define MEMORY_ALL_SYS 1 /* All memory should come from the system
heap. */
/*
* Amount of space to leave in the application heap for the Toolbox to work.
*/
#define TOOLBOX_SPACE (32 * 1024)
static int memoryFlags = 0;
static Handle toolGuardHandle = NULL;
/* This handle must be around so that we don't
* have NewGWorld failures. This handle is
* purgeable. Before we allocate any blocks,
* we see if this handle is still around.
* If it is not, then we try to get it again.
* If we can get it, we lock it and try
* to do the normal allocation, unlocking on
* the way out. If we can't, we go to the
* system heap directly. */
/*
* The following typedef and variable are used to keep track of memory
* blocks that are allocated directly from the System Heap. These chunks
* of memory must always be freed - even if we crash.
*/
typedef struct listEl {
Handle memoryHandle;
struct listEl * next;
} ListEl;
ListEl * systemMemory = NULL;
ListEl * appMemory = NULL;
/*
* Prototypes for functions used only in this file.
*/
static pascal void CleanUpExitProc _ANSI_ARGS_((void));
void ConfigureMemory _ANSI_ARGS_((int flags));
void FreeAllMemory _ANSI_ARGS_((void));
/*
*----------------------------------------------------------------------
*
* TclpSysRealloc --
*
* This function reallocates a chunk of system memory. If the
* chunk is already big enough to hold the new block, then no
* allocation happens.
*
* Results:
* Returns a pointer to the newly allocated block.
*
* Side effects:
* May copy the contents of the original block to the new block
* and deallocate the original block.
*
*----------------------------------------------------------------------
*/
VOID *
TclpSysRealloc(
VOID *oldPtr, /* Original block */
unsigned int size) /* New size of block. */
{
Handle hand;
void *newPtr;
int maxsize;
hand = * (Handle *) ((Ptr) oldPtr - sizeof(Handle));
maxsize = GetHandleSize(hand) - sizeof(Handle);
if (maxsize < size) {
newPtr = TclpSysAlloc(size, 1);
memcpy(newPtr, oldPtr, maxsize);
TclpSysFree(oldPtr);
} else {
newPtr = oldPtr;
}
return newPtr;
}
/*
*----------------------------------------------------------------------
*
* TclpSysAlloc --
*
* Allocate a new block of memory free from the System.
*
* Results:
* Returns a pointer to a new block of memory.
*
* Side effects:
* May obtain memory from app or sys space. Info is added to
* overhead lists etc.
*
*----------------------------------------------------------------------
*/
VOID *
TclpSysAlloc(
long size, /* Size of block to allocate. */
int isBin) /* Is this a bin allocation? */
{
Handle hand = NULL;
ListEl * newMemoryRecord;
if (!(memoryFlags & MEMORY_ALL_SYS)) {
/*
* If the guard handle has been purged, throw it away and try
* to allocate it again.
*/
if ((toolGuardHandle != NULL) && (*toolGuardHandle == NULL)) {
DisposeHandle(toolGuardHandle);
toolGuardHandle = NULL;
}
/*
* If we have never allocated the guard handle, or it was purged
* and thrown away, then try to allocate it again.
*/
if (toolGuardHandle == NULL) {
toolGuardHandle = NewHandle(TOOLBOX_SPACE);
if (toolGuardHandle != NULL) {
HPurge(toolGuardHandle);
}
}
/*
* If we got the handle, lock it and do our allocation.
*/
if (toolGuardHandle != NULL) {
HLock(toolGuardHandle);
hand = NewHandle(size + sizeof(Handle));
HUnlock(toolGuardHandle);
}
}
if (hand != NULL) {
newMemoryRecord = (ListEl *) NewPtr(sizeof(ListEl));
if (newMemoryRecord == NULL) {
DisposeHandle(hand);
return NULL;
}
newMemoryRecord->memoryHandle = hand;
newMemoryRecord->next = appMemory;
appMemory = newMemoryRecord;
} else {
/*
* Ran out of memory in application space. Lets try to get
* more memory from system. Otherwise, we return NULL to
* denote failure.
*/
isBin = 0;
hand = NewHandleSys(size + sizeof(Handle));
if (hand == NULL) {
return NULL;
}
if (systemMemory == NULL) {
/*
* This is the first time we've attempted to allocate memory
* directly from the system heap. We need to now install the
* exit handle to ensure the memory is cleaned up.
*/
TclMacInstallExitToShellPatch(CleanUpExitProc);
}
newMemoryRecord = (ListEl *) NewPtrSys(sizeof(ListEl));
if (newMemoryRecord == NULL) {
DisposeHandle(hand);
return NULL;
}
newMemoryRecord->memoryHandle = hand;
newMemoryRecord->next = systemMemory;
systemMemory = newMemoryRecord;
}
if (isBin) {
HLockHi(hand);
} else {
HLock(hand);
}
(** (Handle **) hand) = hand;
return (*hand + sizeof(Handle));
}
/*
*----------------------------------------------------------------------
*
* TclpSysFree --
*
* Free memory that we allocated back to the system.
*
* Results:
* None.
*
* Side effects:
* Memory is freed.
*
*----------------------------------------------------------------------
*/
void
TclpSysFree(
void * ptr) /* Free this system memory. */
{
Handle hand;
OSErr err;
hand = * (Handle *) ((Ptr) ptr - sizeof(Handle));
DisposeHandle(hand);
*hand = NULL;
err = MemError();
}
/*
*----------------------------------------------------------------------
*
* CleanUpExitProc --
*
* This procedure is invoked as an exit handler when ExitToShell
* is called. It removes any memory that was allocated directly
* from the system heap. This must be called when the application
* quits or the memory will never be freed.
*
* Results:
* None.
*
* Side effects:
* May free memory in the system heap.
*
*----------------------------------------------------------------------
*/
static pascal void
CleanUpExitProc()
{
ListEl * memRecord;
while (systemMemory != NULL) {
memRecord = systemMemory;
systemMemory = memRecord->next;
if (*(memRecord->memoryHandle) != NULL) {
DisposeHandle(memRecord->memoryHandle);
}
DisposePtr((void *) memRecord);
}
}
/*
*----------------------------------------------------------------------
*
* FreeAllMemory --
*
* This procedure frees all memory blocks allocated by the memory
* sub-system. Make sure you don't have any code that references
* any malloced data!
*
* Results:
* None.
*
* Side effects:
* Frees all memory allocated by TclpAlloc.
*
*----------------------------------------------------------------------
*/
void
FreeAllMemory()
{
ListEl * memRecord;
while (systemMemory != NULL) {
memRecord = systemMemory;
systemMemory = memRecord->next;
if (*(memRecord->memoryHandle) != NULL) {
DisposeHandle(memRecord->memoryHandle);
}
DisposePtr((void *) memRecord);
}
while (appMemory != NULL) {
memRecord = appMemory;
appMemory = memRecord->next;
if (*(memRecord->memoryHandle) != NULL) {
DisposeHandle(memRecord->memoryHandle);
}
DisposePtr((void *) memRecord);
}
}
/*
*----------------------------------------------------------------------
*
* ConfigureMemory --
*
* This procedure sets certain flags in this file that control
* how memory is allocated and managed. This call must be made
* before any call to TclpAlloc is made.
*
* Results:
* None.
*
* Side effects:
* Certain state will be changed.
*
*----------------------------------------------------------------------
*/
void
ConfigureMemory(
int flags) /* Flags that control memory alloc scheme. */
{
memoryFlags = flags;
}
|