--- deliantra/server/common/utils.C 2010/03/26 01:04:44 1.95
+++ deliantra/server/common/utils.C 2012/11/11 01:27:44 1.102
@@ -1,22 +1,22 @@
/*
* This file is part of Deliantra, the Roguelike Realtime MMORPG.
- *
- * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
- *
+ *
+ * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
+ *
* Deliantra is free software: you can redistribute it and/or modify it under
* the terms of the Affero GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
- *
+ *
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
- *
+ *
* You should have received a copy of the Affero GNU General Public License
* and the GNU General Public License along with this program. If not, see
* .
- *
+ *
* The authors can be reached via e-mail to
*/
@@ -42,7 +42,6 @@
refcnt_base::refcnt_t refcnt_dummy;
ssize_t slice_alloc;
-rand_gen rndm, rmg_rndm;
#if !GCC_VERSION(3,4)
int least_significant_bit (uint32_t x)
@@ -61,47 +60,6 @@
}
#endif
-void
-tausworthe_random_generator::seed (uint32_t seed)
-{
- state [0] = seed * 69069U; if (state [0] < 2U) state [0] += 2U;
- state [1] = state [0] * 69069U; if (state [0] < 8U) state [0] += 8U;
- state [2] = state [1] * 69069U; if (state [0] < 16U) state [0] += 16U;
- state [3] = state [2] * 69069U; if (state [0] < 128U) state [0] += 128U;
-
- for (int i = 11; --i; )
- next ();
-}
-
-uint32_t
-tausworthe_random_generator::next ()
-{
- state [0] = ((state [0] & 0xFFFFFFFEU) << 18U) ^ (((state [0] << 6U) ^ state [0]) >> 13U);
- state [1] = ((state [1] & 0xFFFFFFF8U) << 2U) ^ (((state [1] << 2U) ^ state [1]) >> 27U);
- state [2] = ((state [2] & 0xFFFFFFF0U) << 7U) ^ (((state [2] << 13U) ^ state [2]) >> 21U);
- state [3] = ((state [3] & 0xFFFFFF80U) << 13U) ^ (((state [3] << 3U) ^ state [3]) >> 12U);
-
- return state [0] ^ state [1] ^ state [2] ^ state [3];
-}
-
-template
-uint32_t
-random_number_generator::get_range (uint32_t num)
-{
- return (this->next () * (uint64_t)num) >> 32U;
-}
-
-// return a number within (min .. max)
-template
-int
-random_number_generator::get_range (int r_min, int r_max)
-{
- return r_min + get_range (max (r_max - r_min + 1, 0));
-}
-
-template struct random_number_generator;
-template struct random_number_generator;
-
/******************************************************************************/
/* Checks a player-provided string which will become the msg property of
@@ -170,7 +128,8 @@
LOG (llevError, "fork abort: %s\n", msg);
}
-void *salloc_ (int n) throw (std::bad_alloc)
+void *
+salloc_ (int n) throw (std::bad_alloc)
{
void *ptr = g_slice_alloc (n);
@@ -181,7 +140,8 @@
return ptr;
}
-void *salloc_ (int n, void *src) throw (std::bad_alloc)
+void *
+salloc_ (int n, void *src) throw (std::bad_alloc)
{
void *ptr = salloc_ (n);
@@ -199,7 +159,8 @@
#define MAGIC 0xa1b2c35543deadLL
-void *g_slice_alloc (unsigned long size)
+void *
+g_slice_alloc (unsigned long size)
{
unsigned long *p = (unsigned long *) (g_slice_alloc)(size + sizeof (unsigned long));
*p++ = size ^ MAGIC;
@@ -207,12 +168,14 @@
return (void *)p;
}
-void *g_slice_alloc0 (unsigned long size)
+void *
+g_slice_alloc0 (unsigned long size)
{
return memset (g_slice_alloc (size), 0, size);
}
-void g_slice_free1 (unsigned long size, void *ptr)
+void
+g_slice_free1 (unsigned long size, void *ptr)
{
//fprintf (stderr, "g_slice_free %ld %p\n", size, ptr);//D
if (expect_true (ptr))
@@ -236,6 +199,42 @@
/******************************************************************************/
+refcnt_buf::refcnt_buf (size_t size)
+{
+ static uint32_t empty_buf [2] = { 0, 1 }; // 2 == never deallocated
+ data = (char *)empty_buf + overhead;
+ assert (overhead == sizeof (empty_buf));
+ inc ();
+}
+
+refcnt_buf::refcnt_buf (void *data, size_t size)
+{
+ _alloc (size);
+ memcpy (this->data, data, size);
+}
+
+refcnt_buf::~refcnt_buf ()
+{
+ dec ();
+}
+
+void
+refcnt_buf::_dealloc ()
+{
+ sfree (data - overhead, size () + overhead);
+}
+
+refcnt_buf &
+refcnt_buf::operator =(const refcnt_buf &src)
+{
+ dec ();
+ data = src.data;
+ inc ();
+ return *this;
+}
+
+/******************************************************************************/
+
int
assign (char *dst, const char *src, int maxsize)
{
@@ -268,7 +267,12 @@
char *
vformat (const char *format, va_list ap)
{
- static dynbuf_text buf; buf.clear ();
+ static dynbuf_text bufs[8];
+ static int bufidx;
+
+ dynbuf_text &buf = bufs [++bufidx & 7];
+
+ buf.clear ();
buf.vprintf (format, ap);
return buf;
}
@@ -284,7 +288,8 @@
return buf;
}
-tstamp now ()
+tstamp
+now ()
{
struct timeval tv;
@@ -359,7 +364,8 @@
0x2d02ef8dL
};
-void thread::start (void *(*start_routine)(void *), void *arg)
+void
+thread::start (void *(*start_routine)(void *), void *arg)
{
pthread_attr_t attr;