source: proiecte/swift/trunk/lib/hoard-371/src/addheaderheap.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.6 KB
Line 
1// -*- C++ -*-
2
3#ifndef _ADDHEADERHEAP_H_
4#define _ADDHEADERHEAP_H_
5
6#include "sassert.h"
7#include "hldefines.h"
8
9namespace Hoard {
10
11/**
12 * @class AddHeaderHeap
13 */
14
15template <class SuperblockType,
16          size_t SuperblockSize,
17          class SuperHeap>
18class AddHeaderHeap {
19private:
20
21  HL::sassert<(((int) SuperHeap::Alignment) % SuperblockSize == 0)> verifySize1;
22  HL::sassert<(((int) SuperHeap::Alignment) >= SuperblockSize)> verifySize2;
23
24  SuperHeap theHeap;
25
26public:
27
28  enum { Alignment = 0 };
29
30  MALLOC_FUNCTION INLINE void * malloc (size_t sz) {
31    // Allocate extra space for the header,
32    // put it at the front of the object,
33    // and return a pointer to just past it.
34    const size_t headerSize = sizeof(typename SuperblockType::Header);
35    void * ptr = theHeap.malloc (sz + headerSize);
36    if (ptr == NULL) {
37      return NULL;
38    }
39    typename SuperblockType::Header * p
40      = new (ptr) typename SuperblockType::Header (sz, sz);
41    assert ((size_t) (p + 1) == (size_t) ptr + headerSize);
42    return reinterpret_cast<void *>(p + 1);
43  }
44
45  INLINE static size_t getSize (void * ptr) {
46    // Find the header (just before the pointer) and return the size
47    // value stored there.
48    typename SuperblockType::Header * p;
49    p = reinterpret_cast<typename SuperblockType::Header *>(ptr);
50    return (p - 1)->getSize (ptr);
51  }
52
53  INLINE void free (void * ptr) {
54    // Find the header (just before the pointer) and free the whole object.
55    typename SuperblockType::Header * p;
56    p = reinterpret_cast<typename SuperblockType::Header *>(ptr);
57    theHeap.free (reinterpret_cast<void *>(p - 1));
58  }
59};
60
61};
62
63#endif
Note: See TracBrowser for help on using the repository browser.