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. |
… | |
… | |
92 | signal (SIGINT , SIG_IGN); |
92 | signal (SIGINT , SIG_IGN); |
93 | signal (SIGTERM, SIG_IGN); |
93 | signal (SIGTERM, SIG_IGN); |
94 | signal (SIGABRT, SIG_IGN); |
94 | signal (SIGABRT, SIG_IGN); |
95 | |
95 | |
96 | signal (SIGSEGV, SIG_DFL); |
96 | signal (SIGSEGV, SIG_DFL); |
|
|
97 | signal (SIGFPE , SIG_DFL); |
|
|
98 | #ifdef SIGBUS |
97 | signal (SIGBUS , SIG_DFL); |
99 | signal (SIGBUS , SIG_DFL); |
|
|
100 | #endif |
98 | signal (SIGILL , SIG_DFL); |
101 | signal (SIGILL , SIG_DFL); |
99 | signal (SIGTRAP, SIG_DFL); |
102 | signal (SIGTRAP, SIG_DFL); |
100 | |
103 | |
101 | // try to put corefiles into a subdirectory, if existing, to allow |
104 | // try to put corefiles into a subdirectory, if existing, to allow |
102 | // an administrator to reduce the I/O load. |
105 | // an administrator to reduce the I/O load. |
… | |
… | |
126 | } |
129 | } |
127 | |
130 | |
128 | LOG (llevError, "fork abort: %s\n", msg); |
131 | LOG (llevError, "fork abort: %s\n", msg); |
129 | } |
132 | } |
130 | |
133 | |
|
|
134 | void * |
131 | void *salloc_ (int n) throw (std::bad_alloc) |
135 | salloc_ (int n) throw (std::bad_alloc) |
132 | { |
136 | { |
133 | void *ptr = g_slice_alloc (n); |
137 | void *ptr = g_slice_alloc (n); |
134 | |
138 | |
135 | if (!ptr) |
139 | if (!ptr) |
136 | throw std::bad_alloc (); |
140 | throw std::bad_alloc (); |
137 | |
141 | |
138 | slice_alloc += n; |
142 | slice_alloc += n; |
139 | return ptr; |
143 | return ptr; |
140 | } |
144 | } |
141 | |
145 | |
|
|
146 | void * |
142 | void *salloc_ (int n, void *src) throw (std::bad_alloc) |
147 | salloc_ (int n, void *src) throw (std::bad_alloc) |
143 | { |
148 | { |
144 | void *ptr = salloc_ (n); |
149 | void *ptr = salloc_ (n); |
145 | |
150 | |
146 | if (src) |
151 | if (src) |
147 | memcpy (ptr, src, n); |
152 | memcpy (ptr, src, n); |
… | |
… | |
155 | |
160 | |
156 | #if DEBUG_SALLOC |
161 | #if DEBUG_SALLOC |
157 | |
162 | |
158 | #define MAGIC 0xa1b2c35543deadLL |
163 | #define MAGIC 0xa1b2c35543deadLL |
159 | |
164 | |
|
|
165 | void * |
160 | void *g_slice_alloc (unsigned long size) |
166 | g_slice_alloc (unsigned long size) |
161 | { |
167 | { |
162 | unsigned long *p = (unsigned long *) (g_slice_alloc)(size + sizeof (unsigned long)); |
168 | unsigned long *p = (unsigned long *) (g_slice_alloc)(size + sizeof (unsigned long)); |
163 | *p++ = size ^ MAGIC; |
169 | *p++ = size ^ MAGIC; |
164 | //fprintf (stderr, "g_slice_alloc %ld %p\n", size, p);//D |
170 | //fprintf (stderr, "g_slice_alloc %ld %p\n", size, p);//D |
165 | return (void *)p; |
171 | return (void *)p; |
166 | } |
172 | } |
167 | |
173 | |
|
|
174 | void * |
168 | void *g_slice_alloc0 (unsigned long size) |
175 | g_slice_alloc0 (unsigned long size) |
169 | { |
176 | { |
170 | return memset (g_slice_alloc (size), 0, size); |
177 | return memset (g_slice_alloc (size), 0, size); |
171 | } |
178 | } |
172 | |
179 | |
|
|
180 | void |
173 | void g_slice_free1 (unsigned long size, void *ptr) |
181 | g_slice_free1 (unsigned long size, void *ptr) |
174 | { |
182 | { |
175 | //fprintf (stderr, "g_slice_free %ld %p\n", size, ptr);//D |
183 | //fprintf (stderr, "g_slice_free %ld %p\n", size, ptr);//D |
176 | if (expect_true (ptr)) |
184 | if (expect_true (ptr)) |
177 | { |
185 | { |
178 | unsigned long *p = (unsigned long *)ptr; |
186 | unsigned long *p = (unsigned long *)ptr; |
… | |
… | |
189 | (g_slice_free1)(s + sizeof (unsigned long), p); |
197 | (g_slice_free1)(s + sizeof (unsigned long), p); |
190 | } |
198 | } |
191 | } |
199 | } |
192 | |
200 | |
193 | #endif |
201 | #endif |
|
|
202 | |
|
|
203 | /******************************************************************************/ |
|
|
204 | |
|
|
205 | refcnt_buf::refcnt_buf (size_t size) |
|
|
206 | { |
|
|
207 | static uint32_t empty_buf [2] = { 0, 1 }; // 2 == never deallocated |
|
|
208 | data = (char *)empty_buf + overhead; |
|
|
209 | assert (overhead == sizeof (empty_buf)); |
|
|
210 | inc (); |
|
|
211 | } |
|
|
212 | |
|
|
213 | refcnt_buf::refcnt_buf (void *data, size_t size) |
|
|
214 | { |
|
|
215 | _alloc (size); |
|
|
216 | memcpy (this->data, data, size); |
|
|
217 | } |
|
|
218 | |
|
|
219 | refcnt_buf::~refcnt_buf () |
|
|
220 | { |
|
|
221 | dec (); |
|
|
222 | } |
|
|
223 | |
|
|
224 | void |
|
|
225 | refcnt_buf::_dealloc () |
|
|
226 | { |
|
|
227 | sfree<char> (data - overhead, size () + overhead); |
|
|
228 | } |
|
|
229 | |
|
|
230 | refcnt_buf & |
|
|
231 | refcnt_buf::operator =(const refcnt_buf &src) |
|
|
232 | { |
|
|
233 | dec (); |
|
|
234 | data = src.data; |
|
|
235 | inc (); |
|
|
236 | return *this; |
|
|
237 | } |
194 | |
238 | |
195 | /******************************************************************************/ |
239 | /******************************************************************************/ |
196 | |
240 | |
197 | int |
241 | int |
198 | assign (char *dst, const char *src, int maxsize) |
242 | assign (char *dst, const char *src, int maxsize) |
… | |
… | |
245 | va_end (ap); |
289 | va_end (ap); |
246 | |
290 | |
247 | return buf; |
291 | return buf; |
248 | } |
292 | } |
249 | |
293 | |
250 | tstamp now () |
294 | tstamp |
|
|
295 | now () |
251 | { |
296 | { |
252 | struct timeval tv; |
297 | struct timeval tv; |
253 | |
298 | |
254 | gettimeofday (&tv, 0); |
299 | gettimeofday (&tv, 0); |
255 | return tstamp (tv.tv_sec) + tstamp (tv.tv_usec) * tstamp (1e-6); |
300 | return tstamp (tv.tv_sec) + tstamp (tv.tv_usec) * tstamp (1e-6); |
… | |
… | |
320 | 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL, |
365 | 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL, |
321 | 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL, |
366 | 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL, |
322 | 0x2d02ef8dL |
367 | 0x2d02ef8dL |
323 | }; |
368 | }; |
324 | |
369 | |
|
|
370 | void |
325 | void thread::start (void *(*start_routine)(void *), void *arg) |
371 | thread::start (void *(*start_routine)(void *), void *arg) |
326 | { |
372 | { |
327 | pthread_attr_t attr; |
373 | pthread_attr_t attr; |
328 | |
374 | |
329 | pthread_attr_init (&attr); |
375 | pthread_attr_init (&attr); |
330 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
376 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |