… | |
… | |
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 *salloc (int size) throw (std::bad_alloc); |
47 | void dealloc (void *p, int s) throw (); |
49 | // also copies src into the new area, like "memdup" |
|
|
50 | void *salloc (int size, void *src) throw (std::bad_alloc); |
|
|
51 | // for symmetry |
|
|
52 | inline void sfree (void *ptr, int size) throw () |
|
|
53 | { |
|
|
54 | g_slice_free1 (size, ptr); |
|
|
55 | } |
48 | |
56 | |
49 | // a STL-compatible allocator that uses g_slice |
57 | // a STL-compatible allocator that uses g_slice |
50 | // boy, this is verbose |
58 | // boy, this is verbose |
51 | template<typename Tp> |
59 | template<typename Tp> |
52 | struct slice_allocator |
60 | struct slice_allocator |
… | |
… | |
75 | pointer address (reference x) const { return &x; } |
83 | pointer address (reference x) const { return &x; } |
76 | const_pointer address (const_reference x) const { return &x; } |
84 | const_pointer address (const_reference x) const { return &x; } |
77 | |
85 | |
78 | pointer allocate (size_type n, const_pointer = 0) |
86 | pointer allocate (size_type n, const_pointer = 0) |
79 | { |
87 | { |
80 | return static_cast<pointer>(alloc (n * sizeof (Tp))); |
88 | return static_cast<pointer>(salloc (n * sizeof (Tp))); |
81 | } |
89 | } |
82 | |
90 | |
83 | void deallocate (pointer p, size_type n) |
91 | void deallocate (pointer p, size_type n) |
84 | { |
92 | { |
85 | dealloc (static_cast<void *>(p), n); |
93 | sfree (static_cast<void *>(p), n * sizeof (Tp)); |
86 | } |
94 | } |
87 | |
95 | |
88 | size_type max_size ()const throw () |
96 | size_type max_size ()const throw () |
89 | { |
97 | { |
90 | return size_t (-1) / sizeof (Tp); |
98 | return size_t (-1) / sizeof (Tp); |
… | |
… | |
101 | } |
109 | } |
102 | }; |
110 | }; |
103 | |
111 | |
104 | struct refcounted |
112 | struct refcounted |
105 | { |
113 | { |
|
|
114 | refcounted () : refcnt (0) { } |
|
|
115 | // virtual ~refcounted (); |
|
|
116 | void refcnt_inc () { ++refcnt; } |
|
|
117 | void refcnt_dec () { --refcnt; } |
|
|
118 | bool dead () { return refcnt == 0; } |
106 | mutable int refcnt; |
119 | mutable int refcnt; |
107 | refcounted () : refcnt (0) { } |
120 | #if 0 |
108 | void refcnt_inc () { ++refcnt; } |
121 | private: |
109 | void refcnt_dec () { --refcnt; |
122 | static refcounted *rc_first; |
110 | if (refcnt < 0)abort();}//D |
123 | refcounted *rc_next; |
|
|
124 | #endif |
111 | }; |
125 | }; |
112 | |
126 | |
113 | template<class T> |
127 | template<class T> |
114 | struct refptr |
128 | struct refptr |
115 | { |
129 | { |
… | |
… | |
210 | inline void assign (char (&dst)[N], const char *src) |
224 | inline void assign (char (&dst)[N], const char *src) |
211 | { |
225 | { |
212 | assign ((char *)&dst, src, N); |
226 | assign ((char *)&dst, src, N); |
213 | } |
227 | } |
214 | |
228 | |
|
|
229 | typedef double tstamp; |
|
|
230 | |
|
|
231 | // return current time as timestampe |
|
|
232 | tstamp now (); |
|
|
233 | |
215 | #endif |
234 | #endif |
216 | |
235 | |