| OLD | NEW |
| 1 // Copyright (c) 2005, Google Inc. | 1 // Copyright (c) 2005, Google Inc. |
| 2 // All rights reserved. | 2 // All rights reserved. |
| 3 // | 3 // |
| 4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
| 6 // met: | 6 // met: |
| 7 // | 7 // |
| 8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
| 9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
| 10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 // --- | 30 // --- |
| 31 // Author: Sanjay Ghemawat | 31 // Author: Sanjay Ghemawat |
| 32 // | 32 // |
| 33 // Routine that uses sbrk/mmap to allocate memory from the system. | 33 // Routine that uses sbrk/mmap to allocate memory from the system. |
| 34 // Useful for implementing malloc. | 34 // Useful for implementing malloc. |
| 35 | 35 |
| 36 #ifndef TCMALLOC_SYSTEM_ALLOC_H_ | 36 #ifndef TCMALLOC_SYSTEM_ALLOC_H_ |
| 37 #define TCMALLOC_SYSTEM_ALLOC_H_ | 37 #define TCMALLOC_SYSTEM_ALLOC_H_ |
| 38 | 38 |
| 39 #include <config.h> | 39 #include <config.h> |
| 40 #include "internal_logging.h" | 40 #include <stddef.h> // for size_t |
| 41 |
| 42 class SysAllocator; |
| 41 | 43 |
| 42 // REQUIRES: "alignment" is a power of two or "0" to indicate default alignment | 44 // REQUIRES: "alignment" is a power of two or "0" to indicate default alignment |
| 43 // | 45 // |
| 44 // Allocate and return "N" bytes of zeroed memory. | 46 // Allocate and return "N" bytes of zeroed memory. |
| 45 // | 47 // |
| 46 // If actual_bytes is NULL then the returned memory is exactly the | 48 // If actual_bytes is NULL then the returned memory is exactly the |
| 47 // requested size. If actual bytes is non-NULL then the allocator | 49 // requested size. If actual bytes is non-NULL then the allocator |
| 48 // may optionally return more bytes than asked for (i.e. return an | 50 // 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 // entire "huge" page if a huge page allocator is in use). |
| 50 // | 52 // |
| (...skipping 17 matching lines...) Expand all Loading... |
| 68 // performance. (Only pages fully covered by the memory region will | 70 // performance. (Only pages fully covered by the memory region will |
| 69 // be released, partial pages will not.) | 71 // be released, partial pages will not.) |
| 70 extern void TCMalloc_SystemRelease(void* start, size_t length); | 72 extern void TCMalloc_SystemRelease(void* start, size_t length); |
| 71 | 73 |
| 72 // Called to ressurect memory which has been previously released | 74 // Called to ressurect memory which has been previously released |
| 73 // to the system via TCMalloc_SystemRelease. An attempt to | 75 // to the system via TCMalloc_SystemRelease. An attempt to |
| 74 // commit a page that is already committed does not cause this | 76 // commit a page that is already committed does not cause this |
| 75 // function to fail. | 77 // function to fail. |
| 76 extern void TCMalloc_SystemCommit(void* start, size_t length); | 78 extern void TCMalloc_SystemCommit(void* start, size_t length); |
| 77 | 79 |
| 78 // Interface to a pluggable system allocator. | 80 // The current system allocator. |
| 79 class SysAllocator { | 81 extern PERFTOOLS_DLL_DECL SysAllocator* sys_alloc; |
| 80 public: | |
| 81 SysAllocator() | |
| 82 : usable_(true), | |
| 83 failed_(false) { | |
| 84 }; | |
| 85 virtual ~SysAllocator() {}; | |
| 86 | |
| 87 virtual void* Alloc(size_t size, size_t *actual_size, size_t alignment) = 0; | |
| 88 | |
| 89 // Populate the map with whatever properties the specified allocator finds | |
| 90 // useful for debugging (such as number of bytes allocated and whether the | |
| 91 // allocator has failed). The callee is responsible for any necessary | |
| 92 // locking (and avoiding deadlock). | |
| 93 virtual void DumpStats(TCMalloc_Printer* printer) = 0; | |
| 94 | |
| 95 // So the allocator can be turned off at compile time | |
| 96 bool usable_; | |
| 97 | |
| 98 // Did this allocator fail? If so, we don't need to retry more than twice. | |
| 99 bool failed_; | |
| 100 }; | |
| 101 | |
| 102 // Register a new system allocator. The priority determines the order in | |
| 103 // which the allocators will be invoked. Allocators with numerically lower | |
| 104 // priority are tried first. To keep things simple, the priority of various | |
| 105 // allocators is known at compile time. | |
| 106 // | |
| 107 // Valid range of priorities: [0, kMaxDynamicAllocators) | |
| 108 // | |
| 109 // Please note that we can't use complex data structures and cause | |
| 110 // recursive calls to malloc within this function. So all data structures | |
| 111 // are statically allocated. | |
| 112 // | |
| 113 // Returns true on success. Does nothing on failure. | |
| 114 extern PERFTOOLS_DLL_DECL bool RegisterSystemAllocator(SysAllocator *allocator, | |
| 115 int priority); | |
| 116 | |
| 117 // Number of SysAllocators known to call RegisterSystemAllocator | |
| 118 static const int kMaxDynamicAllocators = 2; | |
| 119 | |
| 120 // Retrieve the current state of various system allocators. | |
| 121 extern PERFTOOLS_DLL_DECL void DumpSystemAllocatorStats(TCMalloc_Printer* printe
r); | |
| 122 | 82 |
| 123 #endif /* TCMALLOC_SYSTEM_ALLOC_H_ */ | 83 #endif /* TCMALLOC_SYSTEM_ALLOC_H_ */ |
| OLD | NEW |