WZ_DECL_CONST now implies WZ_DECL_WARN_UNUSED_RESULT and also works on MSVC.

WZ_DECL_RESTRICT now works on MSVC as well.
WZ_DECL_THREAD added, declares variables thread local, support for it made mandatory.
WZ_ASSERT_STATIC_STRING (gcc only) to assert that a string is static added.
The sstr functions now support pointer-length strings again.


git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@4908 4a71c877-e1ca-e34f-864e-861f7616d084
master
Per Inge Mathisen 2008-05-04 11:43:28 +00:00
parent bedb5a77cb
commit 6e9732a86a
3 changed files with 74 additions and 71 deletions

View File

@ -118,10 +118,10 @@ static inline size_t strlcat(char *WZ_DECL_RESTRICT dst, const char *WZ_DECL_RES
#define ssprintf(dest, ...) snprintf((dest), sizeof(dest), __VA_ARGS__)
#define sstrcmp(str1, str2) strncmp((str1), (str2), sizeof(str1) > sizeof(str2) ? sizeof(str2) : sizeof(str1))
#else
#define sstrcpy(dest, src) (assert(sizeof(dest) != sizeof(void*)), strlcpy((dest), (src), sizeof(dest)))
#define sstrcat(dest, src) (assert(sizeof(dest) != sizeof(void*)), strlcat((dest), (src), sizeof(dest)))
#define ssprintf(dest, ...) (assert(sizeof(dest) != sizeof(void*)), snprintf((dest), sizeof(dest), __VA_ARGS__))
#define sstrcmp(str1, str2) (assert(sizeof(str1) != sizeof(void*) && sizeof(str2) != sizeof(void*)), strncmp((str1), (str2), sizeof(str1) > sizeof(str2) ? sizeof(str2) : sizeof(str1)))
#define sstrcpy(dest, src) (WZ_ASSERT_STATIC_STRING(dest), strlcpy((dest), (src), sizeof(dest)))
#define sstrcat(dest, src) (WZ_ASSERT_STATIC_STRING(dest), strlcat((dest), (src), sizeof(dest)))
#define ssprintf(dest, ...) (WZ_ASSERT_STATIC_STRING(dest), snprintf((dest), sizeof(dest), __VA_ARGS__))
#define sstrcmp(str1, str2) (WZ_ASSERT_STATIC_STRING(str1), WZ_ASSERT_STATIC_STRING(str2), strncmp((str1), (str2), sizeof(str1) > sizeof(str2) ? sizeof(str2) : sizeof(str1)))
#endif
#endif // __INCLUDED_FRAMEWORK_STRLFUNCS_H__

View File

@ -338,7 +338,7 @@
*
*/
#if WZ_CC_GNU_PREREQ(3,2) || WZ_CC_INTEL_PREREQ(10,0)
# define WZ_DECL_DEPRECATED __attribute__ ((__deprecated__))
# define WZ_DECL_DEPRECATED __attribute__((deprecated))
#elif defined(WZ_CC_MSVC) && (_MSC_VER >= 1300)
# define WZ_DECL_DEPRECATED __declspec(deprecated)
#else
@ -351,7 +351,7 @@
* This function is not used, but shall not generate an unused warning either.
*/
#if WZ_CC_GNU_PREREQ(3,2) || WZ_CC_INTEL_PREREQ(10,0)
# define WZ_DECL_UNUSED __attribute__((__unused__))
# define WZ_DECL_UNUSED __attribute__((unused))
#else
# define WZ_DECL_UNUSED
#endif
@ -362,7 +362,7 @@
* Warn if return value is unused.
*/
#if defined(WZ_CC_GNU)
# define WZ_DECL_WARN_UNUSED_RESULT __attribute__ ((warn_unused_result))
# define WZ_DECL_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
#else
# define WZ_DECL_WARN_UNUSED_RESULT
#endif
@ -370,10 +370,11 @@
/*!
* \def WZ_DECL_PURE
* "Many functions have no effects except the return value and their return value depends only on the parameters and/or global variables. Such a function can be subject to common subexpression elimination and loop optimization just as an arithmetic operator would be."
* "Many functions have no effects except the return value and their return value depends only on the parameters and/or global variables. Such a function can
* be subject to common subexpression elimination and loop optimization just as an arithmetic operator would be."
*/
#if WZ_CC_GNU_PREREQ(2,96) && !defined(WZ_CC_INTEL)
# define WZ_DECL_PURE __attribute__((__pure__))
# define WZ_DECL_PURE __attribute__((pure))
#else
# define WZ_DECL_PURE
#endif
@ -384,6 +385,8 @@
*/
#if WZ_CC_GNU_PREREQ(4,1) && !defined(WZ_CC_INTEL) && !defined(__cplusplus)
# define WZ_DECL_RESTRICT restrict
#elif defined(WZ_CC_MSVC) && (_MSC_VER >= 1300)
# define WZ_DECL_RESTRICT __restrict
#else
# define WZ_DECL_RESTRICT
#endif
@ -391,10 +394,16 @@
/*!
* \def WZ_DECL_CONST
* "Many functions do not examine any values except their arguments, and have no effects except the return value. Basically this is just slightly more strict class than the pure attribute below, since function is not allowed to read global memory."
* GCC: "Many functions do not examine any values except their arguments, and have no effects except the return value. Basically this is just slightly more strict
* class than the pure attribute below, since function is not allowed to read global memory."
* MSVC: "If a function is annotated as noalias, the optimizer can assume that, in addition to the parameters themselves, only first-level indirections of pointer
* parameters are referenced or modified inside the function."
* The gcc const attribute does not permit passing values as pointers, unlike the MSVC attribute. The lowest commmon denominator must be used here.
*/
#if WZ_CC_GNU_PREREQ(2,5) && !defined(WZ_CC_INTEL)
# define WZ_DECL_CONST __attribute__((__const__))
# define WZ_DECL_CONST __attribute__((const,warn_unused_result))
#elif defined(WZ_CC_MSVC) && (_MSC_VER >= 1300)
# define __declspec(noalias)
#else
# define WZ_DECL_CONST
#endif
@ -405,7 +414,7 @@
*/
#if WZ_CC_GNU_PREREQ(2,5) && !defined(WZ_CC_INTEL)
# define WZ_DECL_FORMAT(archetype, string_index, first_to_check) \
__attribute__((__format__ (archetype, string_index, first_to_check)))
__attribute__((format(archetype, string_index, first_to_check)))
#else
# define WZ_DECL_FORMAT(archetype, string_index, first_to_check)
#endif
@ -415,11 +424,34 @@
* "Accesses to objects with types with this attribute are not subjected to type-based alias analysis, but are instead assumed to be able to alias any other type of objects, just like the char type. See -fstrict-aliasing for more information on aliasing issues."
*/
#if WZ_CC_GNU_PREREQ(3,3) && !defined(WZ_CC_INTEL)
# define WZ_DECL_MAY_ALIAS __attribute__((__may_alias__))
# define WZ_DECL_MAY_ALIAS __attribute__((may_alias))
#else
# define WZ_DECL_MAY_ALIAS
#endif
/*! \def WZ_DECL_THREAD
* Declares a variable to be local to the running thread, and not shared between threads.
*/
#if defined(WZ_CC_GNU) || defined(WZ_CC_INTEL)
# define WZ_DECL_THREAD __thread
#elif defined(WZ_CC_MSVC)
# define __declspec(thread)
#else
# error "Thread local storage attribute required"
#endif
/*! \def WZ_ASSERT_STATIC_STRING
* Asserts that two types are equal
*/
#if defined(WZ_CC_GNU) || defined(WZ_CC_INTEL)
# define WZ_ASSERT_STATIC_STRING(_var) assert(__builtin_types_compatible_p(typeof(_var), char[]))
#else
# define WZ_ASSERT_STATIC_STRING(_var)
#endif
/* ---- Global constants ---- */
#define MAX_STR_LENGTH 256

View File

@ -36,8 +36,7 @@ typedef struct { uint16_t x, y, z; } Vector3uw; //Only used for basedef.h BASE_E
* \param v Vector to convert
* \return Float vector
*/
static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
Vector2f Vector2i_To2f(const Vector2i v)
static inline WZ_DECL_CONST Vector2f Vector2i_To2f(const Vector2i v)
{
Vector2f dest = { (float)v.x, (float)v.y };
return dest;
@ -49,8 +48,7 @@ static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
* \param[in] op1,op2 Operands
* \return Result
*/
static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
Vector2i Vector2i_Add(const Vector2i op1, const Vector2i op2)
static inline WZ_DECL_CONST Vector2i Vector2i_Add(const Vector2i op1, const Vector2i op2)
{
Vector2i dest = {
op1.x + op2.x,
@ -65,8 +63,7 @@ static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
* \param op1,op2 Operands
* \return Result
*/
static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
Vector2i Vector2i_Sub(const Vector2i op1, const Vector2i op2)
static inline WZ_DECL_CONST Vector2i Vector2i_Sub(const Vector2i op1, const Vector2i op2)
{
Vector2i dest = {
op1.x - op2.x,
@ -82,8 +79,7 @@ static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
* \param s Scalar
* \return Product
*/
static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
Vector2i Vector2i_Mult(const Vector2i v, const int s)
static inline WZ_DECL_CONST Vector2i Vector2i_Mult(const Vector2i v, const int s)
{
Vector2i dest = { v.x * s, v.y * s };
return dest;
@ -95,8 +91,7 @@ static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
* \param op1,op2 Operands
* \return Scalarproduct of the 2 vectors
*/
static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
int Vector2i_ScalarP(const Vector2i op1, const Vector2i op2)
static inline WZ_DECL_CONST int Vector2i_ScalarP(const Vector2i op1, const Vector2i op2)
{
return op1.x * op2.x + op1.y * op2.y;
}
@ -107,8 +102,7 @@ static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
* \param v Vector
* \return Length
*/
static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
int Vector2i_Length(const Vector2i v)
static inline WZ_DECL_CONST int Vector2i_Length(const Vector2i v)
{
return sqrtf( (float)Vector2i_ScalarP(v, v) );
}
@ -126,8 +120,7 @@ static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
* \param r The radius of the circle
* \return If v falls within the circle
*/
static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
BOOL Vector2i_InCircle(const Vector2i v, const Vector2i c, const unsigned int r)
static inline WZ_DECL_CONST BOOL Vector2i_InCircle(const Vector2i v, const Vector2i c, const unsigned int r)
{
Vector2i delta = Vector2i_Sub(v, c);
// Explictily cast to "unsigned int" because this number never can be
@ -143,8 +136,7 @@ static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
* \param x,y Coordinates
* \return New Vector
*/
static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
Vector2f Vector2f_New(const float x, const float y)
static inline WZ_DECL_CONST Vector2f Vector2f_New(const float x, const float y)
{
Vector2f dest = { x, y };
return dest;
@ -156,8 +148,7 @@ static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
* \param op1,op2 Operands
* \return Result
*/
static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
Vector2f Vector2f_Add(const Vector2f op1, const Vector2f op2)
static inline WZ_DECL_CONST Vector2f Vector2f_Add(const Vector2f op1, const Vector2f op2)
{
Vector2f dest = {
op1.x + op2.x,
@ -172,8 +163,7 @@ static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
* \param op1,op2 Operands
* \return Result
*/
static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
Vector2f Vector2f_Sub(const Vector2f op1, const Vector2f op2)
static inline WZ_DECL_CONST Vector2f Vector2f_Sub(const Vector2f op1, const Vector2f op2)
{
Vector2f dest = {
op1.x - op2.x,
@ -189,8 +179,7 @@ static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
* \param s Scalar
* \return Product
*/
static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
Vector2f Vector2f_Mult(const Vector2f v, const float s)
static inline WZ_DECL_CONST Vector2f Vector2f_Mult(const Vector2f v, const float s)
{
Vector2f dest = { v.x * s, v.y * s };
return dest;
@ -202,8 +191,7 @@ static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
* \param op1,op2 Operands
* \return Scalarproduct of the 2 vectors
*/
static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
float Vector2f_ScalarP(const Vector2f op1, const Vector2f op2)
static inline WZ_DECL_CONST float Vector2f_ScalarP(const Vector2f op1, const Vector2f op2)
{
return op1.x * op2.x + op1.y * op2.y;
}
@ -214,8 +202,7 @@ static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
* \param v Vector
* \return Length
*/
static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
float Vector2f_Length(const Vector2f v)
static inline WZ_DECL_CONST float Vector2f_Length(const Vector2f v)
{
return sqrtf( Vector2f_ScalarP(v, v) );
}
@ -226,8 +213,7 @@ static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
* \param v Vector
* \return Normalised vector, nullvector when input was nullvector or very small
*/
static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
Vector2f Vector2f_Normalise(const Vector2f v)
static inline WZ_DECL_CONST Vector2f Vector2f_Normalise(const Vector2f v)
{
float length = Vector2f_Length(v);
@ -247,8 +233,7 @@ static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
/*!
* \return true if both vectors are equal
*/
static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
BOOL Vector3f_Compare(const Vector3f a, const Vector3f b)
static inline WZ_DECL_CONST BOOL Vector3f_Compare(const Vector3f a, const Vector3f b)
{
return a.x == b.x && a.y == b.y && a.z == b.z;
}
@ -273,8 +258,7 @@ static inline void Vector3f_Set(Vector3f* v, const float x, const float y, const
* \param op1,op2 Operands
* \return Result
*/
static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
Vector3f Vector3f_Add(const Vector3f op1, const Vector3f op2)
static inline WZ_DECL_CONST Vector3f Vector3f_Add(const Vector3f op1, const Vector3f op2)
{
Vector3f dest = {
op1.x + op2.x,
@ -290,8 +274,7 @@ static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
* \param op1,op2 Operands
* \return Result
*/
static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
Vector3f Vector3f_Sub(const Vector3f op1, const Vector3f op2)
static inline WZ_DECL_CONST Vector3f Vector3f_Sub(const Vector3f op1, const Vector3f op2)
{
Vector3f dest = {
op1.x - op2.x,
@ -308,8 +291,7 @@ static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
* \param s Scalar
* \return Product
*/
static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
Vector3f Vector3f_Mult(const Vector3f v, const float s)
static inline WZ_DECL_CONST Vector3f Vector3f_Mult(const Vector3f v, const float s)
{
Vector3f dest = { v.x * s, v.y * s, v.z * s };
return dest;
@ -321,8 +303,7 @@ static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
* \param op1,op2 Operands
* \return Scalarproduct of the 2 vectors
*/
static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
float Vector3f_ScalarP(const Vector3f op1, const Vector3f op2)
static inline WZ_DECL_CONST float Vector3f_ScalarP(const Vector3f op1, const Vector3f op2)
{
return op1.x * op2.x + op1.y * op2.y + op1.z * op2.z;
}
@ -333,8 +314,7 @@ static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
* \param op1,op2 Operands
* \return Crossproduct
*/
static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
Vector3f Vector3f_CrossP(const Vector3f op1, const Vector3f op2)
static inline WZ_DECL_CONST Vector3f Vector3f_CrossP(const Vector3f op1, const Vector3f op2)
{
Vector3f dest = {
op1.y * op2.z - op1.z * op2.y,
@ -350,8 +330,7 @@ static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
* \param v Vector
* \return Length
*/
static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
float Vector3f_Length(const Vector3f v)
static inline WZ_DECL_CONST float Vector3f_Length(const Vector3f v)
{
return sqrtf( Vector3f_ScalarP(v, v) );
}
@ -362,8 +341,7 @@ static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
* \param v Vector
* \return Normalised vector, nullvector when input was nullvector or very small
*/
static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
Vector3f Vector3f_Normalise(const Vector3f v)
static inline WZ_DECL_CONST Vector3f Vector3f_Normalise(const Vector3f v)
{
float length = Vector3f_Length(v);
@ -383,8 +361,7 @@ static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
/*!
* \return true if both vectors are equal
*/
static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
BOOL Vector3i_Compare(const Vector3i a, const Vector3i b)
static inline WZ_DECL_CONST BOOL Vector3i_Compare(const Vector3i a, const Vector3i b)
{
return a.x == b.x && a.y == b.y && a.z == b.z;
}
@ -395,8 +372,7 @@ static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
* \param op1,op2 Operands
* \return Result
*/
static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
Vector3i Vector3i_Sub(const Vector3i op1, const Vector3i op2)
static inline WZ_DECL_CONST Vector3i Vector3i_Sub(const Vector3i op1, const Vector3i op2)
{
Vector3i dest = {
op1.x - op2.x,
@ -413,8 +389,7 @@ static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
* \param s Scalar
* \return Product
*/
static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
Vector3i Vector3i_Mult(const Vector3i v, const int s)
static inline WZ_DECL_CONST Vector3i Vector3i_Mult(const Vector3i v, const int s)
{
Vector3i dest = { v.x * s, v.y * s, v.z * s };
return dest;
@ -426,8 +401,7 @@ static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
* \param op1,op2 Operands
* \return Scalarproduct of the 2 vectors
*/
static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
unsigned int Vector3i_ScalarP(const Vector3i op1, const Vector3i op2)
static inline WZ_DECL_CONST unsigned int Vector3i_ScalarP(const Vector3i op1, const Vector3i op2)
{
return op1.x * op2.x + op1.y * op2.y + op1.z * op2.z;
}
@ -438,8 +412,7 @@ static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
* \param v Vector
* \return Length
*/
static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
float Vector3i_Length(const Vector3i v)
static inline WZ_DECL_CONST float Vector3i_Length(const Vector3i v)
{
return sqrtf( Vector3i_ScalarP(v, v) );
}
@ -450,8 +423,7 @@ static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
* \param v Vector
* \return Normalised vector, nullvector when input was nullvector or very small
*/
static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
Vector3i Vector3i_Normalise(const Vector3i v)
static inline WZ_DECL_CONST Vector3i Vector3i_Normalise(const Vector3i v)
{
float length = Vector3i_Length(v);
@ -479,8 +451,7 @@ static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
* \param r The radius of the sphere
* \return If v falls within the sphere
*/
static inline WZ_DECL_CONST WZ_DECL_WARN_UNUSED_RESULT
BOOL Vector3i_InSphere (const Vector3i v, const Vector3i c, const unsigned int r)
static inline WZ_DECL_CONST BOOL Vector3i_InSphere (const Vector3i v, const Vector3i c, const unsigned int r)
{
Vector3i delta = Vector3i_Sub(v, c);
// Explictily cast to "unsigned int" because this number never can be