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.

228 lines
4.1 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 1994-1995 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: memalloc.c
  6. * Content: allocates memory; uses global blocks and sub-allocates
  7. * out of those using LocalAlloc
  8. *@@BEGIN_MSINTERNAL
  9. * History:
  10. * Date By Reason
  11. * ==== == ======
  12. * 29-dec-94 craige initial implementation
  13. * 12-jan-95 craige use clib as an option
  14. *@@END_MSINTERNAL
  15. *
  16. ***************************************************************************/
  17. #include "ddraw16.h"
  18. #if 0
  19. #define USE_CLIB
  20. #ifdef USE_CLIB
  21. #include <malloc.h>
  22. #else
  23. #define GLOBAL_BLOCK_SIZE 32768
  24. typedef struct _HEAPLIST
  25. {
  26. struct _HEAPLIST FAR *lpLink;
  27. WORD wSel;
  28. GLOBALHANDLE hMem;
  29. } HEAPLIST, FAR *LPHEAPLIST;
  30. static LPHEAPLIST lphlHeap;
  31. /*
  32. * tryAlloc - try to allocate from a heap
  33. */
  34. static LPVOID tryAlloc( WORD sel, WORD size )
  35. {
  36. void NEAR *ptr;
  37. _asm push ds
  38. _asm mov ax, sel
  39. _asm mov ds, ax
  40. ptr = (void NEAR *) LocalAlloc( LPTR, size );
  41. _asm pop ds
  42. if( ptr == (void NEAR *) NULL )
  43. {
  44. return (void FAR *) NULL;
  45. }
  46. return MAKELP( sel, ptr );
  47. } /* tryAlloc */
  48. #endif
  49. #ifdef DEBUG
  50. LONG lAllocCount;
  51. #endif
  52. /*
  53. * MemAlloc - allocate memory from our global pool
  54. */
  55. LPVOID MemAlloc( UINT size )
  56. {
  57. LPVOID lptr;
  58. #ifndef USE_CLIB
  59. LPHEAPLIST curr;
  60. GLOBALHANDLE gh;
  61. LPVOID lpnew;
  62. WORD sel;
  63. #endif
  64. #ifdef DEBUG
  65. lAllocCount++;
  66. #endif
  67. #ifdef USE_CLIB
  68. lptr = _fmalloc( size );
  69. if( lptr != NULL )
  70. {
  71. _fmemset( lptr, 0, size );
  72. }
  73. else
  74. {
  75. DPF( 1, "Alloc of size %u FAILED!", size );
  76. }
  77. return lptr;
  78. #else
  79. /*
  80. * run our current list of GlobalAlloc'ed chunks
  81. */
  82. curr = lphlHeap;
  83. while( curr != NULL )
  84. {
  85. lptr = tryAlloc( curr->wSel, size );
  86. if( lptr != (LPVOID) NULL )
  87. {
  88. return lptr;
  89. }
  90. curr = curr->lpLink;
  91. }
  92. /*
  93. * no luck, allocate a new global chunk
  94. */
  95. gh = GlobalAlloc( GMEM_SHARE | GMEM_MOVEABLE, GLOBAL_BLOCK_SIZE );
  96. if( gh == NULL )
  97. {
  98. return NULL;
  99. }
  100. lpnew = GlobalLock( gh );
  101. if( lpnew == NULL )
  102. {
  103. GlobalFree( gh );
  104. return NULL;
  105. }
  106. /*
  107. * set up a local heap in this new global chunk
  108. */
  109. sel = SELECTOROF( lpnew );
  110. if( !LocalInit( sel, 0, GLOBAL_BLOCK_SIZE-1 ) )
  111. {
  112. GlobalUnlock( gh );
  113. GlobalFree( gh );
  114. return NULL;
  115. }
  116. /*
  117. * chain this new heap into our list
  118. */
  119. curr = tryAlloc( sel, sizeof( HEAPLIST ) );
  120. if( curr == NULL )
  121. {
  122. return NULL;
  123. }
  124. curr->wSel = sel;
  125. curr->hMem = gh;
  126. curr->lpLink = lphlHeap;
  127. lphlHeap = curr;
  128. /*
  129. * go allocate the original request
  130. */
  131. return tryAlloc( sel, size );
  132. #endif
  133. } /* MemAlloc */
  134. /*
  135. * MemFree - free memory from our global pool
  136. */
  137. void MemFree( LPVOID lptr )
  138. {
  139. #ifdef USE_CLIB
  140. _ffree( lptr );
  141. #else
  142. WORD sel;
  143. if( lptr != (LPVOID) NULL )
  144. {
  145. sel = SELECTOROF( lptr );
  146. _asm push ds
  147. _asm mov ax,sel
  148. _asm mov ds,ax
  149. LocalFree( OFFSETOF( lptr ) );
  150. _asm pop ds
  151. }
  152. #endif
  153. #ifdef DEBUG
  154. if( lptr != NULL )
  155. {
  156. lAllocCount--;
  157. if( lAllocCount < 0 )
  158. {
  159. DPF( 1, "Too Many Frees!\n" );
  160. }
  161. }
  162. #endif
  163. } /* MemFree */
  164. /*
  165. * MemInit - initialize the heap manager
  166. */
  167. BOOL MemInit( void )
  168. {
  169. #ifndef USE_CLIB
  170. lphlHeap = NULL;
  171. #endif
  172. #ifdef DEBUG
  173. lAllocCount = 0;
  174. #endif
  175. return TRUE;
  176. } /* MemInit */
  177. /*
  178. * MemFini - finished with our heap manager
  179. */
  180. void MemFini( void )
  181. {
  182. #ifdef DEBUG
  183. if( lAllocCount != 0 )
  184. {
  185. DPF( 1, "Memory still allocated! Alloc count = %ld", lAllocCount );
  186. }
  187. #endif
  188. #ifndef USE_CLIB
  189. LPHEAPLIST curr;
  190. LPHEAPLIST last;
  191. GLOBALHANDLE gh;
  192. curr = lphlHeap;
  193. while( curr != NULL )
  194. {
  195. gh = curr->hMem;
  196. last = curr->lpLink;
  197. // GlobalUnfix( gh );
  198. GlobalUnlock( gh );
  199. GlobalFree( gh );
  200. curr = last;
  201. }
  202. lphlHeap = NULL;
  203. #endif
  204. } /* MemFini */
  205. #endif