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 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 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. |
… | |
… | |
40 | |
40 | |
41 | #include <glib.h> |
41 | #include <glib.h> |
42 | |
42 | |
43 | refcnt_base::refcnt_t refcnt_dummy; |
43 | refcnt_base::refcnt_t refcnt_dummy; |
44 | ssize_t slice_alloc; |
44 | ssize_t slice_alloc; |
45 | rand_gen rndm, rmg_rndm; |
|
|
46 | |
45 | |
47 | void |
46 | #if !GCC_VERSION(3,4) |
48 | tausworthe_random_generator::seed (uint32_t seed) |
47 | int least_significant_bit (uint32_t x) |
49 | { |
48 | { |
50 | state [0] = seed * 69069U; if (state [0] < 2U) state [0] += 2U; |
49 | x &= -x; // this isolates the lowest bit |
51 | state [1] = state [0] * 69069U; if (state [0] < 8U) state [0] += 8U; |
|
|
52 | state [2] = state [1] * 69069U; if (state [0] < 16U) state [0] += 16U; |
|
|
53 | state [3] = state [2] * 69069U; if (state [0] < 128U) state [0] += 128U; |
|
|
54 | |
50 | |
55 | for (int i = 11; --i; ) |
51 | int r = 0; |
56 | next (); |
|
|
57 | } |
|
|
58 | |
52 | |
59 | uint32_t |
53 | if (x & 0xaaaaaaaa) r += 1; |
60 | tausworthe_random_generator::next () |
54 | if (x & 0xcccccccc) r += 2; |
61 | { |
55 | if (x & 0xf0f0f0f0) r += 4; |
62 | state [0] = ((state [0] & 0xFFFFFFFEU) << 18U) ^ (((state [0] << 6U) ^ state [0]) >> 13U); |
56 | if (x & 0xff00ff00) r += 8; |
63 | state [1] = ((state [1] & 0xFFFFFFF8U) << 2U) ^ (((state [1] << 2U) ^ state [1]) >> 27U); |
57 | if (x & 0xffff0000) r += 16; |
64 | state [2] = ((state [2] & 0xFFFFFFF0U) << 7U) ^ (((state [2] << 13U) ^ state [2]) >> 21U); |
|
|
65 | state [3] = ((state [3] & 0xFFFFFF80U) << 13U) ^ (((state [3] << 3U) ^ state [3]) >> 12U); |
|
|
66 | |
58 | |
67 | return state [0] ^ state [1] ^ state [2] ^ state [3]; |
59 | return r; |
68 | } |
60 | } |
69 | |
61 | #endif |
70 | template<class generator> |
|
|
71 | uint32_t |
|
|
72 | random_number_generator<generator>::get_range (uint32_t num) |
|
|
73 | { |
|
|
74 | return (this->next () * (uint64_t)num) >> 32U; |
|
|
75 | } |
|
|
76 | |
|
|
77 | // return a number within (min .. max) |
|
|
78 | template<class generator> |
|
|
79 | int |
|
|
80 | random_number_generator<generator>::get_range (int r_min, int r_max) |
|
|
81 | { |
|
|
82 | return r_min + get_range (max (r_max - r_min + 1, 0)); |
|
|
83 | } |
|
|
84 | |
|
|
85 | template struct random_number_generator<tausworthe_random_generator>; |
|
|
86 | template struct random_number_generator<xorshift_random_generator>; |
|
|
87 | |
62 | |
88 | /******************************************************************************/ |
63 | /******************************************************************************/ |
89 | |
64 | |
90 | /* Checks a player-provided string which will become the msg property of |
65 | /* Checks a player-provided string which will become the msg property of |
91 | * an object for dangerous input. |
66 | * an object for dangerous input. |
… | |
… | |
117 | signal (SIGINT , SIG_IGN); |
92 | signal (SIGINT , SIG_IGN); |
118 | signal (SIGTERM, SIG_IGN); |
93 | signal (SIGTERM, SIG_IGN); |
119 | signal (SIGABRT, SIG_IGN); |
94 | signal (SIGABRT, SIG_IGN); |
120 | |
95 | |
121 | signal (SIGSEGV, SIG_DFL); |
96 | signal (SIGSEGV, SIG_DFL); |
|
|
97 | signal (SIGFPE , SIG_DFL); |
|
|
98 | #ifdef SIGBUS |
122 | signal (SIGBUS , SIG_DFL); |
99 | signal (SIGBUS , SIG_DFL); |
|
|
100 | #endif |
123 | signal (SIGILL , SIG_DFL); |
101 | signal (SIGILL , SIG_DFL); |
124 | signal (SIGTRAP, SIG_DFL); |
102 | signal (SIGTRAP, SIG_DFL); |
125 | |
103 | |
126 | // try to put corefiles into a subdirectory, if existing, to allow |
104 | // try to put corefiles into a subdirectory, if existing, to allow |
127 | // an administrator to reduce the I/O load. |
105 | // an administrator to reduce the I/O load. |
… | |
… | |
151 | } |
129 | } |
152 | |
130 | |
153 | LOG (llevError, "fork abort: %s\n", msg); |
131 | LOG (llevError, "fork abort: %s\n", msg); |
154 | } |
132 | } |
155 | |
133 | |
|
|
134 | void * |
156 | void *salloc_ (int n) throw (std::bad_alloc) |
135 | salloc_ (int n) throw (std::bad_alloc) |
157 | { |
136 | { |
158 | void *ptr = g_slice_alloc (n); |
137 | void *ptr = g_slice_alloc (n); |
159 | |
138 | |
160 | if (!ptr) |
139 | if (!ptr) |
161 | throw std::bad_alloc (); |
140 | throw std::bad_alloc (); |
162 | |
141 | |
163 | slice_alloc += n; |
142 | slice_alloc += n; |
164 | return ptr; |
143 | return ptr; |
165 | } |
144 | } |
166 | |
145 | |
|
|
146 | void * |
167 | void *salloc_ (int n, void *src) throw (std::bad_alloc) |
147 | salloc_ (int n, void *src) throw (std::bad_alloc) |
168 | { |
148 | { |
169 | void *ptr = salloc_ (n); |
149 | void *ptr = salloc_ (n); |
170 | |
150 | |
171 | if (src) |
151 | if (src) |
172 | memcpy (ptr, src, n); |
152 | memcpy (ptr, src, n); |
… | |
… | |
180 | |
160 | |
181 | #if DEBUG_SALLOC |
161 | #if DEBUG_SALLOC |
182 | |
162 | |
183 | #define MAGIC 0xa1b2c35543deadLL |
163 | #define MAGIC 0xa1b2c35543deadLL |
184 | |
164 | |
|
|
165 | void * |
185 | void *g_slice_alloc (unsigned long size) |
166 | g_slice_alloc (unsigned long size) |
186 | { |
167 | { |
187 | 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)); |
188 | *p++ = size ^ MAGIC; |
169 | *p++ = size ^ MAGIC; |
189 | //fprintf (stderr, "g_slice_alloc %ld %p\n", size, p);//D |
170 | //fprintf (stderr, "g_slice_alloc %ld %p\n", size, p);//D |
190 | return (void *)p; |
171 | return (void *)p; |
191 | } |
172 | } |
192 | |
173 | |
|
|
174 | void * |
193 | void *g_slice_alloc0 (unsigned long size) |
175 | g_slice_alloc0 (unsigned long size) |
194 | { |
176 | { |
195 | return memset (g_slice_alloc (size), 0, size); |
177 | return memset (g_slice_alloc (size), 0, size); |
196 | } |
178 | } |
197 | |
179 | |
|
|
180 | void |
198 | void g_slice_free1 (unsigned long size, void *ptr) |
181 | g_slice_free1 (unsigned long size, void *ptr) |
199 | { |
182 | { |
200 | //fprintf (stderr, "g_slice_free %ld %p\n", size, ptr);//D |
183 | //fprintf (stderr, "g_slice_free %ld %p\n", size, ptr);//D |
201 | if (expect_true (ptr)) |
184 | if (expect_true (ptr)) |
202 | { |
185 | { |
203 | unsigned long *p = (unsigned long *)ptr; |
186 | unsigned long *p = (unsigned long *)ptr; |
… | |
… | |
214 | (g_slice_free1)(s + sizeof (unsigned long), p); |
197 | (g_slice_free1)(s + sizeof (unsigned long), p); |
215 | } |
198 | } |
216 | } |
199 | } |
217 | |
200 | |
218 | #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 | } |
219 | |
238 | |
220 | /******************************************************************************/ |
239 | /******************************************************************************/ |
221 | |
240 | |
222 | int |
241 | int |
223 | assign (char *dst, const char *src, int maxsize) |
242 | assign (char *dst, const char *src, int maxsize) |
… | |
… | |
249 | } |
268 | } |
250 | |
269 | |
251 | char * |
270 | char * |
252 | vformat (const char *format, va_list ap) |
271 | vformat (const char *format, va_list ap) |
253 | { |
272 | { |
254 | static dynbuf_text buf; buf.clear (); |
273 | static dynbuf_text bufs[8]; |
|
|
274 | static int bufidx; |
|
|
275 | |
|
|
276 | dynbuf_text &buf = bufs [++bufidx & 7]; |
|
|
277 | |
|
|
278 | buf.clear (); |
255 | buf.vprintf (format, ap); |
279 | buf.vprintf (format, ap); |
256 | return buf; |
280 | return buf; |
257 | } |
281 | } |
258 | |
282 | |
259 | char * |
283 | char * |
… | |
… | |
265 | va_end (ap); |
289 | va_end (ap); |
266 | |
290 | |
267 | return buf; |
291 | return buf; |
268 | } |
292 | } |
269 | |
293 | |
270 | tstamp now () |
294 | tstamp |
|
|
295 | now () |
271 | { |
296 | { |
272 | struct timeval tv; |
297 | struct timeval tv; |
273 | |
298 | |
274 | gettimeofday (&tv, 0); |
299 | gettimeofday (&tv, 0); |
275 | 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); |
… | |
… | |
340 | 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL, |
365 | 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL, |
341 | 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL, |
366 | 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL, |
342 | 0x2d02ef8dL |
367 | 0x2d02ef8dL |
343 | }; |
368 | }; |
344 | |
369 | |
|
|
370 | void |
345 | void thread::start (void *(*start_routine)(void *), void *arg) |
371 | thread::start (void *(*start_routine)(void *), void *arg) |
346 | { |
372 | { |
347 | pthread_attr_t attr; |
373 | pthread_attr_t attr; |
348 | |
374 | |
349 | pthread_attr_init (&attr); |
375 | pthread_attr_init (&attr); |
350 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
376 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |