malloc.h 6.5 KB

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