*** empty log message ***

git-svn-id: svn://svn.code.sf.net/p/actiongame/code/trunk/ac@472 e016b1c5-ed68-44aa-a2f7-31cac88cffde
This commit is contained in:
Lee Salzman
2006-11-05 17:41:27 +00:00
parent 7d429be232
commit 1802cace77
35 changed files with 960 additions and 620 deletions
+8 -100
View File
@@ -1,103 +1,9 @@
// implementation of generic tools
#include "tools.h"
#include <new>
#include "cube.h"
//////////////////////////// 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(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 ///////////////////////
char *path(char *s)
@@ -112,13 +18,14 @@ char *loadfile(char *fn, int *size)
if(!f) return NULL;
fseek(f, 0, SEEK_END);
int len = ftell(f);
if(len<=0) { fclose(f); return NULL; };
fseek(f, 0, SEEK_SET);
char *buf = (char *)malloc(len+1);
if(!buf) return NULL;
char *buf = new char[len+1];
if(!buf) { fclose(f); return NULL; };
buf[len] = 0;
size_t rlen = fread(buf, 1, len, f);
fclose(f);
if(len!=rlen || len<=0)
if(size_t(len)!=rlen)
{
free(buf);
return NULL;
@@ -127,9 +34,9 @@ char *loadfile(char *fn, int *size)
return buf;
};
void endianswap(void *memory, int stride, int length) // little indians as storage format
void endianswap(void *memory, int stride, int length) // little endian as storage format
{
if(*((char *)&stride)) return;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
loop(w, length) loop(i, stride/2)
{
uchar *p = (uchar *)memory+w*stride;
@@ -137,4 +44,5 @@ void endianswap(void *memory, int stride, int length) // little indians as sto
p[i] = p[stride-i-1];
p[stride-i-1] = t;
};
#endif
}