home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / newop.cpp < prev    next >
C/C++ Source or Header  |  1998-06-16  |  2KB  |  73 lines

  1. // newop operator new(size_t) for Microsoft C++
  2. #include <cstdlib>
  3. #include <xstddef>
  4. #include <new>
  5. #include <dbgint.h>
  6.  
  7. #if !defined(_MSC_EXTENSIONS)
  8. #define RESERVE_SIZE    256     /* for out-of-heap handling */
  9.  
  10. static void *pres = 0;
  11.  
  12. _C_LIB_DECL
  13. int _callnewh(size_t size);
  14.  
  15. #ifdef  _DLL
  16. static void __cdecl cleanup_pres(void)
  17.     {    // free reserved block
  18.     if (pres != 0)
  19.         _free_crt(pres);
  20.     }
  21. #endif
  22. _END_C_LIB_DECL
  23.  
  24. void *__cdecl operator new(size_t size) _THROW1(_STD bad_alloc)
  25.     {    // try to allocate size bytes
  26.     static void *pres = 0;
  27.         {_STD _Lockit _Lk;
  28. #ifdef  _DLL
  29.         static int firsttime = 0;
  30.         if (firsttime == 0)
  31.             {    // register routine to clean up reserve space
  32.             atexit(&cleanup_pres);
  33.             ++firsttime;
  34.             }
  35. #endif
  36.         if (pres == 0)
  37.             pres = _malloc_crt(RESERVE_SIZE);
  38.         }
  39.     void *p;
  40.     while ((p = malloc(size)) == 0)
  41.         {    // handle failure to allocate
  42.             {_STD _Lockit _Lk;
  43.             if (pres != 0)
  44.                 {    // free reserve space
  45.                 _free_crt(pres);
  46.                 pres = 0;
  47.                 }
  48.             }
  49.         if (_callnewh(size) == 0)
  50.             break;
  51.         }
  52.     if (p == 0)
  53.         _STD _Nomemory();
  54.     return (p);
  55.     }
  56.  
  57. #endif
  58.  
  59. /*
  60.  * Copyright (c) 1994 by P.J. Plauger.  ALL RIGHTS RESERVED. 
  61.  * Consult your license regarding permissions and restrictions.
  62.  */
  63.  
  64. /*
  65. 941029 pjp: added _STD machinery
  66. 950330 pjp: added throw clause
  67. 950608 pjp: added reserve space
  68. 960214 pjp: added locks
  69. 960313 pjp: tidied headers
  70. 960317 pjp: put new/delete in global namespace
  71. 961026 pjp: added logic to free reserved block
  72.  */
  73.