make LoadBSPMem work in-place

LoadBSPMem now modifies the passed in buffer rathern than allocating new
memory. One step closer to cleaning up brush model loading.
This commit is contained in:
Bill Currie 2010-08-23 09:40:01 +09:00
parent 56fb4d9a22
commit 719ca15516

View file

@ -186,31 +186,32 @@ swap_bsp (bsp_t *bsp, int todisk)
} }
bsp_t * bsp_t *
LoadBSPMem (void *mem, size_t size) LoadBSPMem (void *mem, size_t mem_size)
{ {
dheader_t *header = mem; byte *data = mem;
bsp_t *bsp; bsp_t *bsp;
size_t size;
if (LittleLong (header->version) != BSPVERSION) bsp = calloc (sizeof (bsp_t), 1);
Sys_Error ("version %i, not %i", LittleLong (header->version),
bsp->header = mem;
if (LittleLong (bsp->header->version) != BSPVERSION)
Sys_Error ("version %i, not %i", LittleLong (bsp->header->version),
BSPVERSION); BSPVERSION);
bsp = malloc (sizeof (bsp_t));
bsp->header = 0;
#undef SET_LUMP #undef SET_LUMP
#define SET_LUMP(l,n) \ #define SET_LUMP(l,n) \
do { \ do { \
bsp->num##n = LittleLong (header->lumps[l].filelen); \ size = LittleLong (bsp->header->lumps[l].filelen); \
if (bsp->num##n) { \ data = (byte *) mem + LittleLong (bsp->header->lumps[l].fileofs); \
bsp->n = malloc (bsp->num##n); \ if ((data + size) > ((byte *) mem + mem_size)) { \
memcpy (bsp->n, \ Sys_Error ("invalid lump"); \
(byte *) header + LittleLong (header->lumps[l].fileofs), \
bsp->num##n); \
} else { \
bsp->n = 0; \
} \ } \
bsp->num##n /= sizeof (bsp->n[0]); \ bsp->n = 0; \
if (size) \
bsp->n = (void *) data; \
bsp->num##n = size / sizeof (bsp->n[0]); \
} while (0) } while (0)
SET_LUMP (LUMP_PLANES, planes); SET_LUMP (LUMP_PLANES, planes);
@ -228,15 +229,15 @@ do { \
#undef SET_LUMP #undef SET_LUMP
#define SET_LUMP(l,n) \ #define SET_LUMP(l,n) \
do { \ do { \
bsp->n##size = LittleLong (header->lumps[l].filelen); \ size = LittleLong (bsp->header->lumps[l].filelen); \
if (bsp->n##size) { \ data = (byte *) mem + LittleLong (bsp->header->lumps[l].fileofs); \
bsp->n = malloc (bsp->n##size); \ if ((data + size) > ((byte *) mem + mem_size)) { \
memcpy (bsp->n, \ Sys_Error ("invalid lump"); \
(byte *) header + LittleLong (header->lumps[l].fileofs), \
bsp->n##size); \
} else { \
bsp->n = 0; \
} \ } \
bsp->n = 0; \
if (size) \
bsp->n = (void *) data; \
bsp->n##size = size; \
} while (0) } while (0)
SET_LUMP (LUMP_LIGHTING, lightdata); SET_LUMP (LUMP_LIGHTING, lightdata);
@ -257,7 +258,6 @@ LoadBSPFile (QFile *file, size_t size)
buf = malloc (size); buf = malloc (size);
Qread (file, buf, size); Qread (file, buf, size);
bsp = LoadBSPMem (buf, size); bsp = LoadBSPMem (buf, size);
free (buf);
return bsp; return bsp;
} }