1 /* mempool.c: pooled system memory
2 Copyright (c) 2008-2016 Philip Kendall
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 Author contact information:
19
20 E-mail: philip-fuse@shadowmagic.org.uk
21
22 */
23
24 #include <config.h>
25
26 #include <string.h>
27
28 #ifdef HAVE_LIB_GLIB
29 #include <glib.h>
30 #endif /* #ifdef HAVE_LIB_GLIB */
31
32 #include <libspectrum.h>
33
34 #include "fuse.h"
35 #include "infrastructure/startup_manager.h"
36 #include "mempool.h"
37
38 static GArray *memory_pools;
39
40 const int MEMPOOL_UNTRACKED = -1;
41
42 static int
mempool_init(void * context)43 mempool_init( void *context )
44 {
45 memory_pools = g_array_new( FALSE, FALSE, sizeof( GArray* ) );
46
47 return 0;
48 }
49
50 int
mempool_register_pool(void)51 mempool_register_pool( void )
52 {
53 GArray *pool = g_array_new( FALSE, FALSE, sizeof( void* ) );
54
55 g_array_append_val( memory_pools, pool );
56
57 return memory_pools->len - 1;
58 }
59
60 void*
mempool_malloc(int pool,size_t size)61 mempool_malloc( int pool, size_t size )
62 {
63 void *ptr;
64
65 if( pool == MEMPOOL_UNTRACKED ) return libspectrum_malloc( size );
66
67 if( pool < 0 || pool >= memory_pools->len ) return NULL;
68
69 ptr = libspectrum_malloc( size );
70 if( !ptr ) return NULL;
71
72 g_array_append_val( g_array_index( memory_pools, GArray*, pool ), ptr );
73
74 return ptr;
75 }
76
77 void *
mempool_malloc_n(int pool,size_t nmemb,size_t size)78 mempool_malloc_n( int pool, size_t nmemb, size_t size )
79 {
80 void *ptr;
81
82 if( pool == MEMPOOL_UNTRACKED ) return libspectrum_malloc_n( nmemb, size );
83
84 if( pool < 0 || pool >= memory_pools->len ) return NULL;
85
86 ptr = libspectrum_malloc_n( nmemb, size );
87 if( !ptr ) return NULL;
88
89 g_array_append_val( g_array_index( memory_pools, GArray*, pool ), ptr );
90
91 return ptr;
92 }
93
94 char*
mempool_strdup(int pool,const char * string)95 mempool_strdup( int pool, const char *string )
96 {
97 size_t length = strlen( string ) + 1;
98
99 char *ptr = mempool_malloc( pool, length );
100 if( !ptr ) return NULL;
101
102 memcpy( ptr, string, length );
103
104 return ptr;
105 }
106
107 void
mempool_free(int pool)108 mempool_free( int pool )
109 {
110 size_t i;
111
112 GArray *p = g_array_index( memory_pools, GArray*, pool );
113
114 for( i = 0; i < p->len; i++ )
115 libspectrum_free( g_array_index( p, void*, i ) );
116
117 g_array_set_size( p, 0 );
118 }
119
120 /* Tidy-up function called at end of emulation */
121 static void
mempool_end(void)122 mempool_end( void )
123 {
124 int i;
125 GArray *pool;
126
127 if( !memory_pools ) return;
128
129 for( i = 0; i < memory_pools->len; i++ ) {
130 pool = g_array_index( memory_pools, GArray *, i );
131
132 g_array_free( pool, TRUE );
133 }
134
135 g_array_free( memory_pools, TRUE );
136 memory_pools = NULL;
137 }
138
139 void
mempool_register_startup(void)140 mempool_register_startup( void )
141 {
142 startup_manager_module dependencies[] = { STARTUP_MANAGER_MODULE_SETUID };
143 startup_manager_register( STARTUP_MANAGER_MODULE_MEMPOOL, dependencies,
144 ARRAY_SIZE( dependencies ), mempool_init, NULL,
145 mempool_end );
146 }
147
148 /* Unit test helper routines */
149
150 int
mempool_get_pools(void)151 mempool_get_pools( void )
152 {
153 return memory_pools->len;
154 }
155
156 int
mempool_get_pool_size(int pool)157 mempool_get_pool_size( int pool )
158 {
159 return g_array_index( memory_pools, GArray*, pool )->len;
160 }
161