source: proiecte/swift/trunk/lib/hoard-371/src/alignedsuperblockheap.h @ 176

Last change on this file since 176 was 176, checked in by (none), 14 years ago
  • imported repo from "guagal"
File size: 1.8 KB
RevLine 
[176]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
17namespace Hoard {
18
19template <size_t SuperblockSize>
20class SuperblockStore {
21public:
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
47private:
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
63namespace Hoard {
64
65template <class TheLockType,
66          size_t SuperblockSize>
67class AlignedSuperblockHeapHelper :
68  public ConformantHeap<HL::LockedHeap<TheLockType,
69                                       FixedRequestHeap<SuperblockSize, 
70                                                        // AlignedMmap<SuperblockSize> > > > {};
71                                       SuperblockStore<SuperblockSize> > > > {};
72
73
74template <class TheLockType,
75          size_t SuperblockSize>
76class AlignedSuperblockHeap :
77  public AlignedSuperblockHeapHelper<TheLockType, SuperblockSize> {
78
79  HL::sassert<(AlignedSuperblockHeapHelper<TheLockType, SuperblockSize>::Alignment % SuperblockSize == 0)> EnsureProperAlignment;
80
81};
82
83};
84
85#endif
Note: See TracBrowser for help on using the repository browser.