allocator_interface.h 3.8 KB

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