Skip to content

Commit 2a7e5ed

Browse files
committed
Modify amxpool to use a struct rather than global vars
1 parent 8544ad7 commit 2a7e5ed

4 files changed

Lines changed: 76 additions & 64 deletions

File tree

src/displayapp/screens/Pawn.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,10 @@ static int AMXAPI prun_Overlay(AMX* amx, int index) {
365365
tbl = (AMX_OVERLAYINFO*) (amx->base + hdr->overlays) + index;
366366

367367
amx->codesize = tbl->size;
368-
amx->code = (unsigned char*) amx_poolfind(index);
368+
amx->code = (unsigned char*) amx_poolfind(&PAWN_INST->amx_pool, index);
369369

370370
if (amx->code == NULL) {
371-
if ((amx->code = (unsigned char*) amx_poolalloc(tbl->size, index)) == NULL)
371+
if ((amx->code = (unsigned char*) amx_poolalloc(&PAWN_INST->amx_pool, tbl->size, index)) == NULL)
372372
return AMX_ERR_OVERLAY;
373373

374374
memcpy(amx->code, program + hdr->cod + tbl->offset, tbl->size);
@@ -388,6 +388,7 @@ int Pawn::LoadProgram() {
388388
return AMX_ERR_FORMAT;
389389

390390
memset(&amx, 0, sizeof(amx));
391+
amx.userdata[0] = this;
391392

392393
header = malloc(hdr.cod);
393394
if (header == NULL)
@@ -407,7 +408,7 @@ int Pawn::LoadProgram() {
407408
if (overlaypool == NULL)
408409
return AMX_ERR_MEMORY;
409410

410-
amx_poolinit(overlaypool, max_overlay_size + overlaypool_overhead);
411+
amx_poolinit(&amx_pool, overlaypool, max_overlay_size + overlaypool_overhead);
411412

412413
amx.data = (unsigned char*) datablock;
413414
amx.overlay = prun_Overlay;
@@ -469,8 +470,6 @@ Pawn::Pawn(AppControllers& controllers) : controllers(controllers) {
469470
return;
470471
}
471472

472-
amx.userdata[0] = this;
473-
474473
lv_obj_set_user_data(lv_scr_act(), &amx);
475474

476475
cell* font;

src/displayapp/screens/Pawn.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <chrono>
1010

1111
#include "pawn/amx.h"
12+
#include "pawn/amxpool.h"
1213

1314
namespace Pinetime {
1415
namespace Applications {
@@ -33,8 +34,11 @@ namespace Pinetime {
3334

3435
Widgets::StatusIcons* statusIcons = nullptr;
3536

37+
amxPool amx_pool;
38+
3639
private:
3740
AMX amx;
41+
3842
int refresh_index, touch_index, gesture_index;
3943
lv_task_t* taskRefresh = 0;
4044
unsigned int queued_error = 0;

src/pawn/amxpool.c

Lines changed: 50 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,24 @@ typedef struct tagARENA {
4545
unsigned short lru;
4646
} ARENA;
4747

48-
static void *pool_base;
49-
static unsigned pool_size;
50-
static unsigned short pool_lru;
51-
52-
static void touchblock(ARENA *hdr);
53-
static ARENA *findblock(int index);
48+
static void touchblock(amxPool *pool, ARENA *hdr);
49+
static ARENA *findblock(amxPool *pool, int index);
5450

5551
/* amx_poolinit() initializes the memory pool for the allocated blocks.
5652
* If parameter pool is NULL, the existing pool is cleared (without changing
5753
* its position or size).
5854
*/
59-
void amx_poolinit(void *pool, unsigned size)
55+
void amx_poolinit(amxPool *pool, void *buffer, unsigned size)
6056
{
61-
assert(pool!=NULL || pool_base!=NULL);
62-
if (pool!=NULL) {
57+
assert(buffer!=NULL || pool->base!=NULL);
58+
if (buffer!=NULL) {
6359
assert(size>sizeof(ARENA));
6460
/* save parameters in global variables, then "free" the entire pool */
65-
pool_base=pool;
66-
pool_size=size;
61+
pool->base=buffer;
62+
pool->size=size;
6763
} /* if */
68-
pool_lru=0;
69-
amx_poolfree(NULL);
64+
pool->lru=0;
65+
amx_poolfree(pool, NULL);
7066
}
7167

7268
/* amx_poolfree() releases a block allocated earlier. The parameter must have
@@ -76,25 +72,25 @@ void amx_poolinit(void *pool, unsigned size)
7672
* When parameter "block" is NULL, the pool is re-initialized (meaning that
7773
* all blocks are freed).
7874
*/
79-
void amx_poolfree(void *block)
75+
void amx_poolfree(amxPool *pool, void *block)
8076
{
8177
ARENA *hdr,*hdr2;
8278
unsigned sz;
8379

84-
assert(pool_base!=NULL);
85-
assert(pool_size>sizeof(ARENA));
80+
assert(pool->base!=NULL);
81+
assert(pool->size>sizeof(ARENA));
8682

8783
/* special case: if "block" is NULL, create a single free space */
8884
if (block==NULL) {
8985
/* store an arena header at the start of the pool */
90-
hdr=(ARENA*)pool_base;
91-
hdr->blocksize=pool_size-sizeof(ARENA);
86+
hdr=(ARENA*)pool->base;
87+
hdr->blocksize=pool->size-sizeof(ARENA);
9288
hdr->index=-1;
9389
hdr->lru=0;
9490
} else {
9591
hdr=(ARENA*)((char*)block-sizeof(ARENA));
96-
assert((char*)hdr>=(char*)pool_base && (char*)hdr<(char*)pool_base+pool_size);
97-
assert(hdr->blocksize<pool_size);
92+
assert((char*)hdr>=(char*)pool->base && (char*)hdr<(char*)pool->base+pool->size);
93+
assert(hdr->blocksize<pool->size);
9894

9995
/* free this block */
10096
hdr->index=-1;
@@ -105,11 +101,11 @@ void amx_poolfree(void *block)
105101
hdr->blocksize+=hdr2->blocksize+sizeof(ARENA);
106102

107103
/* try to coalesce with the previous block */
108-
if ((void*)hdr!=pool_base) {
109-
sz=pool_size;
110-
hdr2=(ARENA*)pool_base;
104+
if ((void*)hdr!=pool->base) {
105+
sz=pool->size;
106+
hdr2=(ARENA*)pool->base;
111107
while (sz>0 && (char*)hdr2+hdr2->blocksize+sizeof(ARENA)!=(char*)hdr) {
112-
assert(sz<=pool_size);
108+
assert(sz<=pool->size);
113109
sz-=hdr2->blocksize+sizeof(ARENA);
114110
hdr2=(ARENA*)((char*)hdr2+hdr2->blocksize+sizeof(ARENA));
115111
} /* while */
@@ -136,20 +132,20 @@ void amx_poolfree(void *block)
136132
* every iteration (without considering the size of the block or whether that
137133
* block is adjacent to a free block).
138134
*/
139-
void *amx_poolalloc(unsigned size,int index)
135+
void *amx_poolalloc(amxPool *pool, unsigned size,int index)
140136
{
141137
ARENA *hdr,*hdrlru;
142138
unsigned sz;
143139
unsigned short minlru;
144140

145141
assert(size>0);
146142
assert(index>=0 && index<=SHRT_MAX);
147-
assert(findblock(index)==NULL);
143+
assert(findblock(pool, index)==NULL);
148144

149145
/* align the size to a cell boundary */
150146
if ((size % sizeof(cell))!=0)
151147
size+=sizeof(cell)-(size % sizeof(cell));
152-
if (size+sizeof(ARENA)>pool_size)
148+
if (size+sizeof(ARENA)>pool->size)
153149
return NULL; /* requested block does not fit in the pool */
154150

155151
/* find a block large enough to get the size plus an arena header; at
@@ -158,13 +154,13 @@ void *amx_poolalloc(unsigned size,int index)
158154
* the block with the lowest LRU count and tries again
159155
*/
160156
do {
161-
sz=pool_size;
162-
hdr=(ARENA*)pool_base;
157+
sz=pool->size;
158+
hdr=(ARENA*)pool->base;
163159
hdrlru=hdr;
164160
minlru=USHRT_MAX;
165161
while (sz>0) {
166-
assert(sz<=pool_size);
167-
assert((char*)hdr>=(char*)pool_base && (char*)hdr<(char*)pool_base+pool_size);
162+
assert(sz<=pool->size);
163+
assert((char*)hdr>=(char*)pool->base && (char*)hdr<(char*)pool->base+pool->size);
168164
if (hdr->index==-1 && hdr->blocksize>=size)
169165
break;
170166
if (hdr->index!=-1 && hdr->lru<minlru) {
@@ -174,11 +170,11 @@ void *amx_poolalloc(unsigned size,int index)
174170
sz-=hdr->blocksize+sizeof(ARENA);
175171
hdr=(ARENA*)((char*)hdr+hdr->blocksize+sizeof(ARENA));
176172
} /* while */
177-
assert(sz<=pool_size);
173+
assert(sz<=pool->size);
178174
if (sz==0) {
179175
/* free up memory and try again */
180176
assert(hdrlru->index!=-1);
181-
amx_poolfree((char*)hdrlru+sizeof(ARENA));
177+
amx_poolfree(pool, (char*)hdrlru+sizeof(ARENA));
182178
} /* if */
183179
} while (sz==0);
184180

@@ -194,7 +190,7 @@ void *amx_poolalloc(unsigned size,int index)
194190
} /* if */
195191
hdr->blocksize=size;
196192
hdr->index=(short)index;
197-
touchblock(hdr); /* set LRU field */
193+
touchblock(pool, hdr); /* set LRU field */
198194

199195
return (void*)((char*)hdr+sizeof(ARENA));
200196
}
@@ -204,65 +200,65 @@ void *amx_poolalloc(unsigned size,int index)
204200
* -1 represents a free block (actually, only positive values are valid).
205201
* When amx_poolfind() finds the block, it increments its LRU count.
206202
*/
207-
void *amx_poolfind(int index)
203+
void *amx_poolfind(amxPool *pool, int index)
208204
{
209-
ARENA *hdr=findblock(index);
205+
ARENA *hdr=findblock(pool, index);
210206
if (hdr==NULL)
211207
return NULL;
212-
touchblock(hdr);
208+
touchblock(pool, hdr);
213209
return (void*)((char*)hdr+sizeof(ARENA));
214210
}
215211

216-
int amx_poolprotect(int index)
212+
int amx_poolprotect(amxPool *pool, int index)
217213
{
218-
ARENA *hdr=findblock(index);
214+
ARENA *hdr=findblock(pool, index);
219215
if (hdr==NULL)
220216
return AMX_ERR_GENERAL;
221217
hdr->lru=PROTECT_LRU;
222218
return AMX_ERR_NONE;
223219
}
224220

225-
static ARENA *findblock(int index)
221+
static ARENA *findblock(amxPool *pool, int index)
226222
{
227223
ARENA *hdr;
228224
unsigned sz;
229225

230226
assert(index>=0);
231-
sz=pool_size;
232-
hdr=(ARENA*)pool_base;
227+
sz=pool->size;
228+
hdr=(ARENA*)pool->base;
233229
while (sz>0 && hdr->index!=index) {
234-
assert(sz<=pool_size);
235-
assert((char*)hdr>=(char*)pool_base && (char*)hdr<(char*)pool_base+pool_size);
230+
assert(sz<=pool->size);
231+
assert((char*)hdr>=(char*)pool->base && (char*)hdr<(char*)pool->base+pool->size);
236232
sz-=hdr->blocksize+sizeof(ARENA);
237233
hdr=(ARENA*)((char*)hdr+hdr->blocksize+sizeof(ARENA));
238234
} /* while */
239-
assert(sz<=pool_size);
235+
assert(sz<=pool->size);
240236
return (sz>0 && hdr->index==index) ? hdr : NULL;
241237
}
242238

243-
static void touchblock(ARENA *hdr)
239+
static void touchblock(amxPool *pool, ARENA *hdr)
244240
{
245241
assert(hdr!=NULL);
246-
if (++pool_lru >= PROTECT_LRU)
247-
pool_lru=0;
248-
hdr->lru=pool_lru;
242+
if (++pool->lru >= PROTECT_LRU)
243+
pool->lru=0;
244+
hdr->lru=pool->lru;
249245

250246
/* special case: if the overlay LRU count wrapped back to zero, set the
251247
* LRU count of all blocks to zero, but set the count of the block just
252248
* touched to 1 (skip blocks marked as protected, too)
253249
*/
254-
if (pool_lru==0) {
250+
if (pool->lru==0) {
255251
ARENA *hdr2;
256-
unsigned sz=pool_size;
257-
hdr2=(ARENA*)pool_base;
252+
unsigned sz=pool->size;
253+
hdr2=(ARENA*)pool->base;
258254
while (sz>0) {
259-
assert(sz<=pool_size);
255+
assert(sz<=pool->size);
260256
if (hdr2->lru!=PROTECT_LRU)
261257
hdr2->lru=0;
262258
sz-=hdr2->blocksize+sizeof(ARENA);
263259
hdr2=(ARENA*)((char*)hdr2+hdr2->blocksize+sizeof(ARENA));
264260
} /* while */
265261
assert(sz==0);
266-
hdr->lru=++pool_lru;
262+
hdr->lru=++pool->lru;
267263
} /* if */
268264
}

src/pawn/amxpool.h

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,24 @@
2020
#ifndef AMXPOOL_H_INCLUDED
2121
#define AMXPOOL_H_INCLUDED
2222

23-
void amx_poolinit(void *pool, unsigned size);
24-
void *amx_poolalloc(unsigned size, int index);
25-
void amx_poolfree(void *block);
26-
void *amx_poolfind(int index);
27-
int amx_poolprotect(int index);
23+
#if defined(__cplusplus)
24+
extern "C" {
25+
#endif
2826

27+
typedef struct amxPool {
28+
void *base;
29+
unsigned size;
30+
unsigned short lru;
31+
} amxPool;
32+
33+
void amx_poolinit(amxPool *pool, void *buffer, unsigned size);
34+
void *amx_poolalloc(amxPool *pool, unsigned size, int index);
35+
void amx_poolfree(amxPool *pool, void *block);
36+
void *amx_poolfind(amxPool *pool, int index);
37+
int amx_poolprotect(amxPool *pool, int index);
38+
39+
#if defined(__cplusplus)
40+
}
41+
#endif
2942

3043
#endif /* AMXPOOL_H_INCLUDED */

0 commit comments

Comments
 (0)