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.

184 lines
7.2 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. alloca.h
  5. Abstract:
  6. This module implements a safe stack-based allocator with fallback to the heap.
  7. Author:
  8. Jonathan Schwartz (JSchwart) 16-Mar-2001
  9. Revision History:
  10. --*/
  11. #ifndef _SAFEALLOCA_H_
  12. #define _SAFEALLOCA_H_
  13. #ifdef __cplusplus
  14. extern "C"
  15. {
  16. #endif // __cplusplus
  17. #include <align.h> // ALIGN_WORST
  18. //
  19. // Type definitions
  20. //
  21. typedef ULONG SAFEALLOCA_HEADER;
  22. typedef PVOID (APIENTRY *SAFEALLOC_ALLOC_PROC)(
  23. SIZE_T Size
  24. );
  25. typedef VOID (APIENTRY *SAFEALLOC_FREE_PROC)(
  26. PVOID BaseAddress
  27. );
  28. //
  29. // Constant definitions
  30. //
  31. #define SAFEALLOCA_STACK_HEADER ((SAFEALLOCA_HEADER) 0x6b637453) /* "Stck" */
  32. #define SAFEALLOCA_HEAP_HEADER ((SAFEALLOCA_HEADER) 0x70616548) /* "Heap" */
  33. #define SAFEALLOCA_USE_DEFAULT 0xdeadbeef
  34. //
  35. // We'll be adding ALIGN_WORST bytes to the allocation size to add room for
  36. // the SAFEALLOCA_HEADER -- make sure we'll always have enough space.
  37. //
  38. C_ASSERT(sizeof(SAFEALLOCA_HEADER) <= ALIGN_WORST);
  39. //
  40. // Per-DLL SafeAlloca globals
  41. //
  42. extern SIZE_T g_ulMaxStackAllocSize;
  43. extern SIZE_T g_ulAdditionalProbeSize;
  44. extern SAFEALLOC_ALLOC_PROC g_pfnAllocate;
  45. extern SAFEALLOC_FREE_PROC g_pfnFree;
  46. //
  47. // Functions defined in alloca.lib
  48. //
  49. VOID
  50. SafeAllocaInitialize(
  51. IN OPTIONAL SIZE_T ulMaxStackAllocSize,
  52. IN OPTIONAL SIZE_T ulAdditionalProbeSize,
  53. IN OPTIONAL SAFEALLOC_ALLOC_PROC pfnAllocate,
  54. IN OPTIONAL SAFEALLOC_FREE_PROC pfnFree
  55. );
  56. BOOL
  57. VerifyStackAvailable(
  58. SIZE_T Size
  59. );
  60. //
  61. // Usage:
  62. //
  63. // VOID
  64. // SafeAllocaAllocate(
  65. // PVOID PtrVar,
  66. // SIZE_T BlockSize
  67. // );
  68. //
  69. // (PtrVar == NULL) on failure
  70. //
  71. #define SafeAllocaAllocate(PtrVar, BlockSize) \
  72. \
  73. { \
  74. PVOID *ppvAvoidCast = (PVOID *) &(PtrVar); \
  75. \
  76. (PtrVar) = NULL; \
  77. \
  78. /* Make sure block is below the threshhold and that the probe won't overflow */ \
  79. \
  80. if ((BlockSize) <= g_ulMaxStackAllocSize \
  81. && \
  82. ((BlockSize) + g_ulAdditionalProbeSize >= (BlockSize))) \
  83. { \
  84. if (VerifyStackAvailable((BlockSize) \
  85. + g_ulAdditionalProbeSize \
  86. + ALIGN_WORST)) \
  87. { \
  88. /* \
  89. * Don't need to wrap with try-except since we just probed \
  90. */ \
  91. \
  92. *ppvAvoidCast = _alloca((BlockSize) + ALIGN_WORST); \
  93. } \
  94. \
  95. if ((PtrVar) != NULL) \
  96. { \
  97. *((SAFEALLOCA_HEADER *) (PtrVar)) = SAFEALLOCA_STACK_HEADER; \
  98. *ppvAvoidCast = ((LPBYTE) (PtrVar) + ALIGN_WORST); \
  99. } \
  100. } \
  101. \
  102. /* \
  103. * Stack allocation failed -- try the heap \
  104. */ \
  105. \
  106. if ((PtrVar) == NULL) \
  107. { \
  108. *ppvAvoidCast = g_pfnAllocate((BlockSize) + ALIGN_WORST); \
  109. \
  110. if ((PtrVar) != NULL) \
  111. { \
  112. *((SAFEALLOCA_HEADER *) (PtrVar)) = SAFEALLOCA_HEAP_HEADER; \
  113. *ppvAvoidCast = ((LPBYTE) (PtrVar) + ALIGN_WORST); \
  114. } \
  115. } \
  116. }
  117. //
  118. // Usage:
  119. //
  120. // VOID
  121. // SafeAllocaFree(
  122. // PVOID PtrVar,
  123. // );
  124. //
  125. #define SafeAllocaFree(PtrVar) \
  126. \
  127. if (PtrVar != NULL) \
  128. { \
  129. SAFEALLOCA_HEADER *Tag = (SAFEALLOCA_HEADER *) ((LPBYTE) (PtrVar) \
  130. - ALIGN_WORST); \
  131. \
  132. if (*(SAFEALLOCA_HEADER *) (Tag) == SAFEALLOCA_HEAP_HEADER) \
  133. { \
  134. g_pfnFree(Tag); \
  135. } \
  136. else \
  137. { \
  138. ASSERT(*(SAFEALLOCA_HEADER *) (Tag) == SAFEALLOCA_STACK_HEADER); \
  139. } \
  140. }
  141. #ifdef __cplusplus
  142. }
  143. #endif // __cplusplus
  144. #endif // _SAFEALLOCA_H_