malloc.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * This file has no copyright assigned and is placed in the Public Domain.
  3. * This file is part of the w64 mingw-runtime package.
  4. * No warranty is given; refer to the file DISCLAIMER within this package.
  5. */
  6. #ifndef _MALLOC_H_
  7. #define _MALLOC_H_
  8. #include <_mingw.h>
  9. #pragma pack(push,_CRT_PACKING)
  10. #ifndef _MM_MALLOC_H_INCLUDED
  11. #define _MM_MALLOC_H_INCLUDED
  12. #endif
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. #ifdef _WIN64
  17. #define _HEAP_MAXREQ 0xFFFFFFFFFFFFFFE0
  18. #else
  19. #define _HEAP_MAXREQ 0xFFFFFFE0
  20. #endif
  21. #ifndef _STATIC_ASSERT
  22. #define _STATIC_ASSERT(expr) extern void __static_assert_t(int [(expr)?1:-1])
  23. #endif
  24. /* Return codes for _heapwalk() */
  25. #define _HEAPEMPTY (-1)
  26. #define _HEAPOK (-2)
  27. #define _HEAPBADBEGIN (-3)
  28. #define _HEAPBADNODE (-4)
  29. #define _HEAPEND (-5)
  30. #define _HEAPBADPTR (-6)
  31. /* Values for _heapinfo.useflag */
  32. #define _FREEENTRY 0
  33. #define _USEDENTRY 1
  34. #ifndef _HEAPINFO_DEFINED
  35. #define _HEAPINFO_DEFINED
  36. /* The structure used to walk through the heap with _heapwalk. */
  37. typedef struct _heapinfo {
  38. int *_pentry;
  39. size_t _size;
  40. int _useflag;
  41. } _HEAPINFO;
  42. #endif
  43. extern unsigned int _amblksiz;
  44. #define _mm_free(a) _aligned_free(a)
  45. #define _mm_malloc(a,b) _aligned_malloc(a,b)
  46. #ifndef _CRT_ALLOCATION_DEFINED
  47. #define _CRT_ALLOCATION_DEFINED
  48. void *__cdecl calloc(size_t _NumOfElements,size_t _SizeOfElements);
  49. void __cdecl free(void *_Memory);
  50. void *__cdecl malloc(size_t _Size);
  51. void *__cdecl realloc(void *_Memory,size_t _NewSize);
  52. _CRTIMP void *__cdecl _recalloc(void *_Memory,size_t _Count,size_t _Size);
  53. /* _CRTIMP void __cdecl _aligned_free(void *_Memory);
  54. _CRTIMP void *__cdecl _aligned_malloc(size_t _Size,size_t _Alignment); */
  55. _CRTIMP void *__cdecl _aligned_offset_malloc(size_t _Size,size_t _Alignment,size_t _Offset);
  56. _CRTIMP void *__cdecl _aligned_realloc(void *_Memory,size_t _Size,size_t _Alignment);
  57. _CRTIMP void *__cdecl _aligned_recalloc(void *_Memory,size_t _Count,size_t _Size,size_t _Alignment);
  58. _CRTIMP void *__cdecl _aligned_offset_realloc(void *_Memory,size_t _Size,size_t _Alignment,size_t _Offset);
  59. _CRTIMP void *__cdecl _aligned_offset_recalloc(void *_Memory,size_t _Count,size_t _Size,size_t _Alignment,size_t _Offset);
  60. #endif
  61. #define _MAX_WAIT_MALLOC_CRT 60000
  62. _CRTIMP int __cdecl _resetstkoflw (void);
  63. _CRTIMP unsigned long __cdecl _set_malloc_crt_max_wait(unsigned long _NewValue);
  64. _CRTIMP void *__cdecl _expand(void *_Memory,size_t _NewSize);
  65. _CRTIMP size_t __cdecl _msize(void *_Memory);
  66. #ifdef __GNUC__
  67. #undef _alloca
  68. #define _alloca(x) __builtin_alloca((x))
  69. #else
  70. /* tcc implements alloca internally and exposes it (since commit d778bde7).
  71. /* alloca is declared at include/stddef.h (which is distributed with tcc).
  72. */
  73. #ifdef _alloca
  74. #undef _alloca
  75. #endif
  76. #define _alloca(x) alloca((x))
  77. #endif
  78. _CRTIMP size_t __cdecl _get_sbh_threshold(void);
  79. _CRTIMP int __cdecl _set_sbh_threshold(size_t _NewValue);
  80. _CRTIMP errno_t __cdecl _set_amblksiz(size_t _Value);
  81. _CRTIMP errno_t __cdecl _get_amblksiz(size_t *_Value);
  82. _CRTIMP int __cdecl _heapadd(void *_Memory,size_t _Size);
  83. _CRTIMP int __cdecl _heapchk(void);
  84. _CRTIMP int __cdecl _heapmin(void);
  85. _CRTIMP int __cdecl _heapset(unsigned int _Fill);
  86. _CRTIMP int __cdecl _heapwalk(_HEAPINFO *_EntryInfo);
  87. _CRTIMP size_t __cdecl _heapused(size_t *_Used,size_t *_Commit);
  88. _CRTIMP intptr_t __cdecl _get_heap_handle(void);
  89. #define _ALLOCA_S_THRESHOLD 1024
  90. #define _ALLOCA_S_STACK_MARKER 0xCCCC
  91. #define _ALLOCA_S_HEAP_MARKER 0xDDDD
  92. #if(defined(_X86_) && !defined(__x86_64))
  93. #define _ALLOCA_S_MARKER_SIZE 8
  94. #elif defined(__ia64__) || defined(__x86_64)
  95. #define _ALLOCA_S_MARKER_SIZE 16
  96. #endif
  97. #if !defined(RC_INVOKED)
  98. static __inline void *_MarkAllocaS(void *_Ptr,unsigned int _Marker) {
  99. if(_Ptr) {
  100. *((unsigned int*)_Ptr) = _Marker;
  101. _Ptr = (char*)_Ptr + _ALLOCA_S_MARKER_SIZE;
  102. }
  103. return _Ptr;
  104. }
  105. #endif
  106. #undef _malloca
  107. #define _malloca(size) \
  108. ((((size) + _ALLOCA_S_MARKER_SIZE) <= _ALLOCA_S_THRESHOLD) ? \
  109. _MarkAllocaS(_alloca((size) + _ALLOCA_S_MARKER_SIZE),_ALLOCA_S_STACK_MARKER) : \
  110. _MarkAllocaS(malloc((size) + _ALLOCA_S_MARKER_SIZE),_ALLOCA_S_HEAP_MARKER))
  111. #undef _FREEA_INLINE
  112. #define _FREEA_INLINE
  113. #ifndef RC_INVOKED
  114. #undef _freea
  115. static __inline void __cdecl _freea(void *_Memory) {
  116. unsigned int _Marker;
  117. if(_Memory) {
  118. _Memory = (char*)_Memory - _ALLOCA_S_MARKER_SIZE;
  119. _Marker = *(unsigned int *)_Memory;
  120. if(_Marker==_ALLOCA_S_HEAP_MARKER) {
  121. free(_Memory);
  122. }
  123. #ifdef _ASSERTE
  124. else if(_Marker!=_ALLOCA_S_STACK_MARKER) {
  125. _ASSERTE(("Corrupted pointer passed to _freea",0));
  126. }
  127. #endif
  128. }
  129. }
  130. #endif /* RC_INVOKED */
  131. #ifndef NO_OLDNAMES
  132. #ifdef __GNUC__
  133. #undef alloca
  134. #define alloca(x) __builtin_alloca((x))
  135. #endif
  136. #endif
  137. #ifdef HEAPHOOK
  138. #ifndef _HEAPHOOK_DEFINED
  139. #define _HEAPHOOK_DEFINED
  140. typedef int (__cdecl *_HEAPHOOK)(int,size_t,void *,void **);
  141. #endif
  142. _CRTIMP _HEAPHOOK __cdecl _setheaphook(_HEAPHOOK _NewHook);
  143. #define _HEAP_MALLOC 1
  144. #define _HEAP_CALLOC 2
  145. #define _HEAP_FREE 3
  146. #define _HEAP_REALLOC 4
  147. #define _HEAP_MSIZE 5
  148. #define _HEAP_EXPAND 6
  149. #endif
  150. #ifdef __cplusplus
  151. }
  152. #endif
  153. #pragma pack(pop)
  154. #endif /* _MALLOC_H_ */