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.

497 lines
12 KiB

  1. /*++
  2. Copyright (c) 1990-2001 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. Author:
  14. David J. Gilman (davegi) 19-Oct-1990
  15. Environment:
  16. ULIB, User Mode
  17. --*/
  18. #if ! defined( _ULIBDEF_ )
  19. #define _ULIBDEF_
  20. extern "C" {
  21. #include <stdlib.h>
  22. };
  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_ )
  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( UCHAR, BOOLEAN );
  73. DEFINE_TYPE( int, INT );
  74. DEFINE_TYPE( unsigned int, UINT );
  75. #if !defined(_NATIVE_WCHAR_T_DEFINED)
  76. DEFINE_TYPE(USHORT, WCHAR );
  77. #else
  78. typedef wchar_t WCHAR;
  79. #endif
  80. typedef WCHAR *LPWCH; // pwc
  81. typedef WCHAR *LPWSTR; // pwsz, 0x0000 terminated UNICODE strings only
  82. DEFINE_POINTER_AND_REFERENCE_TYPES( WCHAR );
  83. DEFINE_TYPE( WCHAR, WSTR );
  84. //DEFINE_TYPE( struct tagLC_ID, LC_ID );
  85. //
  86. // Augmented (beyond standard headers) VOID pointer types
  87. //
  88. DEFINE_POINTER_TYPES( VOID );
  89. //
  90. // Ulib specific, primitive types
  91. //
  92. DEFINE_TYPE( STR, CLASS_NAME );
  93. DEFINE_TYPE( ULONG_PTR, CLASS_ID );
  94. //
  95. // Member and non-member function/data modifiers
  96. //
  97. #define CONST const
  98. #define NONVIRTUAL
  99. #define PURE = 0
  100. #define STATIC static
  101. #define VIRTUAL virtual
  102. #define INLINE inline
  103. // #define INLINE // hack to build for mips
  104. #define FRIEND friend
  105. //
  106. // Argument modifiers
  107. //
  108. #define DEFAULT =
  109. #define IN
  110. #define OPTIONAL
  111. #define OUT
  112. #define INOUT
  113. #if !defined(max)
  114. #define max(a,b) (((a) > (b)) ? (a) : (b) )
  115. #endif
  116. #if !defined(min)
  117. #define min(a,b) (((a) < (b)) ? (a) : (b) )
  118. #endif
  119. #if DBG==1
  120. #define REGISTER
  121. #else
  122. #define REGISTER register
  123. #endif // DBG
  124. //
  125. // External constants
  126. //
  127. extern CONST CLASS_ID NIL_CLASS_ID;
  128. //
  129. // GetFileName returns the file name portion of the supplied path name.
  130. //
  131. extern "C" {
  132. #include <string.h>
  133. };
  134. inline
  135. PCCHAR
  136. GetFileName (
  137. IN PCCHAR PathName
  138. )
  139. {
  140. PCCHAR pch;
  141. return((( pch = strrchr( PathName, '\\' )) != NULL ) ?
  142. pch + 1 : PathName );
  143. }
  144. //
  145. // Cast (beProtocol) support
  146. //
  147. // If the ID of the passed object is equal to the ID in this class'
  148. // CLASS_DESCRIPTOR, then the object is of this type and the Cast succeeds
  149. // (i.e. returns Object) otherwise the Cast fails (i.e. returns NULL)
  150. //
  151. #define DECLARE_CAST_MEMBER_FUNCTION( type ) \
  152. STATIC \
  153. P##type \
  154. Cast ( \
  155. PCOBJECT Object \
  156. )
  157. #define DEFINE_CAST_MEMBER_FUNCTION( type ) \
  158. P##type \
  159. type::Cast ( \
  160. PCOBJECT Object \
  161. ) \
  162. { \
  163. if( Object && ( Object->QueryClassId( ) == \
  164. type##_cd->QueryClassId( ))) { \
  165. return(( P##type ) Object ); \
  166. } else { \
  167. return NULL; \
  168. } \
  169. }
  170. #define DEFINE_EXPORTED_CAST_MEMBER_FUNCTION( type, export ) \
  171. P##type \
  172. export \
  173. type::Cast ( \
  174. PCOBJECT Object \
  175. ) \
  176. { \
  177. if( Object && ( Object->QueryClassId( ) == \
  178. type##_cd->QueryClassId( ))) { \
  179. return(( P##type ) Object ); \
  180. } else { \
  181. return NULL; \
  182. } \
  183. }
  184. //
  185. // Constructor support
  186. //
  187. //
  188. // All classes have CLASS_DESCRIPTORS which are static and named
  189. // after the class appended with the suffix _cd. They are passed stored in
  190. // OBJECT and are set by the SetClassDescriptor function. The Construct
  191. // For debugging purposes the class' name is stored in the CLASS_DESCRIPTOR.
  192. // The Construct member function gas a no-op implementation in OBJECT and
  193. // could be overloaded as a private member in any derived class.
  194. //
  195. #define DECLARE_CONSTRUCTOR( c ) \
  196. NONVIRTUAL \
  197. c ( \
  198. )
  199. #define DEFINE_CONSTRUCTOR( d, b ) \
  200. PCCLASS_DESCRIPTOR d##_cd; \
  201. d::d ( \
  202. ) : b( ) \
  203. { \
  204. SetClassDescriptor( ##d##_cd ); \
  205. Construct( ); \
  206. }
  207. #define DEFINE_EXPORTED_CONSTRUCTOR( d, b, e ) \
  208. PCCLASS_DESCRIPTOR d##_cd; \
  209. e d::d ( \
  210. ) : b( ) \
  211. { \
  212. SetClassDescriptor( ##d##_cd ); \
  213. Construct( ); \
  214. }
  215. //
  216. // Debug support.
  217. //
  218. // Use the Debug macros to invoke the following debugging functions.
  219. //
  220. // DebugAbort( str ) - Print a message and abort.
  221. // DebugAssert( exp ) - Assert that an expression is TRUE. Abort if FALSE.
  222. // DebugChkHeap( ) - Validate the heap. Abort if invalid.
  223. // DebugPrint( str ) - Print a string including location (file/line)
  224. // DebugPrintTrace(fmt, ...) - Printf.
  225. //
  226. #if DBG==1
  227. #define DebugAbort( str ) \
  228. DebugPrint( str ); \
  229. ExitProcess( 99 )
  230. #define DebugAssert( exp ) \
  231. ASSERT( exp )
  232. #define DebugCheckHeap( )
  233. #define DebugPrint( str ) \
  234. DebugPrintfReal ( "%s in file: %s on line: %d.\n", \
  235. str, \
  236. __FILE__, \
  237. __LINE__ )
  238. #define DebugPrintTrace( M ) DebugPrintfReal M
  239. #if defined(RUN_ON_W2K)
  240. #if defined(KdPrintEx)
  241. #undef KdPrintEx
  242. #endif
  243. #define KdPrintEx( M ) DebugPrintfReal2 M
  244. ULIB_EXPORT
  245. VOID
  246. DebugPrintfReal2(
  247. IN ULONG ComponentId,
  248. IN ULONG Level,
  249. IN PCSTR Format,
  250. IN ...
  251. );
  252. #endif
  253. ULIB_EXPORT
  254. VOID
  255. DebugPrintfReal(
  256. IN PCSTR Format,
  257. IN ...
  258. );
  259. #define DebugPtrAssert( ptr ) \
  260. ASSERT( ptr != NULL )
  261. //
  262. // UlibGlobalFlag is used to selectively enable debugging options at
  263. // run-time.
  264. //
  265. extern ULONG UlibGlobalFlag;
  266. #elif defined( _AUTOCHECK_DBG_ )
  267. // Autocheck uses the system's DbgPrint function.
  268. //
  269. #define DebugPrint DbgPrint
  270. #define DebugPrintTrace( M ) DbgPrint M
  271. #define DebugAbort( str )
  272. #define DebugAssert( exp )
  273. #define DebugCheckHeap( )
  274. #define DebugPtrAssert( ptr )
  275. #if defined(RUN_ON_W2K)
  276. #if defined(KdPrintEx)
  277. #undef KdPrintEx
  278. #endif
  279. #define KdPrintEx( M ) DebugPrintfReal2 M
  280. ULIB_EXPORT
  281. VOID
  282. DebugPrintfReal2(
  283. IN ULONG ComponentId,
  284. IN ULONG Level,
  285. IN PCSTR Format,
  286. IN ...
  287. );
  288. #endif // defined(RUN_ON_W2K)
  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( _AUTOCHECK_ )
  310. #define MAX_PATH 260
  311. ULIB_EXPORT ULONG64 AutoChkFreeSpaceLeft();
  312. ULIB_EXPORT PVOID AutoChkMalloc(ULONG);
  313. #define MALLOC(bytes) (AutoChkMalloc(bytes))
  314. INLINE
  315. PVOID
  316. NtlibZeroAlloc(
  317. ULONG Size
  318. )
  319. {
  320. PVOID Result;
  321. Result = MALLOC( Size );
  322. if( Result != NULL ) {
  323. memset( Result, 0, Size );
  324. }
  325. return Result;
  326. }
  327. #define CALLOC(nitems, size) (NtlibZeroAlloc(nitems*size))
  328. ULIB_EXPORT PVOID UlibRealloc(PVOID, ULONG);
  329. #define REALLOC(p,s) UlibRealloc(p,s)
  330. ULIB_EXPORT VOID AutoChkMFree(PVOID);
  331. #define FREE(x) ((x) ? (AutoChkMFree(x), (x) = NULL) : 0)
  332. #else // _AUTOCHECK_ not defined
  333. #if defined( _NTAPI_ULIB_ )
  334. #define MALLOC(bytes) (RtlAllocateHeap(RtlProcessHeap(), 0, bytes))
  335. #define CALLOC(nitems, size) \
  336. (RtlAllocateHeap(RtlProcessHeap(), 0, nitems*size))
  337. ULIB_EXPORT PVOID UlibRealloc(PVOID, ULONG);
  338. #define REALLOC(p,s) UlibRealloc(p,s)
  339. #define FREE(x) ((x) ? (RtlFreeHeap(RtlProcessHeap(), 0, x), (x) = NULL) : 0)
  340. #else
  341. #define MALLOC(bytes) ((PVOID) LocalAlloc(0, bytes))
  342. #define CALLOC(nitems, size) ((PVOID) LocalAlloc(LMEM_ZEROINIT, nitems*size))
  343. ULIB_EXPORT PVOID UlibRealloc(PVOID, ULONG);
  344. #define REALLOC(x, size) ((PVOID) LocalReAlloc(x, size, LMEM_MOVEABLE))
  345. #define FREE(x) ((x) ? (LocalFree(x), (x) = NULL) : 0)
  346. #endif
  347. #endif // _AUTOCHECK
  348. #if !defined(_SETUP_LOADER_) && !defined(_AUTOCHECK_)
  349. __inline void * __cdecl operator new(size_t x)
  350. {
  351. return(MALLOC(x));
  352. }
  353. __inline void __cdecl operator delete(void * x)
  354. {
  355. FREE(x);
  356. }
  357. #endif
  358. #endif // _ULIBDEF_