15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2005, Google Inc.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// All rights reserved.
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Redistribution and use in source and binary forms, with or without
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// modification, are permitted provided that the following conditions are
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// met:
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     * Redistributions of source code must retain the above copyright
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// notice, this list of conditions and the following disclaimer.
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     * Redistributions in binary form must reproduce the above
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// copyright notice, this list of conditions and the following disclaimer
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// in the documentation and/or other materials provided with the
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// distribution.
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     * Neither the name of Google Inc. nor the names of its
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// contributors may be used to endorse or promote products derived from
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// this software without specific prior written permission.
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ---
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Author: Sanjay Ghemawat
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Routine that uses sbrk/mmap to allocate memory from the system.
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Useful for implementing malloc.
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef TCMALLOC_SYSTEM_ALLOC_H_
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define TCMALLOC_SYSTEM_ALLOC_H_
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <config.h>
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stddef.h>                     // for size_t
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class SysAllocator;
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// REQUIRES: "alignment" is a power of two or "0" to indicate default alignment
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Allocate and return "N" bytes of zeroed memory.
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If actual_bytes is NULL then the returned memory is exactly the
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// requested size.  If actual bytes is non-NULL then the allocator
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// may optionally return more bytes than asked for (i.e. return an
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// entire "huge" page if a huge page allocator is in use).
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The returned pointer is a multiple of "alignment" if non-zero. The
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// returned pointer will always be aligned suitably for holding a
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// void*, double, or size_t. In addition, if this platform defines
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// CACHELINE_ALIGNED, the return pointer will always be cacheline
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// aligned.
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns NULL when out of memory.
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern void* TCMalloc_SystemAlloc(size_t bytes, size_t *actual_bytes,
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  size_t alignment = 0);
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This call is a hint to the operating system that the pages
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// contained in the specified range of memory will not be used for a
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// while, and can be released for use by other processes or the OS.
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Pages which are released in this way may be destroyed (zeroed) by
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the OS.  The benefit of this function is that it frees memory for
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// use by the system, the cost is that the pages are faulted back into
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the address space next time they are touched, which can impact
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// performance.  (Only pages fully covered by the memory region will
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// be released, partial pages will not.)
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern void TCMalloc_SystemRelease(void* start, size_t length);
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Called to ressurect memory which has been previously released
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// to the system via TCMalloc_SystemRelease.  An attempt to
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// commit a page that is already committed does not cause this
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// function to fail.
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern void TCMalloc_SystemCommit(void* start, size_t length);
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Guards the first page in the supplied range of memory and returns the size
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// of the guard page. Will return 0 if a guard cannot be added to the page
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (e.g. start is not aligned or size is not large enough).
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern size_t TCMalloc_SystemAddGuard(void* start, size_t size);
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The current system allocator.
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern PERFTOOLS_DLL_DECL SysAllocator* sys_alloc;
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif /* TCMALLOC_SYSTEM_ALLOC_H_ */
89