9
9
#include <string.h>
10
10
11
11
#include "io.h"
12
+ #include "tlsf.h"
13
+
14
+ #define POOL_SIZE 1 << 22
12
15
13
16
static const uint32_t mask_lo = 0xffff ;
14
17
static const uint32_t mask_hi = ~(0xffff );
15
18
19
+ char pool [POOL_SIZE ];
20
+
16
21
memory_t * memory_new ()
17
22
{
18
- memory_t * m = malloc ( sizeof ( memory_t ) );
19
- memset ( m -> chunks , 0 , sizeof (m -> chunks ) );
23
+ init_memory_pool ( POOL_SIZE , pool );
24
+ memory_t * m = calloc_ex ( 1 , sizeof (memory_t ), pool );
20
25
return m ;
21
26
}
22
27
23
28
void memory_delete (memory_t * m )
24
29
{
25
30
if (!m )
26
31
return ;
27
-
28
- for (uint32_t i = 0 ; i < (sizeof (m -> chunks ) / sizeof (chunk_t * )); i ++ ) {
29
- chunk_t * c = m -> chunks [i ];
30
- if (c )
31
- free (c );
32
- }
33
- free (m );
32
+ destroy_memory_pool (pool );
34
33
}
35
34
36
35
void memory_read (memory_t * m , uint8_t * dst , uint32_t addr , uint32_t size )
@@ -109,7 +108,7 @@ void memory_write(memory_t *m, uint32_t addr, const uint8_t *src, uint32_t size)
109
108
uint32_t x = p >> 16 ;
110
109
chunk_t * c = m -> chunks [x ];
111
110
if (!c ) {
112
- c = calloc ( 1 , sizeof (chunk_t ));
111
+ c = malloc_ex ( sizeof (chunk_t ), pool );
113
112
m -> chunks [x ] = c ;
114
113
}
115
114
c -> data [p & 0xffff ] = src [i ];
@@ -121,7 +120,7 @@ void memory_write_w(memory_t *m, uint32_t addr, const uint8_t *src)
121
120
const uint32_t addr_lo = addr & mask_lo , addr_hi = addr >> 16 ;
122
121
chunk_t * c = m -> chunks [addr_hi ];
123
122
if (unlikely (!c )) {
124
- c = calloc (1 , sizeof (chunk_t ));
123
+ c = calloc_ex (1 , sizeof (chunk_t ), pool );
125
124
m -> chunks [addr_hi ] = c ;
126
125
}
127
126
* (uint32_t * ) (c -> data + addr_lo ) = * (const uint32_t * ) src ;
@@ -132,7 +131,7 @@ void memory_write_s(memory_t *m, uint32_t addr, const uint8_t *src)
132
131
const uint32_t addr_lo = addr & mask_lo , addr_hi = addr >> 16 ;
133
132
chunk_t * c = m -> chunks [addr_hi ];
134
133
if (unlikely (!c )) {
135
- c = calloc (1 , sizeof (chunk_t ));
134
+ c = calloc_ex (1 , sizeof (chunk_t ), pool );
136
135
m -> chunks [addr_hi ] = c ;
137
136
}
138
137
* (uint16_t * ) (c -> data + addr_lo ) = * (const uint16_t * ) src ;
@@ -143,7 +142,7 @@ void memory_write_b(memory_t *m, uint32_t addr, const uint8_t *src)
143
142
const uint32_t addr_lo = addr & mask_lo , addr_hi = addr >> 16 ;
144
143
chunk_t * c = m -> chunks [addr_hi ];
145
144
if (unlikely (!c )) {
146
- c = calloc (1 , sizeof (chunk_t ));
145
+ c = calloc_ex (1 , sizeof (chunk_t ), pool );
147
146
m -> chunks [addr_hi ] = c ;
148
147
}
149
148
c -> data [addr_lo ] = src [0 ];
@@ -156,8 +155,7 @@ void memory_fill(memory_t *m, uint32_t addr, uint32_t size, uint8_t val)
156
155
uint32_t x = p >> 16 ;
157
156
chunk_t * c = m -> chunks [x ];
158
157
if (!c ) {
159
- c = malloc (sizeof (chunk_t ));
160
- memset (c -> data , 0 , sizeof (c -> data ));
158
+ c = calloc_ex (1 , sizeof (chunk_t ), pool );
161
159
m -> chunks [x ] = c ;
162
160
}
163
161
c -> data [p & 0xffff ] = val ;
0 commit comments