| OLD | NEW |
| 1 // Copyright (c) 2008, Google Inc. | 1 // Copyright (c) 2008, 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 18 matching lines...) Expand all Loading... |
| 29 | 29 |
| 30 // --- | 30 // --- |
| 31 // Author: Sanjay Ghemawat <opensource@google.com> | 31 // Author: Sanjay Ghemawat <opensource@google.com> |
| 32 | 32 |
| 33 #ifndef TCMALLOC_PAGE_HEAP_ALLOCATOR_H_ | 33 #ifndef TCMALLOC_PAGE_HEAP_ALLOCATOR_H_ |
| 34 #define TCMALLOC_PAGE_HEAP_ALLOCATOR_H_ | 34 #define TCMALLOC_PAGE_HEAP_ALLOCATOR_H_ |
| 35 | 35 |
| 36 #include <stddef.h> // for NULL, size_t | 36 #include <stddef.h> // for NULL, size_t |
| 37 | 37 |
| 38 #include "common.h" // for MetaDataAlloc | 38 #include "common.h" // for MetaDataAlloc |
| 39 #include "free_list.h" // for FL_Push/FL_Pop | 39 #include "internal_logging.h" // for ASSERT |
| 40 #include "internal_logging.h" // for ASSERT, CRASH | |
| 41 #include "system-alloc.h" // for TCMalloc_SystemAddGuard | |
| 42 | 40 |
| 43 namespace tcmalloc { | 41 namespace tcmalloc { |
| 44 | 42 |
| 45 // Simple allocator for objects of a specified type. External locking | 43 // Simple allocator for objects of a specified type. External locking |
| 46 // is required before accessing one of these objects. | 44 // is required before accessing one of these objects. |
| 47 template <class T> | 45 template <class T> |
| 48 class PageHeapAllocator { | 46 class PageHeapAllocator { |
| 49 public: | 47 public: |
| 50 // We use an explicit Init function because these variables are statically | 48 // We use an explicit Init function because these variables are statically |
| 51 // allocated and their constructors might not have run by the time some | 49 // allocated and their constructors might not have run by the time some |
| 52 // other static variable tries to allocate memory. | 50 // other static variable tries to allocate memory. |
| 53 void Init() { | 51 void Init() { |
| 54 ASSERT(sizeof(T) <= kAllocIncrement); | 52 ASSERT(sizeof(T) <= kAllocIncrement); |
| 55 inuse_ = 0; | 53 inuse_ = 0; |
| 56 free_area_ = NULL; | 54 free_area_ = NULL; |
| 57 free_avail_ = 0; | 55 free_avail_ = 0; |
| 58 free_list_ = NULL; | 56 free_list_ = NULL; |
| 59 // Reserve some space at the beginning to avoid fragmentation. | 57 // Reserve some space at the beginning to avoid fragmentation. |
| 60 Delete(New()); | 58 Delete(New()); |
| 61 } | 59 } |
| 62 | 60 |
| 63 T* New() { | 61 T* New() { |
| 64 // Consult free list | 62 // Consult free list |
| 65 void* result; | 63 void* result; |
| 66 if (free_list_ != NULL) { | 64 if (free_list_ != NULL) { |
| 67 result = FL_Pop(&free_list_); | 65 result = free_list_; |
| 66 free_list_ = *(reinterpret_cast<void**>(result)); |
| 68 } else { | 67 } else { |
| 69 if (free_avail_ < sizeof(T)) { | 68 if (free_avail_ < sizeof(T)) { |
| 70 // Need more room. We assume that MetaDataAlloc returns | 69 // Need more room. We assume that MetaDataAlloc returns |
| 71 // suitably aligned memory. | 70 // suitably aligned memory. |
| 72 free_area_ = reinterpret_cast<char*>(MetaDataAlloc(kAllocIncrement)); | 71 free_area_ = reinterpret_cast<char*>(MetaDataAlloc(kAllocIncrement)); |
| 73 if (free_area_ == NULL) { | 72 if (free_area_ == NULL) { |
| 74 CRASH("FATAL ERROR: Out of memory trying to allocate internal " | 73 Log(kCrash, __FILE__, __LINE__, |
| 75 "tcmalloc data (%d bytes, object-size %d)\n", | 74 "FATAL ERROR: Out of memory trying to allocate internal " |
| 76 kAllocIncrement, static_cast<int>(sizeof(T))); | 75 "tcmalloc data (bytes, object-size)", |
| 76 kAllocIncrement, sizeof(T)); |
| 77 } | 77 } |
| 78 | 78 free_avail_ = kAllocIncrement; |
| 79 // This guard page protects the metadata from being corrupted by a | |
| 80 // buffer overrun. We currently have no mechanism for freeing it, since | |
| 81 // we never release the metadata buffer. If that changes we'll need to | |
| 82 // add something like TCMalloc_SystemRemoveGuard. | |
| 83 size_t guard_size = TCMalloc_SystemAddGuard(free_area_, | |
| 84 kAllocIncrement); | |
| 85 free_area_ += guard_size; | |
| 86 free_avail_ = kAllocIncrement - guard_size; | |
| 87 if (free_avail_ < sizeof(T)) { | |
| 88 CRASH("FATAL ERROR: Insufficient memory to guard internal tcmalloc " | |
| 89 "data (%d bytes, object-size %d, guard-size %d)\n", | |
| 90 kAllocIncrement, static_cast<int>(sizeof(T)), guard_size); | |
| 91 } | |
| 92 } | 79 } |
| 93 result = free_area_; | 80 result = free_area_; |
| 94 free_area_ += sizeof(T); | 81 free_area_ += sizeof(T); |
| 95 free_avail_ -= sizeof(T); | 82 free_avail_ -= sizeof(T); |
| 96 } | 83 } |
| 97 inuse_++; | 84 inuse_++; |
| 98 return reinterpret_cast<T*>(result); | 85 return reinterpret_cast<T*>(result); |
| 99 } | 86 } |
| 100 | 87 |
| 101 void Delete(T* p) { | 88 void Delete(T* p) { |
| 102 FL_Push(&free_list_, p); | 89 *(reinterpret_cast<void**>(p)) = free_list_; |
| 90 free_list_ = p; |
| 103 inuse_--; | 91 inuse_--; |
| 104 } | 92 } |
| 105 | 93 |
| 106 int inuse() const { return inuse_; } | 94 int inuse() const { return inuse_; } |
| 107 | 95 |
| 108 private: | 96 private: |
| 109 // How much to allocate from system at a time | 97 // How much to allocate from system at a time |
| 110 static const int kAllocIncrement = 128 << 10; | 98 static const int kAllocIncrement = 128 << 10; |
| 111 | 99 |
| 112 // Free area from which to carve new objects | 100 // Free area from which to carve new objects |
| 113 char* free_area_; | 101 char* free_area_; |
| 114 size_t free_avail_; | 102 size_t free_avail_; |
| 115 | 103 |
| 116 // Free list of already carved objects | 104 // Free list of already carved objects |
| 117 void* free_list_; | 105 void* free_list_; |
| 118 | 106 |
| 119 // Number of allocated but unfreed objects | 107 // Number of allocated but unfreed objects |
| 120 int inuse_; | 108 int inuse_; |
| 121 }; | 109 }; |
| 122 | 110 |
| 123 } // namespace tcmalloc | 111 } // namespace tcmalloc |
| 124 | 112 |
| 125 #endif // TCMALLOC_PAGE_HEAP_ALLOCATOR_H_ | 113 #endif // TCMALLOC_PAGE_HEAP_ALLOCATOR_H_ |
| OLD | NEW |