FastMallocWinCE.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
  3. * Copyright (C) 2007-2009 Torch Mobile, Inc. All rights reserved
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public License
  16. * along with this library; see the file COPYING.LIB. If not, write to
  17. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18. * Boston, MA 02110-1301, USA.
  19. *
  20. */
  21. #ifndef WTF_FastMallocWinCE_h
  22. #define WTF_FastMallocWinCE_h
  23. #include <new.h>
  24. #ifdef __cplusplus
  25. #include <new>
  26. #include <wtf/wince/MemoryManager.h>
  27. extern "C" {
  28. #endif
  29. void* fastMalloc(size_t n);
  30. void* fastCalloc(size_t n_elements, size_t element_size);
  31. void fastFree(void* p);
  32. void* fastRealloc(void* p, size_t n);
  33. void* fastZeroedMalloc(size_t n);
  34. // These functions return 0 if an allocation fails.
  35. void* tryFastMalloc(size_t n);
  36. void* tryFastZeroedMalloc(size_t n);
  37. void* tryFastCalloc(size_t n_elements, size_t element_size);
  38. void* tryFastRealloc(void* p, size_t n);
  39. char* fastStrDup(const char*);
  40. #ifndef NDEBUG
  41. void fastMallocForbid();
  42. void fastMallocAllow();
  43. #endif
  44. #if !defined(USE_SYSTEM_MALLOC) || !USE_SYSTEM_MALLOC
  45. #define malloc(n) fastMalloc(n)
  46. #define calloc(n_elements, element_size) fastCalloc(n_elements, element_size)
  47. #define realloc(p, n) fastRealloc(p, n)
  48. #define free(p) fastFree(p)
  49. #define strdup(p) fastStrDup(p)
  50. #else
  51. #define strdup(p) _strdup(p)
  52. #endif
  53. #ifdef __cplusplus
  54. }
  55. #endif
  56. #ifdef __cplusplus
  57. #if !defined(USE_SYSTEM_MALLOC) || !USE_SYSTEM_MALLOC
  58. static inline void* __cdecl operator new(size_t s) { return fastMalloc(s); }
  59. static inline void __cdecl operator delete(void* p) { fastFree(p); }
  60. static inline void* __cdecl operator new[](size_t s) { return fastMalloc(s); }
  61. static inline void __cdecl operator delete[](void* p) { fastFree(p); }
  62. static inline void* operator new(size_t s, const std::nothrow_t&) throw() { return fastMalloc(s); }
  63. static inline void operator delete(void* p, const std::nothrow_t&) throw() { fastFree(p); }
  64. static inline void* operator new[](size_t s, const std::nothrow_t&) throw() { return fastMalloc(s); }
  65. static inline void operator delete[](void* p, const std::nothrow_t&) throw() { fastFree(p); }
  66. #endif
  67. namespace WTF {
  68. // This defines a type which holds an unsigned integer and is the same
  69. // size as the minimally aligned memory allocation.
  70. typedef unsigned long long AllocAlignmentInteger;
  71. namespace Internal {
  72. enum AllocType { // Start with an unusual number instead of zero, because zero is common.
  73. AllocTypeMalloc = 0x375d6750, // Encompasses fastMalloc, fastZeroedMalloc, fastCalloc, fastRealloc.
  74. AllocTypeClassNew, // Encompasses class operator new from FastAllocBase.
  75. AllocTypeClassNewArray, // Encompasses class operator new[] from FastAllocBase.
  76. AllocTypeFastNew, // Encompasses fastNew.
  77. AllocTypeFastNewArray, // Encompasses fastNewArray.
  78. AllocTypeNew, // Encompasses global operator new.
  79. AllocTypeNewArray // Encompasses global operator new[].
  80. };
  81. }
  82. #if ENABLE(FAST_MALLOC_MATCH_VALIDATION)
  83. // Malloc validation is a scheme whereby a tag is attached to an
  84. // allocation which identifies how it was originally allocated.
  85. // This allows us to verify that the freeing operation matches the
  86. // allocation operation. If memory is allocated with operator new[]
  87. // but freed with free or delete, this system would detect that.
  88. // In the implementation here, the tag is an integer prepended to
  89. // the allocation memory which is assigned one of the AllocType
  90. // enumeration values. An alternative implementation of this
  91. // scheme could store the tag somewhere else or ignore it.
  92. // Users of FastMalloc don't need to know or care how this tagging
  93. // is implemented.
  94. namespace Internal {
  95. // Return the AllocType tag associated with the allocated block p.
  96. inline AllocType fastMallocMatchValidationType(const void* p)
  97. {
  98. const AllocAlignmentInteger* type = static_cast<const AllocAlignmentInteger*>(p) - 1;
  99. return static_cast<AllocType>(*type);
  100. }
  101. // Return the address of the AllocType tag associated with the allocated block p.
  102. inline AllocAlignmentInteger* fastMallocMatchValidationValue(void* p)
  103. {
  104. return reinterpret_cast<AllocAlignmentInteger*>(static_cast<char*>(p) - sizeof(AllocAlignmentInteger));
  105. }
  106. // Set the AllocType tag to be associaged with the allocated block p.
  107. inline void setFastMallocMatchValidationType(void* p, AllocType allocType)
  108. {
  109. AllocAlignmentInteger* type = static_cast<AllocAlignmentInteger*>(p) - 1;
  110. *type = static_cast<AllocAlignmentInteger>(allocType);
  111. }
  112. // Handle a detected alloc/free mismatch. By default this calls CRASH().
  113. void fastMallocMatchFailed(void* p);
  114. } // namespace Internal
  115. // This is a higher level function which is used by FastMalloc-using code.
  116. inline void fastMallocMatchValidateMalloc(void* p, Internal::AllocType allocType)
  117. {
  118. if (!p)
  119. return;
  120. Internal::setFastMallocMatchValidationType(p, allocType);
  121. }
  122. // This is a higher level function which is used by FastMalloc-using code.
  123. inline void fastMallocMatchValidateFree(void* p, Internal::AllocType allocType)
  124. {
  125. if (!p)
  126. return;
  127. if (Internal::fastMallocMatchValidationType(p) != allocType)
  128. Internal::fastMallocMatchFailed(p);
  129. Internal::setFastMallocMatchValidationType(p, Internal::AllocTypeMalloc); // Set it to this so that fastFree thinks it's OK.
  130. }
  131. #else
  132. inline void fastMallocMatchValidateMalloc(void*, Internal::AllocType)
  133. {
  134. }
  135. inline void fastMallocMatchValidateFree(void*, Internal::AllocType)
  136. {
  137. }
  138. #endif
  139. } // namespace WTF
  140. #endif
  141. #endif // WTF_FastMallocWinCE_h