1 // Copyright (c) 2008, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 // Author: Sanjay Ghemawat <opensource@google.com>
33 #ifndef TCMALLOC_PAGE_HEAP_ALLOCATOR_H_
34 #define TCMALLOC_PAGE_HEAP_ALLOCATOR_H_
36 #include <stddef.h> // for NULL, size_t
38 #include "common.h" // for MetaDataAlloc
39 #include "internal_logging.h" // for ASSERT
43 // Simple allocator for objects of a specified type. External locking
44 // is required before accessing one of these objects.
46 class PageHeapAllocator
{
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
50 // other static variable tries to allocate memory.
52 ASSERT(sizeof(T
) <= kAllocIncrement
);
57 // Reserve some space at the beginning to avoid fragmentation.
64 if (free_list_
!= NULL
) {
66 free_list_
= *(reinterpret_cast<void**>(result
));
68 if (free_avail_
< sizeof(T
)) {
69 // Need more room. We assume that MetaDataAlloc returns
70 // suitably aligned memory.
71 free_area_
= reinterpret_cast<char*>(MetaDataAlloc(kAllocIncrement
));
72 if (free_area_
== NULL
) {
73 Log(kCrash
, __FILE__
, __LINE__
,
74 "FATAL ERROR: Out of memory trying to allocate internal "
75 "tcmalloc data (bytes, object-size)",
76 kAllocIncrement
, sizeof(T
));
78 free_avail_
= kAllocIncrement
;
81 free_area_
+= sizeof(T
);
82 free_avail_
-= sizeof(T
);
85 return reinterpret_cast<T
*>(result
);
89 *(reinterpret_cast<void**>(p
)) = free_list_
;
94 int inuse() const { return inuse_
; }
97 // How much to allocate from system at a time
98 static const int kAllocIncrement
= 128 << 10;
100 // Free area from which to carve new objects
104 // Free list of already carved objects
107 // Number of allocated but unfreed objects
111 } // namespace tcmalloc
113 #endif // TCMALLOC_PAGE_HEAP_ALLOCATOR_H_