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.

378 lines
12 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. Module Name:
  4. ntrtlbuffer3.h
  5. Abstract:
  6. Author:
  7. Jay Krell (JayKrell) January 2002
  8. Environment:
  9. Revision History:
  10. --*/
  11. #if _MSC_VER > 1000
  12. #pragma once
  13. #endif
  14. //
  15. // RTLBUFFER3_USERMODE -- usermode, not necessarily linked into ntdll.dll
  16. // RTLBUFFER3_KERNELMODE -- kernelmode, not necessarily linked into ntoskrnl.exe
  17. // RTLBUFFER3_NTDLL -- usermode, linked into ntdll.dll
  18. // RTLBUFFER3_NTKERNEL -- kernelmode, linked into ntoskrnl.exe
  19. // (if ntdll.dll and ntoskrnl.exe would only export the publically declared string routines...)
  20. //
  21. // RTLBUFFER3_BOOT -- TBD
  22. //
  23. #if defined(_NTDLLBUILD_)
  24. #define RTLBUFFER3_NTDLL 1
  25. #define RTLBUFFER3_USERMODE 1
  26. #endif
  27. #if defined(NTOS_KERNEL_RUNTIME)
  28. #define RTLBUFFER3_NTKERNEL 1
  29. #define RTLBUFFER3_KERNELMODE 1
  30. #endif
  31. #if defined(RTLBUFFER3_USERMODE) && !defined(RTLBUFFER3_KERNELMODE)
  32. #define RTLBUFFER3_KERNELMODE (!RTLBUFFER3_USERMODE)
  33. #endif
  34. #if !defined(RTLBUFFER3_USERMODE) && defined(RTLBUFFER3_KERNELMODE)
  35. #define RTLBUFFER3_USERMODE (!RTLBUFFER3_KERNELMODE)
  36. #endif
  37. #if !defined(RTLBUFFER3_NTDLL)
  38. #define RTLBUFFER3_NTDLL 0
  39. #endif
  40. #if !defined(RTLBUFFER3_NTKERNEL)
  41. #define RTLBUFFER3_NTKERNEL 0
  42. #endif
  43. #if RTLBUFFER3_NTKERNEL && RTLBUFFER3_NTDLL
  44. #error 1
  45. #endif
  46. #if RTLBUFFER3_NTKERNEL && RTLBUFFER3_USERMODE
  47. #error 2
  48. #endif
  49. #if RTLBUFFER3_KERNELMODE && RTLBUFFER3_NTDLL
  50. #error 3
  51. #endif
  52. #if RTLBUFFER3_KERNELMODE && RTLBUFFER3_USERMODE
  53. #error 4
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57. #include "nt.h"
  58. #include "ntrtl.h"
  59. #if RTLBUFFER3_USERMODE
  60. #include "nturtl.h"
  61. #include "windows.h"
  62. #endif
  63. //
  64. // This costs an extra heap allocation.
  65. //
  66. typedef struct _RTL_MINI_DYNAMIC_BYTE_BUFFER3 {
  67. ULONG_PTR O_p_a_q_u_e[1];
  68. } RTL_MINI_DYNAMIC_BYTE_BUFFER3, *PRTL_MINI_DYNAMIC_BYTE_BUFFER3;
  69. typedef const RTL_MINI_DYNAMIC_BYTE_BUFFER3 * PCRTL_MINI_DYNAMIC_BYTE_BUFFER3;
  70. typedef struct _RTL_BYTE_BUFFER3 {
  71. ULONG_PTR O_p_a_q_u_e[9];
  72. } RTL_BYTE_BUFFER3, *PRTL_BYTE_BUFFER3;
  73. typedef const RTL_BYTE_BUFFER3 * PCRTL_BYTE_BUFFER3;
  74. typedef struct _RTL_BUFFER3_ALLOCATOR {
  75. BOOL (FASTCALL * CanAllocate)(PVOID Context); // eg: false for FixedSizeAllocator
  76. PVOID (FASTCALL * Allocate)(PVOID Context, SIZE_T NumberOfBytes);
  77. VOID (FASTCALL * Free)(PVOID Context, PVOID Pointer);
  78. BOOL (FASTCALL * CanReallocate)(PVOID Context); // eg: false for NtkernelPoolAllocator
  79. PVOID (FASTCALL * Reallocate)(PVOID Context, PVOID OldPointer, SIZE_T NewSize);
  80. SIZE_T (FASTCALL * GetAllocationSize)(PVOID Context, SIZE_T CurrentAllocatedSize, SIZE_T RequiredSize);
  81. } RTL_BUFFER3_ALLOCATOR, *PRTL_BUFFER3_ALLOCATOR;
  82. typedef const RTL_BUFFER3_ALLOCATOR *PCRTL_BUFFER3_ALLOCATOR;
  83. typedef enum _RTL_BUFFER3_RETURN {
  84. RTL_BUFFER3_SUCCESS = 0,
  85. RTL_BUFFER3_INVALID_PARAMETER = 1,
  86. RTL_BUFFER3_INTERNAL_ERROR = 2,
  87. RTL_BUFFER3_CALLBACK_ERROR = 3
  88. } RTL_BUFFER3_RETURN;
  89. //
  90. // These are only "hints" for debugging, and to
  91. // establish "causality" from the macro calls.
  92. //
  93. #define RTL_INIT_BYTE_BUFFER3_FLAGS_PREALLOCATED 0x80000000
  94. #define RTL_INIT_BYTE_BUFFER3_FLAGS_FIXED_SIZE 0x40000000
  95. #define RTL_INIT_BYTE_BUFFER3_FLAGS_DYNAMIC 0x20000000
  96. RTL_BUFFER3_RETURN
  97. FASTCALL
  98. RtlInitByteBuffer3(
  99. ULONG Flags,
  100. PRTL_BYTE_BUFFER3 Buffer,
  101. SIZE_T SizeofBufferForBinaryCompatibilityPurposes,
  102. PBYTE StaticBuffer,
  103. SIZE_T StaticBufferSize,
  104. PCRTL_BUFFER3_ALLOCATOR Allocator,
  105. PVOID AllocatorContext
  106. );
  107. #define RTL_PREALLOCATED_BYTE_BUFFER3(n) \
  108. struct {
  109. RTL_BYTE_BUFFER3 Buffer; \
  110. BYTE StaticBuffer[n]; \
  111. }
  112. #define RtlInitPreallocatedByteBuffer3(flags, buf, a, ac) \
  113. (RtlInitByteBuffer3((flags) | RTL_INIT_BYTE_BUFFER3_FLAGS_PREALLOCATED, &(buf)->Buffer, sizeof(&(buf)->Buffer), (buf)->StaticBuffer, sizeof((buf)->StaticBuffer), (a), (ac)))
  114. #define RtlInitFixedSizeByteBuffer3(flags, buf, statbuf, statbufsiz) \
  115. (RtlInitByteBuffer3((flags) | RTL_INIT_BYTE_BUFFER3_FLAGS_FIXED_SIZE, (buf), sizeof(*(buf)), (statbuf), (statbufsiz), NULL, NULL))
  116. #define RtlInitDynamicByteBuffer3(flags), buf, a, ac) \
  117. (RtlInitByteBuffer3((flags) | RTL_INIT_BYTE_BUFFER3_FLAGS_DYNAMIC, (buf), sizeof(buf), NULL, 0, (a), (ac)))
  118. PVOID
  119. FASTCALL
  120. RtlByteBuffer3GetBuffer(
  121. PRTL_BYTE_BUFFER3 Buffer
  122. );
  123. //
  124. // There is both a "requested" size and an "allocated" size.
  125. // requested <= allocated.
  126. // This returns requested. Allocated is not available.
  127. //
  128. SIZE_T
  129. FASTCALL
  130. RtlByteBuffer3GetSize(
  131. PRTL_BYTE_BUFFER3 Buffer
  132. );
  133. RTL_BUFFER3_RETURN
  134. FASTCALL
  135. RtlByteBuffer3EnsureSize(
  136. PRTL_BYTE_BUFFER3 Buffer,
  137. SIZE_T Size,
  138. OUT PVOID * OutBuffer OPTIONAL
  139. );
  140. RTL_BUFFER3_RETURN
  141. FASTCALL
  142. RtlByteBuffer3AppendBuffer(
  143. PRTL_BYTE_BUFFER3 ToBuffer,
  144. PCRTL_BYTE_BUFFER3 FromBuffer,
  145. );
  146. RTL_BUFFER3_RETURN
  147. FASTCALL
  148. RtlByteBuffer3PrependBuffer(
  149. PRTL_BYTE_BUFFER3 ToBuffer,
  150. PCRTL_BYTE_BUFFER3 FromBuffer,
  151. );
  152. RTL_BUFFER3_RETURN
  153. FASTCALL
  154. RtlByteBuffer3InsertBuffer(
  155. PRTL_BYTE_BUFFER3 ToBuffer,
  156. PCRTL_BYTE_BUFFER3 FromBuffer,
  157. SIZE_T Offset
  158. );
  159. RTL_BUFFER3_RETURN
  160. FASTCALL
  161. RtlByteBuffer3CopyBuffer(
  162. PRTL_BYTE_BUFFER3 ToBuffer,
  163. PCRTL_BYTE_BUFFER3 FromBuffer,
  164. );
  165. VOID
  166. FASTCALL
  167. RtlByteBuffer3TakeRemainingStaticBuffer(
  168. PRTL_BYTE_BUFFER3 Buffer,
  169. OUT PBYTE * RemainingStaticBuffer,
  170. OUT SIZE_T * RemainingStaticBufferSize
  171. );
  172. RTL_BUFFER3_RETURN
  173. FASTCALL
  174. RtlFreeByteBuffer3(
  175. PRTL_BYTE_BUFFER3 Buffer
  176. );
  177. BOOL FASTCALL RtlBuffer3Allocator_CanAllocate_False(PVOID Context);
  178. BOOL FASTCALL RtlBuffer3Allocator_CanAllocate_True(PVOID Context);
  179. BOOL FASTCALL RtlBuffer3Allocator_CanReallocate_False(PVOID Context);
  180. BOOL FASTCALL RtlBuffer3Allocator_CanRellocate_True(PVOID Context);
  181. //
  182. // can be left as all zeros, or even a NULL context
  183. // defaults: heap=GetProcessHeap(), flags=0, SetLastError(ERROR_NO_MEMORY)
  184. //
  185. typedef struct _RTL_BUFFER3_ALLOCATOR_WIN32HEAP {
  186. BOOL UsePrivateHeap;
  187. BOOL DoNotSetLastError;
  188. BOOL UsePrivateError;
  189. HANDLE PrivateHeap;
  190. DWORD HeapFlags;
  191. DWORD PrivateOutOfMemoryError;
  192. } RTL_BUFFER3_ALLOCATOR_WIN32HEAP, *PRTL_BUFFER3_ALLOCATOR_WIN32HEAP;
  193. typedef const RTL_BUFFER3_ALLOCATOR_WIN32HEAP * PCRTL_BUFFER3_ALLOCATOR_WIN32HEAP;
  194. PVOID FASTCALL RtlBuffer3Allocator_Win32HeapAllocate(PVOID VoidContext, SIZE_T NumberOfBytes);
  195. VOID FASTCALL RtlBuffer3Allocator_Win32HeapFree(PVOID VoidContext, PVOID Pointer);
  196. PVOID FASTCALL RtlBuffer3Allocator_Win32HeapReallocate(PVOID Context, PVOID OldPointer, SIZE_T NewSize);
  197. SIZE_T FASTCALL RtlBuffer3Allocator_FixedAllocationSize(PVOID Context, SIZE_T CurrentAllocatedSize, SIZE_T RequiredSize);
  198. SIZE_T FASTCALL RtlBuffer3Allocator_DoublingAllocationSize(PVOID Context, SIZE_T CurrentAllocatedSize, SIZE_T RequiredSize);
  199. SIZE_T FASTCALL RtlBuffer3Allocator_MinimumAlocationSize(PVOID Context, SIZE_T CurrentAllocatedSize, SIZE_T RequiredSize);
  200. #define RtlBuffer3Allocator_Win32Heap \
  201. RtlBuffer3_Allocator_CanAllocate_True, \
  202. RtlBuffer3Allocator_Win32HeapAllocate, \
  203. RtlBuffer3Allocator_Win32HeapFree, \
  204. RtlBuffer3_Allocator_CanReallocate_True, \
  205. RtlBuffer3Allocator_Win32HeapReallocate \
  206. /* user specifies allocationsize */
  207. //
  208. // works in kernelmode and usermode, but kernelmode cannot realloc or use process heap
  209. // context optional in usermode
  210. // defaults: heap=RtlProcessHeap(), flags=0
  211. //
  212. typedef struct _RTL_BUFFER3_ALLOCATOR_NTHEAP {
  213. BOOL UsePrivateHeap; /* Must be true in kernelmode. */
  214. HANDLE PrivateHeap;
  215. ULONG HeapFlags;
  216. } RTL_BUFFER3_ALLOCATOR_NTHEAP, *PRTL_BUFFER3_ALLOCATOR_NTHEAP;
  217. typedef const RTL_BUFFER3_ALLOCATOR_NTHEAP * PCRTL_BUFFER3_ALLOCATOR_NTHEAP;
  218. PVOID FASTCALL RtlBuffer3Allocator_NtHeapHeapAllocate(PVOID VoidContext, SIZE_T NumberOfBytes);
  219. VOID FASTCALL RtlBuffer3Allocator_NtHeapFree(PVOID VoidContext, PVOID Pointer);
  220. PVOID FASTCALL RtlBuffer3Allocator_NtHeapReallocate(PVOID Context, PVOID OldPointer, SIZE_T NewSize);
  221. #define RtlBuffer3Allocator_NtHeapKernelMode \
  222. RtlBuffer3_Allocator_CanAllocate_True, \
  223. RtlBuffer3Allocator_NtHeapAllocate, \
  224. RtlBuffer3Allocator_NtHeapFree, \
  225. RtlBuffer3_Allocator_CanReallocate_False, \
  226. NULL \
  227. /* user specifies allocationsize */
  228. #define RtlBuffer3Allocator_NtHeapUserMode \
  229. RtlBuffer3_Allocator_CanAllocate_True, \
  230. RtlBuffer3Allocator_NtHeapAllocate, \
  231. RtlBuffer3Allocator_NtHeapFree, \
  232. RtlBuffer3_Allocator_CanReallocate_True, \
  233. RtlBuffer3Allocator_NtHeapReallocate \
  234. /* user specifies allocationsize */
  235. #if defined(RTLBUFFER3_KERNELMODE)
  236. #define RtlBuffer3Allocator_NtHeap RtlBuffer3Allocator_NtHeapKernelMode
  237. #else
  238. #define RtlBuffer3Allocator_NtHeap RtlBuffer3Allocator_NtHeapUserMode
  239. #endif
  240. //
  241. // works in kernelmode and usermode
  242. // no context
  243. // realloc in usermode could be provided
  244. //
  245. PVOID FASTCALL RtlBuffer3Allocator_NtStringAllocate(PVOID VoidContext, SIZE_T NumberOfBytes);
  246. VOID FASTCALL RtlBuffer3Allocator_NtStringFree(PVOID VoidContext, PVOID Pointer);
  247. //PVOID FASTCALL RtlBuffer3Allocator_NtStringReallocate(PVOID Context, PVOID OldPointer, SIZE_T NewSize);
  248. #define RtlBuffer3Allocator_NtStringRoutines \
  249. RtlBuffer3_Allocator_CanAllocate_True, \
  250. RtlBuffer3Allocator_NtStringAllocate, \
  251. RtlBuffer3Allocator_NtStringFree, \
  252. RtlBuffer3_Allocator_CanReallocate_False, \
  253. NULL \
  254. /* user specifies allocationsize */
  255. //
  256. // no context
  257. //
  258. PVOID FASTCALL RtlBuffer3Allocator_CrtMalloc(PVOID VoidContext, SIZE_T NumberOfBytes);
  259. VOID FASTCALL RtlBuffer3Allocator_CrtFree(PVOID VoidContext, PVOID Pointer);
  260. PVOID FASTCALL RtlBuffer3Allocator_CrtReallocate(PVOID Context, PVOID OldPointer, SIZE_T NewSize);
  261. #define RtlBuffer3Allocator_CrtMallocFree \
  262. RtlBuffer3_Allocator_CanAllocate_True, \
  263. RtlBuffer3Allocator_CrtMalloc, \
  264. RtlBuffer3Allocator_CrtFree, \
  265. RtlBuffer3_Allocator_CanReallocate_True, \
  266. RtlBuffer3Allocator_CrtReallocate \
  267. /* user specifies allocationsize */
  268. //
  269. // no context
  270. // no realloc
  271. //
  272. PVOID FASTCALL RtlBuffer3Allocator_OperatorNew(PVOID VoidContext, SIZE_T NumberOfBytes);
  273. VOID FASTCALL RtlBuffer3Allocator_OperatorDelete(PVOID VoidContext, PVOID Pointer);
  274. #define RtlBuffer3Allocator_OperatorNewDelete \
  275. RtlBuffer3_Allocator_CanAllocate_True, \
  276. RtlBuffer3Allocator_OperatorNew, \
  277. RtlBuffer3Allocator_OperatorDelete, \
  278. RtlBuffer3_Allocator_CanReallocate_False, \
  279. NULL \
  280. /* user specifies allocationsize */
  281. //
  282. //
  283. // context optional
  284. // defaults are: tag=0, type=nonpaged, priority=normal
  285. // no realloc
  286. // kernelmode only
  287. //
  288. #if defined(_EX_) || defined(_NTDDK_) || defined(_NTIFS_) || defined(_NTOSP_) || defined(_WDM_) || defined(_NTHAL_)
  289. typedef enum _RTL_BUFFER3_ALLOCATOR_NTKERNELPOOL_ALLOCATE {
  290. RTL_BUFFER3_ALLOCATOR_NTKERNELPOOL_ALLOCATE_EX_ALLOCATE_POOL,
  291. RTL_BUFFER3_ALLOCATOR_NTKERNELPOOL_ALLOCATE_EX_ALLOCATE_POOL_WITH_TAG,
  292. RTL_BUFFER3_ALLOCATOR_NTKERNELPOOL_ALLOCATE_EX_ALLOCATE_POOL_WITH_QUOTA,
  293. RTL_BUFFER3_ALLOCATOR_NTKERNELPOOL_ALLOCATE_EX_ALLOCATE_POOL_WITH_QUOTA_TAG,
  294. RTL_BUFFER3_ALLOCATOR_NTKERNELPOOL_ALLOCATE_EX_ALLOCATE_POOL_WITH_TAG_PRIORITY,
  295. } RTL_BUFFER3_ALLOCATOR_NTKERNELPOOL_ALLOCATE;
  296. typedef struct _RTL_BUFFER3_ALLOCATOR_NTKERNELPOOL {
  297. RTL_BUFFER3_ALLOCATOR_NTKERNELPOOL_ALLOCATE WhichFunction;
  298. ULONG Tag;
  299. POOL_TYPE Type;
  300. EX_POOL_PRIORITY Priority;
  301. } RTL_BUFFER3_ALLOCATOR_NTKERNELPOOL, *PRTL_BUFFER3_ALLOCATOR_NTKERNELPOOL;
  302. typedef const RTL_BUFFER3_ALLOCATOR_NTKERNELPOOL * PCRTL_BUFFER3_ALLOCATOR_NTKERNELPOOL;
  303. #endif
  304. PVOID FASTCALL RtlBuffer3Allocator_NtkernelPoolAllocate(PVOID VoidContext, SIZE_T NumberOfBytes);
  305. VOID FASTCALL RtlBuffer3Allocator_NtKernelPoolFree(PVOID VoidContext, PVOID Pointer);
  306. #define RtlBuffer3Allocator_NtkernelPool \
  307. RtlBuffer3Allocator_CanAllocate_True, \
  308. RtlBuffer3Allocator_NtkernelPoolAllocate, \
  309. RtlBuffer3Allocator_NtKernelPoolFree, \
  310. RtlBuffer3Allocator_CanReallocate_False, \
  311. NULL \
  312. /* user specifies allocationsize */
  313. /* TBD
  314. NtkernelTextmodeSetup
  315. NtbootLoader
  316. */
  317. #ifdef __cplusplus
  318. }
  319. #endif