summaryrefslogtreecommitdiff
path: root/graphics/asymptote/memory.h
blob: 5c5d7a41d017c2d0879bc6ce6b00b15fae78fa8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/****
 * memory.h
 *
 * Interface to the Boehm Garbage Collector.
 *****/

#ifndef MEMORY_H
#define MEMORY_H

#include <list>
#include <vector>
#include <stack>
#include <map>
#include <deque>
#include <string>
#include <sstream>

#ifndef NOHASH

#ifdef HAVE_TR1_UNORDERED_MAP

#include <memory>
#include <tr1/unordered_map>
#define EXT std::tr1

#else

#ifdef HAVE_UNORDERED_MAP
#include <unordered_map>
#define EXT std
#else
#define EXT __gnu_cxx
#include <ext/hash_map>
#define unordered_map hash_map
#define unordered_multimap hash_multimap
#endif

#endif

#endif

#ifdef __DECCXX_LIBCXX_RH70
#define CONST
#else
#define CONST const
#endif

#ifdef USEGC

#define GC_THREADS
#ifdef __clang__
#define GC_ATTR_EXPLICIT
#define GC_NOEXCEPT
#endif
#include <gc.h>

#ifdef GC_DEBUG
extern "C" {
#include <gc_backptr.h>
}
#endif

inline void *asy_malloc(size_t n)
{
#ifdef GC_DEBUG
  if(void *mem=GC_debug_malloc_ignore_off_page(n, GC_EXTRAS))
#else
    if(void *mem=GC_malloc_ignore_off_page(n))
#endif
      return mem;
  throw std::bad_alloc();
}

inline void *asy_malloc_atomic(size_t n)
{
#ifdef GC_DEBUG
  if(void *mem=GC_debug_malloc_atomic_ignore_off_page(n, GC_EXTRAS))
#else
    if(void *mem=GC_malloc_atomic_ignore_off_page(n))
#endif
      return mem;
  throw std::bad_alloc();
}

#undef GC_MALLOC
#undef GC_MALLOC_ATOMIC

#define GC_MALLOC(sz) asy_malloc(sz)
#define GC_MALLOC_ATOMIC(sz) asy_malloc_atomic(sz)

#include <gc_allocator.h>
#include <gc_cpp.h>

#define gc_allocator gc_allocator_ignore_off_page

#else // USEGC

using std::allocator;
#define gc_allocator allocator

class gc {};
class gc_cleanup {};

enum GCPlacement {UseGC, NoGC, PointerFreeGC};

inline void* operator new(size_t size, GCPlacement) {
  return operator new(size);
}

inline void* operator new[](size_t size, GCPlacement) {
                           return operator new(size);
                         }

template<class T>
struct GC_type_traits {};

#define GC_DECLARE_PTRFREE(T)                   \
  template<> struct GC_type_traits<T> {}

#endif // USEGC

namespace mem {

#define GC_CONTAINER(KIND)                                              \
  template <typename T>                                                 \
  struct KIND : public std::KIND<T, gc_allocator<T> >, public gc {      \
    KIND() : std::KIND<T, gc_allocator<T> >() {}                        \
    KIND(size_t n) : std::KIND<T, gc_allocator<T> >(n) {}               \
    KIND(size_t n, const T& t) : std::KIND<T, gc_allocator<T> >(n,t) {} \
  }

GC_CONTAINER(list);
GC_CONTAINER(vector);
GC_CONTAINER(deque);

template <typename T, typename Container = vector<T> >
struct stack : public std::stack<T, Container>, public gc {};

template <typename T, typename S >
struct pair : public std::pair<T, S>, public gc {
  pair(T t, S s) : std::pair<T,S>(t,s) {}
};

#define PAIR_ALLOC gc_allocator<std::pair<CONST Key,T> > /* space */

#undef GC_CONTAINER

#define GC_CONTAINER(KIND)                                              \
  template <typename Key,                                               \
            typename T,                                                 \
            typename Compare = std::less<Key> >                         \
  struct KIND : public std::KIND<Key,T,Compare,PAIR_ALLOC>, public gc   \
  {                                                                     \
    KIND() : std::KIND<Key,T,Compare,PAIR_ALLOC> () {}                  \
  }

GC_CONTAINER(map);
GC_CONTAINER(multimap);

#undef GC_CONTAINER

#ifndef NOHASH
#define GC_CONTAINER(KIND)                              \
  template <typename Key, typename T,                   \
            typename Hash = EXT::hash<Key>,             \
            typename Eq = std::equal_to<Key> >          \
  struct KIND : public                                  \
  EXT::KIND<Key,T,Hash,Eq,PAIR_ALLOC>, public gc {      \
    KIND() : EXT::KIND<Key,T,Hash,Eq,PAIR_ALLOC> () {}  \
    KIND(size_t n)                                      \
      : EXT::KIND<Key,T,Hash,Eq,PAIR_ALLOC> (n) {}      \
  }

GC_CONTAINER(unordered_map);
GC_CONTAINER(unordered_multimap);

#undef GC_CONTAINER
#undef EXT
#endif

#undef PAIR_ALLOC

#ifdef USEGC
typedef std::basic_string<char,std::char_traits<char>,
                          gc_allocator<char> > string;
typedef std::basic_stringstream<char,std::char_traits<char>,
                                gc_allocator<char> > stringstream;
typedef std::basic_istringstream<char,std::char_traits<char>,
                                 gc_allocator<char> > istringstream;
typedef std::basic_ostringstream<char,std::char_traits<char>,
                                 gc_allocator<char> > ostringstream;
typedef std::basic_stringbuf<char,std::char_traits<char>,
                             gc_allocator<char> > stringbuf;
inline void compact(int x) {GC_set_dont_expand(x);}
inline std::string stdString(string s) {return std::string(s.c_str());}
#else
inline void compact(int x) {}
typedef std::string string;
typedef std::stringstream stringstream;
typedef std::istringstream istringstream;
typedef std::ostringstream ostringstream;
typedef std::stringbuf stringbuf;
inline std::string stdString(string s) {return s;}
#endif // USEGC

} // namespace mem

#endif