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.

217 lines
5.8 KiB

  1. #include "precomp.h"
  2. DEBUG_FILEZONE(ZONE_T120_T123PSTN);
  3. /* TMemory.cpp
  4. *
  5. * Copyright (c) 1994-1995 by DataBeam Corporation, Lexington, KY
  6. *
  7. * Abstract:
  8. * This is the implementation of the TMemory class.
  9. *
  10. * Private Instance Variables:
  11. * Default_Buffer_Size - Static buffer size
  12. * Base_Buffer - Address of static buffer
  13. * Auxiliary_Buffer - Address of auxiliary buffer, if needed
  14. * Length - Current number of bytes in buffer
  15. * Auxiliary_Buffer_In_Use - TRUE if we are using the aux. buffer
  16. * Prepend_Space - Amount of space to leave open at
  17. * beginning of buffer
  18. * Fatal_Error_Count - Number of times we have attempted to
  19. * allocate a buffer and failed
  20. *
  21. * Caveats:
  22. * None.
  23. *
  24. * Authors:
  25. * James W. Lawwill
  26. */
  27. #include "tmemory2.h"
  28. /*
  29. * TMemory::TMemory (
  30. * ULONG total_size,
  31. * USHORT prepend_space,
  32. * PTMemoryError error)
  33. *
  34. * Public
  35. *
  36. * Functional Description:
  37. * This is the TMemory constructor.
  38. */
  39. TMemory::TMemory (
  40. ULONG total_size,
  41. USHORT prepend_space,
  42. PTMemoryError error)
  43. {
  44. *error = TMEMORY_NO_ERROR;
  45. Fatal_Error_Count = 0;
  46. Prepend_Space = prepend_space;
  47. Default_Buffer_Size = total_size;
  48. Base_Buffer = NULL;
  49. Length = Prepend_Space;
  50. Auxiliary_Buffer = NULL;
  51. Auxiliary_Buffer_In_Use = FALSE;
  52. /*
  53. ** Attempt to allocate our internal buffer
  54. */
  55. Base_Buffer = (FPUChar) LocalAlloc (LMEM_FIXED, total_size);
  56. if (Base_Buffer == NULL)
  57. {
  58. ERROR_OUT(("TMemory: Constructor: Error allocating memory"));
  59. *error = TMEMORY_FATAL_ERROR;
  60. }
  61. }
  62. /*
  63. * TMemory::~TMemory (
  64. * void)
  65. *
  66. * Public
  67. *
  68. * Functional Description:
  69. * This is the TMemory class destructor
  70. */
  71. TMemory::~TMemory (
  72. void)
  73. {
  74. if (Base_Buffer != NULL)
  75. LocalFree ((HLOCAL) Base_Buffer);
  76. if (Auxiliary_Buffer != NULL)
  77. LocalFree ((HLOCAL) Auxiliary_Buffer);
  78. }
  79. /*
  80. * void TMemory::Reset (
  81. * void)
  82. *
  83. * Public
  84. *
  85. * Functional Description:
  86. * This function resets the current buffer pointers and frees the
  87. * Auxiliary buffer (if used)
  88. */
  89. void TMemory::Reset (
  90. void)
  91. {
  92. if (Auxiliary_Buffer_In_Use)
  93. {
  94. Auxiliary_Buffer_In_Use = FALSE;
  95. LocalFree ((HLOCAL) Auxiliary_Buffer);
  96. Auxiliary_Buffer = NULL;
  97. }
  98. Length = Prepend_Space;
  99. }
  100. /*
  101. * TMemoryError TMemory::Append (
  102. * HPUChar address,
  103. * ULONG length)
  104. *
  105. * Public
  106. *
  107. * Functional Description:
  108. * This function appends the buffer passed in, to our internal buffer
  109. */
  110. TMemoryError TMemory::Append (
  111. HPUChar address,
  112. ULONG length)
  113. {
  114. TMemoryError error = TMEMORY_NO_ERROR;
  115. FPUChar new_address;
  116. if (Auxiliary_Buffer_In_Use == FALSE)
  117. {
  118. /*
  119. ** If the proposed buffer length is > our default buffer size,
  120. ** allocate an auxiliary buffer
  121. */
  122. if ((Length + length) > Default_Buffer_Size)
  123. {
  124. Auxiliary_Buffer = (HPUChar) LocalAlloc (LMEM_FIXED, Length + length);
  125. if (Auxiliary_Buffer == NULL)
  126. {
  127. if (Fatal_Error_Count++ >= MAXIMUM_NUMBER_REALLOC_FAILURES)
  128. error = TMEMORY_FATAL_ERROR;
  129. else
  130. error = TMEMORY_NONFATAL_ERROR;
  131. }
  132. else
  133. {
  134. Fatal_Error_Count = 0;
  135. /*
  136. ** Copy our current data into the auxiliary buffer
  137. */
  138. memcpy (Auxiliary_Buffer, Base_Buffer, Length);
  139. memcpy (Auxiliary_Buffer + Length, address, length);
  140. Length += length;
  141. Auxiliary_Buffer_In_Use = TRUE;
  142. }
  143. }
  144. else
  145. {
  146. memcpy (Base_Buffer + Length, address, length);
  147. Length += length;
  148. }
  149. }
  150. else
  151. {
  152. new_address = (FPUChar) LocalReAlloc ((HLOCAL) Auxiliary_Buffer,
  153. Length + length, LMEM_MOVEABLE);
  154. if (new_address == NULL)
  155. {
  156. /*
  157. ** If we have attempted to allocate a buffer before and failed
  158. ** we will eventually return a FATAL ERROR
  159. */
  160. if (Fatal_Error_Count++ >= MAXIMUM_NUMBER_REALLOC_FAILURES)
  161. error = TMEMORY_FATAL_ERROR;
  162. else
  163. error = TMEMORY_NONFATAL_ERROR;
  164. }
  165. else
  166. {
  167. Fatal_Error_Count = 0;
  168. Auxiliary_Buffer = new_address;
  169. memcpy (Auxiliary_Buffer + Length, address, length);
  170. Length += length;
  171. }
  172. }
  173. return (error);
  174. }
  175. /*
  176. * TMemoryError TMemory::GetMemory (
  177. * HPUChar * address,
  178. * FPULong length)
  179. *
  180. * Public
  181. *
  182. * Functional Description:
  183. * This function returns the address and used length of our internal buffer
  184. */
  185. TMemoryError TMemory::GetMemory (
  186. HPUChar * address,
  187. FPULong length)
  188. {
  189. if (Auxiliary_Buffer_In_Use)
  190. *address = (FPUChar) Auxiliary_Buffer;
  191. else
  192. *address = (FPUChar) Base_Buffer;
  193. *length = Length;
  194. return (TMEMORY_NO_ERROR);
  195. }