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 20 matching lines...) Expand all Loading... | |
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 "free_list.h" // for FL_Push/FL_Pop |
40 #include "internal_logging.h" // for ASSERT, CRASH | 40 #include "internal_logging.h" // for ASSERT, CRASH |
41 #include "system-alloc.h" // for TCMalloc_SystemAddGuard | |
41 | 42 |
42 namespace tcmalloc { | 43 namespace tcmalloc { |
43 | 44 |
44 // Simple allocator for objects of a specified type. External locking | 45 // Simple allocator for objects of a specified type. External locking |
45 // is required before accessing one of these objects. | 46 // is required before accessing one of these objects. |
46 template <class T> | 47 template <class T> |
47 class PageHeapAllocator { | 48 class PageHeapAllocator { |
48 public: | 49 public: |
49 // We use an explicit Init function because these variables are statically | 50 // We use an explicit Init function because these variables are statically |
50 // allocated and their constructors might not have run by the time some | 51 // allocated and their constructors might not have run by the time some |
(...skipping 16 matching lines...) Expand all Loading... | |
67 } else { | 68 } else { |
68 if (free_avail_ < sizeof(T)) { | 69 if (free_avail_ < sizeof(T)) { |
69 // Need more room. We assume that MetaDataAlloc returns | 70 // Need more room. We assume that MetaDataAlloc returns |
70 // suitably aligned memory. | 71 // suitably aligned memory. |
71 free_area_ = reinterpret_cast<char*>(MetaDataAlloc(kAllocIncrement)); | 72 free_area_ = reinterpret_cast<char*>(MetaDataAlloc(kAllocIncrement)); |
72 if (free_area_ == NULL) { | 73 if (free_area_ == NULL) { |
73 CRASH("FATAL ERROR: Out of memory trying to allocate internal " | 74 CRASH("FATAL ERROR: Out of memory trying to allocate internal " |
74 "tcmalloc data (%d bytes, object-size %d)\n", | 75 "tcmalloc data (%d bytes, object-size %d)\n", |
75 kAllocIncrement, static_cast<int>(sizeof(T))); | 76 kAllocIncrement, static_cast<int>(sizeof(T))); |
76 } | 77 } |
77 free_avail_ = kAllocIncrement; | 78 |
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_, kAllocIncrement) ; | |
84 free_area_ += guard_size; | |
85 free_avail_ = kAllocIncrement - guard_size; | |
78 } | 86 } |
79 result = free_area_; | 87 result = free_area_; |
jar (doing other things)
2011/11/24 01:08:07
This code assumes that when we get back memory, th
jschuh
2011/11/28 18:33:36
Yes I do.
| |
80 free_area_ += sizeof(T); | 88 free_area_ += sizeof(T); |
81 free_avail_ -= sizeof(T); | 89 free_avail_ -= sizeof(T); |
82 } | 90 } |
83 inuse_++; | 91 inuse_++; |
84 return reinterpret_cast<T*>(result); | 92 return reinterpret_cast<T*>(result); |
85 } | 93 } |
86 | 94 |
87 void Delete(T* p) { | 95 void Delete(T* p) { |
88 FL_Push(&free_list_, p); | 96 FL_Push(&free_list_, p); |
89 inuse_--; | 97 inuse_--; |
(...skipping 12 matching lines...) Expand all Loading... | |
102 // Free list of already carved objects | 110 // Free list of already carved objects |
103 void* free_list_; | 111 void* free_list_; |
104 | 112 |
105 // Number of allocated but unfreed objects | 113 // Number of allocated but unfreed objects |
106 int inuse_; | 114 int inuse_; |
107 }; | 115 }; |
108 | 116 |
109 } // namespace tcmalloc | 117 } // namespace tcmalloc |
110 | 118 |
111 #endif // TCMALLOC_PAGE_HEAP_ALLOCATOR_H_ | 119 #endif // TCMALLOC_PAGE_HEAP_ALLOCATOR_H_ |
OLD | NEW |