1 | /* |
1 | /* |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
4 | * Copyright (©) 2005,2006,2007,2008,2009,2010,2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * |
5 | * |
6 | * Deliantra is free software: you can redistribute it and/or modify it under |
6 | * Deliantra is free software: you can redistribute it and/or modify it under |
7 | * the terms of the Affero GNU General Public License as published by the |
7 | * the terms of the Affero GNU General Public License as published by the |
8 | * Free Software Foundation, either version 3 of the License, or (at your |
8 | * Free Software Foundation, either version 3 of the License, or (at your |
9 | * option) any later version. |
9 | * option) any later version. |
10 | * |
10 | * |
11 | * This program is distributed in the hope that it will be useful, |
11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
14 | * GNU General Public License for more details. |
15 | * |
15 | * |
16 | * You should have received a copy of the Affero GNU General Public License |
16 | * You should have received a copy of the Affero GNU General Public License |
17 | * and the GNU General Public License along with this program. If not, see |
17 | * and the GNU General Public License along with this program. If not, see |
18 | * <http://www.gnu.org/licenses/>. |
18 | * <http://www.gnu.org/licenses/>. |
19 | * |
19 | * |
20 | * The authors can be reached via e-mail to <support@deliantra.net> |
20 | * The authors can be reached via e-mail to <support@deliantra.net> |
21 | */ |
21 | */ |
22 | |
22 | |
23 | /* |
23 | /* |
24 | * General convenience functions for deliantra. |
24 | * General convenience functions for deliantra. |
… | |
… | |
126 | } |
126 | } |
127 | |
127 | |
128 | LOG (llevError, "fork abort: %s\n", msg); |
128 | LOG (llevError, "fork abort: %s\n", msg); |
129 | } |
129 | } |
130 | |
130 | |
|
|
131 | void * |
131 | void *salloc_ (int n) throw (std::bad_alloc) |
132 | salloc_ (int n) throw (std::bad_alloc) |
132 | { |
133 | { |
133 | void *ptr = g_slice_alloc (n); |
134 | void *ptr = g_slice_alloc (n); |
134 | |
135 | |
135 | if (!ptr) |
136 | if (!ptr) |
136 | throw std::bad_alloc (); |
137 | throw std::bad_alloc (); |
137 | |
138 | |
138 | slice_alloc += n; |
139 | slice_alloc += n; |
139 | return ptr; |
140 | return ptr; |
140 | } |
141 | } |
141 | |
142 | |
|
|
143 | void * |
142 | void *salloc_ (int n, void *src) throw (std::bad_alloc) |
144 | salloc_ (int n, void *src) throw (std::bad_alloc) |
143 | { |
145 | { |
144 | void *ptr = salloc_ (n); |
146 | void *ptr = salloc_ (n); |
145 | |
147 | |
146 | if (src) |
148 | if (src) |
147 | memcpy (ptr, src, n); |
149 | memcpy (ptr, src, n); |
… | |
… | |
155 | |
157 | |
156 | #if DEBUG_SALLOC |
158 | #if DEBUG_SALLOC |
157 | |
159 | |
158 | #define MAGIC 0xa1b2c35543deadLL |
160 | #define MAGIC 0xa1b2c35543deadLL |
159 | |
161 | |
|
|
162 | void * |
160 | void *g_slice_alloc (unsigned long size) |
163 | g_slice_alloc (unsigned long size) |
161 | { |
164 | { |
162 | unsigned long *p = (unsigned long *) (g_slice_alloc)(size + sizeof (unsigned long)); |
165 | unsigned long *p = (unsigned long *) (g_slice_alloc)(size + sizeof (unsigned long)); |
163 | *p++ = size ^ MAGIC; |
166 | *p++ = size ^ MAGIC; |
164 | //fprintf (stderr, "g_slice_alloc %ld %p\n", size, p);//D |
167 | //fprintf (stderr, "g_slice_alloc %ld %p\n", size, p);//D |
165 | return (void *)p; |
168 | return (void *)p; |
166 | } |
169 | } |
167 | |
170 | |
|
|
171 | void * |
168 | void *g_slice_alloc0 (unsigned long size) |
172 | g_slice_alloc0 (unsigned long size) |
169 | { |
173 | { |
170 | return memset (g_slice_alloc (size), 0, size); |
174 | return memset (g_slice_alloc (size), 0, size); |
171 | } |
175 | } |
172 | |
176 | |
|
|
177 | void |
173 | void g_slice_free1 (unsigned long size, void *ptr) |
178 | g_slice_free1 (unsigned long size, void *ptr) |
174 | { |
179 | { |
175 | //fprintf (stderr, "g_slice_free %ld %p\n", size, ptr);//D |
180 | //fprintf (stderr, "g_slice_free %ld %p\n", size, ptr);//D |
176 | if (expect_true (ptr)) |
181 | if (expect_true (ptr)) |
177 | { |
182 | { |
178 | unsigned long *p = (unsigned long *)ptr; |
183 | unsigned long *p = (unsigned long *)ptr; |
… | |
… | |
189 | (g_slice_free1)(s + sizeof (unsigned long), p); |
194 | (g_slice_free1)(s + sizeof (unsigned long), p); |
190 | } |
195 | } |
191 | } |
196 | } |
192 | |
197 | |
193 | #endif |
198 | #endif |
|
|
199 | |
|
|
200 | /******************************************************************************/ |
|
|
201 | |
|
|
202 | refcnt_buf::refcnt_buf (size_t size) |
|
|
203 | { |
|
|
204 | static uint32_t empty_buf [2] = { 0, 1 }; // 2 == never deallocated |
|
|
205 | data = (char *)empty_buf + overhead; |
|
|
206 | assert (overhead == sizeof (empty_buf)); |
|
|
207 | inc (); |
|
|
208 | } |
|
|
209 | |
|
|
210 | refcnt_buf::refcnt_buf (void *data, size_t size) |
|
|
211 | { |
|
|
212 | _alloc (size); |
|
|
213 | memcpy (this->data, data, size); |
|
|
214 | } |
|
|
215 | |
|
|
216 | refcnt_buf::~refcnt_buf () |
|
|
217 | { |
|
|
218 | dec (); |
|
|
219 | } |
|
|
220 | |
|
|
221 | void |
|
|
222 | refcnt_buf::_dealloc () |
|
|
223 | { |
|
|
224 | sfree<char> (data - overhead, size () + overhead); |
|
|
225 | } |
|
|
226 | |
|
|
227 | refcnt_buf & |
|
|
228 | refcnt_buf::operator =(const refcnt_buf &src) |
|
|
229 | { |
|
|
230 | dec (); |
|
|
231 | data = src.data; |
|
|
232 | inc (); |
|
|
233 | return *this; |
|
|
234 | } |
194 | |
235 | |
195 | /******************************************************************************/ |
236 | /******************************************************************************/ |
196 | |
237 | |
197 | int |
238 | int |
198 | assign (char *dst, const char *src, int maxsize) |
239 | assign (char *dst, const char *src, int maxsize) |
… | |
… | |
245 | va_end (ap); |
286 | va_end (ap); |
246 | |
287 | |
247 | return buf; |
288 | return buf; |
248 | } |
289 | } |
249 | |
290 | |
250 | tstamp now () |
291 | tstamp |
|
|
292 | now () |
251 | { |
293 | { |
252 | struct timeval tv; |
294 | struct timeval tv; |
253 | |
295 | |
254 | gettimeofday (&tv, 0); |
296 | gettimeofday (&tv, 0); |
255 | return tstamp (tv.tv_sec) + tstamp (tv.tv_usec) * tstamp (1e-6); |
297 | return tstamp (tv.tv_sec) + tstamp (tv.tv_usec) * tstamp (1e-6); |
… | |
… | |
320 | 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL, |
362 | 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL, |
321 | 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL, |
363 | 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL, |
322 | 0x2d02ef8dL |
364 | 0x2d02ef8dL |
323 | }; |
365 | }; |
324 | |
366 | |
|
|
367 | void |
325 | void thread::start (void *(*start_routine)(void *), void *arg) |
368 | thread::start (void *(*start_routine)(void *), void *arg) |
326 | { |
369 | { |
327 | pthread_attr_t attr; |
370 | pthread_attr_t attr; |
328 | |
371 | |
329 | pthread_attr_init (&attr); |
372 | pthread_attr_init (&attr); |
330 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
373 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |