Browse Source

engine: use GCC provided offsetof implementation

pull/2/head
Alibek Omarov 3 years ago
parent
commit
08c04200fb
  1. 8
      engine/client/mod_dbghulls.c
  2. 2
      engine/common/net_encode.h
  3. 4
      engine/eiface.h
  4. 4
      engine/physint.h

8
engine/client/mod_dbghulls.c

@ -17,7 +17,7 @@ GNU General Public License for more details.
#include "mod_local.h" #include "mod_local.h"
#include "xash3d_mathlib.h" #include "xash3d_mathlib.h"
#include "world.h" #include "world.h"
#include "eiface.h" // offsetof
#define MAX_CLIPNODE_DEPTH 256 // should never exceeds #define MAX_CLIPNODE_DEPTH 256 // should never exceeds
@ -67,7 +67,7 @@ _inline void list_del( hullnode_t *entry )
static winding_t * winding_alloc( uint numpoints ) static winding_t * winding_alloc( uint numpoints )
{ {
return (winding_t *)malloc( (int)((winding_t *)0)->p[numpoints] ); return (winding_t *)malloc( offsetof( winding_t, p[numpoints] ) );
} }
static void free_winding( winding_t *w ) static void free_winding( winding_t *w )
@ -84,7 +84,7 @@ static winding_t *winding_copy( winding_t *w )
winding_t *neww; winding_t *neww;
neww = winding_alloc( w->numpoints ); neww = winding_alloc( w->numpoints );
memcpy( neww, w, (int)((winding_t *)0)->p[w->numpoints] ); memcpy( neww, w, offsetof( winding_t, p[w->numpoints] ) );
return neww; return neww;
} }
@ -111,7 +111,7 @@ static void winding_reverse( winding_t *w )
static winding_t *winding_shrink( winding_t *w ) static winding_t *winding_shrink( winding_t *w )
{ {
winding_t *neww = winding_alloc( w->numpoints ); winding_t *neww = winding_alloc( w->numpoints );
memcpy( neww, w, (int)((winding_t *)0)->p[w->numpoints] ); memcpy( neww, w, offsetof( winding_t, p[w->numpoints] ));
free_winding( w ); free_winding( w );
return neww; return neww;

2
engine/common/net_encode.h

@ -28,8 +28,6 @@ GNU General Public License for more details.
#define DT_STRING BIT( 7 ) // A null terminated string, sent as 8 byte chars #define DT_STRING BIT( 7 ) // A null terminated string, sent as 8 byte chars
#define DT_SIGNED BIT( 8 ) // sign modificator #define DT_SIGNED BIT( 8 ) // sign modificator
#undef offsetof
#define offsetof( s, m ) (size_t)&(((s *)0)->m)
#define NUM_FIELDS( x ) ((sizeof( x ) / sizeof( x[0] )) - 1) #define NUM_FIELDS( x ) ((sizeof( x ) / sizeof( x[0] )) - 1)
// helper macroses // helper macroses

4
engine/eiface.h

@ -366,8 +366,12 @@ typedef enum _fieldtypes
} FIELDTYPE; } FIELDTYPE;
#ifndef offsetof #ifndef offsetof
#ifdef __GNUC__
#define offsetof(s,m) __builtin_offsetof(s,m)
#else
#define offsetof(s,m) (size_t)&(((s *)0)->m) #define offsetof(s,m) (size_t)&(((s *)0)->m)
#endif #endif
#endif
#define _FIELD(type,name,fieldtype,count,flags) { fieldtype, #name, offsetof(type, name), count, flags } #define _FIELD(type,name,fieldtype,count,flags) { fieldtype, #name, offsetof(type, name), count, flags }
#define DEFINE_FIELD(type,name,fieldtype) _FIELD(type, name, fieldtype, 1, 0) #define DEFINE_FIELD(type,name,fieldtype) _FIELD(type, name, fieldtype, 1, 0)

4
engine/physint.h

@ -16,9 +16,11 @@ GNU General Public License for more details.
#ifndef PHYSINT_H #ifndef PHYSINT_H
#define PHYSINT_H #define PHYSINT_H
#include "eiface.h" // offsetof
#define SV_PHYSICS_INTERFACE_VERSION 6 #define SV_PHYSICS_INTERFACE_VERSION 6
#define STRUCT_FROM_LINK( l, t, m ) ((t *)((byte *)l - (int)&(((t *)0)->m))) #define STRUCT_FROM_LINK( l, t, m ) ((t *)((byte *)l - offsetof(t, m)))
#define EDICT_FROM_AREA( l ) STRUCT_FROM_LINK( l, edict_t, area ) #define EDICT_FROM_AREA( l ) STRUCT_FROM_LINK( l, edict_t, area )
// values that can be returned with pfnServerState // values that can be returned with pfnServerState

Loading…
Cancel
Save