… | |
… | |
42 | { |
42 | { |
43 | g_slice_free1 (s, p); |
43 | g_slice_free1 (s, p); |
44 | } |
44 | } |
45 | }; |
45 | }; |
46 | |
46 | |
|
|
47 | void *salloc_ (int n) throw (std::bad_alloc); |
|
|
48 | void *salloc_ (int n, void *src) throw (std::bad_alloc); |
|
|
49 | |
47 | // strictly the same as g_slice_alloc, but never returns 0 |
50 | // strictly the same as g_slice_alloc, but never returns 0 |
48 | void *salloc (int size) throw (std::bad_alloc); |
51 | template<typename T> |
|
|
52 | inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); } |
|
|
53 | |
49 | // also copies src into the new area, like "memdup" |
54 | // also copies src into the new area, like "memdup" |
50 | void *salloc (int size, void *src) throw (std::bad_alloc); |
55 | // if src is 0, clears the memory |
|
|
56 | template<typename T> |
|
|
57 | inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); } |
|
|
58 | |
|
|
59 | // clears the memory |
|
|
60 | template<typename T> |
|
|
61 | inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); } |
|
|
62 | |
51 | // for symmetry |
63 | // for symmetry |
|
|
64 | template<typename T> |
52 | inline void sfree (void *ptr, int size) throw () |
65 | inline void sfree (T *ptr, int n = 1) throw () |
53 | { |
66 | { |
54 | g_slice_free1 (size, ptr); |
67 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
55 | } |
68 | } |
56 | |
69 | |
57 | // a STL-compatible allocator that uses g_slice |
70 | // a STL-compatible allocator that uses g_slice |
58 | // boy, this is verbose |
71 | // boy, this is verbose |
59 | template<typename Tp> |
72 | template<typename Tp> |
… | |
… | |
83 | pointer address (reference x) const { return &x; } |
96 | pointer address (reference x) const { return &x; } |
84 | const_pointer address (const_reference x) const { return &x; } |
97 | const_pointer address (const_reference x) const { return &x; } |
85 | |
98 | |
86 | pointer allocate (size_type n, const_pointer = 0) |
99 | pointer allocate (size_type n, const_pointer = 0) |
87 | { |
100 | { |
88 | return static_cast<pointer>(salloc (n * sizeof (Tp))); |
101 | return salloc<Tp> (n); |
89 | } |
102 | } |
90 | |
103 | |
91 | void deallocate (pointer p, size_type n) |
104 | void deallocate (pointer p, size_type n) |
92 | { |
105 | { |
93 | sfree (static_cast<void *>(p), n * sizeof (Tp)); |
106 | sfree<Tp> (p, n); |
94 | } |
107 | } |
95 | |
108 | |
96 | size_type max_size ()const throw () |
109 | size_type max_size ()const throw () |
97 | { |
110 | { |
98 | return size_t (-1) / sizeof (Tp); |
111 | return size_t (-1) / sizeof (Tp); |
… | |
… | |
110 | }; |
123 | }; |
111 | |
124 | |
112 | struct refcounted |
125 | struct refcounted |
113 | { |
126 | { |
114 | refcounted () : refcnt (0) { } |
127 | refcounted () : refcnt (0) { } |
115 | // virtual ~refcounted (); |
128 | virtual ~refcounted (); |
116 | void refcnt_inc () { ++refcnt; } |
129 | void refcnt_inc () { ++refcnt; } |
117 | void refcnt_dec () { --refcnt; } |
130 | void refcnt_dec () { --refcnt; } |
118 | bool dead () { return refcnt == 0; } |
131 | bool dead () { return refcnt == 0; } |
119 | mutable int refcnt; |
132 | mutable int refcnt; |
120 | #if 0 |
133 | #if 0 |
… | |
… | |
152 | T &operator * () const { return *p; } |
165 | T &operator * () const { return *p; } |
153 | T *operator ->() const { return p; } |
166 | T *operator ->() const { return p; } |
154 | |
167 | |
155 | operator T *() const { return p; } |
168 | operator T *() const { return p; } |
156 | }; |
169 | }; |
|
|
170 | |
|
|
171 | typedef refptr<player> player_ptr; |
|
|
172 | typedef refptr<object> object_ptr; |
|
|
173 | typedef refptr<archetype> arch_ptr; |
157 | |
174 | |
158 | struct str_hash |
175 | struct str_hash |
159 | { |
176 | { |
160 | std::size_t operator ()(const char *s) const |
177 | std::size_t operator ()(const char *s) const |
161 | { |
178 | { |