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.

157 lines
3.2 KiB

  1. /**************************************************************************************************
  2. FILENAME: Alloc.h
  3. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  4. **************************************************************************************************/
  5. #ifndef _ALLOC_H_
  6. #define _ALLOC_H_
  7. //Allocates or reallocates a memory block automatically handling locking of the pointer etc.
  8. BOOL
  9. AllocateMemory(
  10. IN DWORD SizeInBytes,
  11. IN OUT PHANDLE phMemory,
  12. IN OUT PVOID* ppMemory
  13. );
  14. struct _SA_CONTEXT;
  15. struct _SLAB_HEADER;
  16. struct _PACKET_HEADER;
  17. //
  18. // This is the general slab allocator context.
  19. //
  20. typedef struct _SA_CONTEXT {
  21. //
  22. // Doubly linked list of allocated slabs
  23. //
  24. struct _SLAB_HEADER *pAllocatedSlabs;
  25. //
  26. // We keep up to two freed slabs lying around
  27. //
  28. struct _SLAB_HEADER *pFreeSlabs;
  29. //
  30. // Size of a packet in bytes
  31. //
  32. DWORD dwPacketSize;
  33. //
  34. // Size of a slab in bytes, usually a multiple of the page size.
  35. //
  36. DWORD dwSlabSize;
  37. //
  38. // Packets per slab
  39. //
  40. DWORD dwPacketsPerSlab;
  41. } SA_CONTEXT, *PSA_CONTEXT;
  42. //
  43. // Small header at the top of the page, which contains the slab flink/blink,
  44. // and a pointer to the free packets in the slab
  45. //
  46. typedef struct _SLAB_HEADER {
  47. //
  48. // Doubly linked list flink blinks.
  49. //
  50. struct _SLAB_HEADER *pNext;
  51. struct _SLAB_HEADER *pPrev;
  52. //
  53. // List of freed packets in this slab. Note that this is the list of
  54. // packets that were allocated and freed. Packets that have never
  55. // been allocated so far are not on this list.
  56. //
  57. struct _PACKET_HEADER *pFree;
  58. //
  59. // Number of unallocated/freed packets in this slab. 0 if slab is full.
  60. //
  61. DWORD dwFreeCount;
  62. } SLAB_HEADER, *PSLAB_HEADER;
  63. //
  64. // Header at the top of a packet
  65. //
  66. typedef struct _PACKET_HEADER {
  67. union {
  68. //
  69. // In "brand new" packets (ie packets that have never been allocated),
  70. // the packet header contains garbage.
  71. //
  72. //
  73. // In allocated packets, the packet header contains a pointer back
  74. // to the slab header
  75. //
  76. struct _SLAB_HEADER *pSlab;
  77. //
  78. // In packets that have been allocated and freed, the packet header
  79. // contains a pointer to the next freed pointer. (Packets are put
  80. // on a singly linked list when they are freed.)
  81. //
  82. struct _PACKET_HEADER *pNext;
  83. };
  84. } PACKET_HEADER, *PPACKET_HEADER;
  85. //
  86. // Returns a packet of size SIZE_OF_PACKET bytes. May return NULL if the
  87. // system is out of free memory.
  88. //
  89. PVOID
  90. SaAllocatePacket(
  91. IN OUT PSA_CONTEXT pSaContext
  92. );
  93. //
  94. // Frees a packet allocated by SaAllocatePacket.
  95. //
  96. VOID
  97. SaFreePacket(
  98. IN PSA_CONTEXT pSaContext,
  99. IN PVOID pMemory
  100. );
  101. //
  102. // Frees all slabs currently in use
  103. //
  104. VOID
  105. SaFreeAllPackets(
  106. IN OUT PSA_CONTEXT pSaContext
  107. );
  108. //
  109. // Initialises a slab-allocator context.
  110. //
  111. BOOL
  112. SaInitialiseContext(
  113. IN OUT PSA_CONTEXT pSaContext,
  114. IN CONST DWORD dwPacketSize,
  115. IN CONST DWORD dwSlabSize
  116. );
  117. //
  118. // Frees all memory associated with a context, and resets the context
  119. //
  120. VOID
  121. SaFreeContext(
  122. IN OUT PSA_CONTEXT pSaContext
  123. );
  124. #endif