uobject.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. ******************************************************************************
  5. *
  6. * Copyright (C) 2002-2012, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. ******************************************************************************
  10. * file name: uobject.h
  11. * encoding: UTF-8
  12. * tab size: 8 (not used)
  13. * indentation:4
  14. *
  15. * created on: 2002jun26
  16. * created by: Markus W. Scherer
  17. */
  18. #include "unicode/uobject.h"
  19. #include "cmemory.h"
  20. U_NAMESPACE_BEGIN
  21. #if U_OVERRIDE_CXX_ALLOCATION
  22. /*
  23. * Default implementation of UMemory::new/delete
  24. * using uprv_malloc() and uprv_free().
  25. *
  26. * For testing, this is used together with a list of imported symbols to verify
  27. * that ICU is not using the global ::new and ::delete operators.
  28. *
  29. * These operators can be implemented like this or any other appropriate way
  30. * when customizing ICU for certain environments.
  31. * Whenever ICU is customized in binary incompatible ways please be sure
  32. * to use library name suffixes to distinguish such libraries from
  33. * the standard build.
  34. *
  35. * Instead of just modifying these C++ new/delete operators, it is usually best
  36. * to modify the uprv_malloc()/uprv_free()/uprv_realloc() functions in cmemory.c.
  37. *
  38. * Memory test on Windows/MSVC 6:
  39. * The global operators new and delete look as follows:
  40. * 04F 00000000 UNDEF notype () External | ??2@YAPAXI@Z (void * __cdecl operator new(unsigned int))
  41. * 03F 00000000 UNDEF notype () External | ??3@YAXPAX@Z (void __cdecl operator delete(void *))
  42. *
  43. * These lines are from output generated by the MSVC 6 tool dumpbin with
  44. * dumpbin /symbols *.obj
  45. *
  46. * ??2@YAPAXI@Z and ??3@YAXPAX@Z are the linker symbols in the .obj
  47. * files and are imported from msvcrtd.dll (in a debug build).
  48. *
  49. * Make sure that with the UMemory operators new and delete defined these two symbols
  50. * do not appear in the dumpbin /symbols output for the ICU libraries!
  51. *
  52. * If such a symbol appears in the output then look in the preceding lines in the output
  53. * for which file and function calls the global new or delete operator,
  54. * and replace with uprv_malloc/uprv_free.
  55. */
  56. void * U_EXPORT2 UMemory::operator new(size_t size) noexcept {
  57. return uprv_malloc(size);
  58. }
  59. void U_EXPORT2 UMemory::operator delete(void *p) noexcept {
  60. if(p!=nullptr) {
  61. uprv_free(p);
  62. }
  63. }
  64. void * U_EXPORT2 UMemory::operator new[](size_t size) noexcept {
  65. return uprv_malloc(size);
  66. }
  67. void U_EXPORT2 UMemory::operator delete[](void *p) noexcept {
  68. if(p!=nullptr) {
  69. uprv_free(p);
  70. }
  71. }
  72. #if U_HAVE_DEBUG_LOCATION_NEW
  73. void * U_EXPORT2 UMemory::operator new(size_t size, const char* /*file*/, int /*line*/) noexcept {
  74. return UMemory::operator new(size);
  75. }
  76. void U_EXPORT2 UMemory::operator delete(void* p, const char* /*file*/, int /*line*/) noexcept {
  77. UMemory::operator delete(p);
  78. }
  79. #endif /* U_HAVE_DEBUG_LOCATION_NEW */
  80. #endif
  81. UObject::~UObject() {}
  82. UClassID UObject::getDynamicClassID() const { return nullptr; }
  83. U_NAMESPACE_END
  84. U_NAMESPACE_USE
  85. U_CAPI void U_EXPORT2
  86. uprv_deleteUObject(void *obj) {
  87. delete static_cast<UObject *>(obj);
  88. }