home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples1.exe / smc / alloc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  19.7 KB  |  849 lines

  1. /*****************************************************************************/
  2.  
  3. #include "smcPCH.h"
  4. #pragma hdrstop
  5.  
  6. /*****************************************************************************/
  7.  
  8. #include "error.h"
  9. #include "alloc.h"
  10.  
  11. /*****************************************************************************/
  12. #ifdef  DEBUG
  13. #define MEMALLOC_DISP   0
  14. #else
  15. #define MEMALLOC_DISP   0
  16. #endif
  17. /*****************************************************************************/
  18. #if     COUNT_CYCLES
  19. #define ALLOC_CYCLES    0
  20. #else
  21. #define ALLOC_CYCLES    0
  22. #endif
  23. /*****************************************************************************/
  24.  
  25. #if     MEMALLOC_DISP
  26.  
  27. static  unsigned        totSize;
  28. static  unsigned        maxSize;
  29.  
  30. inline
  31. void    updateMemSize(int size)
  32. {
  33.     totSize += size;
  34.     if  (maxSize < totSize)
  35.          maxSize = totSize;
  36. }
  37.  
  38. #endif
  39.  
  40. // -w- -L -d -CS -Dbool=Bool -OWFC1.DLL            @WFC1.rsp
  41. // -w- -L -d -CS -Dbool=Bool -OWFC2.DLL -mWFC1.DLL @WFC3.rsp
  42.  
  43. // -OWFC1.DLL            @wfc1.rsp
  44. // -OWFC2.DLL -mWFC1.DLL @wfc2.rsp
  45.  
  46. /*****************************************************************************/
  47.  
  48. void    *           LowLevelAlloc(size_t sz)
  49. {
  50.  
  51. #if MEMALLOC_DISP
  52.     printf("LLalloc: alloc %04X bytes\n", sz); updateMemSize(sz);
  53. #endif
  54.  
  55.     return  VirtualAlloc(NULL, sz, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
  56. }
  57.  
  58. void                LowLevelFree(void *blk)
  59. {
  60.     if  (blk)
  61.         VirtualFree(blk, 0, MEM_RELEASE);
  62. }
  63.  
  64. /*****************************************************************************/
  65. #if 0
  66. /*****************************************************************************
  67.  *
  68.  *  Initialize a committing allocator. If uncommitted (win32-style) memory
  69.  *  management is supported by our host OS, the parameters have the following
  70.  *  meaning:
  71.  *
  72.  *      iniSize ... ignored
  73.  *
  74.  *      incSize ... how much more memory to commit each time we run
  75.  *                  out of space (0 --> use a reasonable default)
  76.  *
  77.  *      maxSize ... gives the max. size we'll ever need to allocate
  78.  *
  79.  *  If the host OS doesn't support uncommitted memory allocation (e.g. we're
  80.  *  on the MAC), the parameters are interpreted as follows:
  81.  *
  82.  *      iniSize ... initial allocation (0 --> use a reasonable default)
  83.  *
  84.  *      incSize ... if non-zero, indicates how much to grow the allocation
  85.  *                  when we run out of space; if 0, allocation will double
  86.  *                  whenever space is exhausted
  87.  *
  88.  *      maxSize ... ignored
  89.  */
  90.  
  91. bool        commitAllocator::cmaInitT(Compiler comp, size_t iniSize,
  92.                                                      size_t incSize,
  93.                                                      size_t maxSize)
  94. {
  95.     cmaRetNull = true;
  96.  
  97.     /* Remember the compiler we belong to */
  98.  
  99.     cmaComp    = comp;
  100.  
  101. #if _OS_COMMIT_ALLOC
  102.  
  103.     assert(maxSize);
  104.  
  105.     maxSize +=  (OS_page_size - 1);
  106.     maxSize &= ~(OS_page_size - 1);
  107.  
  108.     cmaMaxSize = maxSize;
  109.     cmaIncSize = incSize ? incSize
  110.                          : 2*OS_page_size;
  111.  
  112.     /* Grab max. logical space but don't commit anything yet */
  113.  
  114. #if ALLOC_CYCLES
  115.     unsigned        start = GetCycleCount32();
  116. #endif
  117.  
  118.     cmaBase =
  119.     cmaNext =
  120.     cmaLast = (BYTE *)VirtualAlloc(0, maxSize, MEM_RESERVE, PAGE_READWRITE);
  121.     if  (!cmaBase)
  122.         return true;
  123.  
  124. #if ALLOC_CYCLES
  125.     cycleExtra += GetCycleCount32() - start;
  126. #endif
  127.  
  128. #else
  129.  
  130.     cmaIncSize = incSize;
  131.  
  132.     /* Make sure the initial size is reasonable */
  133.  
  134.     if  (iniSize)
  135.     {
  136.         iniSize +=  (OS_page_size - 1);
  137.         iniSize &= ~(OS_page_size - 1);
  138.     }
  139.     else
  140.     {
  141.         iniSize = OS_page_size;
  142.     }
  143.  
  144.     cmaBase =
  145.     cmaNext = (BYTE *)VirtualAlloc(0, iniSize, MEM_COMMIT , PAGE_READWRITE);
  146.     if  (!cmaBase)
  147.         return true;
  148.  
  149. #if MEMALLOC_DISP
  150.     printf("cmaInit: alloc %04X bytes\n", iniSize); updateMemSize(iniSize);
  151. #endif
  152.  
  153.     cmaLast = cmaBase + iniSize;
  154.  
  155. #endif
  156.  
  157.     return false;
  158. }
  159.  
  160. void        commitAllocator::cmaInit(Compiler comp, size_t iniSize,
  161.                                                     size_t incSize,
  162.                                                     size_t maxSize)
  163. {
  164.     if  (cmaInitT(comp, iniSize, incSize, maxSize))
  165.         cmaComp->cmpFatal(ERRnoMemory);
  166.  
  167.     cmaRetNull = false;
  168. }
  169.  
  170. /*****************************************************************************
  171.  *
  172.  *  This function gets called by caAlloc when it runs out of space. It
  173.  *  keeps committing more memory until we have enough room for the
  174.  *  attempted allocation.
  175.  */
  176.  
  177. void    *   commitAllocator::cmaMore(size_t sz)
  178. {
  179.     /* Undo the increment done in caGetm() */
  180.  
  181.     cmaNext -= sz;
  182.  
  183. #if _OS_COMMIT_ALLOC
  184.  
  185.     /* Keep grabbing more memory until we succeed */
  186.  
  187.     for (;;)
  188.     {
  189.         size_t      sizeInc;
  190.         size_t      sizeCur = cmaLast - cmaBase;
  191.  
  192.         /* Figure out how much more memory to commit */
  193.  
  194.         sizeInc = cmaIncSize;
  195.         if  (sizeCur + sizeInc > cmaMaxSize)
  196.             sizeInc = cmaMaxSize - sizeCur;
  197.  
  198.         assert(sizeInc);
  199.  
  200. #if ALLOC_CYCLES
  201.         unsigned        start = GetCycleCount32();
  202. #endif
  203.  
  204.         /* Commit a few more memory pages */
  205.  
  206.         if  (!VirtualAlloc(cmaLast, sizeInc, MEM_COMMIT, PAGE_READWRITE))
  207.         {
  208.             if  (cmaRetNull)
  209.                 return 0;
  210.  
  211.             cmaComp->cmpFatal(ERRnoMemory);
  212.         }
  213.  
  214. #ifdef DEBUG
  215.         memset(cmaLast, 0xDD, sizeInc);
  216. #endif
  217.  
  218. #if ALLOC_CYCLES
  219.         cycleExtra += GetCycleCount32() - start;
  220. #endif
  221.  
  222. #if MEMALLOC_DISP
  223.         printf("cmaMore: alloc %04X bytes\n", sizeInc); updateMemSize(sizeInc);
  224. #endif
  225.  
  226.         /* Bump the last available byte pointer */
  227.  
  228.         cmaLast += sizeInc;
  229.  
  230.         /* Do we have enough room now? */
  231.  
  232.         if  (cmaNext + sz <= cmaLast)
  233.         {
  234.             void    *   temp;
  235.  
  236.             temp = cmaNext;
  237.                    cmaNext += sz;
  238.  
  239.             return  temp;
  240.         }
  241.     }
  242.  
  243. #else
  244.  
  245.     /* Figure out how much more memory to allocate */
  246.  
  247.     BYTE    *   baseNew;
  248.     size_t      sizeNew;
  249. #ifdef DEBUG
  250.     size_t      sizeInc;
  251. #endif
  252.     size_t      sizeCur = cmaLast - cmaBase;
  253.  
  254.     sizeNew = cmaIncSize;
  255.     if  (!sizeNew)
  256.         sizeNew = sizeCur;
  257. #ifdef DEBUG
  258.     sizeInc  = sizeNew;             // remember how much more we're grabbing
  259. #endif
  260.     sizeNew += sizeCur;
  261.  
  262.     /* Allocate the new, larger block */
  263.  
  264.     baseNew = (BYTE *)VirtualAlloc(0, sizeNew, MEM_COMMIT, PAGE_READWRITE);
  265.     if  (!baseNew)
  266.         cmaComp->cmpFatal(ERRnoMemory);
  267.  
  268. #if MEMALLOC_DISP
  269.     printf("cmaMore: alloc %04X bytes\n", sizeNew); updateMemSize(sizeNew);
  270. #endif
  271.  
  272.     /* Copy the old block to the new one */
  273.  
  274.     memcpy(baseNew, cmaBase, sizeCur);
  275.  
  276.     /* Release the old block, it's no longer needed */
  277.  
  278.     VirtualFree(cmaBase, 0, MEM_RELEASE);
  279.  
  280.     /* Update the various pointers */
  281.  
  282.     cmaNext += baseNew - cmaBase;
  283.     cmaBase  = baseNew;
  284.     cmaLast  = baseNew + sizeNew;
  285.  
  286. #ifdef DEBUG
  287.     memset(cmaNext, 0xDD, sizeInc);
  288. #endif
  289.  
  290.     return  cmaGetm(sz);
  291.  
  292. #endif
  293.  
  294. }
  295.  
  296. void        commitAllocator::cmaDone()
  297. {
  298.  
  299. #if _OS_COMMIT_ALLOC
  300.  
  301.     /* Decommit any extra memory we've allocated */
  302.  
  303. #if 0
  304.  
  305.     printf("Unused committed space: %u bytes\n", cmaLast - cmaNext);
  306.  
  307.     if  (cmaLast != cmaBase)
  308.         VirtualAlloc(0, cmaLast - cmaBase, MEM_DECOMMIT, 0);
  309.  
  310. #endif
  311.  
  312. #else
  313.  
  314.     // ISSUE: is it worth shrinking the block? Not likely .....
  315.  
  316. #endif
  317.  
  318. }
  319.  
  320. void        commitAllocator::cmaFree()
  321. {
  322.     VirtualFree(cmaBase, 0, MEM_RELEASE);
  323.  
  324.     cmaBase =
  325.     cmaNext =
  326.     cmaLast = 0;
  327. }
  328.  
  329. /*****************************************************************************/
  330. #endif//0
  331. /*****************************************************************************/
  332.  
  333. bool                norls_allocator::nraInit(Compiler comp, size_t pageSize,
  334.                                                                bool   preAlloc)
  335. {
  336.     bool            result = false;
  337.  
  338.     /* Remember the compiler we belong to */
  339.  
  340.     nraComp      = comp;
  341.  
  342.     nraRetNull   = true;
  343.  
  344. #ifdef  DEBUG
  345.     nraSelf      = this;
  346. #endif
  347.  
  348.     nraPageList  = NULL;    // temp hack for FJIT problem
  349.     nraPageLast  = NULL;
  350.  
  351.     nraFreeNext  = NULL;
  352.     nraFreeLast  = NULL;
  353.  
  354.     nraPageSize  = pageSize ? pageSize
  355.                             : 4*OS_page_size;
  356.  
  357.     if  (preAlloc)
  358.     {
  359.         const   void *  temp;
  360.  
  361.         /* Make sure we don't toss a fatal error exception */
  362.  
  363.         nraAllocNewPageNret = true;
  364.  
  365.         /* Grab the initial page(s) */
  366.  
  367.         temp = nraAllocNewPage(0);
  368.  
  369.         /* Check whether we've succeeded or not */
  370.  
  371.         if  (!temp)
  372.             result = true;
  373.     }
  374.  
  375.     nraAllocNewPageNret = false;
  376.  
  377.     return  result;
  378. }
  379.  
  380. bool        norls_allocator::nraStart(Compiler comp, size_t initSize,
  381.                                                      size_t pageSize)
  382. {
  383.     /* Add the page descriptor overhead to the required size */
  384.  
  385. //  initSize += offsetof(norls_pagdesc, nrpContents);
  386.     initSize += (size_t)&(((norls_pagdesc *)0)->nrpContents);
  387.  
  388.     /* Round the initial size to a OS page multiple */
  389.  
  390.     initSize +=  (OS_page_size - 1);
  391.     initSize &= ~(OS_page_size - 1);
  392.  
  393.     /* Initialize the allocator by allocating one big page */
  394.  
  395.     if  (nraInit(comp, initSize))
  396.         return  true;
  397.  
  398.     /* Now go back to the 'true' page size */
  399.  
  400.     nraPageSize  = pageSize ? pageSize
  401.                             : 4*OS_page_size;
  402.  
  403.     return  false;
  404. }
  405.  
  406. /*---------------------------------------------------------------------------*/
  407.  
  408. void    *           norls_allocator::nraAllocNewPage(size_t sz)
  409. {
  410.     norls_pagdesc * newPage;
  411.     size_t          sizPage;
  412.  
  413.     /* Do we have a page that's now full? */
  414.  
  415.     if  (nraPageLast)
  416.     {
  417.         /* Undo the "+=" done in nraAlloc() */
  418.  
  419.         nraFreeNext -= sz;
  420.  
  421.         /* Save the actual used size of the page */
  422.  
  423.         nraPageLast->nrpUsedSize = nraFreeNext - nraPageLast->nrpContents;
  424.     }
  425.  
  426.     /* Make sure we grab enough to satisfy the allocation request */
  427.  
  428.     sizPage = nraPageSize;
  429.  
  430.     if  (sizPage < sz + sizeof(norls_pagdesc))
  431.     {
  432.         /* The allocation doesn't fit in a default-sized page */
  433.  
  434. #ifdef  DEBUG
  435. //      if  (nraPageLast) printf("NOTE: wasted %u bytes in last page\n", nraPageLast->nrpPageSize - nraPageLast->nrpUsedSize);
  436. #endif
  437.  
  438.         sizPage = sz + sizeof(norls_pagdesc);
  439.     }
  440.  
  441.     /* Round to the nearest multiple of OS page size */
  442.  
  443.     sizPage +=  (OS_page_size - 1);
  444.     sizPage &= ~(OS_page_size - 1);
  445.  
  446.     /* Allocate the new page */
  447.  
  448. #if ALLOC_CYCLES
  449.     unsigned        start = GetCycleCount32();
  450. #endif
  451.     newPage = (norls_pagdesc *)VirtualAlloc(NULL, sizPage, MEM_COMMIT, PAGE_READWRITE);
  452. #if ALLOC_CYCLES
  453.     cycleExtra += GetCycleCount32() - start;
  454. #endif
  455.     if  (!newPage)
  456.     {
  457.         if  (nraAllocNewPageNret)
  458.             return  NULL;
  459.  
  460.         nraComp->cmpFatal(ERRnoMemory);
  461.     }
  462.  
  463. #if MEMALLOC_DISP
  464.     printf("nraPage: alloc %04X bytes\n", sizPage); updateMemSize(sizPage);
  465. #endif
  466.  
  467. #ifndef NDEBUG
  468.     newPage->nrpSelfPtr = newPage;
  469. #endif
  470.  
  471.     /* Append the new page to the end of the list */
  472.  
  473.     newPage->nrpNextPage = NULL;
  474.     newPage->nrpPageSize = sizPage;
  475.     newPage->nrpPrevPage = nraPageLast;
  476.  
  477.     if  (nraPageLast)
  478.         nraPageLast->nrpNextPage = newPage;
  479.     else
  480.         nraPageList              = newPage;
  481.     nraPageLast = newPage;
  482.  
  483.     /* Set up the 'next' and 'last' pointers */
  484.  
  485.     nraFreeNext = newPage->nrpContents + sz;
  486.     nraFreeLast = newPage->nrpPageSize + (BYTE *)newPage;
  487.  
  488. #ifdef DEBUG
  489.     memset(newPage->nrpContents, 0xDD, nraFreeLast - newPage->nrpContents);
  490. #endif
  491.  
  492.     assert(nraFreeNext <= nraFreeLast);
  493.  
  494.     return  newPage->nrpContents;
  495. }
  496.  
  497. void                norls_allocator::nraDone()
  498. {
  499.     /* Do nothing if we have no pages at all */
  500.  
  501.     if  (!nraPageList)
  502.         return;
  503.  
  504.     /* We'll release all but the very first page */
  505.  
  506.     for (;;)
  507.     {
  508.         norls_pagdesc * temp;
  509.  
  510.         /* Get the next page, and stop if there aren't any more */
  511.  
  512.         temp = nraPageList->nrpNextPage;
  513.         if  (!temp)
  514.             break;
  515.  
  516.         /* Remove the next page from the list */
  517.  
  518.         nraPageList->nrpNextPage = temp->nrpNextPage;
  519.  
  520. #if 0
  521. #ifdef DEBUG
  522.  
  523.         if  (this == &stmt_cmp::scAlloc)
  524.             printf("StmtCmp");
  525.         else if (this == &stmtExpr::sxAlloc)
  526.             printf("StmtExp");
  527.         else
  528.             printf("Unknown");
  529.  
  530.         printf(": done page at %08X\n", temp);
  531.  
  532. #endif
  533. #endif
  534.  
  535.         VirtualFree(temp, 0, MEM_RELEASE);
  536.     }
  537.  
  538.     /* We now have exactly one page */
  539.  
  540.     nraPageLast = nraPageList;
  541.  
  542.     assert(nraPageList->nrpPrevPage == NULL);
  543.     assert(nraPageList->nrpNextPage == NULL);
  544.  
  545.     /* Reset the pointers, the whole page is free now */
  546.  
  547.     nraFreeNext  = nraPageList->nrpContents;
  548.     nraFreeLast  = nraPageList->nrpPageSize + (BYTE *)nraPageList;
  549. }
  550.  
  551. void                norls_allocator::nraFree()
  552. {
  553.     /* Free all of the allocated pages */
  554.  
  555.     while   (nraPageList)
  556.     {
  557.         norls_pagdesc * temp;
  558.  
  559.         temp = nraPageList;
  560.                nraPageList = temp->nrpNextPage;
  561.  
  562. #if 0
  563. #ifdef DEBUG
  564.  
  565.         if  (this == &stmt_cmp::scAlloc)
  566.             printf("StmtCmp");
  567.         else if (this == &stmtExpr::sxAlloc)
  568.             printf("StmtExp");
  569.         else
  570.             printf("Unknown");
  571.  
  572.         printf(": free page at %08X\n", temp);
  573.  
  574. #endif
  575. #endif
  576.  
  577.         VirtualFree(temp, 0, MEM_RELEASE);
  578.     }
  579. }
  580.  
  581. void                norls_allocator::nraToss(nraMarkDsc *mark)
  582. {
  583.     void    *       last = mark->nmPage;
  584.  
  585.     if  (!last)
  586.     {
  587.         if  (!nraPageList)
  588.             return;
  589.  
  590.         nraFreeNext  = nraPageList->nrpContents;
  591.         nraFreeLast  = nraPageList->nrpPageSize + (BYTE *)nraPageList;
  592.  
  593.         return;
  594.     }
  595.  
  596.     /* Free up all the new pages we've added at the end of the list */
  597.  
  598.     while (nraPageLast != last)
  599.     {
  600.         norls_pagdesc * temp;
  601.  
  602.         /* Remove the last page from the end of the list */
  603.  
  604.         temp = nraPageLast;
  605.                nraPageLast = temp->nrpPrevPage;
  606.  
  607. #if MEMALLOC_DISP
  608.         printf("nraToss: free  %04X bytes\n", temp->nrpPageSize); updateMemSize(-(int)temp->nrpPageSize);
  609. #endif
  610.  
  611.         /* The new last page has no 'next' page */
  612.  
  613.         nraPageLast->nrpNextPage = NULL;
  614.  
  615.         VirtualFree(temp, 0, MEM_RELEASE);
  616.     }
  617.  
  618.     nraFreeNext = mark->nmNext;
  619.     nraFreeLast = mark->nmLast;
  620. }
  621.  
  622. /*****************************************************************************/
  623. #ifdef DEBUG
  624. /*****************************************************************************/
  625.  
  626. void    *           norls_allocator::nraAlloc(size_t sz)
  627. {
  628.     void    *   block;
  629.  
  630.     assert((sz & (sizeof(int) - 1)) == 0);
  631.     assert(nraSelf == this);
  632.  
  633.     block = nraFreeNext;
  634.             nraFreeNext += sz;
  635.  
  636.     if  (nraFreeNext > nraFreeLast)
  637.         block = nraAllocNewPage(sz);
  638.  
  639.     return  block;
  640. }
  641.  
  642. /*****************************************************************************/
  643. #endif
  644. /*****************************************************************************/
  645.  
  646. size_t              norls_allocator::nraTotalSizeAlloc()
  647. {
  648.     norls_pagdesc * page;
  649.     size_t          size = 0;
  650.  
  651.     for (page = nraPageList; page; page = page->nrpNextPage)
  652.         size += page->nrpPageSize;
  653.  
  654.     return  size;
  655. }
  656.  
  657. size_t              norls_allocator::nraTotalSizeUsed()
  658. {
  659.     norls_pagdesc * page;
  660.     size_t          size = 0;
  661.  
  662.     if  (nraPageLast)
  663.         nraPageLast->nrpUsedSize = nraFreeNext - nraPageLast->nrpContents;
  664.  
  665.     for (page = nraPageList; page; page = page->nrpNextPage)
  666.         size += page->nrpUsedSize;
  667.  
  668.     return  size;
  669. }
  670.  
  671. /*****************************************************************************/
  672.  
  673. void    *   norls_allocator::nraPageWalkerStart()
  674. {
  675.     /* Make sure the actual used size for the current page is recorded */
  676.  
  677.     if  (nraPageLast)
  678.         nraPageLast->nrpUsedSize = nraFreeNext - nraPageList->nrpContents;
  679.  
  680.     /* Return the first page */
  681.  
  682.     return  nraPageList;
  683. }
  684.  
  685. void    *   norls_allocator::nraPageWalkerNext(void *page)
  686. {
  687.     norls_pagdesc * temp = (norls_pagdesc *)page;
  688.  
  689. #ifndef NDEBUG
  690.     assert(temp);
  691.     assert(temp->nrpSelfPtr == temp);
  692. #endif
  693.  
  694.     return  temp->nrpNextPage;
  695. }
  696.  
  697. void    *   norls_allocator::nraPageGetData(void *page)
  698. {
  699.     norls_pagdesc * temp = (norls_pagdesc *)page;
  700.  
  701. #ifndef NDEBUG
  702.     assert(temp);
  703.     assert(temp->nrpSelfPtr == temp);
  704. #endif
  705.  
  706.     return  temp->nrpContents;
  707. }
  708.  
  709. size_t      norls_allocator::nraPageGetSize(void *page)
  710. {
  711.     norls_pagdesc * temp = (norls_pagdesc *)page;
  712.  
  713. #ifndef NDEBUG
  714.     assert(temp);
  715.     assert(temp->nrpSelfPtr == temp);
  716. #endif
  717.  
  718.     return  temp->nrpUsedSize;
  719. }
  720.  
  721. /*****************************************************************************
  722.  *
  723.  *  Initialize the block allocator.
  724.  */
  725.  
  726. bool                block_allocator::baInit(Compiler comp)
  727. {
  728.     /* Remember the compiler pointer */
  729.  
  730.     baComp     = comp;
  731.  
  732.     /* Normally we toss out-of-memory fatal errors */
  733.  
  734.     baGetMnret = false;
  735.  
  736.     return  false;
  737. }
  738.  
  739. /*****************************************************************************
  740.  *
  741.  *  Shut down the block allocator.
  742.  */
  743.  
  744. void                block_allocator::baDone()
  745. {
  746. }
  747.  
  748. /*****************************************************************************/
  749.  
  750. void    *           block_allocator::baGetM(size_t sz)
  751. {
  752.     void    *   block;
  753.  
  754.     assert((sz & (sizeof(int) - 1)) == 0);
  755.  
  756.     block = malloc(sz);
  757.     if  (!block && !baGetMnret)
  758.         baComp->cmpFatal(ERRnoMemory);
  759.  
  760. #if MEMALLOC_DISP
  761.     printf("baGetM : alloc %04X bytes\n", sz); updateMemSize(sz);
  762. #endif
  763.  
  764.     return  block;
  765. }
  766.  
  767. void    *   block_allocator::baGet0(size_t sz)
  768. {
  769.     void    *   block;
  770.  
  771.     /* Try to allocate the block but don't toss an out-of-memory error */
  772.  
  773.     baGetMnret = true;
  774.     block = baGetM(sz);
  775.     baGetMnret = false;
  776.  
  777.     return  block;
  778. }
  779.  
  780. void        block_allocator::baRlsM(void *block)
  781. {
  782.     assert(block);          // caller should check for NULL
  783.       free(block);
  784. }
  785.  
  786. /*****************************************************************************/
  787. #ifdef DEBUG
  788. /*****************************************************************************
  789.  *
  790.  *  The following are the debug versions of the general memory allocator
  791.  *  routines. They (optionally) log information about each allocation to
  792.  *  make it easier to track down things like memory consumption, leaks,
  793.  *  and so on.
  794.  *
  795.  */
  796.  
  797. void    *           block_allocator::baAlloc      (size_t size)
  798. {
  799.     void    *       block;
  800.  
  801.     assert((size & (sizeof(int) - 1)) == 0);
  802.  
  803.     block = baGetM(size);
  804.  
  805.     return  block;
  806. }
  807.  
  808. void    *           block_allocator::baAllocOrNull(size_t size)
  809. {
  810.     void    *       block;
  811.  
  812.     assert((size & (sizeof(int) - 1)) == 0);
  813.  
  814.     block = baGet0(size);
  815.  
  816.     if  (block == NULL)
  817.         return  block;
  818.  
  819. //  recordBlockAlloc(this, block, tsize);
  820.  
  821.     return  block;
  822. }
  823.  
  824. void                block_allocator::baFree       (void *block)
  825. {
  826. //  recordBlockFree (this, block);
  827.  
  828.     baRlsM(block);
  829. }
  830.  
  831. /*****************************************************************************/
  832. #endif// DEBUG
  833. /*****************************************************************************
  834.  *
  835.  *  Display memory alloc stats.
  836.  */
  837.  
  838. #if MEMALLOC_DISP
  839.  
  840. void                dispMemAllocStats()
  841. {
  842.     printf("Total allocated memory: %8u (0x%08X) bytes.\n", totSize, totSize);
  843.     printf("Max.  allocated memory: %8u (0x%08X) bytes.\n", maxSize, maxSize);
  844. }
  845.  
  846. #endif
  847.  
  848. /*****************************************************************************/
  849.