From: lordhavoc Date: Thu, 11 Apr 2002 09:33:15 +0000 (+0000) Subject: removed silly uses of 'long' (now int, or qbyte *, depending on the way it was used... X-Git-Tag: RELEASE_0_2_0_RC1~535 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=8d4a595ba1b90a62dc781b93b12c2edbfff5080f;p=xonotic%2Fdarkplaces.git removed silly uses of 'long' (now int, or qbyte *, depending on the way it was used), and changed memheader sentinels to work on processors which can not handle unaligned memory access (Sparc for example) git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@1734 d7cf8633-e32d-0410-b094-e92efae38249 --- diff --git a/gl_backend.c b/gl_backend.c index 0859df0b..4a15b6d8 100644 --- a/gl_backend.c +++ b/gl_backend.c @@ -861,7 +861,7 @@ void R_Mesh_AddTransparent(void) if (center < 0.0f) center = 0.0f; center += 8388608.0f; - i = *((long *)¢er) & 0x7FFFFF; + i = *((int *)¢er) & 0x7FFFFF; i = min(i, (TRANSDEPTHRES - 1)); #endif tri->next = buf_sorttranstri_list[i]; diff --git a/gl_rsurf.c b/gl_rsurf.c index 11741d35..76493932 100644 --- a/gl_rsurf.c +++ b/gl_rsurf.c @@ -579,7 +579,7 @@ static void RSurfShader_Sky(msurface_t *firstsurf) #if SLOWMATH length = 3.0f / sqrt(number); #else - *((long *)&length) = 0x5f3759df - ((* (long *) &number) >> 1); + *((int *)&length) = 0x5f3759df - ((* (int *) &number) >> 1); length = 3.0f * (length * (1.5f - (number * 0.5f * length * length))); #endif @@ -653,7 +653,7 @@ static void RSurfShader_Sky(msurface_t *firstsurf) #if SLOWMATH length = 3.0f / sqrt(number); #else - *((long *)&length) = 0x5f3759df - ((* (long *) &number) >> 1); + *((int *)&length) = 0x5f3759df - ((* (int *) &number) >> 1); length = 3.0f * (length * (1.5f - (number * 0.5f * length * length))); #endif diff --git a/mathlib.c b/mathlib.c index d45bb356..bb44908d 100644 --- a/mathlib.c +++ b/mathlib.c @@ -149,7 +149,7 @@ float Q_RSqrt(float number) if (number == 0.0f) return 0.0f; - *((long *)&y) = 0x5f3759df - ((* (long *) &number) >> 1); + *((int *)&y) = 0x5f3759df - ((* (int *) &number) >> 1); return y * (1.5f - (number * 0.5f * y * y)); } @@ -161,7 +161,7 @@ void _VectorNormalizeFast(vec3_t v) if (number != 0.0) { - *((long *)&y) = 0x5f3759df - ((* (long *) &number) >> 1); + *((int *)&y) = 0x5f3759df - ((* (int *) &number) >> 1); y = y * (1.5f - (number * 0.5f * y * y)); VectorScale(v, y, v); diff --git a/model_alias.c b/model_alias.c index 926e64f7..eb5696a4 100644 --- a/model_alias.c +++ b/model_alias.c @@ -95,7 +95,7 @@ static void Mod_ConvertAliasVerts (int inverts, vec3_t scale, vec3_t translate, Con_Printf("Mod_ConvertAliasVerts: \"%s\", %i invalid normal indices found\n", loadmodel->name, invalidnormals); } -static void Mod_MDL_LoadFrames (long datapointer, int inverts, int outverts, vec3_t scale, vec3_t translate) +static void Mod_MDL_LoadFrames (qbyte * datapointer, int inverts, int outverts, vec3_t scale, vec3_t translate) { daliasframetype_t *pframetype; daliasframe_t *pinframe; @@ -265,13 +265,13 @@ void Mod_LoadAliasModel (model_t *mod, void *buffer) daliasframetype_t *pinframetype; daliasgroup_t *pinframegroup; float scales, scalet, scale[3], translate[3], interval; - long datapointer, startframes, startskins; + qbyte *datapointer, *startframes, *startskins; char name[MAX_QPATH]; qbyte *skintemp = NULL; modelyawradius = 0; modelradius = 0; - datapointer = (long) buffer; + datapointer = buffer; pinmodel = (mdl_t *)datapointer; datapointer += sizeof(mdl_t); @@ -548,7 +548,7 @@ void Mod_LoadQ2AliasModel (model_t *mod, void *buffer) { int *vertremap; md2_t *pinmodel; - long base; + qbyte *base; int version, end; int i, j, k, hashindex, num, numxyz, numst, xyz, st; float *stverts, s, t; @@ -559,7 +559,7 @@ void Mod_LoadQ2AliasModel (model_t *mod, void *buffer) float st[2]; } *hash, **md2verthash, *md2verthashdata; - long datapointer; + qbyte *datapointer; md2frame_t *pinframe; char *inskin; md2triangle_t *intri; @@ -567,7 +567,7 @@ void Mod_LoadQ2AliasModel (model_t *mod, void *buffer) int skinwidth, skinheight; pinmodel = buffer; - base = (long) buffer; + base = buffer; version = LittleLong (pinmodel->version); if (version != MD2ALIAS_VERSION) diff --git a/model_brush.c b/model_brush.c index c36ea49f..dc5b2431 100644 --- a/model_brush.c +++ b/model_brush.c @@ -975,7 +975,7 @@ void Mod_GenerateWarpMesh (msurface_t *surf) if (mesh->numtriangles < 1) Host_Error("Mod_GenerateWarpMesh: no triangles?\n"); mesh->index = Mem_Alloc(loadmodel->mempool, mesh->numtriangles * sizeof(int[3]) + mesh->numverts * sizeof(surfvertex_t)); - mesh->vertex = (surfvertex_t *)((long) mesh->index + mesh->numtriangles * sizeof(int[3])); + mesh->vertex = (surfvertex_t *)((qbyte *) mesh->index + mesh->numtriangles * sizeof(int[3])); memset(mesh->vertex, 0, mesh->numverts * sizeof(surfvertex_t)); for (i = 0;i < mesh->numtriangles;i++) @@ -1013,7 +1013,7 @@ void Mod_GenerateVertexLitMesh (msurface_t *surf) mesh->numverts = surf->poly_numverts; mesh->numtriangles = surf->poly_numverts - 2; mesh->index = Mem_Alloc(loadmodel->mempool, mesh->numtriangles * sizeof(int[3]) + mesh->numverts * sizeof(surfvertex_t)); - mesh->vertex = (surfvertex_t *)((long) mesh->index + mesh->numtriangles * sizeof(int[3])); + mesh->vertex = (surfvertex_t *)((qbyte *) mesh->index + mesh->numtriangles * sizeof(int[3])); memset(mesh->vertex, 0, mesh->numverts * sizeof(surfvertex_t)); index = mesh->index; @@ -1080,7 +1080,7 @@ void Mod_GenerateLightmappedMesh (msurface_t *surf) mesh->numverts = surf->poly_numverts; mesh->numtriangles = surf->poly_numverts - 2; mesh->index = Mem_Alloc(loadmodel->mempool, mesh->numtriangles * sizeof(int[3]) + mesh->numverts * sizeof(surfvertex_t)); - mesh->vertex = (surfvertex_t *)((long) mesh->index + mesh->numtriangles * sizeof(int[3])); + mesh->vertex = (surfvertex_t *)((qbyte *) mesh->index + mesh->numtriangles * sizeof(int[3])); memset(mesh->vertex, 0, mesh->numverts * sizeof(surfvertex_t)); index = mesh->index; @@ -1131,7 +1131,7 @@ void Mod_GenerateVertexMesh (msurface_t *surf) mesh->numverts = surf->poly_numverts; mesh->numtriangles = surf->poly_numverts - 2; mesh->index = Mem_Alloc(loadmodel->mempool, mesh->numtriangles * sizeof(int[3]) + mesh->numverts * sizeof(surfvertex_t)); - mesh->vertex = (surfvertex_t *)((long) mesh->index + mesh->numtriangles * sizeof(int[3])); + mesh->vertex = (surfvertex_t *)((qbyte *) mesh->index + mesh->numtriangles * sizeof(int[3])); memset(mesh->vertex, 0, mesh->numverts * sizeof(surfvertex_t)); index = mesh->index; @@ -1302,7 +1302,7 @@ static int Mod_SurfaceQSortCompare(const void *voida, const void *voidb) a = *((const msurface_t **)voida); b = *((const msurface_t **)voidb); if (a->shader != b->shader) - return (long) a->shader - (long) b->shader; + return (qbyte *) a->shader - (qbyte *) b->shader; if (a->texinfo->texture != b->texinfo->texture); return a->texinfo->texture - b->texinfo->texture; return 0; @@ -2049,7 +2049,7 @@ static void Mod_FinalizePortals(void) } loadmodel->portals = Mem_Alloc(loadmodel->mempool, numportals * sizeof(mportal_t) + numpoints * sizeof(mvertex_t)); loadmodel->numportals = numportals; - loadmodel->portalpoints = (void *) ((long) loadmodel->portals + numportals * sizeof(mportal_t)); + loadmodel->portalpoints = (void *) ((qbyte *) loadmodel->portals + numportals * sizeof(mportal_t)); loadmodel->numportalpoints = numpoints; // clear all leaf portal chains for (i = 0;i < loadmodel->numleafs;i++) diff --git a/model_shared.c b/model_shared.c index 4151100e..f9fc38ae 100644 --- a/model_shared.c +++ b/model_shared.c @@ -24,7 +24,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "quakedef.h" -model_t *loadmodel; +model_t *loadmodel; // LordHavoc: increased from 512 to 2048 #define MAX_MOD_KNOWN 2048 diff --git a/model_sprite.c b/model_sprite.c index d2ccfcf5..346fddb1 100644 --- a/model_sprite.c +++ b/model_sprite.c @@ -51,7 +51,7 @@ void Mod_Sprite_StripExtension(char *in, char *out) *out++ = 0; } -void Mod_Sprite_SharedSetup(long datapointer, int version, int *palette) +void Mod_Sprite_SharedSetup(qbyte *datapointer, int version, int *palette) { int i, j, k, groupframes, realframes, x, y, origin[2], width, height; dspriteframetype_t *pinframetype; @@ -61,7 +61,7 @@ void Mod_Sprite_SharedSetup(long datapointer, int version, int *palette) float modelradius, interval; char tempname[MAX_QPATH], name[MAX_QPATH]; qbyte *pixbuf; - long startframes; + void *startframes; modelradius = 0; Mod_Sprite_StripExtension(loadmodel->name, tempname); @@ -188,9 +188,9 @@ void Mod_Sprite_SharedSetup(long datapointer, int version, int *palette) { pixbuf = Mem_Alloc(tempmempool, width*height*4); if (version == SPRITE32_VERSION) - memcpy(pixbuf, (qbyte *)datapointer, width*height*4); + memcpy(pixbuf, datapointer, width*height*4); else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION) - Image_Copy8bitRGBA((qbyte *)datapointer, pixbuf, width*height, palette); + Image_Copy8bitRGBA(datapointer, pixbuf, width*height, palette); loadmodel->sprdata_frames[realframes].texture = R_LoadTexture (loadmodel->texturepool, name, width, height, pixbuf, TEXTYPE_RGBA, TEXF_ALPHA | (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_PRECACHE); @@ -241,7 +241,14 @@ Mod_LoadSpriteModel */ void Mod_LoadSpriteModel (model_t *mod, void *buffer) { - int version; + int version, i, rendermode; + qbyte palette[256][4], *in; + dsprite_t *pinqsprite; + dspritehl_t *pinhlsprite; + qbyte *datapointer; + + datapointer = buffer; + loadmodel->SERAddEntity = Mod_Sprite_SERAddEntity; loadmodel->Draw = R_DrawSpriteModel; loadmodel->DrawSky = NULL; @@ -250,43 +257,31 @@ void Mod_LoadSpriteModel (model_t *mod, void *buffer) version = LittleLong(((dsprite_t *)buffer)->version); if (version == SPRITE_VERSION || SPRITE32_VERSION) { - dsprite_t *pinsprite; - long datapointer; - - datapointer = (long) buffer; - - pinsprite = (dsprite_t *)datapointer; + pinqsprite = (dsprite_t *)datapointer; datapointer += sizeof(dsprite_t); - loadmodel->numframes = LittleLong (pinsprite->numframes); - loadmodel->sprnum_type = LittleLong (pinsprite->type); - loadmodel->synctype = LittleLong (pinsprite->synctype); + loadmodel->numframes = LittleLong (pinqsprite->numframes); + loadmodel->sprnum_type = LittleLong (pinqsprite->type); + loadmodel->synctype = LittleLong (pinqsprite->synctype); - Mod_Sprite_SharedSetup(datapointer, LittleLong (pinsprite->version), d_8to24table); + Mod_Sprite_SharedSetup(datapointer, LittleLong (pinqsprite->version), d_8to24table); } else if (version == SPRITEHL_VERSION) { - int i, rendermode; - qbyte palette[256][4], *in; - dspritehl_t *pinsprite; - long datapointer; - - datapointer = (long) buffer; - - pinsprite = (dspritehl_t *)datapointer; + pinhlsprite = (dspritehl_t *)datapointer; datapointer += sizeof(dspritehl_t); - loadmodel->numframes = LittleLong (pinsprite->numframes); - loadmodel->sprnum_type = LittleLong (pinsprite->type); - loadmodel->synctype = LittleLong (pinsprite->synctype); - rendermode = pinsprite->rendermode; + loadmodel->numframes = LittleLong (pinhlsprite->numframes); + loadmodel->sprnum_type = LittleLong (pinhlsprite->type); + loadmodel->synctype = LittleLong (pinhlsprite->synctype); + rendermode = pinhlsprite->rendermode; - in = (qbyte *)datapointer; + in = datapointer; datapointer += 2; i = in[0] + in[1] * 256; if (i != 256) Host_Error ("Mod_LoadHLSprite: unexpected number of palette colors %i (should be 256)", i); - in = (qbyte *)datapointer; + in = datapointer; datapointer += 768; switch(rendermode) { @@ -336,7 +331,7 @@ void Mod_LoadSpriteModel (model_t *mod, void *buffer) return; } - Mod_Sprite_SharedSetup(datapointer, LittleLong (pinsprite->version), (int *)(&palette[0][0])); + Mod_Sprite_SharedSetup(datapointer, LittleLong (pinhlsprite->version), (int *)(&palette[0][0])); } else Host_Error ("Mod_LoadSpriteModel: %s has wrong version number (%i should be 1 (quake) or 32 (sprite32) or 2 (halflife)", loadmodel->name, version); diff --git a/r_clip.c b/r_clip.c index 8625a2a1..547bcda1 100644 --- a/r_clip.c +++ b/r_clip.c @@ -211,7 +211,7 @@ int R_Clip_TriangleToPlane(vec3_t point1, vec3_t point2, vec3_t point3, tinyplan number = DotProduct(p->normal, p->normal); if (number >= 0.1f) { - *((long *)&y) = 0x5f3759df - ((* (long *) &number) >> 1); + *((int *)&y) = 0x5f3759df - ((* (int *) &number) >> 1); y = y * (1.5f - (number * 0.5f * y * y)); VectorScale(p->normal, y, p->normal); p->dist = DotProduct(point1, p->normal); diff --git a/r_light.c b/r_light.c index 4264ea65..a7e392c7 100644 --- a/r_light.c +++ b/r_light.c @@ -916,7 +916,7 @@ void R_LightModel(int numverts, float colorr, float colorg, float colorb, int wo t = 1.0f / sqrt(dist2); #else number = DotProduct(v, v); - *((long *)&t) = 0x5f3759df - ((* (long *) &number) >> 1); + *((int *)&t) = 0x5f3759df - ((* (int *) &number) >> 1); t = t * (1.5f - (number * 0.5f * t * t)); #endif // DotProduct(avn,v) * t is dotproduct with a normalized v, diff --git a/zone.c b/zone.c index dd8074c1..fb485f84 100644 --- a/zone.c +++ b/zone.c @@ -81,7 +81,7 @@ void *_Mem_Alloc(mempool_t *pool, int size, char *filename, int fileline) clump->largestavailable = MEMBITS - needed; j = 0; choseclump: - mem = (memheader_t *)((long) clump->block + j * MEMUNIT); + mem = (memheader_t *)((qbyte *) clump->block + j * MEMUNIT); mem->clump = clump; clump->blocksinuse += needed; for (i = j + needed;j < i;j++) @@ -100,13 +100,14 @@ choseclump: mem->fileline = fileline; mem->size = size; mem->pool = pool; - mem->sentinel1 = MEMHEADER_SENTINEL; - *((int *)((long) mem + sizeof(memheader_t) + mem->size)) = MEMHEADER_SENTINEL; + mem->sentinel1 = MEMHEADER_SENTINEL1; + // we have to use only a single byte for this sentinel, because it may not be aligned, and some platforms can't use unaligned accesses + *((qbyte *) mem + sizeof(memheader_t) + mem->size) = MEMHEADER_SENTINEL2; // append to head of list mem->chain = pool->chain; pool->chain = mem; - memset((void *)((long) mem + sizeof(memheader_t)), 0, mem->size); - return (void *)((long) mem + sizeof(memheader_t)); + memset((void *)((qbyte *) mem + sizeof(memheader_t)), 0, mem->size); + return (void *)((qbyte *) mem + sizeof(memheader_t)); } void _Mem_Free(void *data, char *filename, int fileline) @@ -119,10 +120,10 @@ void _Mem_Free(void *data, char *filename, int fileline) Sys_Error("Mem_Free: data == NULL (called at %s:%i)", filename, fileline); - mem = (memheader_t *)((long) data - sizeof(memheader_t)); - if (mem->sentinel1 != MEMHEADER_SENTINEL) + mem = (memheader_t *)((qbyte *) data - sizeof(memheader_t)); + if (mem->sentinel1 != MEMHEADER_SENTINEL1) Sys_Error("Mem_Free: trashed header sentinel 1 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline); - if (*((int *)((long) mem + sizeof(memheader_t) + mem->size)) != MEMHEADER_SENTINEL) + if (*((qbyte *) mem + sizeof(memheader_t) + mem->size) != MEMHEADER_SENTINEL2) Sys_Error("Mem_Free: trashed header sentinel 2 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline); pool = mem->pool; Con_DPrintf("Mem_Free: pool %s, alloc %s:%i, free %s:%i, size %i bytes\n", pool->name, mem->filename, mem->fileline, filename, fileline, mem->size); @@ -138,7 +139,7 @@ void _Mem_Free(void *data, char *filename, int fileline) Sys_Error("Mem_Free: trashed clump sentinel 1 (free at %s:%i)", filename, fileline); if (clump->sentinel2 != MEMCLUMP_SENTINEL) Sys_Error("Mem_Free: trashed clump sentinel 2 (free at %s:%i)", filename, fileline); - firstblock = ((long) mem - (long) clump->block); + firstblock = ((qbyte *) mem - (qbyte *) clump->block); if (firstblock & (MEMUNIT - 1)) Sys_Error("Mem_Free: address not valid in clump (free at %s:%i)", filename, fileline); firstblock /= MEMUNIT; @@ -215,7 +216,7 @@ void _Mem_FreePool(mempool_t **pool, char *filename, int fileline) // free memory owned by the pool while ((*pool)->chain) - Mem_Free((void *)((long) (*pool)->chain + sizeof(memheader_t))); + Mem_Free((void *)((qbyte *) (*pool)->chain + sizeof(memheader_t))); // free the pool itself memset(*pool, 0xBF, sizeof(mempool_t)); @@ -231,7 +232,7 @@ void _Mem_EmptyPool(mempool_t *pool, char *filename, int fileline) // free memory owned by the pool while (pool->chain) - Mem_Free((void *)((long) pool->chain + sizeof(memheader_t))); + Mem_Free((void *)((qbyte *) pool->chain + sizeof(memheader_t))); } void _Mem_CheckSentinels(void *data, char *filename, int fileline) @@ -241,10 +242,10 @@ void _Mem_CheckSentinels(void *data, char *filename, int fileline) if (data == NULL) Sys_Error("Mem_CheckSentinels: data == NULL (sentinel check at %s:%i)", filename, fileline); - mem = (memheader_t *)((long) data - sizeof(memheader_t)); - if (mem->sentinel1 != MEMHEADER_SENTINEL) + mem = (memheader_t *)((qbyte *) data - sizeof(memheader_t)); + if (mem->sentinel1 != MEMHEADER_SENTINEL1) Sys_Error("Mem_CheckSentinels: trashed header sentinel 1 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline); - if (*((int *)((long) mem + sizeof(memheader_t) + mem->size)) != MEMHEADER_SENTINEL) + if (*((qbyte *) mem + sizeof(memheader_t) + mem->size) != MEMHEADER_SENTINEL2) Sys_Error("Mem_CheckSentinels: trashed header sentinel 2 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline); } @@ -265,7 +266,7 @@ void _Mem_CheckSentinelsGlobal(char *filename, int fileline) for (pool = poolchain;pool;pool = pool->next) { for (mem = pool->chain;mem;mem = mem->chain) - _Mem_CheckSentinels((void *)((long) mem + sizeof(memheader_t)), filename, fileline); + _Mem_CheckSentinels((void *)((qbyte *) mem + sizeof(memheader_t)), filename, fileline); for (clump = pool->clumpchain;clump;clump = clump->chain) _Mem_CheckClumpSentinels(clump, filename, fileline); } diff --git a/zone.h b/zone.h index 0b77463a..7a069181 100644 --- a/zone.h +++ b/zone.h @@ -28,8 +28,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define MEMBITS (MEMCLUMPSIZE / MEMUNIT) #define MEMBITINTS (MEMBITS / 32) -#define MEMHEADER_SENTINEL 0xABADCAFE -#define MEMCLUMP_SENTINEL 0xDEADF00D +#define MEMHEADER_SENTINEL1 0xDEADF00D +#define MEMHEADER_SENTINEL2 0xDF +#define MEMCLUMP_SENTINEL 0xABADCAFE typedef struct memheader_s { @@ -44,9 +45,9 @@ typedef struct memheader_s // file name and line where Mem_Alloc was called char *filename; int fileline; - // should always be MEMHEADER_SENTINEL + // should always be MEMHEADER_SENTINEL1 int sentinel1; - // immediately followed by data, which is followed by another MEMHEADER_SENTINEL + // immediately followed by data, which is followed by a MEMHEADER_SENTINEL2 byte } memheader_t;