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.

289 lines
5.7 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4. #include <string.h>
  5. #include "extract.h"
  6. /*
  7. * ERROR MESSAGES
  8. */
  9. char errFarMemory[] = "Error: Out of far memory. Current used: %lud bytes\n";
  10. char errNearMemory[] = "Error: Out of near memory. Current used: %u bytes\n";
  11. char errRealloc[] = "\t(Attempting to realloc %u bytes to %u bytes)\n";
  12. /*
  13. * Keeps count of how much memory has been used by system.
  14. */
  15. WORD wNearMemoryUsed = 0;
  16. DWORD dwFarMemoryUsed = 0L;
  17. #if 0
  18. LPSTR FarMalloc(int size, BOOL fZero)
  19. {
  20. LPSTR fp;
  21. fp = _fmalloc(size);
  22. if (fp == 0L) {
  23. fprintf(stderr, errFarMemory, dwFarMemoryUsed);
  24. exit(2);
  25. }
  26. dwFarMemoryUsed += (DWORD) size;
  27. if (fZero)
  28. lmemzero(fp, size);
  29. return fp;
  30. }
  31. void FarFree(LPSTR lpblock)
  32. {
  33. WORD size;
  34. size = _fmsize(lpblock);
  35. dwFarMemoryUsed -= (DWORD) size;
  36. _ffree(lpblock);
  37. }
  38. #endif
  39. #ifdef HEAPDEBUG
  40. void NearHeapCheck()
  41. {
  42. char foo[5];
  43. switch (_nheapchk()) {
  44. case _HEAPOK:
  45. return;
  46. case _HEAPEMPTY:
  47. fprintf(stderr, "Non-Initted heap\n");
  48. return;
  49. case _HEAPBADBEGIN:
  50. fprintf(stderr, "NearHeapCheck: BAD BEGIN!\n");
  51. break;
  52. case _HEAPBADNODE:
  53. fprintf(stderr, "NearHeapCheck: BAD NODE!\n");
  54. break;
  55. }
  56. fprintf(stderr, "BOGUS HEAP DETECTED!!!!, BREAK HERE!\n");
  57. gets(foo);
  58. }
  59. #endif
  60. /*
  61. * @doc EXTRACT
  62. * @api PSTR | NearMalloc | Allocate a near memory buffer.
  63. * @parm WORD | size | Specifies the size in bytes of the block to be
  64. * allocated.
  65. * @parm BOOL | fZero | Specifies whether to zero initialize the
  66. * allocated buffer.
  67. *
  68. * @rdesc Returns a near pointer to the new buffer.
  69. *
  70. * @comm If the requested amount of memory can not be allocated, an
  71. * error message is displayed on stderr, and the program exited. If
  72. * memory allocation is successful, the count of near memory currently
  73. * allocated is updated to reflect the new block.
  74. *
  75. */
  76. PSTR NearMalloc(WORD size, BOOL fZero)
  77. {
  78. PSTR pstr;
  79. #ifdef HEAPDEBUG
  80. NearHeapCheck();
  81. #endif
  82. pstr = _nmalloc(size);
  83. if (!pstr) {
  84. fprintf(stderr, errNearMemory, wNearMemoryUsed);
  85. exit(2);
  86. }
  87. wNearMemoryUsed += size;
  88. if (fZero)
  89. memset(pstr, '\0', size);
  90. return pstr;
  91. }
  92. /*
  93. * @doc EXTRACT
  94. * @api PSTR | NearRealloc | Change the size of a dynamically
  95. * allocated near memory block.
  96. *
  97. * @parm PSTR | pblock | Specifies the old memory block that is to be
  98. * resized.
  99. * @parm WORD | newsize | Specifies the requested new size in bytes of
  100. * the reallocated block.
  101. *
  102. * @rdesc Returns a pointer to the new memory block, with size as
  103. * specified by <p newsize>. The block contents will be indentical to
  104. * the contents of <p pblock> up to the smaller of the old size and new
  105. * size. This pointer may be different from <p pblock>.
  106. *
  107. * @comm If the requested amount of memory cannot be allocated, an
  108. * error message is displayed on stderr and the program exited. If
  109. * memory allocation is successful, the count of near memory currently
  110. * allocated is updated to reflect the new size of <p pblock>.
  111. *
  112. */
  113. PSTR NearRealloc(PSTR pblock, WORD newsize)
  114. {
  115. WORD oldsize;
  116. PSTR pnew;
  117. #ifdef HEAPDEBUG
  118. NearHeapCheck();
  119. #endif
  120. oldsize = _nmsize(pblock);
  121. // dprintf("(Attempting to realloc %u bytes to %u bytes)\n", oldsize, newsize);
  122. pnew = realloc(pblock, newsize);
  123. if (!pnew) {
  124. fprintf(stderr, errNearMemory, wNearMemoryUsed);
  125. fprintf(stderr, errRealloc, oldsize, newsize);
  126. exit(2);
  127. }
  128. wNearMemoryUsed += (newsize - oldsize);
  129. #ifdef HEAPDEBUG
  130. NearHeapCheck();
  131. #endif
  132. return pnew;
  133. }
  134. /*
  135. * @doc EXTRACT
  136. * @api void | NearFree | Frees a block of dynamically allocated near
  137. * memory.
  138. * @parm PSTR | pblock | Specifies the block to be freed.
  139. *
  140. * @comm The pointer <p pblock> should not be referenced again after
  141. * calling this function, as the memory may be re-used by subsequent
  142. * calls to <f NearMalloc>, <f NearRealloc>, or <f StringAlloc>.
  143. *
  144. * The count of near memory currently allocated is updated to reflect
  145. * the freed block.
  146. *
  147. */
  148. void NearFree(PSTR pblock)
  149. {
  150. WORD size;
  151. #ifdef HEAPDEBUG
  152. NearHeapCheck();
  153. #endif
  154. size = (WORD) _nmsize(pblock);
  155. wNearMemoryUsed -= size;
  156. _nfree(pblock);
  157. #ifdef HEAPDEBUG
  158. NearHeapCheck();
  159. #endif
  160. }
  161. /*
  162. * @doc EXTRACT
  163. * @api WORD | NearSize | Returns the size in bytes of a near block
  164. * allocated using <f NearMalloc> or <f StringAlloc>.
  165. *
  166. * @parm PSTR | pblock | Specifies the block to return the size of.
  167. *
  168. * @rdesc Returns the size in bytes of <p pblock>.
  169. *
  170. * @xref NearMalloc, StringAlloc, NearFree
  171. *
  172. */
  173. WORD NearSize(PSTR pblock)
  174. {
  175. #ifdef HEAPDEBUG
  176. NearHeapCheck();
  177. #endif
  178. return (WORD) _nmsize(pblock);
  179. }
  180. /*
  181. * @doc EXTRACT
  182. * @api PSTR | StringAlloc | Allocates near memory and copies a
  183. * string into it.
  184. *
  185. * @parm PSTR | string | Specifies the null-terminated string to
  186. * preserve.
  187. *
  188. * @rdesc Returns a near pointer to an allocated memory block
  189. * containing a copy of string <p string>.
  190. *
  191. * @comm This function uses <f NearMalloc> to allocate the memory
  192. * buffer that is returned. This buffer must be freed with <f NearFree>
  193. * when it is no longer needed.
  194. *
  195. */
  196. PSTR StringAlloc(PSTR string)
  197. {
  198. PSTR pstr;
  199. WORD size;
  200. #ifdef HEAPDEBUG
  201. NearHeapCheck();
  202. #endif
  203. size = (WORD) strlen(string) + 1;
  204. pstr = _nmalloc(size);
  205. if (!pstr) {
  206. fprintf(stderr, errNearMemory, wNearMemoryUsed);
  207. exit(2);
  208. }
  209. wNearMemoryUsed += size;
  210. strcpy(pstr, string);
  211. #ifdef HEAPDEBUG
  212. NearHeapCheck();
  213. #endif
  214. return pstr;
  215. }
  216. #ifdef DEBUG
  217. static FILE *fpCOM1 = stdaux;
  218. static BOOL fFixed = False;
  219. #include <fcntl.h>
  220. #include <io.h>
  221. #include <stdarg.h>
  222. void cdecl COMprintf(PSTR format, ...)
  223. {
  224. va_list arglist;
  225. if (!fFixed) {
  226. setmode(fileno(fpCOM1), O_TEXT);
  227. }
  228. va_start (arglist, format);
  229. vfprintf(fpCOM1, format, arglist);
  230. va_end (arglist);
  231. }
  232. #endif