-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmempool.c
160 lines (113 loc) · 3.42 KB
/
mempool.c
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
/* mempool.c: pooled system memory
Copyright (c) 2008-2016 Philip Kendall
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: [email protected]
*/
#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 "infrastructure/startup_manager.h"
#include "mempool.h"
static GArray *memory_pools;
const int MEMPOOL_UNTRACKED = -1;
static int
mempool_init( void *context )
{
memory_pools = g_array_new( FALSE, FALSE, sizeof( GArray* ) );
return 0;
}
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_malloc( 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;
}
void *
mempool_malloc_n( int pool, size_t nmemb, size_t size )
{
void *ptr;
if( pool == MEMPOOL_UNTRACKED ) return libspectrum_malloc_n( nmemb, size );
if( pool < 0 || pool >= memory_pools->len ) return NULL;
ptr = libspectrum_malloc_n( nmemb, 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_malloc( 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 */
static 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;
}
void
mempool_register_startup( void )
{
startup_manager_module dependencies[] = { STARTUP_MANAGER_MODULE_SETUID };
startup_manager_register( STARTUP_MANAGER_MODULE_MEMPOOL, dependencies,
ARRAY_SIZE( dependencies ), mempool_init, NULL,
mempool_end );
}
/* 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;
}