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

Last change on this file since 176 was 176, checked in by (none), 14 years ago
  • imported repo from "guagal"
File size: 3.8 KB
Line 
1/* -*- C++ -*- */
2
3/*
4
5  Heap Layers: An Extensible Memory Allocation Infrastructure
6 
7  Copyright (C) 2000-2005 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 _STLALLOCATOR_H_
28#define _STLALLOCATOR_H_
29
30#include <stdio.h>
31#include <new>
32#include <stdlib.h>
33
34#include <memory> // STL
35
36using namespace std;
37
38/**
39 * @class STLAllocator
40 * @brief An allocator adapter for STL.
41 *
42 * This mixin lets you use any Heap Layers allocator as the allocator
43 * for an STL container.
44 *
45 * Example:
46 * <TT>
47 *   typedef STLAllocator<int, MyHeapType> MyAllocator;<BR>
48 *   list<int, MyAllocator> l;<BR>
49 * </TT>
50 */
51
52namespace HL {
53
54template <class T, class Super>
55class STLAllocator : public Super {
56public:
57
58  typedef T value_type;
59  typedef std::size_t size_type;
60  typedef std::ptrdiff_t difference_type;
61  typedef T * pointer;
62  typedef const T * const_pointer;
63  typedef T& reference;
64  typedef const T& const_reference;
65
66  STLAllocator (void) {}
67  virtual ~STLAllocator (void) {}
68
69  STLAllocator (const STLAllocator& s)
70    : Super (s)
71  {}
72
73#if defined(_WIN32)
74  char * _Charalloc (size_type n) {
75    return (char *) allocate (n);
76  }
77#endif
78
79#if defined(__SUNPRO_CC)
80  inline void * allocate (size_type n,
81                          const void * = 0) {
82    if (n) {
83      return reinterpret_cast<void *>(Super::malloc (sizeof(T) * n));
84    } else {
85      return (void *) 0;
86    }
87  }
88#else
89  inline pointer allocate (size_type n,
90                          const void * = 0) {
91    if (n) {
92      return reinterpret_cast<pointer>(Super::malloc (sizeof(T) * n));
93    } else {
94      return 0;
95    }
96  }
97#endif
98
99  inline void deallocate (void * p, size_type) {
100    Super::free (p);
101  }
102
103  inline void deallocate (pointer p, size_type) {
104    Super::free (p);
105  }
106 
107  pointer address (reference x) const { return &x; }
108  const_pointer address (const_reference x) const { return &x; }
109
110  void construct (pointer p, const T& val) { new (p) T (val); }
111  void destroy (pointer p) { p->~T(); }
112
113  /// Make the maximum size be the largest possible object.
114  size_type max_size(void) const
115  {
116    size_type n = (size_type)(-1);
117    return (n > 0 ? n : (size_type)(n));
118  }
119
120  template <class U> STLAllocator( const STLAllocator<U, Super> &) {}
121  template <class U> struct rebind { typedef STLAllocator<U,Super> other; };
122
123};
124
125  template <typename T, class S>
126  inline bool operator!=(const STLAllocator<T,S>& a, const STLAllocator<T,S>& b) {
127    return (&a != &b);
128  }
129
130  template <typename T, class S>
131  inline bool operator==(const STLAllocator<T,S>& a, const STLAllocator<T,S>& b) {
132    return (&a == &b);
133  }
134
135#if 0
136
137  #include <memory> // STL
138  #include <stdlib.h>
139
140  template <typename T, class Super>
141  class STLAllocator : public std::allocator<T>, public Super {
142  public:
143    inline T * allocate (std::size_t n,
144                         const void * = 0) {
145      if (n) {
146        return reinterpret_cast<T *>(Super::malloc (sizeof(T) * n));
147      } else {
148        return 0;
149      }
150    }
151   
152    inline void deallocate (void * p, std::size_t) {
153      Super::free (p);
154    }
155   
156    inline void deallocate (T * p, std::size_t) {
157      Super::free (p);
158    }
159   
160  };
161
162#endif
163
164}
165
166
167#endif
Note: See TracBrowser for help on using the repository browser.