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 "internal_logging.h" // for ASSERT, CRASH | 39 #include "internal_logging.h" // for ASSERT |
40 | 40 |
41 namespace tcmalloc { | 41 namespace tcmalloc { |
42 | 42 |
43 // Simple allocator for objects of a specified type. External locking | 43 // Simple allocator for objects of a specified type. External locking |
44 // is required before accessing one of these objects. | 44 // is required before accessing one of these objects. |
45 template <class T> | 45 template <class T> |
46 class PageHeapAllocator { | 46 class PageHeapAllocator { |
47 public: | 47 public: |
48 // We use an explicit Init function because these variables are statically | 48 // We use an explicit Init function because these variables are statically |
49 // 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 |
(...skipping 13 matching lines...) Expand all Loading... |
63 void* result; | 63 void* result; |
64 if (free_list_ != NULL) { | 64 if (free_list_ != NULL) { |
65 result = free_list_; | 65 result = free_list_; |
66 free_list_ = *(reinterpret_cast<void**>(result)); | 66 free_list_ = *(reinterpret_cast<void**>(result)); |
67 } else { | 67 } else { |
68 if (free_avail_ < sizeof(T)) { | 68 if (free_avail_ < sizeof(T)) { |
69 // Need more room. We assume that MetaDataAlloc returns | 69 // Need more room. We assume that MetaDataAlloc returns |
70 // suitably aligned memory. | 70 // suitably aligned memory. |
71 free_area_ = reinterpret_cast<char*>(MetaDataAlloc(kAllocIncrement)); | 71 free_area_ = reinterpret_cast<char*>(MetaDataAlloc(kAllocIncrement)); |
72 if (free_area_ == NULL) { | 72 if (free_area_ == NULL) { |
73 CRASH("FATAL ERROR: Out of memory trying to allocate internal " | 73 Log(kCrash, __FILE__, __LINE__, |
74 "tcmalloc data (%d bytes, object-size %d)\n", | 74 "FATAL ERROR: Out of memory trying to allocate internal " |
75 kAllocIncrement, static_cast<int>(sizeof(T))); | 75 "tcmalloc data (bytes, object-size)", |
| 76 kAllocIncrement, sizeof(T)); |
76 } | 77 } |
77 free_avail_ = kAllocIncrement; | 78 free_avail_ = kAllocIncrement; |
78 } | 79 } |
79 result = free_area_; | 80 result = free_area_; |
80 free_area_ += sizeof(T); | 81 free_area_ += sizeof(T); |
81 free_avail_ -= sizeof(T); | 82 free_avail_ -= sizeof(T); |
82 } | 83 } |
83 inuse_++; | 84 inuse_++; |
84 return reinterpret_cast<T*>(result); | 85 return reinterpret_cast<T*>(result); |
85 } | 86 } |
(...skipping 17 matching lines...) Expand all Loading... |
103 // Free list of already carved objects | 104 // Free list of already carved objects |
104 void* free_list_; | 105 void* free_list_; |
105 | 106 |
106 // Number of allocated but unfreed objects | 107 // Number of allocated but unfreed objects |
107 int inuse_; | 108 int inuse_; |
108 }; | 109 }; |
109 | 110 |
110 } // namespace tcmalloc | 111 } // namespace tcmalloc |
111 | 112 |
112 #endif // TCMALLOC_PAGE_HEAP_ALLOCATOR_H_ | 113 #endif // TCMALLOC_PAGE_HEAP_ALLOCATOR_H_ |
OLD | NEW |