Remove memory pool
FossilOrigin-Name: 0097baa3a78b36f6358e9877ad0c14ed4225486be33b821a408adf2e0213e0c0
This commit is contained in:
parent
7ab817d420
commit
f904d80214
6 changed files with 16 additions and 189 deletions
|
@ -100,7 +100,7 @@ spawnstate(dynent *d) // reset player state not persistent accross spawns
|
||||||
dynent *
|
dynent *
|
||||||
newdynent() // create a new blank player or monster
|
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.x = 0;
|
||||||
d->o.y = 0;
|
d->o.y = 0;
|
||||||
d->o.z = 0;
|
d->o.z = 0;
|
||||||
|
@ -186,7 +186,7 @@ void
|
||||||
zapdynent(dynent *&d)
|
zapdynent(dynent *&d)
|
||||||
{
|
{
|
||||||
if (d)
|
if (d)
|
||||||
gp()->dealloc(d, sizeof(dynent));
|
free(d);
|
||||||
d = NULL;
|
d = NULL;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -19,8 +19,8 @@ itoa(char *s, int i)
|
||||||
char *
|
char *
|
||||||
exchangestr(char *o, const char *n)
|
exchangestr(char *o, const char *n)
|
||||||
{
|
{
|
||||||
gp()->deallocstr(o);
|
free(o);
|
||||||
return newstring(n);
|
return strdup(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
// contains ALL vars/commands/aliases
|
// contains ALL vars/commands/aliases
|
||||||
|
@ -132,7 +132,7 @@ parseexp(char *&p, int right) // parse any nested set of () or []
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
char *s = newstring(word, p - word - 1);
|
char *s = strndup(word, p - word - 1);
|
||||||
if (left == '(') {
|
if (left == '(') {
|
||||||
string t;
|
string t;
|
||||||
// evaluate () exps directly, and substitute result
|
// evaluate () exps directly, and substitute result
|
||||||
|
@ -154,7 +154,7 @@ parseword(char *&p) // parse single argument, including expressions
|
||||||
p++;
|
p++;
|
||||||
char *word = p;
|
char *word = p;
|
||||||
p += strcspn(p, "\"\r\n\0");
|
p += strcspn(p, "\"\r\n\0");
|
||||||
char *s = newstring(word, p - word);
|
char *s = strndup(word, p - word);
|
||||||
if (*p == '\"')
|
if (*p == '\"')
|
||||||
p++;
|
p++;
|
||||||
return s;
|
return s;
|
||||||
|
@ -167,7 +167,7 @@ parseword(char *&p) // parse single argument, including expressions
|
||||||
p += strcspn(p, "; \t\r\n\0");
|
p += strcspn(p, "; \t\r\n\0");
|
||||||
if (p - word == 0)
|
if (p - word == 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
return newstring(word, p - word);
|
return strndup(word, p - word);
|
||||||
}
|
}
|
||||||
|
|
||||||
OFString *
|
OFString *
|
||||||
|
|
|
@ -32,10 +32,11 @@ static void
|
||||||
conline(OFString *sf, bool highlight) // add a line to the console buffer
|
conline(OFString *sf, bool highlight) // add a line to the console buffer
|
||||||
{
|
{
|
||||||
cline cl;
|
cline cl;
|
||||||
cl.cref = conlines.length() > 100
|
// constrain the buffer size
|
||||||
? conlines.pop().cref
|
cl.cref = conlines.length() > 100 ? conlines.pop().cref
|
||||||
: newstringbuf(""); // constrain the buffer size
|
: (char *)calloc(_MAXDEFSTR, 1);
|
||||||
cl.outtime = lastmillis; // for how long to keep line on screen
|
// for how long to keep line on screen
|
||||||
|
cl.outtime = lastmillis;
|
||||||
conlines.insert(0, cl);
|
conlines.insert(0, cl);
|
||||||
if (highlight) // show line in a different colour, for chat etc.
|
if (highlight) // show line in a different colour, for chat etc.
|
||||||
{
|
{
|
||||||
|
|
|
@ -102,7 +102,7 @@ void
|
||||||
monsterclear() // called after map start of when toggling edit mode to
|
monsterclear() // called after map start of when toggling edit mode to
|
||||||
// reset/spawn all monsters to initial state
|
// reset/spawn all monsters to initial state
|
||||||
{
|
{
|
||||||
loopv(monsters) gp()->dealloc(monsters[i], sizeof(dynent));
|
loopv(monsters) free(monsters[i]);
|
||||||
monsters.setsize(0);
|
monsters.setsize(0);
|
||||||
numkilled = 0;
|
numkilled = 0;
|
||||||
monstertotal = 0;
|
monstertotal = 0;
|
||||||
|
|
85
src/tools.h
85
src/tools.h
|
@ -119,81 +119,22 @@ struct sprintf_s_f {
|
||||||
|
|
||||||
extern void endianswap(void *, int, int);
|
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 <class T> struct vector {
|
template <class T> struct vector {
|
||||||
T *buf;
|
T *buf;
|
||||||
int alen;
|
int alen;
|
||||||
int ulen;
|
int ulen;
|
||||||
pool *p;
|
|
||||||
|
|
||||||
vector()
|
vector()
|
||||||
{
|
{
|
||||||
this->p = gp();
|
|
||||||
alen = 8;
|
alen = 8;
|
||||||
buf = (T *)p->alloc(alen * sizeof(T));
|
buf = (T *)malloc(alen * sizeof(T));
|
||||||
ulen = 0;
|
ulen = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
~vector()
|
~vector()
|
||||||
{
|
{
|
||||||
setsize(0);
|
setsize(0);
|
||||||
p->dealloc(buf, alen * sizeof(T));
|
free(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
vector(vector<T> &v);
|
vector(vector<T> &v);
|
||||||
|
@ -271,9 +212,7 @@ template <class T> struct vector {
|
||||||
void
|
void
|
||||||
realloc()
|
realloc()
|
||||||
{
|
{
|
||||||
int olen = alen;
|
buf = (T *)::realloc(buf, (alen *= 2) * sizeof(T));
|
||||||
buf = (T *)p->realloc(
|
|
||||||
buf, olen * sizeof(T), (alen *= 2) * sizeof(T));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
T
|
T
|
||||||
|
@ -306,22 +245,4 @@ template <class T> struct vector {
|
||||||
} else \
|
} else \
|
||||||
for (int i = (v).length() - 1; i >= 0; i--)
|
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
|
#endif
|
||||||
|
|
95
src/tools.mm
95
src/tools.mm
|
@ -3,101 +3,6 @@
|
||||||
#include "tools.h"
|
#include "tools.h"
|
||||||
#include <new>
|
#include <new>
|
||||||
|
|
||||||
//////////////////////////// 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 ///////////////////////
|
///////////////////////// misc tools ///////////////////////
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue