allocator_interface.h 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //===-- allocator_interface.h ---------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Public interface header for allocator used in sanitizers (ASan/TSan/MSan).
  10. //===----------------------------------------------------------------------===//
  11. #ifndef SANITIZER_ALLOCATOR_INTERFACE_H
  12. #define SANITIZER_ALLOCATOR_INTERFACE_H
  13. #include <stddef.h>
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. /* Returns the estimated number of bytes that will be reserved by allocator
  18. for request of "size" bytes. If allocator can't allocate that much
  19. memory, returns the maximal possible allocation size, otherwise returns
  20. "size". */
  21. size_t __sanitizer_get_estimated_allocated_size(size_t size);
  22. /* Returns true if p was returned by the allocator and
  23. is not yet freed. */
  24. int __sanitizer_get_ownership(const volatile void *p);
  25. /* Returns the number of bytes reserved for the pointer p.
  26. Requires (get_ownership(p) == true) or (p == 0). */
  27. size_t __sanitizer_get_allocated_size(const volatile void *p);
  28. /* Number of bytes, allocated and not yet freed by the application. */
  29. size_t __sanitizer_get_current_allocated_bytes(void);
  30. /* Number of bytes, mmaped by the allocator to fulfill allocation requests.
  31. Generally, for request of X bytes, allocator can reserve and add to free
  32. lists a large number of chunks of size X to use them for future requests.
  33. All these chunks count toward the heap size. Currently, allocator never
  34. releases memory to OS (instead, it just puts freed chunks to free
  35. lists). */
  36. size_t __sanitizer_get_heap_size(void);
  37. /* Number of bytes, mmaped by the allocator, which can be used to fulfill
  38. allocation requests. When a user program frees memory chunk, it can first
  39. fall into quarantine and will count toward __sanitizer_get_free_bytes()
  40. later. */
  41. size_t __sanitizer_get_free_bytes(void);
  42. /* Number of bytes in unmapped pages, that are released to OS. Currently,
  43. always returns 0. */
  44. size_t __sanitizer_get_unmapped_bytes(void);
  45. /* Malloc hooks that may be optionally provided by user.
  46. __sanitizer_malloc_hook(ptr, size) is called immediately after
  47. allocation of "size" bytes, which returned "ptr".
  48. __sanitizer_free_hook(ptr) is called immediately before
  49. deallocation of "ptr". */
  50. void __sanitizer_malloc_hook(const volatile void *ptr, size_t size);
  51. void __sanitizer_free_hook(const volatile void *ptr);
  52. /* Installs a pair of hooks for malloc/free.
  53. Several (currently, 5) hook pairs may be installed, they are executed
  54. in the order they were installed and after calling
  55. __sanitizer_malloc_hook/__sanitizer_free_hook.
  56. Unlike __sanitizer_malloc_hook/__sanitizer_free_hook these hooks can be
  57. chained and do not rely on weak symbols working on the platform, but
  58. require __sanitizer_install_malloc_and_free_hooks to be called at startup
  59. and thus will not be called on malloc/free very early in the process.
  60. Returns the number of hooks currently installed or 0 on failure.
  61. Not thread-safe, should be called in the main thread before starting
  62. other threads.
  63. */
  64. int __sanitizer_install_malloc_and_free_hooks(
  65. void (*malloc_hook)(const volatile void *, size_t),
  66. void (*free_hook)(const volatile void *));
  67. /* Drains allocator quarantines (calling thread's and global ones), returns
  68. freed memory back to OS and releases other non-essential internal allocator
  69. resources in attempt to reduce process RSS.
  70. Currently available with ASan only.
  71. */
  72. void __sanitizer_purge_allocator(void);
  73. #ifdef __cplusplus
  74. } // extern "C"
  75. #endif
  76. #endif