33
33
// Routine that uses sbrk/mmap to allocate memory from the system.
34
34
// Useful for implementing malloc.
36
#ifndef TCMALLOC_SYSTEM_ALLOC_H__
37
#define TCMALLOC_SYSTEM_ALLOC_H__
36
#ifndef TCMALLOC_SYSTEM_ALLOC_H_
37
#define TCMALLOC_SYSTEM_ALLOC_H_
40
#include "internal_logging.h"
39
42
// REQUIRES: "alignment" is a power of two or "0" to indicate default alignment
41
// Allocate and return "N" bytes of zeroed memory. The returned
42
// pointer is a multiple of "alignment" if non-zero. Returns NULL
43
// when out of memory.
44
extern void* TCMalloc_SystemAlloc(size_t bytes, size_t alignment = 0);
46
#endif /* TCMALLOC_SYSTEM_ALLOC_H__ */
44
// Allocate and return "N" bytes of zeroed memory.
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).
51
// The returned pointer is a multiple of "alignment" if non-zero.
53
// Returns NULL when out of memory.
54
extern void* TCMalloc_SystemAlloc(size_t bytes, size_t *actual_bytes,
55
size_t alignment = 0);
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);
68
// Interface to a pluggable system allocator.
75
virtual ~SysAllocator() {};
77
virtual void* Alloc(size_t size, size_t *actual_size, size_t alignment) = 0;
79
// Populate the map with whatever properties the specified allocator finds
80
// useful for debugging (such as number of bytes allocated and whether the
81
// allocator has failed). The callee is responsible for any necessary
82
// locking (and avoiding deadlock).
83
virtual void DumpStats(TCMalloc_Printer* printer) = 0;
85
// So the allocator can be turned off at compile time
88
// Did this allocator fail? If so, we don't need to retry more than twice.
92
// Register a new system allocator. The priority determines the order in
93
// which the allocators will be invoked. Allocators with numerically lower
94
// priority are tried first. To keep things simple, the priority of various
95
// allocators is known at compile time.
97
// Valid range of priorities: [0, kMaxDynamicAllocators)
99
// Please note that we can't use complex data structures and cause
100
// recursive calls to malloc within this function. So all data structures
101
// are statically allocated.
103
// Returns true on success. Does nothing on failure.
104
extern PERFTOOLS_DLL_DECL bool RegisterSystemAllocator(SysAllocator *allocator,
107
// Number of SysAllocators known to call RegisterSystemAllocator
108
static const int kMaxDynamicAllocators = 2;
110
// Retrieve the current state of various system allocators.
111
extern PERFTOOLS_DLL_DECL void DumpSystemAllocatorStats(TCMalloc_Printer* printer);
113
#endif /* TCMALLOC_SYSTEM_ALLOC_H_ */