OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2005, Google Inc. |
| 2 // All rights reserved. |
| 3 // |
| 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted provided that the following conditions are |
| 6 // met: |
| 7 // |
| 8 // * Redistributions of source code must retain the above copyright |
| 9 // notice, this list of conditions and the following disclaimer. |
| 10 // * Redistributions in binary form must reproduce the above |
| 11 // copyright notice, this list of conditions and the following disclaimer |
| 12 // in the documentation and/or other materials provided with the |
| 13 // distribution. |
| 14 // * Neither the name of Google Inc. nor the names of its |
| 15 // contributors may be used to endorse or promote products derived from |
| 16 // this software without specific prior written permission. |
| 17 // |
| 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
| 30 // --- |
| 31 // Author: Sanjay Ghemawat |
| 32 // |
| 33 // Routine that uses sbrk/mmap to allocate memory from the system. |
| 34 // Useful for implementing malloc. |
| 35 |
| 36 #ifndef TCMALLOC_SYSTEM_ALLOC_H_ |
| 37 #define TCMALLOC_SYSTEM_ALLOC_H_ |
| 38 |
| 39 #include <config.h> |
| 40 #include "internal_logging.h" |
| 41 |
| 42 // REQUIRES: "alignment" is a power of two or "0" to indicate default alignment |
| 43 // |
| 44 // Allocate and return "N" bytes of zeroed memory. |
| 45 // |
| 46 // If actual_bytes is NULL then the returned memory is exactly the |
| 47 // requested size. If actual bytes is non-NULL then the allocator |
| 48 // may optionally return more bytes than asked for (i.e. return an |
| 49 // entire "huge" page if a huge page allocator is in use). |
| 50 // |
| 51 // The returned pointer is a multiple of "alignment" if non-zero. |
| 52 // |
| 53 // Returns NULL when out of memory. |
| 54 extern void* TCMalloc_SystemAlloc(size_t bytes, size_t *actual_bytes, |
| 55 size_t alignment = 0); |
| 56 |
| 57 // This call is a hint to the operating system that the pages |
| 58 // contained in the specified range of memory will not be used for a |
| 59 // while, and can be released for use by other processes or the OS. |
| 60 // Pages which are released in this way may be destroyed (zeroed) by |
| 61 // the OS. The benefit of this function is that it frees memory for |
| 62 // use by the system, the cost is that the pages are faulted back into |
| 63 // the address space next time they are touched, which can impact |
| 64 // performance. (Only pages fully covered by the memory region will |
| 65 // be released, partial pages will not.) |
| 66 extern void TCMalloc_SystemRelease(void* start, size_t length); |
| 67 |
| 68 // Called to ressurect memory which has been previously released |
| 69 // to the system via TCMalloc_SystemRelease. An attempt to |
| 70 // commit a page that is already committed does not cause this |
| 71 // function to fail. |
| 72 extern void TCMalloc_SystemCommit(void* start, size_t length); |
| 73 |
| 74 // Interface to a pluggable system allocator. |
| 75 class SysAllocator { |
| 76 public: |
| 77 SysAllocator() |
| 78 : usable_(true), |
| 79 failed_(false) { |
| 80 }; |
| 81 virtual ~SysAllocator() {}; |
| 82 |
| 83 virtual void* Alloc(size_t size, size_t *actual_size, size_t alignment) = 0; |
| 84 |
| 85 // Populate the map with whatever properties the specified allocator finds |
| 86 // useful for debugging (such as number of bytes allocated and whether the |
| 87 // allocator has failed). The callee is responsible for any necessary |
| 88 // locking (and avoiding deadlock). |
| 89 virtual void DumpStats(TCMalloc_Printer* printer) = 0; |
| 90 |
| 91 // So the allocator can be turned off at compile time |
| 92 bool usable_; |
| 93 |
| 94 // Did this allocator fail? If so, we don't need to retry more than twice. |
| 95 bool failed_; |
| 96 }; |
| 97 |
| 98 // Register a new system allocator. The priority determines the order in |
| 99 // which the allocators will be invoked. Allocators with numerically lower |
| 100 // priority are tried first. To keep things simple, the priority of various |
| 101 // allocators is known at compile time. |
| 102 // |
| 103 // Valid range of priorities: [0, kMaxDynamicAllocators) |
| 104 // |
| 105 // Please note that we can't use complex data structures and cause |
| 106 // recursive calls to malloc within this function. So all data structures |
| 107 // are statically allocated. |
| 108 // |
| 109 // Returns true on success. Does nothing on failure. |
| 110 extern PERFTOOLS_DLL_DECL bool RegisterSystemAllocator(SysAllocator *allocator, |
| 111 int priority); |
| 112 |
| 113 // Number of SysAllocators known to call RegisterSystemAllocator |
| 114 static const int kMaxDynamicAllocators = 2; |
| 115 |
| 116 // Retrieve the current state of various system allocators. |
| 117 extern PERFTOOLS_DLL_DECL void DumpSystemAllocatorStats(TCMalloc_Printer* printe
r); |
| 118 |
| 119 #endif /* TCMALLOC_SYSTEM_ALLOC_H_ */ |
OLD | NEW |