only allocate vectors when used

git-svn-id: svn://svn.code.sf.net/p/actiongame/code/trunk/ac@1756 e016b1c5-ed68-44aa-a2f7-31cac88cffde
This commit is contained in:
Lee Salzman
2008-04-08 09:26:12 +00:00
parent 3b23ffed01
commit b7ba3bfe3d
2 changed files with 24 additions and 30 deletions
-2
View File
@@ -283,8 +283,6 @@ VARP(bloodttl, 0, 10000, 30000);
void render_particles(int time)
{
extern float aspect;
extern int farplane, fov;
bool rendered = false;
for(int i = MAXPARTYPES-1; i>=0; i--) if(parlist[i])
{
+24 -28
View File
@@ -187,50 +187,42 @@ typedef databuf<uchar> ucharbuf;
template <class T> struct vector
{
static const int MINSIZE = 8;
T *buf;
int alen;
int ulen;
int alen, ulen;
vector()
vector() : buf(NULL), alen(0), ulen(0)
{
alen = 8;
buf = (T *)new uchar[alen*sizeof(T)];
ulen = 0;
}
vector(const vector<T> &v)
{
alen = v.length();
buf = (T *)new uchar[alen*sizeof(T)];
ulen = 0;
*this = v;
}
~vector() { setsize(0); delete[] (uchar *)buf; }
~vector() { setsize(0); if(buf) delete[] (uchar *)buf; }
vector<T> &operator=(const vector<T> &v)
{
setsize(0);
if(v.length() > alen) vrealloc(v.length());
loopv(v) add(v[i]);
return *this;
}
T &add(const T &x)
{
if(ulen==alen) vrealloc();
if(ulen==alen) vrealloc(ulen+1);
new (&buf[ulen]) T(x);
return buf[ulen++];
}
T &add()
{
if(ulen==alen) vrealloc();
if(ulen==alen) vrealloc(ulen+1);
new (&buf[ulen]) T;
return buf[ulen++];
}
T &dup()
{
if(ulen==alen) vrealloc();
if(ulen==alen) vrealloc(ulen+1);
new (&buf[ulen]) T(buf[ulen-1]);
return buf[ulen++];
}
@@ -257,25 +249,29 @@ template <class T> struct vector
const T *getbuf() const { return buf; }
template<class ST>
void sort(int (__cdecl *cf)(ST *, ST *), int i = 0, int n = -1) { qsort(&buf[i], n<0?ulen:n, sizeof(T), (int (__cdecl *)(const void *,const void *))cf); }
void *_realloc(void *p, int oldsize, int newsize)
void sort(int (__cdecl *cf)(ST *, ST *), int i = 0, int n = -1)
{
void *np = new uchar[newsize];
memcpy(np, p, newsize>oldsize ? oldsize : newsize);
delete[] (uchar *)p;
return np;
qsort(&buf[i], n<0 ? ulen : n, sizeof(T), (int (__cdecl *)(const void *,const void *))cf);
}
void vrealloc()
void vrealloc(int sz)
{
int olen = alen;
buf = (T *)_realloc(buf, olen*sizeof(T), (alen *= 2)*sizeof(T));
if(!alen) alen = max(MINSIZE, sz);
else while(alen < sz) alen *= 2;
if(alen <= olen) return;
uchar *newbuf = new uchar[alen*sizeof(T)];
if(olen > 0)
{
memcpy(newbuf, buf, olen*sizeof(T));
delete[] (uchar *)buf;
}
buf = (T *)newbuf;
}
databuf<T> reserve(int sz)
{
while(alen-ulen<sz) vrealloc();
if(ulen+sz > alen) vrealloc(ulen+sz);
return databuf<T>(&buf[ulen], sz);
}