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 (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team |
4 | * Copyright (©) 2005,2006,2007,2008,2009 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * |
6 | * |
6 | * Deliantra is free software: you can redistribute it and/or modify it under |
7 | * 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 |
8 | * 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 |
9 | * Free Software Foundation, either version 3 of the License, or (at your |
9 | * option) any later version. |
10 | * option) any later version. |
10 | * |
11 | * |
11 | * This program is distributed in the hope that it will be useful, |
12 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * GNU General Public License for more details. |
15 | * |
16 | * |
16 | * You should have received a copy of the Affero GNU General Public License |
17 | * 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 |
18 | * and the GNU General Public License along with this program. If not, see |
18 | * <http://www.gnu.org/licenses/>. |
19 | * <http://www.gnu.org/licenses/>. |
19 | * |
20 | * |
20 | * The authors can be reached via e-mail to <support@deliantra.net> |
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
21 | */ |
22 | */ |
22 | |
23 | |
23 | /* |
24 | /* |
24 | * General convenience functions for deliantra. |
25 | * General convenience functions for deliantra. |
… | |
… | |
40 | |
41 | |
41 | #include <glib.h> |
42 | #include <glib.h> |
42 | |
43 | |
43 | refcnt_base::refcnt_t refcnt_dummy; |
44 | refcnt_base::refcnt_t refcnt_dummy; |
44 | ssize_t slice_alloc; |
45 | ssize_t slice_alloc; |
45 | rand_gen rndm, rmg_rndm; |
|
|
46 | |
|
|
47 | #if !GCC_VERSION(3,4) |
|
|
48 | int least_significant_bit (uint32_t x) |
|
|
49 | { |
|
|
50 | x &= -x; // this isolates the lowest bit |
|
|
51 | |
|
|
52 | int r = 0; |
|
|
53 | |
|
|
54 | if (x & 0xaaaaaaaa) r += 1; |
|
|
55 | if (x & 0xcccccccc) r += 2; |
|
|
56 | if (x & 0xf0f0f0f0) r += 4; |
|
|
57 | if (x & 0xff00ff00) r += 8; |
|
|
58 | if (x & 0xffff0000) r += 16; |
|
|
59 | |
|
|
60 | return r; |
|
|
61 | } |
|
|
62 | #endif |
|
|
63 | |
|
|
64 | void |
|
|
65 | tausworthe_random_generator::seed (uint32_t seed) |
|
|
66 | { |
|
|
67 | state [0] = seed * 69069U; if (state [0] < 2U) state [0] += 2U; |
|
|
68 | state [1] = state [0] * 69069U; if (state [0] < 8U) state [0] += 8U; |
|
|
69 | state [2] = state [1] * 69069U; if (state [0] < 16U) state [0] += 16U; |
|
|
70 | state [3] = state [2] * 69069U; if (state [0] < 128U) state [0] += 128U; |
|
|
71 | |
|
|
72 | for (int i = 11; --i; ) |
|
|
73 | next (); |
|
|
74 | } |
|
|
75 | |
|
|
76 | uint32_t |
|
|
77 | tausworthe_random_generator::next () |
|
|
78 | { |
|
|
79 | state [0] = ((state [0] & 0xFFFFFFFEU) << 18U) ^ (((state [0] << 6U) ^ state [0]) >> 13U); |
|
|
80 | state [1] = ((state [1] & 0xFFFFFFF8U) << 2U) ^ (((state [1] << 2U) ^ state [1]) >> 27U); |
|
|
81 | state [2] = ((state [2] & 0xFFFFFFF0U) << 7U) ^ (((state [2] << 13U) ^ state [2]) >> 21U); |
|
|
82 | state [3] = ((state [3] & 0xFFFFFF80U) << 13U) ^ (((state [3] << 3U) ^ state [3]) >> 12U); |
|
|
83 | |
|
|
84 | return state [0] ^ state [1] ^ state [2] ^ state [3]; |
|
|
85 | } |
|
|
86 | |
|
|
87 | template<class generator> |
|
|
88 | uint32_t |
|
|
89 | random_number_generator<generator>::get_range (uint32_t num) |
|
|
90 | { |
|
|
91 | return (this->next () * (uint64_t)num) >> 32U; |
|
|
92 | } |
|
|
93 | |
|
|
94 | // return a number within (min .. max) |
|
|
95 | template<class generator> |
|
|
96 | int |
|
|
97 | random_number_generator<generator>::get_range (int r_min, int r_max) |
|
|
98 | { |
|
|
99 | return r_min + get_range (max (r_max - r_min + 1, 0)); |
|
|
100 | } |
|
|
101 | |
|
|
102 | template struct random_number_generator<tausworthe_random_generator>; |
|
|
103 | template struct random_number_generator<xorshift_random_generator>; |
|
|
104 | |
46 | |
105 | /******************************************************************************/ |
47 | /******************************************************************************/ |
106 | |
48 | |
107 | /* Checks a player-provided string which will become the msg property of |
49 | /* Checks a player-provided string which will become the msg property of |
108 | * an object for dangerous input. |
50 | * an object for dangerous input. |
109 | */ |
51 | */ |
110 | bool |
52 | bool |
111 | msg_is_safe (const char *msg) |
53 | msg_is_safe (const char *msg) |
112 | { |
54 | { |
113 | bool safe = true; |
55 | bool safe = true; |
114 | |
56 | |
115 | /* Trying to cheat by getting data into the object */ |
57 | /* Trying to cheat by getting data into the object */ |
… | |
… | |
134 | signal (SIGINT , SIG_IGN); |
76 | signal (SIGINT , SIG_IGN); |
135 | signal (SIGTERM, SIG_IGN); |
77 | signal (SIGTERM, SIG_IGN); |
136 | signal (SIGABRT, SIG_IGN); |
78 | signal (SIGABRT, SIG_IGN); |
137 | |
79 | |
138 | signal (SIGSEGV, SIG_DFL); |
80 | signal (SIGSEGV, SIG_DFL); |
|
|
81 | signal (SIGFPE , SIG_DFL); |
|
|
82 | #ifdef SIGBUS |
139 | signal (SIGBUS , SIG_DFL); |
83 | signal (SIGBUS , SIG_DFL); |
|
|
84 | #endif |
140 | signal (SIGILL , SIG_DFL); |
85 | signal (SIGILL , SIG_DFL); |
141 | signal (SIGTRAP, SIG_DFL); |
86 | signal (SIGTRAP, SIG_DFL); |
142 | |
87 | |
143 | // try to put corefiles into a subdirectory, if existing, to allow |
88 | // try to put corefiles into a subdirectory, if existing, to allow |
144 | // an administrator to reduce the I/O load. |
89 | // an administrator to reduce the I/O load. |
… | |
… | |
168 | } |
113 | } |
169 | |
114 | |
170 | LOG (llevError, "fork abort: %s\n", msg); |
115 | LOG (llevError, "fork abort: %s\n", msg); |
171 | } |
116 | } |
172 | |
117 | |
173 | void *salloc_ (int n) throw (std::bad_alloc) |
118 | void * |
|
|
119 | salloc_ (int n) |
174 | { |
120 | { |
175 | void *ptr = g_slice_alloc (n); |
121 | void *ptr = g_slice_alloc (n); |
176 | |
122 | |
177 | if (!ptr) |
123 | if (!ptr) |
178 | throw std::bad_alloc (); |
124 | throw std::bad_alloc (); |
179 | |
125 | |
180 | slice_alloc += n; |
126 | slice_alloc += n; |
181 | return ptr; |
127 | return ptr; |
182 | } |
128 | } |
183 | |
129 | |
184 | void *salloc_ (int n, void *src) throw (std::bad_alloc) |
130 | void * |
|
|
131 | salloc_ (int n, void *src) |
185 | { |
132 | { |
186 | void *ptr = salloc_ (n); |
133 | void *ptr = salloc_ (n); |
187 | |
134 | |
188 | if (src) |
135 | if (src) |
189 | memcpy (ptr, src, n); |
136 | memcpy (ptr, src, n); |
… | |
… | |
197 | |
144 | |
198 | #if DEBUG_SALLOC |
145 | #if DEBUG_SALLOC |
199 | |
146 | |
200 | #define MAGIC 0xa1b2c35543deadLL |
147 | #define MAGIC 0xa1b2c35543deadLL |
201 | |
148 | |
|
|
149 | void * |
202 | void *g_slice_alloc (unsigned long size) |
150 | g_slice_alloc (unsigned long size) |
203 | { |
151 | { |
204 | unsigned long *p = (unsigned long *) (g_slice_alloc)(size + sizeof (unsigned long)); |
152 | unsigned long *p = (unsigned long *) (g_slice_alloc)(size + sizeof (unsigned long)); |
205 | *p++ = size ^ MAGIC; |
153 | *p++ = size ^ MAGIC; |
206 | //fprintf (stderr, "g_slice_alloc %ld %p\n", size, p);//D |
154 | //fprintf (stderr, "g_slice_alloc %ld %p\n", size, p);//D |
207 | return (void *)p; |
155 | return (void *)p; |
208 | } |
156 | } |
209 | |
157 | |
|
|
158 | void * |
210 | void *g_slice_alloc0 (unsigned long size) |
159 | g_slice_alloc0 (unsigned long size) |
211 | { |
160 | { |
212 | return memset (g_slice_alloc (size), 0, size); |
161 | return memset (g_slice_alloc (size), 0, size); |
213 | } |
162 | } |
214 | |
163 | |
|
|
164 | void |
215 | void g_slice_free1 (unsigned long size, void *ptr) |
165 | g_slice_free1 (unsigned long size, void *ptr) |
216 | { |
166 | { |
217 | //fprintf (stderr, "g_slice_free %ld %p\n", size, ptr);//D |
167 | //fprintf (stderr, "g_slice_free %ld %p\n", size, ptr);//D |
218 | if (expect_true (ptr)) |
168 | if (ecb_expect_true (ptr)) |
219 | { |
169 | { |
220 | unsigned long *p = (unsigned long *)ptr; |
170 | unsigned long *p = (unsigned long *)ptr; |
221 | unsigned long s = *--p ^ MAGIC; |
171 | unsigned long s = *--p ^ MAGIC; |
222 | |
172 | |
223 | if (size != (unsigned long)(*p ^ MAGIC)) |
173 | if (size != (unsigned long)(*p ^ MAGIC)) |
… | |
… | |
231 | (g_slice_free1)(s + sizeof (unsigned long), p); |
181 | (g_slice_free1)(s + sizeof (unsigned long), p); |
232 | } |
182 | } |
233 | } |
183 | } |
234 | |
184 | |
235 | #endif |
185 | #endif |
|
|
186 | |
|
|
187 | /******************************************************************************/ |
|
|
188 | |
|
|
189 | refcnt_buf::refcnt_buf (size_t size) |
|
|
190 | { |
|
|
191 | static uint32_t empty_buf [2] = { 0, 1 }; // 2 == never deallocated |
|
|
192 | data = (char *)empty_buf + overhead; |
|
|
193 | assert (overhead == sizeof (empty_buf)); |
|
|
194 | inc (); |
|
|
195 | } |
|
|
196 | |
|
|
197 | refcnt_buf::refcnt_buf (void *data, size_t size) |
|
|
198 | { |
|
|
199 | _alloc (size); |
|
|
200 | memcpy (this->data, data, size); |
|
|
201 | } |
|
|
202 | |
|
|
203 | refcnt_buf::~refcnt_buf () |
|
|
204 | { |
|
|
205 | dec (); |
|
|
206 | } |
|
|
207 | |
|
|
208 | void |
|
|
209 | refcnt_buf::_dealloc () |
|
|
210 | { |
|
|
211 | sfree<char> (data - overhead, size () + overhead); |
|
|
212 | } |
|
|
213 | |
|
|
214 | refcnt_buf & |
|
|
215 | refcnt_buf::operator =(const refcnt_buf &src) |
|
|
216 | { |
|
|
217 | dec (); |
|
|
218 | data = src.data; |
|
|
219 | inc (); |
|
|
220 | return *this; |
|
|
221 | } |
236 | |
222 | |
237 | /******************************************************************************/ |
223 | /******************************************************************************/ |
238 | |
224 | |
239 | int |
225 | int |
240 | assign (char *dst, const char *src, int maxsize) |
226 | assign (char *dst, const char *src, int maxsize) |
… | |
… | |
266 | } |
252 | } |
267 | |
253 | |
268 | char * |
254 | char * |
269 | vformat (const char *format, va_list ap) |
255 | vformat (const char *format, va_list ap) |
270 | { |
256 | { |
271 | static dynbuf_text buf; buf.clear (); |
257 | static dynbuf_text bufs[8]; |
|
|
258 | static int bufidx; |
|
|
259 | |
|
|
260 | dynbuf_text &buf = bufs [++bufidx & 7]; |
|
|
261 | |
|
|
262 | buf.clear (); |
272 | buf.vprintf (format, ap); |
263 | buf.vprintf (format, ap); |
273 | return buf; |
264 | return buf; |
274 | } |
265 | } |
275 | |
266 | |
276 | char * |
267 | char * |
… | |
… | |
282 | va_end (ap); |
273 | va_end (ap); |
283 | |
274 | |
284 | return buf; |
275 | return buf; |
285 | } |
276 | } |
286 | |
277 | |
287 | tstamp now () |
278 | tstamp |
|
|
279 | now () |
288 | { |
280 | { |
289 | struct timeval tv; |
281 | struct timeval tv; |
290 | |
282 | |
291 | gettimeofday (&tv, 0); |
283 | gettimeofday (&tv, 0); |
292 | return tstamp (tv.tv_sec) + tstamp (tv.tv_usec) * tstamp (1e-6); |
284 | return tstamp (tv.tv_sec) + tstamp (tv.tv_usec) * tstamp (1e-6); |
… | |
… | |
357 | 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL, |
349 | 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL, |
358 | 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL, |
350 | 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL, |
359 | 0x2d02ef8dL |
351 | 0x2d02ef8dL |
360 | }; |
352 | }; |
361 | |
353 | |
|
|
354 | void |
362 | void thread::start (void *(*start_routine)(void *), void *arg) |
355 | thread::start (void *(*start_routine)(void *), void *arg) |
363 | { |
356 | { |
364 | pthread_attr_t attr; |
357 | pthread_attr_t attr; |
365 | |
358 | |
366 | pthread_attr_init (&attr); |
359 | pthread_attr_init (&attr); |
367 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
360 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |