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.

393 lines
7.0 KiB

  1. /* asmalloc.c -- microsoft 80x86 assembler
  2. **
  3. ** microsoft (r) macro assembler
  4. ** copyright (c) microsoft corp 1986, 1987. all rights reserved
  5. **
  6. ** randy nevin
  7. ** michael hobbs 7/87
  8. ** 10/90 - Quick conversion to 32 bit by Jeff Spencer
  9. **
  10. ** Storage allocation/deallocation
  11. **
  12. ** dalloc, dfree
  13. ** talloc, tfree
  14. ** nearalloc, faralloc, pBCBalloc, freefarbuf
  15. */
  16. #include <stdio.h>
  17. #include "asm86.h"
  18. #include "asmfcn.h"
  19. /*
  20. * dalloc/dfree
  21. *
  22. * Allocation/deallocation of descriptor nodes. Uses a list of
  23. * deallocated descriptors available for allocation.
  24. */
  25. /* dscfree list descriptor */
  26. struct dscfree {
  27. struct dscfree *strnext; /* next string in list */
  28. UCHAR size; /* allocated size */
  29. UCHAR text[1]; /* text of string */
  30. };
  31. static struct dscfree *dscrfree = (struct dscfree *)NULL;
  32. #define nearheap(fp) ((int)(((long)(char far *)&pcoffset) >> 16) == highWord(fp))
  33. /*
  34. * dalloc - allocate descriptor from temporary list
  35. */
  36. DSCREC * PASCAL CODESIZE
  37. dalloc (
  38. ){
  39. register struct dscfree *t;
  40. if (!(t = dscrfree))
  41. t = (struct dscfree *)nalloc( sizeof(DSCREC), "dalloc");
  42. else
  43. dscrfree = t->strnext;
  44. return((DSCREC *)t);
  45. }
  46. /*
  47. * dfree - return descriptor to free list
  48. */
  49. VOID PASCAL CODESIZE
  50. dfree (
  51. UCHAR *p
  52. ){
  53. register struct dscfree *tp;
  54. tp = (struct dscfree *)p;
  55. tp->strnext = dscrfree;
  56. dscrfree = tp;
  57. }
  58. /*
  59. * talloc, tfree
  60. *
  61. * Allocation\deallocation of memory.
  62. * Allocation is made with minimum size of TEMPMAX bytes.
  63. * Any allocation request for <= TEMPMAX bytes will be made
  64. * by grabbing a block off the free list. Deallocation of these
  65. * blocks returns them to the free list. For blocks larger than
  66. * TEMPMAX bytes, nalloc() and free() are called.
  67. */
  68. #define TEMPMAX 32
  69. static TEXTSTR FAR *tempfree = (TEXTSTR FAR *)NULL;
  70. #ifndef M8086
  71. /*
  72. * talloc - allocate space from temporary list
  73. */
  74. UCHAR * PASCAL
  75. talloc(
  76. UINT nbytes
  77. ){
  78. register TEXTSTR *t;
  79. if (nbytes > TEMPMAX)
  80. t = (TEXTSTR *) nalloc(nbytes, "talloc");
  81. else if (!(t = tempfree))
  82. t = (TEXTSTR *) nalloc (TEMPMAX, "talloc");
  83. else
  84. tempfree = t->strnext;
  85. return ((UCHAR *)t);
  86. }
  87. /*
  88. * tfree - return temp allocation to free list
  89. */
  90. VOID PASCAL
  91. tfree (
  92. UCHAR *ap,
  93. UINT nbytes
  94. ){
  95. register TEXTSTR *tp;
  96. if (nbytes > TEMPMAX)
  97. free (ap);
  98. else {
  99. tp = (TEXTSTR *)ap;
  100. tp->strnext = tempfree;
  101. tempfree = tp;
  102. }
  103. }
  104. #else
  105. /*
  106. * talloc - allocate space from temporary list
  107. */
  108. UCHAR FAR * PASCAL CODESIZE
  109. talloc(
  110. USHORT nbytes
  111. ){
  112. TEXTSTR FAR *t;
  113. if (nbytes > TEMPMAX)
  114. t = (TEXTSTR FAR *) falloc(nbytes, "talloc");
  115. else if (!(t = tempfree))
  116. t = (TEXTSTR FAR *) falloc(TEMPMAX, "talloc");
  117. else
  118. tempfree = t->strnext;
  119. return ((UCHAR FAR *)t);
  120. }
  121. /*
  122. * tfree - return temp allocation to free list
  123. */
  124. VOID PASCAL CODESIZE
  125. tfree (
  126. UCHAR FAR *ap,
  127. UINT nbytes
  128. ){
  129. register TEXTSTR FAR *tp;
  130. if (nbytes > TEMPMAX)
  131. _ffree (ap);
  132. else {
  133. tp = (TEXTSTR FAR *)ap;
  134. tp->strnext = tempfree;
  135. tempfree = tp;
  136. }
  137. }
  138. #endif /* NOT M8086 */
  139. #ifndef M8086
  140. /**** nearalloc - normal near memory allocation
  141. *
  142. * nearalloc (usize, szfunc)
  143. *
  144. * Entry usize = number of bytes to allocate
  145. * szfunc = name of calling routine
  146. * Returns Pointer to block if successful
  147. * Calls malloc(), memerror()
  148. * Note Generates error if malloc unsuccessful
  149. * IF NOT M8086, nalloc AND falloc MAP TO THIS FUNCTION
  150. */
  151. UCHAR * CODESIZE PASCAL
  152. nearalloc(
  153. UINT usize,
  154. char * szfunc
  155. ){
  156. register char * pchT;
  157. if (!(pchT = malloc(usize)))
  158. memerror(szfunc);
  159. return(pchT);
  160. }
  161. #else
  162. /**** nearalloc - normal near memory allocation
  163. *
  164. * nearalloc (usize)
  165. *
  166. * Entry usize = number of bytes to allocate
  167. * Returns Pointer to block if successful
  168. * Calls malloc(), memerror()
  169. * Note Generates error if malloc unsuccessful
  170. */
  171. UCHAR * CODESIZE PASCAL
  172. nearalloc(
  173. USHORT usize
  174. ){
  175. register char * pchT;
  176. if (!(pchT = malloc(usize)))
  177. outofmem();
  178. return(pchT);
  179. }
  180. /**** faralloc - Routine for normal far memory allocation
  181. *
  182. * faralloc (usize)
  183. *
  184. * Entry usize = number of bytes to allocate
  185. * Returns Pointer to block if successful
  186. * Calls _fmalloc(), nearheap(), freefarbuf(), memerror(), _ffree()
  187. * Note Should call instead of _fmalloc(),
  188. * at least after the first call to pBCBalloc() has been made.
  189. * Not called by pBCBalloc.
  190. * Generates error if memory full
  191. */
  192. UCHAR FAR * CODESIZE PASCAL
  193. faralloc(
  194. USHORT usize
  195. ){
  196. char FAR * fpchT;
  197. #ifdef BCBOPT
  198. /* need check of _fmalloc 'ing into near heap too */
  199. while ( (!(fpchT = _fmalloc(usize)) || nearheap(fpchT)) && pBCBAvail) {
  200. fBuffering = FALSE; /* can't buffer any more */
  201. if (fpchT)
  202. _ffree(fpchT);
  203. freefarbuf();
  204. }
  205. #endif
  206. #ifdef FLATMODEL /* If 32 bit small model then use normal malloc */
  207. fpchT = malloc(usize);
  208. #else
  209. fpchT = _fmalloc(usize);
  210. #endif
  211. if (!fpchT)
  212. outofmem();
  213. return (fpchT);
  214. }
  215. #ifdef BCBOPT
  216. /**** pBCBalloc - allocate a BCB and associated buffer
  217. *
  218. * pBCBalloc ()
  219. *
  220. * Entry fBuffering must be TRUE
  221. * Returns pointer to BCB (connected to buffer if bufalloc() successful)
  222. * Calls bufalloc()
  223. * Note Returns a BCB even if buffer allocation fails
  224. * Returns NULL only after Out-of-memory error
  225. */
  226. BCB * FAR PASCAL
  227. pBCBalloc(
  228. UINT cbBuf
  229. ){
  230. register BCB * pBCBT;
  231. char FARIO * pfchT;
  232. pBCBT = (BCB *) nearalloc(sizeof(BCB));
  233. #ifndef XENIX286
  234. if ((pfchT = _fmalloc(cbBuf)) && nearheap(pfchT)) {
  235. _ffree(pfchT);
  236. pfchT = NULL;
  237. }
  238. if (!(pfchT))
  239. #else
  240. pfchT = NULL;
  241. #endif
  242. {
  243. fBuffering = FALSE; /* can't buffer anymore */
  244. pBCBT->filepos = 0;
  245. }
  246. #ifndef XENIX286
  247. else {
  248. pFCBCur->cbufCur = cbBuf;
  249. pBCBT->pBCBPrev = pBCBAvail;
  250. pBCBAvail = pBCBT;
  251. }
  252. #endif
  253. pFCBCur->pbufCur = pBCBT->pbuf = pfchT;
  254. pBCBT->pBCBNext = NULL;
  255. pBCBT->fInUse = 0;
  256. return(pBCBT);
  257. }
  258. #endif //BCBOPT
  259. #ifdef BCBOPT
  260. /**** freefarbuf - free a file buffer
  261. *
  262. * freefarbuf ()
  263. *
  264. * Entry
  265. * Returns
  266. * Calls _ffree()
  267. * Note Frees the last-allocated file buffer
  268. */
  269. freefarbuf(
  270. ){
  271. while (pBCBAvail && pBCBAvail->fInUse)
  272. pBCBAvail = pBCBAvail->pBCBPrev;
  273. if (pBCBAvail) {
  274. #ifdef XENIX286
  275. free(pBCBAvail->pbuf);
  276. #else
  277. _ffree(pBCBAvail->pbuf);
  278. #endif
  279. pBCBAvail->pbuf = NULL;
  280. pBCBAvail = pBCBAvail->pBCBPrev;
  281. }
  282. }
  283. #endif //BCBOPT
  284. #endif /* M8086 */
  285. #if 0
  286. /* sleazy way to check for valid heap
  287. * _mcalls tells how calls to malloc were made so that
  288. * a countdown breakpoint can be set for _mcalls iterations
  289. */
  290. extern char *_nmalloc();
  291. long _mcalls;
  292. UCHAR *
  293. malloc (
  294. UINT n
  295. ){
  296. register UINT fb;
  297. fb = _freect(0); /* walks near heap - usually loops if corrupt */
  298. _mcalls++;
  299. return (_nmalloc(n));
  300. }
  301. #endif /* 0 */