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
|
/*
FALCON - The Falcon Programming Language.
FILE: flc_memory.cpp
Basic memory manager functions and function pointers.
-------------------------------------------------------------------
Author: Giancarlo Niccolai
Begin: 2004-08-01
-------------------------------------------------------------------
(C) Copyright 2004: the FALCON developers (see list in AUTHORS file)
See LICENSE file for licensing details.
*/
#include <falcon/memory.h>
#if defined(__SUNPRO_CC)
#include <stdio.h>
#elif defined(__BORLANDC__)
#include <stdio.h>
#include <stdlib.h>
#else
#include <cstdio>
#include <cstdlib>
#endif
#ifdef FALCON_SYSTEM_WIN
#include <falcon/mt_win.h>
#else
#include <falcon/mt_posix.h>
#endif
namespace Falcon {
/*
#ifdef _MSC_VER
const builder_t Builder;
#else
const builder_t Builder = {};
#endif
*/
void * DflMemAlloc( size_t amount )
{
void *ret = malloc( amount + sizeof(size_t) );
if ( ret == 0 ) {
printf( "Falcon: fatal allocation error when allocating %d bytes\n", (int) amount );
exit(1);
}
return ret;
}
void DflMemFree( void *mem )
{
free( mem );
}
void * DflMemRealloc( void *mem, size_t amount )
{
void *ret = realloc( mem, amount );
if ( ret == 0 && amount != 0 ) {
printf( "Falcon: fatal reallocation error when allocating %d bytes\n", (int) amount );
exit(1);
}
return ret;
}
void * DflAccountMemAlloc( size_t amount )
{
size_t *ret = (size_t*) malloc( amount + sizeof(size_t) );
if ( ret == 0 ) {
printf( "Falcon: fatal allocation error when allocating %d bytes\n", (int) amount );
exit(1);
}
gcMemAccount( amount );
*ret = amount;
return ret+1;
}
void DflAccountMemFree( void *mem )
{
if ( mem != 0 )
{
size_t *smem = (size_t*) mem;
gcMemUnaccount( smem[-1] );
free( smem-1 );
}
}
void * DflAccountMemRealloc( void *mem, size_t amount )
{
if ( amount == 0 )
{
DflAccountMemFree( mem );
return 0;
}
if ( mem == 0 )
return DflAccountMemAlloc( amount );
size_t *smem = (size_t*) mem;
smem--;
size_t oldalloc = *smem;
size_t *nsmem = (size_t*) realloc( smem, amount + sizeof( size_t ) );
if ( nsmem == 0 ) {
printf( "Falcon: fatal reallocation error when allocating %d bytes\n", (int) amount );
exit(1);
}
*nsmem = amount;
if( amount > oldalloc )
gcMemAccount( amount - oldalloc );
else
gcMemUnaccount( oldalloc - amount );
return nsmem+1;
}
//===================================================================================
// Account functions
//
static Mutex *s_gcMutex = 0;
static size_t s_allocatedMem = 0;
void gcMemAccount( size_t mem )
{
if( s_gcMutex == 0 )
s_gcMutex = new Mutex;
s_gcMutex->lock();
s_allocatedMem += mem;
s_gcMutex->unlock();
}
void gcMemUnaccount( size_t mem )
{
if( s_gcMutex == 0 )
s_gcMutex = new Mutex;
s_gcMutex->lock();
s_allocatedMem -= mem;
s_gcMutex->unlock();
}
size_t gcMemAllocated()
{
if( s_gcMutex == 0 )
s_gcMutex = new Mutex;
s_gcMutex->lock();
register uint32 val = s_allocatedMem;
s_gcMutex->unlock();
return val;
}
void gcMemShutdown()
{
delete s_gcMutex;
s_gcMutex = 0;
}
//============================================================
// Global function pointers.
//
void * (*gcAlloc) ( size_t ) = DflAccountMemAlloc;
void (*gcFree) ( void * ) = DflAccountMemFree;
void * (*gcRealloc) ( void *, size_t ) = DflAccountMemRealloc;
/*
void * (*memAlloc) ( size_t ) = DflMemAlloc;
void (*memFree) ( void * ) = DflMemFree;
void * (*memRealloc) ( void *, size_t ) = DflMemRealloc;
*/
/*
In phase 1, we're accounting everything to verify the basic solidity of our GC system.
*/
void * (*memAlloc) ( size_t ) = DflAccountMemAlloc;
void (*memFree) ( void * ) = DflAccountMemFree;
void * (*memRealloc) ( void *, size_t ) = DflAccountMemRealloc;
}
/* end of flc_memory.cpp */
|