cmemory.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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-2015, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. ******************************************************************************
  10. *
  11. * File cmemory.c ICU Heap allocation.
  12. * All ICU heap allocation, both for C and C++ new of ICU
  13. * class types, comes through these functions.
  14. *
  15. * If you have a need to replace ICU allocation, this is the
  16. * place to do it.
  17. *
  18. * Note that uprv_malloc(0) returns a non-nullptr pointer,
  19. * and that a subsequent free of that pointer value is a NOP.
  20. *
  21. ******************************************************************************
  22. */
  23. #include "unicode/uclean.h"
  24. #include "cmemory.h"
  25. #include "putilimp.h"
  26. #include "uassert.h"
  27. #include <stdlib.h>
  28. /* uprv_malloc(0) returns a pointer to this read-only data. */
  29. static const int32_t zeroMem[] = {0, 0, 0, 0, 0, 0};
  30. /* Function Pointers for user-supplied heap functions */
  31. static const void *pContext;
  32. static UMemAllocFn *pAlloc;
  33. static UMemReallocFn *pRealloc;
  34. static UMemFreeFn *pFree;
  35. #if U_DEBUG && defined(UPRV_MALLOC_COUNT)
  36. #include <stdio.h>
  37. static int n=0;
  38. static long b=0;
  39. #endif
  40. U_CAPI void * U_EXPORT2
  41. uprv_malloc(size_t s) {
  42. #if U_DEBUG && defined(UPRV_MALLOC_COUNT)
  43. #if 1
  44. putchar('>');
  45. fflush(stdout);
  46. #else
  47. fprintf(stderr,"MALLOC\t#%d\t%ul bytes\t%ul total\n", ++n,s,(b+=s)); fflush(stderr);
  48. #endif
  49. #endif
  50. if (s > 0) {
  51. if (pAlloc) {
  52. return (*pAlloc)(pContext, s);
  53. } else {
  54. return uprv_default_malloc(s);
  55. }
  56. } else {
  57. return (void *)zeroMem;
  58. }
  59. }
  60. U_CAPI void * U_EXPORT2
  61. uprv_realloc(void * buffer, size_t size) {
  62. #if U_DEBUG && defined(UPRV_MALLOC_COUNT)
  63. putchar('~');
  64. fflush(stdout);
  65. #endif
  66. if (buffer == zeroMem) {
  67. return uprv_malloc(size);
  68. } else if (size == 0) {
  69. if (pFree) {
  70. (*pFree)(pContext, buffer);
  71. } else {
  72. uprv_default_free(buffer);
  73. }
  74. return (void *)zeroMem;
  75. } else {
  76. if (pRealloc) {
  77. return (*pRealloc)(pContext, buffer, size);
  78. } else {
  79. return uprv_default_realloc(buffer, size);
  80. }
  81. }
  82. }
  83. U_CAPI void U_EXPORT2
  84. uprv_free(void *buffer) {
  85. #if U_DEBUG && defined(UPRV_MALLOC_COUNT)
  86. putchar('<');
  87. fflush(stdout);
  88. #endif
  89. if (buffer != zeroMem) {
  90. if (pFree) {
  91. (*pFree)(pContext, buffer);
  92. } else {
  93. uprv_default_free(buffer);
  94. }
  95. }
  96. }
  97. U_CAPI void * U_EXPORT2
  98. uprv_calloc(size_t num, size_t size) {
  99. void *mem = nullptr;
  100. size *= num;
  101. mem = uprv_malloc(size);
  102. if (mem) {
  103. uprv_memset(mem, 0, size);
  104. }
  105. return mem;
  106. }
  107. U_CAPI void U_EXPORT2
  108. u_setMemoryFunctions(const void *context, UMemAllocFn *a, UMemReallocFn *r, UMemFreeFn *f, UErrorCode *status)
  109. {
  110. if (U_FAILURE(*status)) {
  111. return;
  112. }
  113. if (a==nullptr || r==nullptr || f==nullptr) {
  114. *status = U_ILLEGAL_ARGUMENT_ERROR;
  115. return;
  116. }
  117. pContext = context;
  118. pAlloc = a;
  119. pRealloc = r;
  120. pFree = f;
  121. }
  122. U_CFUNC UBool cmemory_cleanup() {
  123. pContext = nullptr;
  124. pAlloc = nullptr;
  125. pRealloc = nullptr;
  126. pFree = nullptr;
  127. return true;
  128. }