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

  1. /*****************************************************************************/
  2. #ifndef _ALLOC_H_
  3. #define _ALLOC_H_
  4. /*****************************************************************************/
  5.  
  6. #pragma warning(disable:4200)
  7.  
  8. /*****************************************************************************/
  9.  
  10. #define _OS_COMMIT_ALLOC    1       // depends on host OS, use "1" for Win32
  11.  
  12. /*****************************************************************************/
  13.  
  14. void    *           LowLevelAlloc(size_t sz);
  15. void                LowLevelFree (void *blk);
  16.  
  17. /*****************************************************************************/
  18.  
  19. struct nraMarkDsc
  20. {
  21.     void    *       nmPage;
  22.     BYTE    *       nmNext;
  23.     BYTE    *       nmLast;
  24. };
  25.  
  26. // The following s/b inside 'struct norls_allocator', moved here temporarily.
  27.  
  28. struct norls_pagdesc
  29. {
  30.     norls_pagdesc * nrpNextPage;
  31.     norls_pagdesc * nrpPrevPage;
  32. #ifndef NDEBUG
  33.     void    *       nrpSelfPtr;
  34. #endif
  35.     size_t          nrpPageSize;    // # of bytes allocated
  36.     size_t          nrpUsedSize;    // # of bytes actually used
  37.     BYTE            nrpContents[];
  38. };
  39.  
  40. class norls_allocator
  41. {
  42. public:
  43.  
  44.     bool            nraInit (Compiler comp, size_t pageSize = 0,
  45.                                             bool   preAlloc = false);
  46.     bool            nraStart(Compiler comp, size_t initSize,
  47.                                             size_t pageSize = 0);
  48.  
  49.     void            nraFree ();
  50.     void            nraDone ();
  51.  
  52. private:
  53.  
  54.     Compiler        nraComp;
  55.  
  56.     norls_pagdesc * nraPageList;
  57.     norls_pagdesc * nraPageLast;
  58.  
  59.     bool            nraRetNull;         // OOM returns NULL (longjmp otherwise)
  60.  
  61.     BYTE    *       nraFreeNext;        // these two (when non-zero) will
  62.     BYTE    *       nraFreeLast;        // always point into 'nraPageLast'
  63.  
  64.     size_t          nraPageSize;
  65.  
  66.     bool            nraAllocNewPageNret;
  67.     void    *       nraAllocNewPage(size_t sz);
  68.  
  69. #ifdef  DEBUG
  70.     void    *       nraSelf;
  71. #endif
  72.  
  73. public:
  74.  
  75.     void    *       nraAlloc(size_t sz);
  76.  
  77.     /* The following used for mark/release operation */
  78.  
  79.     void            nraMark(nraMarkDsc *mark)
  80.     {
  81.         mark->nmPage = nraPageLast;
  82.         mark->nmNext = nraFreeNext;
  83.         mark->nmLast = nraFreeLast;
  84.     }
  85.  
  86. private:
  87.  
  88.     void            nraToss(nraMarkDsc *mark);
  89.  
  90. public:
  91.  
  92.     void            nraRlsm(nraMarkDsc *mark)
  93.     {
  94.         if (nraPageLast != mark->nmPage)
  95.         {
  96.             nraToss(mark);
  97.         }
  98.         else
  99.         {
  100.             nraFreeNext = mark->nmNext;
  101.             nraFreeLast = mark->nmLast;
  102.         }
  103.     }
  104.  
  105.     size_t          nraTotalSizeAlloc();
  106.     size_t          nraTotalSizeUsed ();
  107.  
  108.     /* The following used to visit all of the allocated pages */
  109.  
  110.     void    *       nraPageWalkerStart();
  111.     void    *       nraPageWalkerNext (void *page);
  112.  
  113.     void    *       nraPageGetData(void *page);
  114.     size_t          nraPageGetSize(void *page);
  115. };
  116.  
  117. #ifndef DEBUG
  118.  
  119. inline
  120. void    *           norls_allocator::nraAlloc(size_t sz)
  121. {
  122.     void    *   block;
  123.  
  124.     block = nraFreeNext;
  125.             nraFreeNext += sz;
  126.  
  127.     if  (nraFreeNext > nraFreeLast)
  128.         block = nraAllocNewPage(sz);
  129.  
  130.     return  block;
  131. }
  132.  
  133. #endif
  134.  
  135. /*****************************************************************************
  136.  *
  137.  *  This is a generic block allocator.
  138.  */
  139.  
  140. class block_allocator
  141. {
  142. public:
  143.  
  144.     bool            baInit(Compiler comp);
  145.     void            baDone();
  146.  
  147. private:
  148.  
  149.     Compiler        baComp;
  150.  
  151.     bool            baGetMnret;
  152.  
  153.     void    *       baGetM(size_t size);        // out of memory -> calls longjmp
  154.     void    *       baGet0(size_t size);        // out of memory -> returns NULL
  155.  
  156.     void            baRlsM(void *block);
  157.  
  158. public:
  159.  
  160.     void    *       baAlloc      (size_t size);
  161.     void    *       baAllocOrNull(size_t size);
  162.     void            baFree       (void *block);
  163. };
  164.  
  165. /*---------------------------------------------------------------------------*/
  166. #ifndef DEBUG
  167.  
  168. /* The non-debug case: map into calls of the non-debug routines */
  169.  
  170. inline
  171. void    *           block_allocator::baAlloc      (size_t size)
  172. {
  173.     return baGetM(size);
  174. }
  175.  
  176. inline
  177. void    *           block_allocator::baAllocOrNull(size_t size)
  178. {
  179.     return baGet0(size);
  180. }
  181.  
  182. inline
  183. void                block_allocator::baFree       (void *block)
  184. {
  185.     baRlsM(block);
  186. }
  187.  
  188. #endif
  189.  
  190. /*****************************************************************************/
  191. #endif
  192. /*****************************************************************************/
  193.