malloc.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* Prototypes and definition for malloc implementation.
  2. Copyright (C) 1996-2021 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifndef _MALLOC_H
  16. #define _MALLOC_H 1
  17. #include <features.h>
  18. #include <stddef.h>
  19. #include <stdio.h>
  20. #ifdef _LIBC
  21. # define __MALLOC_HOOK_VOLATILE
  22. # define __MALLOC_DEPRECATED
  23. #else
  24. # define __MALLOC_HOOK_VOLATILE volatile
  25. # define __MALLOC_DEPRECATED __attribute_deprecated__
  26. #endif
  27. __BEGIN_DECLS
  28. /* Allocate SIZE bytes of memory. */
  29. extern void *malloc (size_t __size) __THROW __attribute_malloc__
  30. __attribute_alloc_size__ ((1)) __wur;
  31. /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */
  32. extern void *calloc (size_t __nmemb, size_t __size)
  33. __THROW __attribute_malloc__ __attribute_alloc_size__ ((1, 2)) __wur;
  34. /* Re-allocate the previously allocated block in __ptr, making the new
  35. block SIZE bytes long. */
  36. /* __attribute_malloc__ is not used, because if realloc returns
  37. the same pointer that was passed to it, aliasing needs to be allowed
  38. between objects pointed by the old and new pointers. */
  39. extern void *realloc (void *__ptr, size_t __size)
  40. __THROW __attribute_warn_unused_result__ __attribute_alloc_size__ ((2));
  41. /* Re-allocate the previously allocated block in PTR, making the new
  42. block large enough for NMEMB elements of SIZE bytes each. */
  43. /* __attribute_malloc__ is not used, because if reallocarray returns
  44. the same pointer that was passed to it, aliasing needs to be allowed
  45. between objects pointed by the old and new pointers. */
  46. extern void *reallocarray (void *__ptr, size_t __nmemb, size_t __size)
  47. __THROW __attribute_warn_unused_result__ __attribute_alloc_size__ ((2, 3));
  48. /* Free a block allocated by `malloc', `realloc' or `calloc'. */
  49. extern void free (void *__ptr) __THROW;
  50. /* Allocate SIZE bytes allocated to ALIGNMENT bytes. */
  51. extern void *memalign (size_t __alignment, size_t __size)
  52. __THROW __attribute_malloc__ __attribute_alloc_size__ ((2)) __wur;
  53. /* Allocate SIZE bytes on a page boundary. */
  54. extern void *valloc (size_t __size) __THROW __attribute_malloc__
  55. __attribute_alloc_size__ ((1)) __wur;
  56. /* Equivalent to valloc(minimum-page-that-holds(n)), that is, round up
  57. __size to nearest pagesize. */
  58. extern void *pvalloc (size_t __size) __THROW __attribute_malloc__ __wur;
  59. /* Underlying allocation function; successive calls should return
  60. contiguous pieces of memory. */
  61. extern void *(*__morecore) (ptrdiff_t __size) __MALLOC_DEPRECATED;
  62. /* Default value of `__morecore'. */
  63. extern void *__default_morecore (ptrdiff_t __size)
  64. __THROW __attribute_malloc__ __MALLOC_DEPRECATED;
  65. /* SVID2/XPG mallinfo structure */
  66. struct mallinfo
  67. {
  68. int arena; /* non-mmapped space allocated from system */
  69. int ordblks; /* number of free chunks */
  70. int smblks; /* number of fastbin blocks */
  71. int hblks; /* number of mmapped regions */
  72. int hblkhd; /* space in mmapped regions */
  73. int usmblks; /* always 0, preserved for backwards compatibility */
  74. int fsmblks; /* space available in freed fastbin blocks */
  75. int uordblks; /* total allocated space */
  76. int fordblks; /* total free space */
  77. int keepcost; /* top-most, releasable (via malloc_trim) space */
  78. };
  79. /* SVID2/XPG mallinfo2 structure which can handle allocations
  80. bigger than 4GB. */
  81. struct mallinfo2
  82. {
  83. size_t arena; /* non-mmapped space allocated from system */
  84. size_t ordblks; /* number of free chunks */
  85. size_t smblks; /* number of fastbin blocks */
  86. size_t hblks; /* number of mmapped regions */
  87. size_t hblkhd; /* space in mmapped regions */
  88. size_t usmblks; /* always 0, preserved for backwards compatibility */
  89. size_t fsmblks; /* space available in freed fastbin blocks */
  90. size_t uordblks; /* total allocated space */
  91. size_t fordblks; /* total free space */
  92. size_t keepcost; /* top-most, releasable (via malloc_trim) space */
  93. };
  94. /* Returns a copy of the updated current mallinfo. */
  95. extern struct mallinfo mallinfo (void) __THROW __MALLOC_DEPRECATED;
  96. /* Returns a copy of the updated current mallinfo. */
  97. extern struct mallinfo2 mallinfo2 (void) __THROW;
  98. /* SVID2/XPG mallopt options */
  99. #ifndef M_MXFAST
  100. # define M_MXFAST 1 /* maximum request size for "fastbins" */
  101. #endif
  102. #ifndef M_NLBLKS
  103. # define M_NLBLKS 2 /* UNUSED in this malloc */
  104. #endif
  105. #ifndef M_GRAIN
  106. # define M_GRAIN 3 /* UNUSED in this malloc */
  107. #endif
  108. #ifndef M_KEEP
  109. # define M_KEEP 4 /* UNUSED in this malloc */
  110. #endif
  111. /* mallopt options that actually do something */
  112. #define M_TRIM_THRESHOLD -1
  113. #define M_TOP_PAD -2
  114. #define M_MMAP_THRESHOLD -3
  115. #define M_MMAP_MAX -4
  116. #define M_CHECK_ACTION -5
  117. #define M_PERTURB -6
  118. #define M_ARENA_TEST -7
  119. #define M_ARENA_MAX -8
  120. /* General SVID/XPG interface to tunable parameters. */
  121. extern int mallopt (int __param, int __val) __THROW;
  122. /* Release all but __pad bytes of freed top-most memory back to the
  123. system. Return 1 if successful, else 0. */
  124. extern int malloc_trim (size_t __pad) __THROW;
  125. /* Report the number of usable allocated bytes associated with allocated
  126. chunk __ptr. */
  127. extern size_t malloc_usable_size (void *__ptr) __THROW;
  128. /* Prints brief summary statistics on stderr. */
  129. extern void malloc_stats (void) __THROW;
  130. /* Output information about state of allocator to stream FP. */
  131. extern int malloc_info (int __options, FILE *__fp) __THROW;
  132. /* Hooks for debugging and user-defined versions. */
  133. extern void (*__MALLOC_HOOK_VOLATILE __free_hook) (void *__ptr,
  134. const void *)
  135. __MALLOC_DEPRECATED;
  136. extern void *(*__MALLOC_HOOK_VOLATILE __malloc_hook)(size_t __size,
  137. const void *)
  138. __MALLOC_DEPRECATED;
  139. extern void *(*__MALLOC_HOOK_VOLATILE __realloc_hook)(void *__ptr,
  140. size_t __size,
  141. const void *)
  142. __MALLOC_DEPRECATED;
  143. extern void *(*__MALLOC_HOOK_VOLATILE __memalign_hook)(size_t __alignment,
  144. size_t __size,
  145. const void *)
  146. __MALLOC_DEPRECATED;
  147. extern void (*__MALLOC_HOOK_VOLATILE __after_morecore_hook) (void)
  148. __MALLOC_DEPRECATED;
  149. __END_DECLS
  150. #endif /* malloc.h */