gc_cpp.cc 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (c) 1994 by Xerox Corporation. All rights reserved.
  3. *
  4. * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  5. * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
  6. *
  7. * Permission is hereby granted to copy this code for any purpose,
  8. * provided the above notices are retained on all copies.
  9. */
  10. /*************************************************************************
  11. This implementation module for gc_c++.h provides an implementation of
  12. the global operators "new" and "delete" that calls the Boehm
  13. allocator. All objects allocated by this implementation will be
  14. uncollectible but part of the root set of the collector.
  15. You should ensure (using implementation-dependent techniques) that the
  16. linker finds this module before the library that defines the default
  17. built-in "new" and "delete".
  18. **************************************************************************/
  19. #ifdef HAVE_CONFIG_H
  20. # include "config.h"
  21. #endif
  22. #ifndef GC_BUILD
  23. # define GC_BUILD
  24. #endif
  25. #include "gc_cpp.h"
  26. #if !defined(GC_NEW_DELETE_NEED_THROW) && defined(__GNUC__) \
  27. && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2))
  28. # define GC_NEW_DELETE_NEED_THROW
  29. #endif
  30. #ifdef GC_NEW_DELETE_NEED_THROW
  31. # include <new> /* for std::bad_alloc */
  32. # define GC_DECL_NEW_THROW throw(std::bad_alloc)
  33. # define GC_DECL_DELETE_THROW throw()
  34. #else
  35. # define GC_DECL_NEW_THROW /* empty */
  36. # define GC_DECL_DELETE_THROW /* empty */
  37. #endif /* !GC_NEW_DELETE_NEED_THROW */
  38. void* operator new( size_t size ) GC_DECL_NEW_THROW {
  39. return GC_MALLOC_UNCOLLECTABLE(size);
  40. }
  41. #if !defined(__CYGWIN__)
  42. void operator delete( void* obj ) GC_DECL_DELETE_THROW {
  43. GC_FREE(obj);
  44. }
  45. #endif /* !__CYGWIN__ */
  46. #ifdef GC_OPERATOR_NEW_ARRAY
  47. void* operator new[]( size_t size ) GC_DECL_NEW_THROW {
  48. return GC_MALLOC_UNCOLLECTABLE(size);
  49. }
  50. void operator delete[]( void* obj ) GC_DECL_DELETE_THROW {
  51. GC_FREE(obj);
  52. }
  53. #endif /* GC_OPERATOR_NEW_ARRAY */
  54. #ifdef _MSC_VER
  55. // This new operator is used by VC++ in case of Debug builds!
  56. void* operator new( size_t size, int /* nBlockUse */,
  57. const char * szFileName, int nLine ) GC_DECL_NEW_THROW
  58. {
  59. # ifndef GC_DEBUG
  60. return GC_malloc_uncollectable(size);
  61. # else
  62. return GC_debug_malloc_uncollectable(size, szFileName, nLine);
  63. # endif
  64. }
  65. # if _MSC_VER > 1020
  66. // This new operator is used by VC++ 7.0 and later in Debug builds.
  67. void* operator new[]( size_t size, int nBlockUse,
  68. const char* szFileName, int nLine ) GC_DECL_NEW_THROW
  69. {
  70. return operator new(size, nBlockUse, szFileName, nLine);
  71. }
  72. # endif
  73. #endif /* _MSC_VER */