system-alloc.h 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. // Copyright (c) 2005, Google Inc.
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // ---
  31. // Author: Sanjay Ghemawat
  32. //
  33. // Routine that uses sbrk/mmap to allocate memory from the system.
  34. // Useful for implementing malloc.
  35. #ifndef TCMALLOC_SYSTEM_ALLOC_H_
  36. #define TCMALLOC_SYSTEM_ALLOC_H_
  37. #include <config.h>
  38. #include <stddef.h> // for size_t
  39. class SysAllocator;
  40. // REQUIRES: "alignment" is a power of two or "0" to indicate default alignment
  41. //
  42. // Allocate and return "N" bytes of zeroed memory.
  43. //
  44. // If actual_bytes is NULL then the returned memory is exactly the
  45. // requested size. If actual bytes is non-NULL then the allocator
  46. // may optionally return more bytes than asked for (i.e. return an
  47. // entire "huge" page if a huge page allocator is in use).
  48. //
  49. // The returned pointer is a multiple of "alignment" if non-zero. The
  50. // returned pointer will always be aligned suitably for holding a
  51. // void*, double, or size_t. In addition, if this platform defines
  52. // CACHELINE_ALIGNED, the return pointer will always be cacheline
  53. // aligned.
  54. //
  55. // Returns NULL when out of memory.
  56. extern PERFTOOLS_DLL_DECL
  57. void* TCMalloc_SystemAlloc(size_t bytes, size_t *actual_bytes,
  58. size_t alignment = 0);
  59. // This call is a hint to the operating system that the pages
  60. // contained in the specified range of memory will not be used for a
  61. // while, and can be released for use by other processes or the OS.
  62. // Pages which are released in this way may be destroyed (zeroed) by
  63. // the OS. The benefit of this function is that it frees memory for
  64. // use by the system, the cost is that the pages are faulted back into
  65. // the address space next time they are touched, which can impact
  66. // performance. (Only pages fully covered by the memory region will
  67. // be released, partial pages will not.)
  68. //
  69. // Returns false if release failed or not supported.
  70. extern PERFTOOLS_DLL_DECL
  71. bool TCMalloc_SystemRelease(void* start, size_t length);
  72. // Called to ressurect memory which has been previously released
  73. // to the system via TCMalloc_SystemRelease. An attempt to
  74. // commit a page that is already committed does not cause this
  75. // function to fail.
  76. extern PERFTOOLS_DLL_DECL
  77. void TCMalloc_SystemCommit(void* start, size_t length);
  78. // The current system allocator.
  79. extern PERFTOOLS_DLL_DECL SysAllocator* sys_alloc;
  80. // Number of bytes taken from system.
  81. extern PERFTOOLS_DLL_DECL size_t TCMalloc_SystemTaken;
  82. #endif /* TCMALLOC_SYSTEM_ALLOC_H_ */