source: proiecte/swift/trunk/lib/hoard-371/src/heaplayers/experimental/topslotheap.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.5 KB
Line 
1/* -*- C++ -*- */
2
3#ifndef _TOPSLOTHEAP_H_
4#define _TOPSLOTHEAP_H_
5
6#include <assert.h>
7
8#include "bigchunk.h"
9
10/*
11  TopSlotHeap.
12
13  malloc returns objects of size chunkSize.
14
15  free returns objects of size chunkSize.
16
17*/
18
19template <int chunkSize, int slotSize, class Super>
20class TopSlotHeap : public Super {
21public:
22
23  TopSlotHeap (void)
24    : myChunks (NULL)
25  {}
26
27  // Get a chunkSize object.
28  inline void * malloc (size_t sz);
29
30  // Free a chunkSize object.
31  inline void free (void * ptr);
32
33protected:
34
35        virtual inline void localFree (void * ptr);
36
37       
38private:
39
40  BigChunk<chunkSize, slotSize, TopSlotHeap> * myChunks;
41
42};
43
44
45template <int chunkSize, int slotSize, class Super>
46void * TopSlotHeap<chunkSize, slotSize, Super>::malloc (size_t sz)
47{
48  assert (sz <= chunkSize);
49  if (myChunks == NULL) {
50        return new (Super::malloc (chunkSize)) BigChunk<chunkSize, slotSize, TopSlotHeap>;
51  } else {
52        printf ("Recycled a chunk.\n");
53        BigChunk<chunkSize, slotSize, TopSlotHeap> * ch = myChunks;
54        myChunks = myChunks->getNext();
55        ch->setNext (NULL);
56        ch->setHeap (NULL);
57        return ch;
58  }
59}
60
61
62template <int chunkSize, int slotSize, class Super>
63void TopSlotHeap<chunkSize, slotSize, Super>::free (void * ptr) {
64  printf ("Freed a chunk.\n");
65  BigChunk<chunkSize, slotSize, TopSlotHeap> * ch = (BigChunk<chunkSize, slotSize, TopSlotHeap> *) ptr;
66  ch->setNext (myChunks);
67  ch->setHeap (this);
68  myChunks = ch;
69}
70
71
72template <int chunkSize, int slotSize, class Super>
73void TopSlotHeap<chunkSize, slotSize, Super>::localFree (void * ptr) {
74        free (ptr);
75}
76
77
78#endif
Note: See TracBrowser for help on using the repository browser.