1 | // -*- C++ -*- |
---|
2 | |
---|
3 | #ifndef _ALIGNEDSUPERBLOCKHEAP_H_ |
---|
4 | #define _ALIGNEDSUPERBLOCKHEAP_H_ |
---|
5 | |
---|
6 | #include "mmapheap.h" |
---|
7 | #include "sassert.h" |
---|
8 | |
---|
9 | #include "conformantheap.h" |
---|
10 | #include "lockedheap.h" |
---|
11 | #include "fixedrequestheap.h" |
---|
12 | #include "dllist.h" |
---|
13 | |
---|
14 | // Always requests aligned superblocks. |
---|
15 | #include "alignedmmap.h" |
---|
16 | |
---|
17 | namespace Hoard { |
---|
18 | |
---|
19 | template <size_t SuperblockSize> |
---|
20 | class SuperblockStore { |
---|
21 | public: |
---|
22 | |
---|
23 | enum { Alignment = AlignedMmap<SuperblockSize>::Alignment }; |
---|
24 | |
---|
25 | void * malloc (size_t sz) { |
---|
26 | sz = sz; // to avoid warning. |
---|
27 | assert (sz == SuperblockSize); |
---|
28 | if (_freeSuperblocks.isEmpty()) { |
---|
29 | // Get more memory. |
---|
30 | void * ptr = _superblockSource.malloc (ChunksToGrab * SuperblockSize); |
---|
31 | if (!ptr) { |
---|
32 | return NULL; |
---|
33 | } |
---|
34 | char * p = (char *) ptr; |
---|
35 | for (int i = 0; i < ChunksToGrab; i++) { |
---|
36 | _freeSuperblocks.insert ((DLList::Entry *) p); |
---|
37 | p += SuperblockSize; |
---|
38 | } |
---|
39 | } |
---|
40 | return _freeSuperblocks.get(); |
---|
41 | } |
---|
42 | |
---|
43 | void free (void * ptr) { |
---|
44 | _freeSuperblocks.insert ((DLList::Entry *) ptr); |
---|
45 | } |
---|
46 | |
---|
47 | private: |
---|
48 | |
---|
49 | #if defined(__SVR4) |
---|
50 | enum { ChunksToGrab = 63 }; |
---|
51 | #else |
---|
52 | enum { ChunksToGrab = 1 }; |
---|
53 | #endif |
---|
54 | |
---|
55 | AlignedMmap<SuperblockSize> _superblockSource; |
---|
56 | DLList _freeSuperblocks; |
---|
57 | |
---|
58 | }; |
---|
59 | |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | namespace Hoard { |
---|
64 | |
---|
65 | template <class TheLockType, |
---|
66 | size_t SuperblockSize> |
---|
67 | class AlignedSuperblockHeapHelper : |
---|
68 | public ConformantHeap<HL::LockedHeap<TheLockType, |
---|
69 | FixedRequestHeap<SuperblockSize, |
---|
70 | // AlignedMmap<SuperblockSize> > > > {}; |
---|
71 | SuperblockStore<SuperblockSize> > > > {}; |
---|
72 | |
---|
73 | |
---|
74 | template <class TheLockType, |
---|
75 | size_t SuperblockSize> |
---|
76 | class AlignedSuperblockHeap : |
---|
77 | public AlignedSuperblockHeapHelper<TheLockType, SuperblockSize> { |
---|
78 | |
---|
79 | HL::sassert<(AlignedSuperblockHeapHelper<TheLockType, SuperblockSize>::Alignment % SuperblockSize == 0)> EnsureProperAlignment; |
---|
80 | |
---|
81 | }; |
---|
82 | |
---|
83 | }; |
---|
84 | |
---|
85 | #endif |
---|