00001 // Copyright (c) 2005, Google Inc. 00002 // All rights reserved. 00003 // 00004 // Redistribution and use in source and binary forms, with or without 00005 // modification, are permitted provided that the following conditions are 00006 // met: 00007 // 00008 // * Redistributions of source code must retain the above copyright 00009 // notice, this list of conditions and the following disclaimer. 00010 // * Redistributions in binary form must reproduce the above 00011 // copyright notice, this list of conditions and the following disclaimer 00012 // in the documentation and/or other materials provided with the 00013 // distribution. 00014 // * Neither the name of Google Inc. nor the names of its 00015 // contributors may be used to endorse or promote products derived from 00016 // this software without specific prior written permission. 00017 // 00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 00030 // --- 00031 // Author: Sanjay Ghemawat 00032 // 00033 // Routine that uses sbrk/mmap to allocate memory from the system. 00034 // Useful for implementing malloc. 00035 00036 #ifndef TCMALLOC_SYSTEM_ALLOC_H__ 00037 #define TCMALLOC_SYSTEM_ALLOC_H__ 00038 00039 #include "config.h" 00040 00041 // REQUIRES: "alignment" is a power of two or "0" to indicate default alignment 00042 // 00043 // Allocate and return "N" bytes of zeroed memory. 00044 // 00045 // If actual_bytes is NULL then the returned memory is exactly the 00046 // requested size. If actual bytes is non-NULL then the allocator 00047 // may optionally return more bytes than asked for (i.e. return an 00048 // entire "huge" page if a huge page allocator is in use). 00049 // 00050 // The returned pointer is a multiple of "alignment" if non-zero. 00051 // 00052 // Returns NULL when out of memory. 00053 extern void* TCMalloc_SystemAlloc(size_t bytes, size_t *actual_bytes, 00054 size_t alignment = 0); 00055 00056 // This call is a hint to the operating system that the pages 00057 // contained in the specified range of memory will not be used for a 00058 // while, and can be released for use by other processes or the OS. 00059 // Pages which are released in this way may be destroyed (zeroed) by 00060 // the OS. The benefit of this function is that it frees memory for 00061 // use by the system, the cost is that the pages are faulted back into 00062 // the address space next time they are touched, which can impact 00063 // performance. (Only pages fully covered by the memory region will 00064 // be released, partial pages will not.) 00065 extern void TCMalloc_SystemRelease(void* start, size_t length); 00066 00067 // Interface to a pluggable system allocator. 00068 class SysAllocator { 00069 public: 00070 SysAllocator() 00071 : usable_(true), 00072 failed_(false) { 00073 }; 00074 virtual ~SysAllocator() {}; 00075 00076 virtual void* Alloc(size_t size, size_t *actual_size, size_t alignment) = 0; 00077 00078 // So the allocator can be turned off at compile time 00079 bool usable_; 00080 00081 // Did this allocator fail? If so, we don't need to retry more than twice. 00082 bool failed_; 00083 }; 00084 00085 // Register a new system allocator. The priority determines the order in 00086 // which the allocators will be invoked. Allocators with numerically lower 00087 // priority are tried first. To keep things simple, the priority of various 00088 // allocators is known at compile time. 00089 // 00090 // Valid range of priorities: [0, kMaxDynamicAllocators) 00091 // 00092 // Please note that we can't use complex data structures and cause 00093 // recursive calls to malloc within this function. So all data structures 00094 // are statically allocated. 00095 // 00096 // Returns true on success. Does nothing on failure. 00097 extern PERFTOOLS_DLL_DECL bool RegisterSystemAllocator(SysAllocator *allocator, 00098 int priority); 00099 00100 // Number of SysAllocators known to call RegisterSystemAllocator 00101 static const int kMaxDynamicAllocators = 2; 00102 00103 #endif /* TCMALLOC_SYSTEM_ALLOC_H__ */