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
|
/* mempool.c: pooled system memory
Copyright (c) 2008 Philip Kendall
$Id: mempool.c 4717 2012-06-07 03:54:45Z fredm $
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Author contact information:
E-mail: philip-fuse@shadowmagic.org.uk
*/
#include <config.h>
#include <string.h>
#ifdef HAVE_LIB_GLIB
#include <glib.h>
#endif /* #ifdef HAVE_LIB_GLIB */
#include <libspectrum.h>
#include "fuse.h"
#include "mempool.h"
static GArray *memory_pools;
const int MEMPOOL_UNTRACKED = -1;
void
mempool_init( void )
{
memory_pools = g_array_new( FALSE, FALSE, sizeof( GArray* ) );
}
int
mempool_register_pool( void )
{
GArray *pool = g_array_new( FALSE, FALSE, sizeof( void* ) );
g_array_append_val( memory_pools, pool );
return memory_pools->len - 1;
}
void*
mempool_alloc( int pool, size_t size )
{
void *ptr;
if( pool == MEMPOOL_UNTRACKED ) return libspectrum_malloc( size );
if( pool < 0 || pool >= memory_pools->len ) return NULL;
ptr = libspectrum_malloc( size );
if( !ptr ) return NULL;
g_array_append_val( g_array_index( memory_pools, GArray*, pool ), ptr );
return ptr;
}
char*
mempool_strdup( int pool, const char *string )
{
size_t length = strlen( string ) + 1;
char *ptr = mempool_alloc( pool, length );
if( !ptr ) return NULL;
memcpy( ptr, string, length );
return ptr;
}
void
mempool_free( int pool )
{
size_t i;
GArray *p = g_array_index( memory_pools, GArray*, pool );
for( i = 0; i < p->len; i++ )
libspectrum_free( g_array_index( p, void*, i ) );
g_array_set_size( p, 0 );
}
/* Tidy-up function called at end of emulation */
void
mempool_end( void )
{
int i;
GArray *pool;
if( !memory_pools ) return;
for( i = 0; i < memory_pools->len; i++ ) {
pool = g_array_index( memory_pools, GArray *, i );
g_array_free( pool, TRUE );
}
g_array_free( memory_pools, TRUE );
memory_pools = NULL;
}
/* Unit test helper routines */
int
mempool_get_pools( void )
{
return memory_pools->len;
}
int
mempool_get_pool_size( int pool )
{
return g_array_index( memory_pools, GArray*, pool )->len;
}
|