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.

72 lines
1.5 KiB

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