system-alloc.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
166a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// Copyright (c) 2005, Google Inc. 266a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// All rights reserved. 366a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// 466a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// Redistribution and use in source and binary forms, with or without 566a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// modification, are permitted provided that the following conditions are 666a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// met: 766a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// 866a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// * Redistributions of source code must retain the above copyright 966a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// notice, this list of conditions and the following disclaimer. 1066a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// * Redistributions in binary form must reproduce the above 1166a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// copyright notice, this list of conditions and the following disclaimer 1266a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// in the documentation and/or other materials provided with the 1366a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// distribution. 1466a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// * Neither the name of Google Inc. nor the names of its 1566a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// contributors may be used to endorse or promote products derived from 1666a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// this software without specific prior written permission. 1766a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// 1866a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1966a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2066a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2166a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2266a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2366a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2466a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2566a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2666a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2766a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2866a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2966a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis 3066a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// --- 3166a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// Author: Sanjay Ghemawat 3266a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// 3366a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// Routine that uses sbrk/mmap to allocate memory from the system. 3466a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// Useful for implementing malloc. 3566a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis 3666a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis#ifndef TCMALLOC_SYSTEM_ALLOC_H_ 3766a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis#define TCMALLOC_SYSTEM_ALLOC_H_ 3866a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis 3966a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis#include <config.h> 4066a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis#include <stddef.h> // for size_t 4166a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis 4266a37686207944273ced825e0e8b6b6375f8c3deJamie Gennisclass SysAllocator; 4366a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis 4466a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// REQUIRES: "alignment" is a power of two or "0" to indicate default alignment 4566a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// 4666a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// Allocate and return "N" bytes of zeroed memory. 4766a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// 4866a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// If actual_bytes is NULL then the returned memory is exactly the 4966a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// requested size. If actual bytes is non-NULL then the allocator 5066a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// may optionally return more bytes than asked for (i.e. return an 5166a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// entire "huge" page if a huge page allocator is in use). 5266a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// 5366a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// The returned pointer is a multiple of "alignment" if non-zero. The 5466a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// returned pointer will always be aligned suitably for holding a 5566a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// void*, double, or size_t. In addition, if this platform defines 5666a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// CACHELINE_ALIGNED, the return pointer will always be cacheline 5766a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// aligned. 5866a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// 5966a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// Returns NULL when out of memory. 6066a37686207944273ced825e0e8b6b6375f8c3deJamie Gennisextern void* TCMalloc_SystemAlloc(size_t bytes, size_t *actual_bytes, 6166a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis size_t alignment = 0); 6266a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis 6366a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// This call is a hint to the operating system that the pages 6466a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// contained in the specified range of memory will not be used for a 6566a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// while, and can be released for use by other processes or the OS. 6666a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// Pages which are released in this way may be destroyed (zeroed) by 6766a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// the OS. The benefit of this function is that it frees memory for 6866a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// use by the system, the cost is that the pages are faulted back into 6966a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// the address space next time they are touched, which can impact 7066a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// performance. (Only pages fully covered by the memory region will 7166a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// be released, partial pages will not.) 7266a37686207944273ced825e0e8b6b6375f8c3deJamie Gennisextern void TCMalloc_SystemRelease(void* start, size_t length); 7366a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis 7466a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// Called to ressurect memory which has been previously released 7566a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// to the system via TCMalloc_SystemRelease. An attempt to 7666a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// commit a page that is already committed does not cause this 7766a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// function to fail. 7866a37686207944273ced825e0e8b6b6375f8c3deJamie Gennisextern void TCMalloc_SystemCommit(void* start, size_t length); 7966a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis 8066a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// Guards the first page in the supplied range of memory and returns the size 8166a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// of the guard page. Will return 0 if a guard cannot be added to the page 8266a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// (e.g. start is not aligned or size is not large enough). 8366a37686207944273ced825e0e8b6b6375f8c3deJamie Gennisextern size_t TCMalloc_SystemAddGuard(void* start, size_t size); 8466a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis 8566a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis// The current system allocator. 8666a37686207944273ced825e0e8b6b6375f8c3deJamie Gennisextern PERFTOOLS_DLL_DECL SysAllocator* sys_alloc; 8766a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis 8866a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis#endif /* TCMALLOC_SYSTEM_ALLOC_H_ */ 8966a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis