… | |
… | |
9 | |
9 | |
10 | #include <cstddef> |
10 | #include <cstddef> |
11 | |
11 | |
12 | #include <glib.h> |
12 | #include <glib.h> |
13 | |
13 | |
|
|
14 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
|
|
15 | #define AUTODECL(var,expr) typeof(expr) var = (expr) |
|
|
16 | |
14 | // makes dynamically allocated objects zero-initialised |
17 | // makes dynamically allocated objects zero-initialised |
15 | struct zero_initialised |
18 | struct zero_initialised |
16 | { |
19 | { |
17 | void *operator new (size_t s, void *p) |
20 | void *operator new (size_t s, void *p) |
18 | { |
21 | { |
… | |
… | |
39 | { |
42 | { |
40 | g_slice_free1 (s, p); |
43 | g_slice_free1 (s, p); |
41 | } |
44 | } |
42 | }; |
45 | }; |
43 | |
46 | |
44 | void throw_bad_alloc () throw (std::bad_alloc); |
47 | // strictly the same as g_slice_alloc, but never returns 0 |
45 | |
|
|
46 | void *alloc (int s) throw (std::bad_alloc); |
48 | void *alloc (int s) throw (std::bad_alloc); |
|
|
49 | // for symmetry |
47 | void dealloc (void *p, int s) throw (); |
50 | inline void dealloc (void *p, int s) throw () |
|
|
51 | { |
|
|
52 | g_slice_free1 (s, p); |
|
|
53 | } |
48 | |
54 | |
49 | // a STL-compatible allocator that uses g_slice |
55 | // a STL-compatible allocator that uses g_slice |
50 | // boy, this is verbose |
56 | // boy, this is verbose |
51 | template<typename Tp> |
57 | template<typename Tp> |
52 | struct slice_allocator |
58 | struct slice_allocator |
… | |
… | |
80 | return static_cast<pointer>(alloc (n * sizeof (Tp))); |
86 | return static_cast<pointer>(alloc (n * sizeof (Tp))); |
81 | } |
87 | } |
82 | |
88 | |
83 | void deallocate (pointer p, size_type n) |
89 | void deallocate (pointer p, size_type n) |
84 | { |
90 | { |
85 | dealloc (static_cast<void *>(p), n); |
91 | dealloc (static_cast<void *>(p), n * sizeof (Tp)); |
86 | } |
92 | } |
87 | |
93 | |
88 | size_type max_size ()const throw () |
94 | size_type max_size ()const throw () |
89 | { |
95 | { |
90 | return size_t (-1) / sizeof (Tp); |
96 | return size_t (-1) / sizeof (Tp); |