Leaked source code of windows server 2003
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.

410 lines
14 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. Module Name:
  4. ntrtlbuffer2.h
  5. Abstract:
  6. Author:
  7. Jay Krell (JayKrell) January 2002
  8. Environment:
  9. Revision History:
  10. --*/
  11. #ifndef _NTRTL_BUFFER2_
  12. #define _NTRTL_BUFFER2_
  13. #if _MSC_VER >= 1100
  14. #pragma once
  15. #endif
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. #if defined (_MSC_VER)
  20. #if ( _MSC_VER >= 800 )
  21. #pragma warning(disable:4514)
  22. #if _MSC_VER >= 1200
  23. #pragma warning(push)
  24. #endif
  25. #pragma warning(disable:4001)
  26. #pragma warning(disable:4201)
  27. #pragma warning(disable:4214)
  28. #endif
  29. #endif
  30. //
  31. // Don't use NTSYSAPI directly so you can more easily
  32. // statically link to these functions, independently
  33. // of how you link to the rest of ntdll.
  34. //
  35. #if !defined(RTL_BUFFER2_API)
  36. #define RTL_BUFFER2_API NTSYSAPI
  37. #endif
  38. typedef struct _RTL_BUFFER2 {
  39. PVOID O_p_a_q_u_e[8];
  40. } RTL_BUFFER2, *PRTL_BUFFER2;
  41. typedef const RTL_BUFFER2 * PCRTL_BUFFER2;
  42. #define RTL_BUFFER2_WITH_STATIC_BUFFER(nbytes) \
  43. struct { \
  44. RTL_BUFFER2 Buffer; \
  45. BYTE StaticBuffer[nbytes]; \
  46. }
  47. //
  48. // this then begs for all the nt/win32 flavors..
  49. //
  50. #define RtlInitBuffer2WithStaticBuffer(b, c) \
  51. RtlInitBuffer2((b), (c), (b)->StaticBuffer, sizeof((b)->StaticBuffer))
  52. RTL_BUFFER2_API
  53. NTSTATUS
  54. NTAPI
  55. RtlInitBuffer2(
  56. PRTL_BUFFER2 Buffer,
  57. struct _RTL_BUFFER_CLASS * Class,
  58. PVOID StaticBuffer,
  59. SIZE_T StaticBufferSize
  60. );
  61. RTL_BUFFER2_API
  62. VOID
  63. FASTCALL
  64. RtlFreeBuffer2(
  65. PRTL_BUFFER2 Buffer
  66. );
  67. RTL_BUFFER2_API
  68. NTSTATUS
  69. FASTCALL
  70. RtlEnsureBufferSize2(
  71. PRTL_BUFFER2 Buffer,
  72. SIZE_T Size
  73. );
  74. RTL_BUFFER2_API
  75. NTSTATUS
  76. NTAPI
  77. RtlEnsureBufferSizeEx2(
  78. PRTL_BUFFER2 Buffer,
  79. SIZE_T Size
  80. OUT PVOID * p
  81. );
  82. RTL_BUFFER2_API
  83. SIZE_T
  84. FASTCALL
  85. RtlGetAllocatedBufferSize2(
  86. PRTL_BUFFER2 Buffer
  87. );
  88. RTL_BUFFER2_API
  89. SIZE_T
  90. FASTCALL
  91. RtlGetRequestedBufferSize2(
  92. PRTL_BUFFER2 Buffer
  93. );
  94. RTL_BUFFER2_API
  95. PVOID
  96. FASTCALL
  97. RtlGetBuffer2(
  98. PRTL_BUFFER2 Buffer
  99. );
  100. /*NOT IMPLEMENTED*/#define RTL_BUFFER2_CLASS_FLAGS_IS_ARRAY (0x00000001)
  101. /*NOT IMPLEMENTED*/#define RTL_BUFFER2_CLASS_FLAGS_IS_NUL_TERMINATED_STRING (0x00000002)
  102. /*CONSIDER*/#define RTL_BUFFER2_CLASS_FLAGS_IS_FREE_PRESERVE_LAST_ERROR (0x00000004)
  103. //
  104. // string routines is the recommended default, and works in usermode and kernelmode
  105. // in usermode, it actually is equivalent to RTL_BUFFER2_CLASS_ALLOCATOR_NTRTL_PROCESS_HEAP
  106. //
  107. #define RTL_BUFFER2_CLASS_ALLOCATOR_NTRTL_STRING_CALLBACKS (0x00000001)
  108. #define RTL_BUFFER2_CLASS_ALLOCATOR_IMALLOC (0x00000002)
  109. #define RTL_BUFFER2_CLASS_ALLOCATOR_NTRTL_HEAP_CALLBACKS (0x00000004)
  110. #define RTL_BUFFER2_CLASS_ALLOCATOR_WIN32_HEAP_CALLBACKS (0x00000008)
  111. #define RTL_BUFFER2_CLASS_ALLOCATOR_CRT_CALLBACKS (0x00000010)
  112. #define RTL_BUFFER2_CLASS_ALLOCATOR_POOL_CALLBACKS (0x00000020)
  113. //
  114. // doubling is the recommended default
  115. //
  116. #define RTL_BUFFER2_CLASS_ALLOCATE_SIZE_DOUBLING (0x00000001)
  117. #define RTL_BUFFER2_CLASS_ALLOCATE_SIZE_ONLY_NEEDED (0x00000002)
  118. #define RTL_BUFFER2_CLASS_ALLOCATE_SIZE_FACTOR (0x00000004)
  119. #define RTL_BUFFER2_CLASS_ALLOCATE_SIZE_CALLBACK (0x00000008)
  120. #define RTL_BUFFER2_CLASS_ERROR_NULL (0x00000001)
  121. #define RTL_BUFFER2_CLASS_ERROR_NULL_SETLASTWIN32ERROR_CALLBACK (0x00000002)
  122. #define RTL_BUFFER2_CLASS_ERROR_NULL_CALLBACK (0x00000004)
  123. #define RTL_BUFFER2_CLASS_ERROR_WIN32_RAISEEXCEPTION_CALLBACK (0x00000008)
  124. #define RTL_BUFFER2_CLASS_ERROR_NTRTL_RAISESTATUS_CALLBACK (0x00000010)
  125. struct IMallocVtbl;
  126. typedef struct IMallocVtbl IMallocVtbl;
  127. #if defined(interface)
  128. interface IMalloc;
  129. typedef interface IMalloc IMalloc;
  130. #else
  131. struct IMalloc;
  132. typedef struct IMalloc IMalloc;
  133. #endif
  134. //
  135. // ntrtlbuffer2 has almost no static dependencies
  136. // most linkage is via callbacks provided by the client
  137. // the callbacks are meant to be link compatible with several
  138. // widely available libraries, like ntoskrnl.exe, ntdll.dll, kernel32.dll, msvcrt.dll
  139. // working in the boot environment is TBD
  140. //
  141. #define RtlBuffer2InitCommon(c) do { \
  142. (c)->ClassSize = sizeof(*(c)); \
  143. (c)->AllocationSizeType = RTL_BUFFER2_CLASS_ALLOCATE_SIZE_DOUBLING; /* may be superceded by client */ \
  144. (c)->ErrorType = RTL_BUFFER2_CLASS_ERROR_NULL; /* may be superceded by client */ \
  145. } while(0)
  146. #define RtlBuffer2InitWin32(c) do { \
  147. RtlBuffer2InitCommon(c); \
  148. (c)->AllocatorType = RTL_BUFFER2_CLASS_ALLOCATOR_WIN32_HEAP_CALLBACKS; \
  149. (c)->uAllocator.Win32Heap.HeapHandle = GetProcessHeap(); /* may be superceded by client */ \
  150. (c)->uAllocator.Win32Heap.Allocate = HeapAlloc; \
  151. (c)->uAllocator.Win32Heap.Reallocate = HeapReAlloc; \
  152. (c)->uAllocator.Win32Heap.Free = HeapFree; \
  153. } while(0)
  154. #define RtlBuffer2InitCommonNrtlHeap() do { \
  155. RtlBuffer2InitCommon(c); \
  156. (c)->AllocatorType = RTL_BUFFER2_CLASS_ALLOCATOR_NTRTL_HEAP_CALLBACKS; \
  157. (c)->uAllocator.NtrtlHeap.Allocate = RtlAllocateHeap; \
  158. (c)->uAllocator.NtrtlHeap.Free = RtlFreeHeap; \
  159. } while(0)
  160. #define RtlBuffer2InitUsermodeNrtlHeap() do { \
  161. RtlBuffer2InitCommonNrtl(c); \
  162. (c)->uAllocator.NtrtlHeap.HeapHandle = RtlProcessHeap(); \
  163. (c)->uAllocator.NtrtlHeap.Reallocate = RtlReAllocateHeap; \
  164. } while(0)
  165. #define RtlBuffer2InitNtdllHeap(c) RtlBuffer2InitUsermodeNrtlHeap(c)
  166. #define RtlBuffer2InitKernelmodeNtrtlheap(c) do { \
  167. RtlBuffer2InitCommonNrtl(c); \
  168. (c)->uAllocator.NtrtlHeap.HeapHandle = NULL; /* must be superceded by client */ \
  169. } while(0)
  170. #define RtlBuffer2InitKernelmodeNtrtlStringRoutines(c) do { \
  171. RtlBuffer2InitCommon(c); \
  172. (c)->AllocatorType = RTL_BUFFER2_CLASS_ALLOCATOR_NTRTL_STRING_CALLBACKS; \
  173. (c)->uAllocator.NtrtlStringRoutines.Allocate = RtlAllocateStringRoutine; \
  174. (c)->uAllocator.NtrtlStringRoutines.Free = RtlFreeStringRoutine; \
  175. } while(0)
  176. #define RtlBuffer2InitCommonNtpool() do { \
  177. RtlBuffer2InitCommon(c); \
  178. (c)->AllocatorType = RTL_BUFFER2_CLASS_ALLOCATOR_POOL_CALLBACKS; \
  179. (c)->uAllocator.Pool.Tag = 0; /* may be superceded by client */ \
  180. (c)->uAllocator.Pool.Priority = 0; /* may be superceded by client */ \
  181. (c)->uAllocator.Pool.Type = 0; /* may be superceded by client */ \
  182. } while(0)
  183. #ifndef POOL_TAGGING
  184. #define RtlBuffer2InitNtpool() do { \
  185. RtlBuffer2InitCommonNtpool(c); \
  186. (c)->uAllocator.Pool.Allocate = ExAllocatePool; \
  187. (c)->uAllocator.Pool.Free = ExFreePool; \
  188. (c)->uAllocator.Pool.ExAllocatePoolWithQuota = ExAllocatePoolWithQuota;
  189. } while(0)
  190. #else
  191. #define RtlBuffer2InitNtpool() do { \
  192. RtlBuffer2InitCommonNtpool(c); \
  193. (c)->uAllocator.Pool.AllocateWithTag = ExAllocatePoolWithTag; \
  194. (c)->uAllocator.Pool.FreeWithTag = ExFreePoolWithTag; \
  195. (c)->uAllocator.Pool.ExAllocatePoolWithQuotaTag = ExAllocatePoolWithQuotaTag;
  196. } while(0)
  197. #endif
  198. #define RtlBuffer2InitCrt() do { \
  199. RtlBuffer2InitCommon(c); \
  200. (c)->AllocatorType = RTL_BUFFER2_CLASS_ALLOCATOR_WIN32_CRT_CALLBACKS; \
  201. (c)->uAllocator.Crt.Allocate = malloc; \
  202. (c)->uAllocator.Crt.Reallocate = realloc; \
  203. (c)->uAllocator.Crt.Free = free; \
  204. } while(0)
  205. #define RtlBuffer2InitIMalloc(c, im) do { \
  206. RtlBuffer2InitCommon(c); \
  207. (c)->AllocatorType = RTL_BUFFER2_CLASS_ALLOCATOR_IMALLOC; \
  208. (c)->uAllocator.Malloc = (im); \
  209. } while(0)
  210. typedef struct _RTL_BUFFER2_CLASS_NTRTL_HEAP {
  211. PVOID HeapHandle;
  212. PVOID (NTAPI * Allocate)(PVOID Heap, ULONG Flags, SIZE_T Size);
  213. BOOLEAN (NTAPI * Free)(PVOID Heap, ULONG Flags, LPVOID p);
  214. // realloc can be null, like in kernelmode
  215. PVOID (NTAPI * Reallocate)(PVOID Heap, ULONG Flags, PVOID p, SIZE_T Size);
  216. } RTL_BUFFER2_CLASS_NTRTL_HEAP, *PRTL_BUFFER2_CLASS_NTRTL_HEAP;
  217. typedef struct _RTL_BUFFER2_CLASS_NTKERNEL_LESS_TYPED_POOL {
  218. ULONG Tag;
  219. ULONG Type;
  220. ULONG Priority;
  221. PVOID (NTAPI * Allocate)(IN ULONG PoolType, IN SIZE_T NumberOfBytes);
  222. PVOID (NTAPI * AllocateWithQuota)(IN ULONG PoolType, IN SIZE_T NumberOfBytes);
  223. PVOID (NTAPI * AllocateWithTag)(IN ULONG PoolType, IN SIZE_T NumberOfBytes, IN ULONG Tag);
  224. PVOID (NTAPI * AllocateWithQuotaTag)(IN ULONG PoolType, IN SIZE_T NumberOfBytes, IN ULONG Tag);
  225. PVOID (NTAPI * AllocateWithTagPriority)(IN ULONG PoolType, IN SIZE_T NumberOfBytes, IN ULONG Tag, IN ULONG Priority);
  226. VOID (NTAPI * Free)(PVOID p);
  227. VOID (NTAPI * FreeWithTag)(PVOID p, ULONG Tag);
  228. } RTL_BUFFER2_CLASS_NTKERNEL_LESS_TYPED_POOL, *PRTL_BUFFER2_CLASS_NTKERNEL_LESS_TYPED_POOL;
  229. #if defined(_EX_) || defined(_NTDDK_) || defined(_NTIFS_) || defined(_NTOSP_) || defined(_WDM_) || defined(_NTHAL_)
  230. typedef struct _RTL_BUFFER2_CLASS_NTKERNEL_POOL {
  231. ULONG Tag;
  232. POOL_TYPE Type;
  233. EX_POOL_PRIORITY Priority;
  234. //
  235. // fill in whichever pointers you want, like, whichever isn't a macro..
  236. //
  237. PVOID (NTAPI * Allocate)(IN POOL_TYPE PoolType, IN SIZE_T NumberOfBytes);
  238. PVOID (NTAPI * AllocateWithQuota)(IN POOL_TYPE PoolType, IN SIZE_T NumberOfBytes);
  239. PVOID (NTAPI * AllocateWithTag)(IN POOL_TYPE PoolType, IN SIZE_T NumberOfBytes, IN ULONG Tag);
  240. PVOID (NTAPI * AllocateWithQuotaTag)(IN POOL_TYPE PoolType, IN SIZE_T NumberOfBytes, IN ULONG Tag);
  241. PVOID (NTAPI * AllocateWithTagPriority)(IN POOL_TYPE PoolType, IN SIZE_T NumberOfBytes, IN ULONG Tag, IN EX_POOL_PRIORITY Priority);
  242. VOID (NTAPI * Free)(PVOID p);
  243. VOID (NTAPI * FreeWithTag)(PVOID p, ULONG Tag);
  244. } RTL_BUFFER2_CLASS_NTKERNEL_POOL, *PRTL_BUFFER2_CLASS_NTKERNEL_POOL;
  245. #else
  246. typedef RTL_BUFFER2_CLASS_NTKERNEL_LESS_TYPED_POOL RTL_BUFFER2_CLASS_NTKERNEL_POOL;
  247. typedef PRTL_BUFFER2_CLASS_NTKERNEL_LESS_TYPED_POOL PRTL_BUFFER2_CLASS_NTKERNEL_POOL;
  248. #endif
  249. typedef struct _RTL_BUFFER2_CLASS_WIN32_HEAP {
  250. HANDLE HeapHandle;
  251. LPVOID (WINAPI * Allocate)(HANDLE Heap, DWORD Flags, SIZE_T Size);
  252. BOOL (WINAPI * Free)(HANDLE Heap, DWORD Flags, LPVOID p);
  253. PVOID (WINAPI * Reallocate)(HANDLE Heap, DWORD Flags, LPVOID p, SIZE_T Size);
  254. } RTL_BUFFER2_CLASS_WIN32_HEAP, *PRTL_BUFFER2_CLASS_WIN32_HEAP;
  255. typedef struct _RTL_BUFFER2_CLASS_CRT {
  256. void * (__cdecl * Alloc)(size_t);
  257. void (__cdecl * Free)(void *);
  258. void * (__cdecl * Reallocate)(void *, size_t);
  259. } RTL_BUFFER2_CLASS_CRT, *PRTL_BUFFER2_CLASS_CRT;
  260. typedef struct _RTL_BUFFER2_CLASS_NTRTL_STRING_ROUTINES {
  261. PVOID (NTAPI * Allocate) (SIZE_T NumberOfBytes);
  262. VOID (NTAPI * Free) (PVOID Buffer);
  263. } RTL_BUFFER2_CLASS_NTRTL_STRING_ROUTINES, *PRTL_BUFFER2_CLASS_NTRTL_STRING_ROUTINES;
  264. #if defined(RTL_BUFFER2_CLASS_DEFAULT_IS_NTRTL_HEAP)
  265. #define RTL_BUFFER2_CLASS_UALLOCATOR_FIRST RTL_BUFFER2_CLASS_NTRTL_HEAP First;
  266. #define RTL_BUFFER2_CLASS_UALLOCATOR_STATIC_INIT { }
  267. ..
  268. .. constness falls apart on the point of getting the process heap..
  269. .. should be "design" more around the const non const axis wrt
  270. .. readonly memory, or only wrt when init calls are made?
  271. ..
  272. #endif
  273. typedef union _RTL_BUFFER2_CLASS_UALLOCATOR {
  274. #if defined(RTL_BUFFER2_CLASS_UALLOCATOR_FIRST)
  275. // This is intended to be an aid to static and/or const static initialization.
  276. RTL_BUFFER2_CLASS_UALLOCATOR_FIRST
  277. #endif
  278. IMalloc * Malloc;
  279. RTL_BUFFER2_CLASS_NTRTL_HEAP NtrtlHeap;
  280. RTL_BUFFER2_CLASS_NTKERNEL_POOL NtkernelPool;
  281. RTL_BUFFER2_CLASS_NTKERNEL_LESS_TYPED_POOL NtkernelLessTypedPool;
  282. RTL_BUFFER2_CLASS_WIN32_HEAP Win32Heap;
  283. RTL_BUFFER2_CLASS_CRT Crt;
  284. RTL_BUFFER2_CLASS_NTRTL_STRING_ROUTINES NtrtlStringRoutines;
  285. PVOID Pad[8];
  286. } RTL_BUFFER2_CLASS_UALLOCATOR, *PRTL_BUFFER2_CLASS_UALLOCATOR;
  287. typedef const RTL_BUFFER2_CLASS_UALLOCATOR * PCRTL_BUFFER2_CLASS_UALLOCATOR;
  288. #ifndef SORTPP_PASS
  289. C_ASSERT(sizeof(RTL_BUFFER2_CLASS_NTKERNEL_LESS_TYPED_POOL) >= sizeof(RTL_BUFFER2_CLASS_NTKERNEL_POOL));
  290. C_ASSERT(RTL_FIELD_SIZE(RTL_BUFFER2_CLASS_UALLOCATOR, Pad) > sizeof(RTL_BUFFER2_CLASS_UALLOCATOR));
  291. #endif
  292. typedef struct _RTL_BUFFER2_CLASS {
  293. ULONG ClassSize;
  294. ULONG Flags;
  295. ULONG ArrayElementSize;
  296. ULONG AllocatorType;
  297. RTL_BUFFER2_CLASS_UALLOCATOR uAllocator;
  298. ULONG AllocationSizeType;
  299. union {
  300. struct {
  301. SIZE_T Multiply;
  302. SIZE_T Divide;
  303. } Factor;
  304. struct {
  305. PVOID Context;
  306. SIZE_T (__stdcall * GetRecommendedAllocationSize)(
  307. PVOID Context,
  308. SIZE_T CurrentAllocationSize,
  309. SIZE_T RequiredAllocationSize
  310. );
  311. } Callback;
  312. } uAllocationSize;
  313. ULONG ErrorType;
  314. union {
  315. struct {
  316. // This could use fleshing out..like PVOID Context, PCSTR File, ULONG Line,
  317. // SIZE_T Size, etc..
  318. VOID (__stdcall * OutOfMemory)(VOID);
  319. } Callbacks;
  320. struct {
  321. VOID (NTAPI * RaiseStatus)(NTSTATUS Status);
  322. NTSTATUS OutOfMemoryStatus;
  323. } RtlRaise;
  324. struct {
  325. VOID (WINAPI * RaiseException)(DWORD ExceptionCode, DWORD ExceptionFlags, DWORD NumberOfArguments, CONST ULONG_PTR * Arguments);
  326. DWORD OutOfMemoryExceptionCode;
  327. } Win32Raise;
  328. } uError;
  329. /* CONSIDER
  330. struct {
  331. DWORD (WINAPI * GetLastErrorCallback)(VOID);
  332. VOID (WINAPI * SetLastErrorCallback)(DWORD Error);
  333. } FreePreserveLastError;
  334. */
  335. } RTL_BUFFER2_CLASS, *RTL_BUFFER2_CLASS;
  336. #endif
  337. #ifdef __cplusplus
  338. } // extern "C"
  339. #endif
  340. #if defined (_MSC_VER) && ( _MSC_VER >= 800 )
  341. #if _MSC_VER >= 1200
  342. #pragma warning(pop)
  343. #else
  344. #pragma warning(default:4001)
  345. #pragma warning(default:4201)
  346. #pragma warning(default:4214)
  347. #endif
  348. #endif
  349. #endif