always.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Command & Conquer *
  23. * *
  24. * $Archive:: /Commando/Code/wwlib/always.h $*
  25. * *
  26. * $Author:: Steve_t $*
  27. * *
  28. * $Modtime:: 8/28/01 3:21p $*
  29. * *
  30. * $Revision:: 13 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if _MSC_VER >= 1000
  36. #pragma once
  37. #endif // _MSC_VER >= 1000
  38. #ifndef ALWAYS_H
  39. #define ALWAYS_H
  40. #include <assert.h>
  41. // Disable warning about exception handling not being enabled. It's used as part of STL - in a part of STL we don't use.
  42. #pragma warning(disable : 4530)
  43. /*
  44. ** Define for debug memory allocation to include __FILE__ and __LINE__ for every memory allocation.
  45. ** This helps find leaks.
  46. */
  47. //#define STEVES_NEW_CATCHER
  48. #ifdef _DEBUG
  49. #ifdef _MSC_VER
  50. #ifdef STEVES_NEW_CATCHER
  51. #include <crtdbg.h>
  52. #include <stdlib.h>
  53. #include <malloc.h>
  54. #define malloc(s) _malloc_dbg(s, _NORMAL_BLOCK, __FILE__, __LINE__)
  55. #define calloc(c, s) _calloc_dbg(c, s, _NORMAL_BLOCK, __FILE__, __LINE__)
  56. #define realloc(p, s) _realloc_dbg(p, s, _NORMAL_BLOCK, __FILE__, __LINE__)
  57. #define _expand(p, s) _expand_dbg(p, s, _NORMAL_BLOCK, __FILE__, __LINE__)
  58. #define free(p) _free_dbg(p, _NORMAL_BLOCK)
  59. #define _msize(p) _msize_dbg(p, _NORMAL_BLOCK)
  60. void* __cdecl operator new(unsigned int s);
  61. #endif //STEVES_NEW_CATCHER
  62. #endif //_MSC_VER
  63. #endif //_DEBUG
  64. #ifndef _OPERATOR_NEW_DEFINED_
  65. #define _OPERATOR_NEW_DEFINED_
  66. extern void * __cdecl operator new (size_t size);
  67. extern void __cdecl operator delete (void *p);
  68. extern void * __cdecl operator new[] (size_t size);
  69. extern void __cdecl operator delete[] (void *p);
  70. // additional overloads to account for VC/MFC funky versions
  71. extern void* __cdecl operator new (size_t nSize, const char *, int);
  72. extern void __cdecl operator delete (void *, const char *, int);
  73. extern void* __cdecl operator new[] (size_t nSize, const char *, int);
  74. extern void __cdecl operator delete[] (void *, const char *, int);
  75. // additional overloads for 'placement new'
  76. //inline void* __cdecl operator new (size_t s, void *p) { return p; }
  77. //inline void __cdecl operator delete (void *, void *p) { }
  78. inline void* __cdecl operator new[] (size_t s, void *p) { return p; }
  79. inline void __cdecl operator delete[] (void *, void *p) { }
  80. #endif
  81. #if (defined(_DEBUG) || defined(_INTERNAL))
  82. #define MSGW3DNEW(MSG) new( MSG, 0 )
  83. #define MSGW3DNEWARRAY(MSG) new( MSG, 0 )
  84. #define W3DNEW new("W3D_" __FILE__, 0)
  85. #define W3DNEWARRAY new("W3A_" __FILE__, 0)
  86. #else
  87. #define MSGW3DNEW(MSG) new
  88. #define MSGW3DNEWARRAY(MSG) new
  89. #define W3DNEW new
  90. #define W3DNEWARRAY new
  91. #endif
  92. // ----------------------------------------------------------------------------
  93. extern void* createW3DMemPool(const char *poolName, int allocationSize);
  94. extern void* allocateFromW3DMemPool(void* p, int allocationSize);
  95. extern void* allocateFromW3DMemPool(void* p, int allocationSize, const char* msg, int unused);
  96. extern void freeFromW3DMemPool(void* pool, void* p);
  97. // ----------------------------------------------------------------------------
  98. #define W3DMPO_GLUE(ARGCLASS) \
  99. private: \
  100. static void* getClassMemoryPool() \
  101. { \
  102. /* \
  103. Note that this static variable will be initialized exactly once: the first time \
  104. control flows over this section of code. This allows us to neatly resolve the \
  105. order-of-execution problem for static variables, ensuring this is not executed \
  106. prior to the initialization of TheMemoryPoolFactory. \
  107. */ \
  108. static void* The##ARGCLASS##Pool = createW3DMemPool(#ARGCLASS, sizeof(ARGCLASS)); \
  109. return The##ARGCLASS##Pool; \
  110. } \
  111. protected: \
  112. virtual int glueEnforcer() const { return sizeof(this); } \
  113. public: \
  114. inline void* operator new(size_t s) { return allocateFromW3DMemPool(getClassMemoryPool(), s); } \
  115. inline void operator delete(void *p) { freeFromW3DMemPool(getClassMemoryPool(), p); } \
  116. inline void* operator new(size_t s, const char* msg, int unused) { return allocateFromW3DMemPool(getClassMemoryPool(), s, msg, unused); } \
  117. inline void operator delete(void *p, const char* msg, int unused) { freeFromW3DMemPool(getClassMemoryPool(), p); } \
  118. // ----------------------------------------------------------------------------
  119. class W3DMPO
  120. {
  121. private:
  122. static void* getClassMemoryPool()
  123. {
  124. assert(0); // must replace this via W3DMPO_GLUE
  125. return 0;
  126. }
  127. protected:
  128. // we never call this; it is present to cause compile errors in descendent classes
  129. virtual int glueEnforcer() const = 0;
  130. public:
  131. virtual ~W3DMPO() { /* nothing */ }
  132. };
  133. // ----------------------------------------------------------------------------
  134. // Jani: Intel's C++ compiler issues too many warnings in WW libraries when using warning level 4
  135. #if defined (__ICL) // Detect Intel compiler
  136. #pragma warning (3)
  137. #pragma warning ( disable: 981 ) // parameters defined in unspecified order
  138. #pragma warning ( disable: 279 ) // controlling expressaion is constant
  139. #pragma warning ( disable: 271 ) // trailing comma is nonstandard
  140. #pragma warning ( disable: 171 ) // invalid type conversion
  141. #pragma warning ( disable: 1 ) // last line of file ends without a newline
  142. #endif
  143. // Jani: MSVC doesn't necessarily inline code with inline keyword. Using __forceinline results better inlining
  144. // and also prints out a warning if inlining wasn't possible. __forceinline is MSVC specific.
  145. #if defined(_MSC_VER)
  146. #define WWINLINE __forceinline
  147. #else
  148. #define WWINLINE inline
  149. #endif
  150. /*
  151. ** Define the MIN and MAX macros.
  152. ** NOTE: Joe used to #include <minmax.h> in the various compiler header files. This
  153. ** header defines 'min' and 'max' macros which conflict with the surrender code so
  154. ** I'm relpacing all occurances of 'min' and 'max with 'MIN' and 'MAX'. For code which
  155. ** is out of our domain (e.g. Max sdk) I'm declaring template functions for 'min' and 'max'
  156. */
  157. #define NOMINMAX
  158. #ifndef MAX
  159. #define MAX(a,b) (((a) > (b)) ? (a) : (b))
  160. #endif
  161. #ifndef MIN
  162. #define MIN(a,b) (((a) < (b)) ? (a) : (b))
  163. #endif
  164. #ifdef min
  165. #undef min
  166. #endif
  167. #ifdef max
  168. #undef max
  169. #endif
  170. template <class T> T min(T a,T b)
  171. {
  172. if (a<b) {
  173. return a;
  174. } else {
  175. return b;
  176. }
  177. }
  178. template <class T> T max(T a,T b)
  179. {
  180. if (a>b) {
  181. return a;
  182. } else {
  183. return b;
  184. }
  185. }
  186. /*
  187. ** This includes the minimum set of compiler defines and pragmas in order to bring the
  188. ** various compilers to a common behavior such that the C&C engine will compile without
  189. ** error or warning.
  190. */
  191. #if defined(__BORLANDC__)
  192. #include "borlandc.h"
  193. #endif
  194. #if defined(_MSC_VER)
  195. #include "visualc.h"
  196. #endif
  197. #if defined(__WATCOMC__)
  198. #include "watcom.h"
  199. #endif
  200. #ifndef NULL
  201. #define NULL 0
  202. #endif
  203. /**********************************************************************
  204. ** This macro serves as a general way to determine the number of elements
  205. ** within an array.
  206. */
  207. #ifndef ARRAY_SIZE
  208. #define ARRAY_SIZE(x) int(sizeof(x)/sizeof(x[0]))
  209. #endif
  210. #ifndef size_of
  211. #define size_of(typ,id) sizeof(((typ*)0)->id)
  212. #endif
  213. #endif