Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

533 lines
13 KiB

  1. /*++
  2. Copyright (c) 1990-2000 Microsoft Corporation
  3. Module Name:
  4. ulibdef.hxx
  5. Abstract:
  6. This module contains primitive support for the ULIB class hierarchy
  7. and it's clients. This support includes:
  8. - type definitions
  9. - manifest constants
  10. - debugging support
  11. - memory leakage support
  12. - external references
  13. Environment:
  14. ULIB, User Mode
  15. --*/
  16. #if ! defined( _ULIBDEF_ )
  17. #define _ULIBDEF_
  18. #if !defined( _EFICHECK_ )
  19. extern "C" {
  20. #include <stdlib.h>
  21. };
  22. #endif
  23. // The ULIB_EXPORT macro marks functions that are to be
  24. // exported. The source files for ULIB.DLL will have _ULIB_MEMBER_
  25. // defined; clients will not.
  26. //
  27. #if defined ( _AUTOCHECK_ ) || defined( _EFICHECK_ )
  28. #define ULIB_EXPORT
  29. #elif defined ( _ULIB_MEMBER_ )
  30. #define ULIB_EXPORT __declspec(dllexport)
  31. #else
  32. #define ULIB_EXPORT __declspec(dllimport)
  33. #endif
  34. #pragma warning(disable:4091) // Symbol not defined
  35. //
  36. // Macros for defining types:
  37. //
  38. // - pointers (Ptype)
  39. // - pointers to constants (PCtype)
  40. // - references (Rtype)
  41. // - references to constants (RCtype)
  42. //
  43. #define DEFINE_POINTER_TYPES( type ) \
  44. typedef type* P##type; \
  45. typedef const type* PC##type
  46. #define DEFINE_REFERENCE_TYPES( type ) \
  47. typedef type& R##type; \
  48. typedef const type& RC##type
  49. #define DEFINE_POINTER_AND_REFERENCE_TYPES( type ) \
  50. DEFINE_POINTER_TYPES( type ); \
  51. DEFINE_REFERENCE_TYPES( type )
  52. #define DEFINE_TYPE( basetype, newtype ) \
  53. typedef basetype newtype; \
  54. DEFINE_POINTER_AND_REFERENCE_TYPES( newtype )
  55. #define DECLARE_CLASS( c ) \
  56. class c; \
  57. DEFINE_POINTER_AND_REFERENCE_TYPES( c );\
  58. extern PCCLASS_DESCRIPTOR c##_cd
  59. //
  60. // Primitive types.
  61. //
  62. DEFINE_TYPE( unsigned char, UCHAR );
  63. DEFINE_TYPE( unsigned short, USHORT );
  64. DEFINE_TYPE( unsigned long, ULONG );
  65. DEFINE_TYPE( char, CCHAR );
  66. DEFINE_TYPE( short, CSHORT );
  67. DEFINE_TYPE( ULONG, CLONG );
  68. DEFINE_TYPE( short, SSHORT );
  69. DEFINE_TYPE( long, SLONG );
  70. DEFINE_TYPE( UCHAR, BYTE );
  71. DEFINE_TYPE( char, STR );
  72. DEFINE_TYPE( UINT8, BOOLEAN );
  73. DEFINE_TYPE( int, INT );
  74. DEFINE_TYPE( unsigned int, UINT );
  75. DEFINE_TYPE(UINT16, WCHAR );
  76. typedef WCHAR *LPWCH; // pwc
  77. typedef WCHAR *LPWSTR; // pwsz, 0x0000 terminated UNICODE strings only
  78. DEFINE_POINTER_AND_REFERENCE_TYPES( WCHAR );
  79. DEFINE_TYPE( WCHAR, WSTR );
  80. //DEFINE_TYPE( struct tagLC_ID, LC_ID );
  81. //
  82. // Augmented (beyond standard headers) VOID pointer types
  83. //
  84. DEFINE_POINTER_TYPES( VOID );
  85. //
  86. // Ulib specific, primitive types
  87. //
  88. DEFINE_TYPE( STR, CLASS_NAME );
  89. DEFINE_TYPE( ULONG_PTR, CLASS_ID );
  90. //
  91. // Member and non-member function/data modifiers
  92. //
  93. #define CONST const
  94. #define NONVIRTUAL
  95. #define PURE = 0
  96. #define STATIC static
  97. #define VIRTUAL virtual
  98. #define INLINE inline
  99. // #define INLINE // hack to build for mips
  100. #define FRIEND friend
  101. //
  102. // Argument modifiers
  103. //
  104. #define DEFAULT =
  105. #define IN
  106. #define OPTIONAL
  107. #define OUT
  108. #define INOUT
  109. #if !defined(max)
  110. #define max(a,b) (((a) > (b)) ? (a) : (b) )
  111. #endif
  112. #if !defined(min)
  113. #define min(a,b) (((a) < (b)) ? (a) : (b) )
  114. #endif
  115. #if DBG==1
  116. #define REGISTER
  117. #else
  118. #define REGISTER register
  119. #endif // DBG
  120. //
  121. // External constants
  122. //
  123. extern CONST CLASS_ID NIL_CLASS_ID;
  124. //
  125. // GetFileName returns the file name portion of the supplied path name.
  126. //
  127. #if !defined(_EFICHECK_)
  128. // can't use CRT under EFI.
  129. extern "C" {
  130. #include <string.h>
  131. };
  132. inline
  133. PCCHAR
  134. GetFileName (
  135. IN PCCHAR PathName
  136. )
  137. {
  138. PCCHAR pch;
  139. return((( pch = strrchr( PathName, '\\' )) != NULL ) ?
  140. pch + 1 : PathName );
  141. }
  142. #endif
  143. //
  144. // Cast (beProtocol) support
  145. //
  146. // If the ID of the passed object is equal to the ID in this class'
  147. // CLASS_DESCRIPTOR, then the object is of this type and the Cast succeeds
  148. // (i.e. returns Object) otherwise the Cast fails (i.e. returns NULL)
  149. //
  150. #define DECLARE_CAST_MEMBER_FUNCTION( type ) \
  151. STATIC \
  152. P##type \
  153. Cast ( \
  154. PCOBJECT Object \
  155. )
  156. #define DEFINE_CAST_MEMBER_FUNCTION( type ) \
  157. P##type \
  158. type::Cast ( \
  159. PCOBJECT Object \
  160. ) \
  161. { \
  162. if( Object && ( Object->QueryClassId( ) == \
  163. type##_cd->QueryClassId( ))) { \
  164. return(( P##type ) Object ); \
  165. } else { \
  166. return NULL; \
  167. } \
  168. }
  169. #define DEFINE_EXPORTED_CAST_MEMBER_FUNCTION( type, export ) \
  170. P##type \
  171. export \
  172. type::Cast ( \
  173. PCOBJECT Object \
  174. ) \
  175. { \
  176. if( Object && ( Object->QueryClassId( ) == \
  177. type##_cd->QueryClassId( ))) { \
  178. return(( P##type ) Object ); \
  179. } else { \
  180. return NULL; \
  181. } \
  182. }
  183. //
  184. // Constructor support
  185. //
  186. //
  187. // All classes have CLASS_DESCRIPTORS which are static and named
  188. // after the class appended with the suffix _cd. They are passed stored in
  189. // OBJECT and are set by the SetClassDescriptor function. The Construct
  190. // For debugging purposes the class' name is stored in the CLASS_DESCRIPTOR.
  191. // The Construct member function gas a no-op implementation in OBJECT and
  192. // could be overloaded as a private member in any derived class.
  193. //
  194. #define DECLARE_CONSTRUCTOR( c ) \
  195. NONVIRTUAL \
  196. c ( \
  197. )
  198. #define DEFINE_CONSTRUCTOR( d, b ) \
  199. PCCLASS_DESCRIPTOR d##_cd; \
  200. d::d ( \
  201. ) : b( ) \
  202. { \
  203. SetClassDescriptor( ##d##_cd ); \
  204. Construct( ); \
  205. }
  206. #define DEFINE_EXPORTED_CONSTRUCTOR( d, b, e ) \
  207. PCCLASS_DESCRIPTOR d##_cd; \
  208. e d::d ( \
  209. ) : b( ) \
  210. { \
  211. SetClassDescriptor( ##d##_cd ); \
  212. Construct( ); \
  213. }
  214. //
  215. // Debug support.
  216. //
  217. // Use the Debug macros to invoke the following debugging functions.
  218. //
  219. // DebugAbort( str ) - Print a message and abort.
  220. // DebugAssert( exp ) - Assert that an expression is TRUE. Abort if FALSE.
  221. // DebugChkHeap( ) - Validate the heap. Abort if invalid.
  222. // DebugPrint( str ) - Print a string including location (file/line)
  223. // DebugPrintTrace(fmt, ...) - Printf.
  224. //
  225. #if defined(_AUTOCHECK_DBG_) && defined( _EFICHECK_ )
  226. #include <efidebug.h>
  227. #define EFICHK_DBUG_MASK EFI_DBUG_MASK
  228. #define DebugAbort( str ) \
  229. DbgAssert((CHAR8*)__FILE__,__LINE__,(CHAR8*)#str)
  230. #define DebugAssert( exp ) \
  231. if (!(exp)) { DbgAssert((CHAR8*)__FILE__,__LINE__, (CHAR8*)#exp ); }
  232. #define DebugCheckHeap( )
  233. #define DebugPrint( str ) \
  234. DbgPrint( D_ERROR,(CHAR8*)str)
  235. #define DebugPrintTrace( M )
  236. // DebugPrintfReal M // this doesn't work
  237. extern "C" {
  238. ULIB_EXPORT
  239. VOID
  240. __cdecl
  241. DebugPrintfReal(
  242. IN char * Format,
  243. IN ...
  244. );
  245. }
  246. #define DebugPtrAssert( ptr ) \
  247. DebugAssert( ptr != NULL )
  248. //
  249. // UlibGlobalFlag is used to selectively enable debugging options at
  250. // run-time.
  251. //
  252. extern ULONG UlibGlobalFlag;
  253. #elif DBG==1
  254. #include <assert.h>
  255. #define DebugAbort( str ) \
  256. DebugPrint( str ); \
  257. ExitProcess( 99 )
  258. #define DebugAssert( exp ) \
  259. assert( exp )
  260. #define DebugCheckHeap( )
  261. #define DebugPrint( str ) \
  262. DebugPrintfReal ( "%s in file: %s on line: %d.\n", \
  263. str, \
  264. __FILE__, \
  265. __LINE__ )
  266. #define DebugPrintTrace( M ) DebugPrintfReal M
  267. ULIB_EXPORT
  268. VOID
  269. DebugPrintfReal(
  270. IN PCSTR Format,
  271. IN ...
  272. );
  273. #define DebugPtrAssert( ptr ) \
  274. DebugAssert( ptr != NULL )
  275. //
  276. // UlibGlobalFlag is used to selectively enable debugging options at
  277. // run-time.
  278. //
  279. extern ULONG UlibGlobalFlag;
  280. #elif defined( _AUTOCHECK_DBG_ )
  281. // Autocheck uses the system's DbgPrint function.
  282. //
  283. #define DebugPrint DbgPrint
  284. #define DebugPrintTrace( M ) DbgPrint M
  285. #define DebugAbort( str )
  286. #define DebugAssert( exp )
  287. #define DebugCheckHeap( )
  288. #define DebugPtrAssert( ptr )
  289. #else // DBG == 0 and _AUTOCHECK_DBG_ not defined.
  290. #define DebugAbort( str )
  291. #define DebugAssert( exp )
  292. #define DebugCheckHeap( )
  293. #define DebugPrint( str )
  294. #define DebugPrintTrace( M )
  295. #define DebugPtrAssert( ptr )
  296. #endif // DBG
  297. //
  298. // DELETE macro that NULLifizes the pointer to the deleted object.
  299. //
  300. // Undo any previous definitions of DELETE.
  301. #undef DELETE
  302. // #define DELETE(x) FREE(x), x = NULL;
  303. #define DELETE( x ) \
  304. delete x, x = NULL
  305. #define DELETE_ARRAY( x ) \
  306. delete [] x, x = NULL
  307. #define NEW new
  308. #define DumpStats
  309. #if defined( _EFICHECK_ )
  310. #include <efi.h>
  311. #include <efilib.h>
  312. #define MALLOC(bytes) (AllocatePool(bytes))
  313. INLINE
  314. PVOID
  315. NtlibZeroAlloc(
  316. ULONG Size
  317. )
  318. {
  319. PVOID Result;
  320. Result = MALLOC( Size );
  321. if( Result != NULL ) {
  322. memset( Result, 0, Size );
  323. }
  324. return Result;
  325. }
  326. #define CALLOC(nitems, size) (NtlibZeroAlloc(nitems*size))
  327. ULIB_EXPORT PVOID UlibRealloc(PVOID, ULONG);
  328. // this REALLOC won't work for EFI's heap -- it needs the old size.
  329. // #define REALLOC(p,s) UlibRealloc(p,s)
  330. #define FREE(x) ((x) ? (FreePool(x), (x) = NULL) : 0)
  331. #elif defined( _AUTOCHECK_ )
  332. ULIB_EXPORT PVOID AutoChkMalloc(ULONG);
  333. #define MALLOC(bytes) (AutoChkMalloc(bytes))
  334. INLINE
  335. PVOID
  336. NtlibZeroAlloc(
  337. ULONG Size
  338. )
  339. {
  340. PVOID Result;
  341. Result = MALLOC( Size );
  342. if( Result != NULL ) {
  343. memset( Result, 0, Size );
  344. }
  345. return Result;
  346. }
  347. #define CALLOC(nitems, size) (NtlibZeroAlloc(nitems*size))
  348. ULIB_EXPORT PVOID UlibRealloc(PVOID, ULONG);
  349. #define REALLOC(p,s) UlibRealloc(p,s)
  350. ULIB_EXPORT VOID AutoChkMFree(PVOID);
  351. #define FREE(x) ((x) ? (AutoChkMFree(x), (x) = NULL) : 0)
  352. #else // _AUTOCHECK_ not defined
  353. #if defined( _NTAPI_ULIB_ )
  354. #define MALLOC(bytes) (RtlAllocateHeap(RtlProcessHeap(), 0, bytes))
  355. #define CALLOC(nitems, size) \
  356. (RtlAllocateHeap(RtlProcessHeap(), 0, nitems*size))
  357. ULIB_EXPORT PVOID UlibRealloc(PVOID, ULONG);
  358. #define REALLOC(p,s) UlibRealloc(p,s)
  359. #define FREE(x) ((x) ? (RtlFreeHeap(RtlProcessHeap(), 0, x), (x) = NULL) : 0)
  360. #else
  361. #define MALLOC(bytes) ((PVOID) LocalAlloc(0, bytes))
  362. #define CALLOC(nitems, size) ((PVOID) LocalAlloc(LMEM_ZEROINIT, nitems*size))
  363. ULIB_EXPORT PVOID UlibRealloc(PVOID, ULONG);
  364. #define REALLOC(x, size) ((PVOID) LocalReAlloc(x, size, LMEM_MOVEABLE))
  365. #define FREE(x) ((x) ? (LocalFree(x), (x) = NULL) : 0)
  366. #endif
  367. #endif // _AUTOCHECK
  368. #if !defined(_SETUP_LOADER_) && !defined(_AUTOCHECK_) && !defined( _EFICHECK_ )
  369. __inline void * __cdecl operator new(size_t x)
  370. {
  371. return(MALLOC(x));
  372. }
  373. __inline void __cdecl operator delete(void * x)
  374. {
  375. FREE(x);
  376. }
  377. #endif
  378. #endif // _ULIBDEF_