compat.h:

create a field_offset macro that takes a structure type and a field and
	returns the offset of the field within the structure
everything else:
	use field_offset to calculate the size of variable sized structs
This commit is contained in:
Bill Currie 2001-05-31 18:11:05 +00:00
parent d2a4faae6e
commit 06064ec390
12 changed files with 33 additions and 26 deletions

View file

@ -88,4 +88,7 @@ extern int vsnprintf(char *s, size_t maxlen, const char *format, va_list arg);
# define strncaseequal(a,b,c) (strncasecmp (a, b, c) == 0)
#endif
#undef field_offset
#define field_offset(type,field) ((int)&(((type *)0)->field))
#endif // __compat_h

View file

@ -115,7 +115,7 @@ Mod_LoadAliasModel (model_t *mod, void *buffer)
// allocate space for a working header, plus all the data except the frames,
// skin and group info
//
size = (int) &((aliashdr_t *) 0)->frames[LittleLong (pinmodel->numframes)];
size = field_offset (aliashdr_t, frames[LittleLong (pinmodel->numframes)]);
pheader = Hunk_AllocName (size, loadname);
memset (pheader, 0, size);
pmodel = &pheader->mdl;

View file

@ -45,6 +45,7 @@
#include "QF/qendian.h"
#include "QF/sys.h"
#include "compat.h"
#include "d_iface.h"
extern char loadname[];
@ -125,16 +126,15 @@ Mod_LoadAllSkins (int numskins, daliasskintype_t *pskintype, int *pskinindex)
pinskingroup = (daliasskingroup_t *) pskintype;
groupskins = LittleLong (pinskingroup->numskins);
t = (int) &((maliasskingroup_t *) 0)->skindescs[groupskins];
t = field_offset (maliasskingroup_t, skindescs[groupskins]);
paliasskingroup = Hunk_AllocName (t, loadname);
paliasskingroup->numskins = groupskins;
*pskinindex = (byte *) paliasskingroup - (byte *) pheader;
pinskinintervals = (daliasskininterval_t *) (pinskingroup + 1);
poutskinintervals =
Hunk_AllocName (groupskins * sizeof (float), loadname);
poutskinintervals = Hunk_AllocName (groupskins * sizeof (float),
loadname);
paliasskingroup->intervals =
(byte *) poutskinintervals - (byte *) pheader;
for (gnum = 0; gnum < groupskins; gnum++) {
@ -250,7 +250,8 @@ Mod_LoadAliasGroup (void *pin, maliasframedesc_t *frame)
numframes = LittleLong (pingroup->numframes);
paliasgroup = Hunk_AllocName (sizeof (maliasgroup_t) + (numframes - 1) * sizeof (paliasgroup->frames[0]), loadname);
paliasgroup = Hunk_AllocName (field_offset (maliasgroup_t,
frames[numframes]), loadname);
paliasgroup->numframes = numframes;
for (i = 0; i < 3; i++) {

View file

@ -196,7 +196,6 @@ Mod_LoadTextures (lump_t *l)
tx->height = mt->height;
for (j = 0; j < MIPLEVELS; j++)
tx->offsets[j] =
mt->offsets[j] + sizeof (texture_t) - sizeof (miptex_t);
// the pixels immediately follow the structures
memcpy (tx + 1, mt + 1, pixels);

View file

@ -71,18 +71,22 @@ Mod_Init (void)
{
int x, y, m;
byte *dest;
int mip0size = 16*16;
int mip1size = 8*8;
int mip2size = 4*4;
int mip3size = 2*2;
memset (mod_novis, 0xff, sizeof (mod_novis));
r_notexture_mip =
Hunk_AllocName (sizeof (texture_t) + 16 * 16 + 8 * 8 + 4 * 4 + 2 * 2,
"notexture");
r_notexture_mip = Hunk_AllocName (sizeof (texture_t)
+ mip0size + mip1size
+ mip2size + mip3size, "notexture");
r_notexture_mip->width = r_notexture_mip->height = 16;
r_notexture_mip->offsets[0] = sizeof (texture_t);
r_notexture_mip->offsets[1] = r_notexture_mip->offsets[0] + 16 * 16;
r_notexture_mip->offsets[2] = r_notexture_mip->offsets[1] + 8 * 8;
r_notexture_mip->offsets[3] = r_notexture_mip->offsets[2] + 4 * 4;
r_notexture_mip->offsets[1] = r_notexture_mip->offsets[0] + mip0size;
r_notexture_mip->offsets[2] = r_notexture_mip->offsets[1] + mip1size;
r_notexture_mip->offsets[3] = r_notexture_mip->offsets[2] + mip2size;
for (m = 0; m < 4; m++) {
dest = (byte *) r_notexture_mip + r_notexture_mip->offsets[m];

View file

@ -37,6 +37,8 @@
#include "QF/qendian.h"
#include "QF/sys.h"
#include "compat.h"
extern model_t *loadmodel;
extern char loadname[];
@ -61,11 +63,8 @@ Mod_LoadSpriteGroup (void *pin, mspriteframe_t **ppframe, int framenum)
numframes = LittleLong (pingroup->numframes);
pspritegroup = Hunk_AllocName (sizeof (mspritegroup_t) +
(numframes -
1) * sizeof (pspritegroup->frames[0]),
loadname);
pspritegroup = Hunk_AllocName (field_offset (mspritegroup_t,
frames[numframes]), loadname);
pspritegroup->numframes = numframes;
@ -120,7 +119,7 @@ Mod_LoadSpriteModel (model_t *mod, void *buffer)
numframes = LittleLong (pin->numframes);
size = sizeof (msprite_t) + (numframes - 1) * sizeof (psprite->frames);
size = field_offset (msprite_t, frames[numframes]);
psprite = Hunk_AllocName (size, loadname);

View file

@ -67,7 +67,6 @@ Mod_LoadSpriteFrame (void *pin, mspriteframe_t **ppframe, int framenum)
size = width * height;
pspriteframe = Hunk_AllocName (sizeof (mspriteframe_t) + size * r_pixbytes,
loadname);
memset (pspriteframe, 0, sizeof (mspriteframe_t) + size);

View file

@ -44,6 +44,8 @@
#include "QF/vfs.h"
#include "QF/zone.h"
#include "compat.h"
tex_t *
LoadPCX (VFile *f, int convert, byte *pal)
{
@ -87,7 +89,7 @@ LoadPCX (VFile *f, int convert, byte *pal)
count = (pcx->xmax + 1) * (pcx->ymax + 1);
if (convert)
count *= 4;
tex = Hunk_TempAlloc (sizeof (tex_t) + count);
tex = Hunk_TempAlloc (field_offset (tex_t, data[count]));
tex->width = pcx->xmax + 1;
tex->height = pcx->ymax + 1;
if (convert) {

View file

@ -39,7 +39,6 @@
#include <time.h>
#include "QF/cmd.h"
#include "compat.h"
#include "QF/console.h"
#include "QF/cvar.h"
#include "QF/draw.h"
@ -52,6 +51,7 @@
#include "QF/tga.h"
#include "QF/vfs.h" // MAX_OSPATH
#include "compat.h"
#include "glquake.h"
#include "r_cvar.h"
#include "r_local.h"
@ -572,7 +572,7 @@ SCR_ScreenShot (int width, int height)
float fracw, frach;
tex_t *tex;
tex = Hunk_TempAlloc (sizeof (tex_t) + vid.width * vid.height * 3);
tex = Hunk_TempAlloc (field_offset (tex_t, data[vid.width * vid.height * 3]));
if (!tex)
return 0;

View file

@ -607,7 +607,7 @@ SCR_ScreenShot (int width, int height)
fracw = (float) vid.width / (float) w;
frach = (float) vid.height / (float) h;
tex = Hunk_TempAlloc (sizeof (tex_t) + w * h);
tex = Hunk_TempAlloc (field_offset (tex_t, data[w * h]));
if (!tex)
return 0;

View file

@ -181,7 +181,7 @@ Skin_Cache (skin_t *skin)
}
pixels = 320 * 200;
out = Cache_Alloc (&skin->data.cache, sizeof (tex_t) + pixels, skin->name);
out = Cache_Alloc (&skin->data.cache, field_offset (tex_t, data[pixels]), skin->name);
if (!out)
Sys_Error ("Skin_Cache: couldn't allocate");
opix = out->data;

View file

@ -182,7 +182,7 @@ Skin_Cache (skin_t *skin)
}
pixels = 320 * 200;
out = Cache_Alloc (&skin->data.cache, sizeof (tex_t) + pixels, skin->name);
out = Cache_Alloc (&skin->data.cache, field_offset (tex_t, data[pixels]), skin->name);
if (!out)
Sys_Error ("Skin_Cache: couldn't allocate");
opix = out->data;