ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/utils.C
(Generate patch)

Comparing deliantra/server/common/utils.C (file contents):
Revision 1.95 by root, Fri Mar 26 01:04:44 2010 UTC vs.
Revision 1.102 by root, Sun Nov 11 01:27:44 2012 UTC

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 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.
40 40
41#include <glib.h> 41#include <glib.h>
42 42
43refcnt_base::refcnt_t refcnt_dummy; 43refcnt_base::refcnt_t refcnt_dummy;
44ssize_t slice_alloc; 44ssize_t slice_alloc;
45rand_gen rndm, rmg_rndm;
46 45
47#if !GCC_VERSION(3,4) 46#if !GCC_VERSION(3,4)
48int least_significant_bit (uint32_t x) 47int least_significant_bit (uint32_t x)
49{ 48{
50 x &= -x; // this isolates the lowest bit 49 x &= -x; // this isolates the lowest bit
59 58
60 return r; 59 return r;
61} 60}
62#endif 61#endif
63 62
64void
65tausworthe_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
76uint32_t
77tausworthe_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
87template<class generator>
88uint32_t
89random_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)
95template<class generator>
96int
97random_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
102template struct random_number_generator<tausworthe_random_generator>;
103template struct random_number_generator<xorshift_random_generator>;
104
105/******************************************************************************/ 63/******************************************************************************/
106 64
107/* 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
108 * an object for dangerous input. 66 * an object for dangerous input.
109 */ 67 */
168 } 126 }
169 127
170 LOG (llevError, "fork abort: %s\n", msg); 128 LOG (llevError, "fork abort: %s\n", msg);
171} 129}
172 130
131void *
173void *salloc_ (int n) throw (std::bad_alloc) 132salloc_ (int n) throw (std::bad_alloc)
174{ 133{
175 void *ptr = g_slice_alloc (n); 134 void *ptr = g_slice_alloc (n);
176 135
177 if (!ptr) 136 if (!ptr)
178 throw std::bad_alloc (); 137 throw std::bad_alloc ();
179 138
180 slice_alloc += n; 139 slice_alloc += n;
181 return ptr; 140 return ptr;
182} 141}
183 142
143void *
184void *salloc_ (int n, void *src) throw (std::bad_alloc) 144salloc_ (int n, void *src) throw (std::bad_alloc)
185{ 145{
186 void *ptr = salloc_ (n); 146 void *ptr = salloc_ (n);
187 147
188 if (src) 148 if (src)
189 memcpy (ptr, src, n); 149 memcpy (ptr, src, n);
197 157
198#if DEBUG_SALLOC 158#if DEBUG_SALLOC
199 159
200#define MAGIC 0xa1b2c35543deadLL 160#define MAGIC 0xa1b2c35543deadLL
201 161
162void *
202void *g_slice_alloc (unsigned long size) 163g_slice_alloc (unsigned long size)
203{ 164{
204 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));
205 *p++ = size ^ MAGIC; 166 *p++ = size ^ MAGIC;
206 //fprintf (stderr, "g_slice_alloc %ld %p\n", size, p);//D 167 //fprintf (stderr, "g_slice_alloc %ld %p\n", size, p);//D
207 return (void *)p; 168 return (void *)p;
208} 169}
209 170
171void *
210void *g_slice_alloc0 (unsigned long size) 172g_slice_alloc0 (unsigned long size)
211{ 173{
212 return memset (g_slice_alloc (size), 0, size); 174 return memset (g_slice_alloc (size), 0, size);
213} 175}
214 176
177void
215void g_slice_free1 (unsigned long size, void *ptr) 178g_slice_free1 (unsigned long size, void *ptr)
216{ 179{
217 //fprintf (stderr, "g_slice_free %ld %p\n", size, ptr);//D 180 //fprintf (stderr, "g_slice_free %ld %p\n", size, ptr);//D
218 if (expect_true (ptr)) 181 if (expect_true (ptr))
219 { 182 {
220 unsigned long *p = (unsigned long *)ptr; 183 unsigned long *p = (unsigned long *)ptr;
231 (g_slice_free1)(s + sizeof (unsigned long), p); 194 (g_slice_free1)(s + sizeof (unsigned long), p);
232 } 195 }
233} 196}
234 197
235#endif 198#endif
199
200/******************************************************************************/
201
202refcnt_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
210refcnt_buf::refcnt_buf (void *data, size_t size)
211{
212 _alloc (size);
213 memcpy (this->data, data, size);
214}
215
216refcnt_buf::~refcnt_buf ()
217{
218 dec ();
219}
220
221void
222refcnt_buf::_dealloc ()
223{
224 sfree<char> (data - overhead, size () + overhead);
225}
226
227refcnt_buf &
228refcnt_buf::operator =(const refcnt_buf &src)
229{
230 dec ();
231 data = src.data;
232 inc ();
233 return *this;
234}
236 235
237/******************************************************************************/ 236/******************************************************************************/
238 237
239int 238int
240assign (char *dst, const char *src, int maxsize) 239assign (char *dst, const char *src, int maxsize)
266} 265}
267 266
268char * 267char *
269vformat (const char *format, va_list ap) 268vformat (const char *format, va_list ap)
270{ 269{
271 static dynbuf_text buf; buf.clear (); 270 static dynbuf_text bufs[8];
271 static int bufidx;
272
273 dynbuf_text &buf = bufs [++bufidx & 7];
274
275 buf.clear ();
272 buf.vprintf (format, ap); 276 buf.vprintf (format, ap);
273 return buf; 277 return buf;
274} 278}
275 279
276char * 280char *
282 va_end (ap); 286 va_end (ap);
283 287
284 return buf; 288 return buf;
285} 289}
286 290
287tstamp now () 291tstamp
292now ()
288{ 293{
289 struct timeval tv; 294 struct timeval tv;
290 295
291 gettimeofday (&tv, 0); 296 gettimeofday (&tv, 0);
292 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);
357 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL, 362 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL,
358 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL, 363 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL,
359 0x2d02ef8dL 364 0x2d02ef8dL
360}; 365};
361 366
367void
362void thread::start (void *(*start_routine)(void *), void *arg) 368thread::start (void *(*start_routine)(void *), void *arg)
363{ 369{
364 pthread_attr_t attr; 370 pthread_attr_t attr;
365 371
366 pthread_attr_init (&attr); 372 pthread_attr_init (&attr);
367 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); 373 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines