From f904d8021427b8124f59f97133f758f3d13af232 Mon Sep 17 00:00:00 2001 From: Jonathan Schleifer Date: Sat, 8 Mar 2025 01:09:36 +0000 Subject: [PATCH] Remove memory pool FossilOrigin-Name: 0097baa3a78b36f6358e9877ad0c14ed4225486be33b821a408adf2e0213e0c0 --- src/clientgame.mm | 4 +- src/commands.mm | 10 ++--- src/console.mm | 9 +++-- src/monster.mm | 2 +- src/tools.h | 85 ++---------------------------------------- src/tools.mm | 95 ----------------------------------------------- 6 files changed, 16 insertions(+), 189 deletions(-) diff --git a/src/clientgame.mm b/src/clientgame.mm index 4b2067d..4e784fd 100644 --- a/src/clientgame.mm +++ b/src/clientgame.mm @@ -100,7 +100,7 @@ spawnstate(dynent *d) // reset player state not persistent accross spawns dynent * newdynent() // create a new blank player or monster { - dynent *d = (dynent *)gp()->alloc(sizeof(dynent)); + dynent *d = (dynent *)malloc(sizeof(dynent)); d->o.x = 0; d->o.y = 0; d->o.z = 0; @@ -186,7 +186,7 @@ void zapdynent(dynent *&d) { if (d) - gp()->dealloc(d, sizeof(dynent)); + free(d); d = NULL; }; diff --git a/src/commands.mm b/src/commands.mm index 84b9576..bdd1bbb 100644 --- a/src/commands.mm +++ b/src/commands.mm @@ -19,8 +19,8 @@ itoa(char *s, int i) char * exchangestr(char *o, const char *n) { - gp()->deallocstr(o); - return newstring(n); + free(o); + return strdup(n); } // contains ALL vars/commands/aliases @@ -132,7 +132,7 @@ parseexp(char *&p, int right) // parse any nested set of () or [] return NULL; } } - char *s = newstring(word, p - word - 1); + char *s = strndup(word, p - word - 1); if (left == '(') { string t; // evaluate () exps directly, and substitute result @@ -154,7 +154,7 @@ parseword(char *&p) // parse single argument, including expressions p++; char *word = p; p += strcspn(p, "\"\r\n\0"); - char *s = newstring(word, p - word); + char *s = strndup(word, p - word); if (*p == '\"') p++; return s; @@ -167,7 +167,7 @@ parseword(char *&p) // parse single argument, including expressions p += strcspn(p, "; \t\r\n\0"); if (p - word == 0) return NULL; - return newstring(word, p - word); + return strndup(word, p - word); } OFString * diff --git a/src/console.mm b/src/console.mm index e85eb55..a53d60e 100644 --- a/src/console.mm +++ b/src/console.mm @@ -32,10 +32,11 @@ static void conline(OFString *sf, bool highlight) // add a line to the console buffer { cline cl; - cl.cref = conlines.length() > 100 - ? conlines.pop().cref - : newstringbuf(""); // constrain the buffer size - cl.outtime = lastmillis; // for how long to keep line on screen + // constrain the buffer size + cl.cref = conlines.length() > 100 ? conlines.pop().cref + : (char *)calloc(_MAXDEFSTR, 1); + // for how long to keep line on screen + cl.outtime = lastmillis; conlines.insert(0, cl); if (highlight) // show line in a different colour, for chat etc. { diff --git a/src/monster.mm b/src/monster.mm index 285ae04..4d1813c 100644 --- a/src/monster.mm +++ b/src/monster.mm @@ -102,7 +102,7 @@ void monsterclear() // called after map start of when toggling edit mode to // reset/spawn all monsters to initial state { - loopv(monsters) gp()->dealloc(monsters[i], sizeof(dynent)); + loopv(monsters) free(monsters[i]); monsters.setsize(0); numkilled = 0; monstertotal = 0; diff --git a/src/tools.h b/src/tools.h index 8606fd4..7eeeda9 100644 --- a/src/tools.h +++ b/src/tools.h @@ -119,81 +119,22 @@ struct sprintf_s_f { extern void endianswap(void *, int, int); -// memory pool that uses buckets and linear allocation for small objects -// VERY fast, and reasonably good memory reuse - -struct pool { - enum { POOLSIZE = 4096 }; // can be absolutely anything - enum { PTRSIZE = sizeof(char *) }; - enum { - MAXBUCKETS = 65 - }; // meaning up to size 256 on 32bit pointer systems - enum { MAXREUSESIZE = MAXBUCKETS * PTRSIZE - PTRSIZE }; - - inline size_t - bucket(size_t s) - { - return (s + PTRSIZE - 1) >> PTRBITS; - } - - enum { PTRBITS = PTRSIZE == 2 ? 1 : PTRSIZE == 4 ? 2 : 3 }; - - char *p; - size_t left; - char *blocks; - void *reuse[MAXBUCKETS]; - - pool(); - ~pool() { dealloc_block(blocks); }; - - void *alloc(size_t size); - void dealloc(void *p, size_t size); - void *realloc(void *p, size_t oldsize, size_t newsize); - - char *string(const char *s, size_t l); - - char * - string(const char *s) - { - return string(s, strlen(s)); - } - - void - deallocstr(char *s) - { - dealloc(s, strlen(s) + 1); - } - - char * - stringbuf(const char *s) - { - return string(s, _MAXDEFSTR - 1); - } - - void dealloc_block(void *b); - void allocnext(size_t allocsize); -}; - -pool *gp(); - template struct vector { T *buf; int alen; int ulen; - pool *p; vector() { - this->p = gp(); alen = 8; - buf = (T *)p->alloc(alen * sizeof(T)); + buf = (T *)malloc(alen * sizeof(T)); ulen = 0; } ~vector() { setsize(0); - p->dealloc(buf, alen * sizeof(T)); + free(buf); } vector(vector &v); @@ -271,9 +212,7 @@ template struct vector { void realloc() { - int olen = alen; - buf = (T *)p->realloc( - buf, olen * sizeof(T), (alen *= 2) * sizeof(T)); + buf = (T *)::realloc(buf, (alen *= 2) * sizeof(T)); } T @@ -306,22 +245,4 @@ template struct vector { } else \ for (int i = (v).length() - 1; i >= 0; i--) -inline char * -newstring(const char *s) -{ - return gp()->string(s); -} - -inline char * -newstring(const char *s, size_t l) -{ - return gp()->string(s, l); -} - -inline char * -newstringbuf(const char *s) -{ - return gp()->stringbuf(s); -} - #endif diff --git a/src/tools.mm b/src/tools.mm index 272e77a..bb0e516 100644 --- a/src/tools.mm +++ b/src/tools.mm @@ -3,101 +3,6 @@ #include "tools.h" #include -//////////////////////////// pool /////////////////////////// - -pool::pool() -{ - blocks = 0; - allocnext(POOLSIZE); - for (int i = 0; i < MAXBUCKETS; i++) - reuse[i] = NULL; -}; - -void * -pool::alloc(size_t size) -{ - if (size > MAXREUSESIZE) { - return malloc(size); - } else { - size = bucket(size); - void **r = (void **)reuse[size]; - if (r) { - reuse[size] = *r; - return (void *)r; - } else { - size <<= PTRBITS; - if (left < size) - allocnext(POOLSIZE); - char *r = p; - p += size; - left -= size; - return r; - }; - }; -}; - -void -pool::dealloc(void *p, size_t size) -{ - if (size > MAXREUSESIZE) { - free(p); - } else { - size = bucket(size); - if (size) // only needed for 0-size free, are there any? - { - *((void **)p) = reuse[size]; - reuse[size] = p; - }; - }; -}; - -void * -pool::realloc(void *p, size_t oldsize, size_t newsize) -{ - void *np = alloc(newsize); - if (!oldsize) - return np; - memcpy(np, p, newsize > oldsize ? oldsize : newsize); - dealloc(p, oldsize); - return np; -}; - -void -pool::dealloc_block(void *b) -{ - if (b) { - dealloc_block(*((char **)b)); - free(b); - }; -} - -void -pool::allocnext(size_t allocsize) -{ - char *b = (char *)malloc(allocsize + PTRSIZE); - *((char **)b) = blocks; - blocks = b; - p = b + PTRSIZE; - left = allocsize; -}; - -char * -pool::string(const char *s, size_t l) -{ - char *b = (char *)alloc(l + 1); - strncpy(b, s, l); - b[l] = 0; - return b; -} - -pool * -gp() // useful for global buffers that need to be initialisation order - // independant -{ - static pool *p = NULL; - return p ? p : (p = new pool()); -}; - ///////////////////////// misc tools /////////////////////// void