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

Last change on this file since 176 was 176, checked in by (none), 14 years ago
  • imported repo from "guagal"
File size: 2.8 KB
Line 
1/* -*- C++ -*- */
2
3/*
4
5  Heap Layers: An Extensible Memory Allocation Infrastructure
6 
7  Copyright (C) 2000-2003 by Emery Berger
8  http://www.cs.umass.edu/~emery
9  emery@cs.umass.edu
10 
11  This program is free software; you can redistribute it and/or modify
12  it under the terms of the GNU General Public License as published by
13  the Free Software Foundation; either version 2 of the License, or
14  (at your option) any later version.
15 
16  This program is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  GNU General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
25*/
26
27#ifndef _SIZEHEAP_H_
28#define _SIZEHEAP_H_
29
30/**
31 * @file sizeheap.h
32 * @brief Contains UseSizeHeap and SizeHeap.
33 */
34
35#include <assert.h>
36
37#include "addheap.h"
38
39#if 1
40
41/**
42 * @class UseSizeHeap
43 * @brief Adds a getSize method to access the size of an allocated object.
44 * @see SizeHeap
45 */
46
47namespace HL {
48
49template <class Super>
50class UseSizeHeap : public Super {
51public:
52 
53  inline UseSizeHeap (void) {}
54 
55  inline static size_t getSize (const void * ptr) {
56    return ((freeObject *) ptr - 1)->sz;
57  }
58
59protected:
60  union freeObject {
61    size_t sz;
62    double _dummy; // for alignment.
63  };
64};
65
66/**
67 * @class SizeHeap
68 * @brief Allocates extra room for the size of an object.
69 */
70
71template <class SuperHeap>
72class SizeHeap : public UseSizeHeap<SuperHeap> {
73  typedef typename UseSizeHeap<SuperHeap>::freeObject freeObject;
74public:
75  inline SizeHeap (void) {}
76  inline void * malloc (const size_t sz) {
77    // Add room for a size field.
78    freeObject * ptr = (freeObject *)
79      SuperHeap::malloc (sz + sizeof(freeObject));
80    // Store the requested size.
81    ptr->sz = sz;
82    return (void *) (ptr + 1);
83  }
84  inline void free (void * ptr) {
85    SuperHeap::free ((freeObject *) ptr - 1);
86  }
87};
88
89};
90
91#else
92
93template <class Super>
94class SizeHeap : public Super {
95public:
96 
97  inline void * malloc (size_t sz) {
98    // Add room for a size field.
99          assert (sizeof(size_t) <= sizeof(double));
100    void * ptr = Super::malloc (sz + sizeof(double));
101    // Store the requested size.
102    *((size_t *) ptr) = sz;
103    return (void *) ((double *) ptr + 1);
104  }
105 
106  inline void free (void * ptr) {
107    void * origPtr = (void *) ((double *) ptr - 1);
108        Super::free (origPtr);
109  }
110
111  inline static size_t getSize (void * ptr) {
112    return *((size_t *) ((double *) ptr - 1));
113  }
114};
115
116
117
118template <class Super>
119class UseSizeHeap : public Super {
120public:
121 
122  inline static size_t getSize (void * ptr) {
123    return *((size_t *) ((double *) ptr - 1));
124  }
125};
126
127#endif
128
129#endif
Note: See TracBrowser for help on using the repository browser.