Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame^] | 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 | // --- |
| 32 | // Author: Sanjay Ghemawat |
| 33 | // |
| 34 | // Routine that uses sbrk/mmap to allocate memory from the system. |
| 35 | // Useful for implementing malloc. |
| 36 | |
| 37 | #ifndef TCMALLOC_SYSTEM_ALLOC_H_ |
| 38 | #define TCMALLOC_SYSTEM_ALLOC_H_ |
| 39 | |
| 40 | #include <config.h> |
| 41 | #include <stddef.h> // for size_t |
| 42 | |
| 43 | class SysAllocator; |
| 44 | |
| 45 | // REQUIRES: "alignment" is a power of two or "0" to indicate default alignment |
| 46 | // |
| 47 | // Allocate and return "N" bytes of zeroed memory. |
| 48 | // |
| 49 | // If actual_bytes is NULL then the returned memory is exactly the |
| 50 | // requested size. If actual bytes is non-NULL then the allocator |
| 51 | // may optionally return more bytes than asked for (i.e. return an |
| 52 | // entire "huge" page if a huge page allocator is in use). |
| 53 | // |
| 54 | // The returned pointer is a multiple of "alignment" if non-zero. The |
| 55 | // returned pointer will always be aligned suitably for holding a |
| 56 | // void*, double, or size_t. In addition, if this platform defines |
| 57 | // CACHELINE_ALIGNED, the return pointer will always be cacheline |
| 58 | // aligned. |
| 59 | // |
| 60 | // Returns NULL when out of memory. |
| 61 | extern PERFTOOLS_DLL_DECL |
| 62 | void* TCMalloc_SystemAlloc(size_t bytes, size_t *actual_bytes, |
| 63 | size_t alignment = 0); |
| 64 | |
| 65 | // This call is a hint to the operating system that the pages |
| 66 | // contained in the specified range of memory will not be used for a |
| 67 | // while, and can be released for use by other processes or the OS. |
| 68 | // Pages which are released in this way may be destroyed (zeroed) by |
| 69 | // the OS. The benefit of this function is that it frees memory for |
| 70 | // use by the system, the cost is that the pages are faulted back into |
| 71 | // the address space next time they are touched, which can impact |
| 72 | // performance. (Only pages fully covered by the memory region will |
| 73 | // be released, partial pages will not.) |
| 74 | // |
| 75 | // Returns false if release failed or not supported. |
| 76 | extern PERFTOOLS_DLL_DECL |
| 77 | bool TCMalloc_SystemRelease(void* start, size_t length); |
| 78 | |
| 79 | // Called to ressurect memory which has been previously released |
| 80 | // to the system via TCMalloc_SystemRelease. An attempt to |
| 81 | // commit a page that is already committed does not cause this |
| 82 | // function to fail. |
| 83 | extern PERFTOOLS_DLL_DECL |
| 84 | void TCMalloc_SystemCommit(void* start, size_t length); |
| 85 | |
| 86 | // The current system allocator. |
| 87 | extern PERFTOOLS_DLL_DECL SysAllocator* sys_alloc; |
| 88 | |
| 89 | // Number of bytes taken from system. |
| 90 | extern PERFTOOLS_DLL_DECL size_t TCMalloc_SystemTaken; |
| 91 | |
| 92 | #endif /* TCMALLOC_SYSTEM_ALLOC_H_ */ |