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.97 by root, Fri Jul 2 02:00:47 2010 UTC vs.
Revision 1.107 by root, Sat Nov 17 23:33:17 2018 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,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.
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;
45 45
46#if !GCC_VERSION(3,4)
47int least_significant_bit (uint32_t x)
48{
49 x &= -x; // this isolates the lowest bit
50
51 int r = 0;
52
53 if (x & 0xaaaaaaaa) r += 1;
54 if (x & 0xcccccccc) r += 2;
55 if (x & 0xf0f0f0f0) r += 4;
56 if (x & 0xff00ff00) r += 8;
57 if (x & 0xffff0000) r += 16;
58
59 return r;
60}
61#endif
62
63/******************************************************************************/ 46/******************************************************************************/
64 47
65/* Checks a player-provided string which will become the msg property of 48/* Checks a player-provided string which will become the msg property of
66 * an object for dangerous input. 49 * an object for dangerous input.
67 */ 50 */
68bool 51bool
69msg_is_safe (const char *msg) 52msg_is_safe (const char *msg)
70{ 53{
71 bool safe = true; 54 bool safe = true;
72 55
73 /* Trying to cheat by getting data into the object */ 56 /* Trying to cheat by getting data into the object */
92 signal (SIGINT , SIG_IGN); 75 signal (SIGINT , SIG_IGN);
93 signal (SIGTERM, SIG_IGN); 76 signal (SIGTERM, SIG_IGN);
94 signal (SIGABRT, SIG_IGN); 77 signal (SIGABRT, SIG_IGN);
95 78
96 signal (SIGSEGV, SIG_DFL); 79 signal (SIGSEGV, SIG_DFL);
80 signal (SIGFPE , SIG_DFL);
81#ifdef SIGBUS
97 signal (SIGBUS , SIG_DFL); 82 signal (SIGBUS , SIG_DFL);
83#endif
98 signal (SIGILL , SIG_DFL); 84 signal (SIGILL , SIG_DFL);
99 signal (SIGTRAP, SIG_DFL); 85 signal (SIGTRAP, SIG_DFL);
100 86
101 // try to put corefiles into a subdirectory, if existing, to allow 87 // try to put corefiles into a subdirectory, if existing, to allow
102 // an administrator to reduce the I/O load. 88 // an administrator to reduce the I/O load.
126 } 112 }
127 113
128 LOG (llevError, "fork abort: %s\n", msg); 114 LOG (llevError, "fork abort: %s\n", msg);
129} 115}
130 116
131void *salloc_ (int n) throw (std::bad_alloc) 117void *
118salloc_ (int n)
132{ 119{
133 void *ptr = g_slice_alloc (n); 120 void *ptr = g_slice_alloc (n);
134 121
135 if (!ptr) 122 if (!ptr)
136 throw std::bad_alloc (); 123 throw std::bad_alloc ();
137 124
138 slice_alloc += n; 125 slice_alloc += n;
139 return ptr; 126 return ptr;
140} 127}
141 128
142void *salloc_ (int n, void *src) throw (std::bad_alloc) 129void *
130salloc_ (int n, void *src)
143{ 131{
144 void *ptr = salloc_ (n); 132 void *ptr = salloc_ (n);
145 133
146 if (src) 134 if (src)
147 memcpy (ptr, src, n); 135 memcpy (ptr, src, n);
155 143
156#if DEBUG_SALLOC 144#if DEBUG_SALLOC
157 145
158#define MAGIC 0xa1b2c35543deadLL 146#define MAGIC 0xa1b2c35543deadLL
159 147
148void *
160void *g_slice_alloc (unsigned long size) 149g_slice_alloc (unsigned long size)
161{ 150{
162 unsigned long *p = (unsigned long *) (g_slice_alloc)(size + sizeof (unsigned long)); 151 unsigned long *p = (unsigned long *) (g_slice_alloc)(size + sizeof (unsigned long));
163 *p++ = size ^ MAGIC; 152 *p++ = size ^ MAGIC;
164 //fprintf (stderr, "g_slice_alloc %ld %p\n", size, p);//D 153 //fprintf (stderr, "g_slice_alloc %ld %p\n", size, p);//D
165 return (void *)p; 154 return (void *)p;
166} 155}
167 156
157void *
168void *g_slice_alloc0 (unsigned long size) 158g_slice_alloc0 (unsigned long size)
169{ 159{
170 return memset (g_slice_alloc (size), 0, size); 160 return memset (g_slice_alloc (size), 0, size);
171} 161}
172 162
163void
173void g_slice_free1 (unsigned long size, void *ptr) 164g_slice_free1 (unsigned long size, void *ptr)
174{ 165{
175 //fprintf (stderr, "g_slice_free %ld %p\n", size, ptr);//D 166 //fprintf (stderr, "g_slice_free %ld %p\n", size, ptr);//D
176 if (expect_true (ptr)) 167 if (expect_true (ptr))
177 { 168 {
178 unsigned long *p = (unsigned long *)ptr; 169 unsigned long *p = (unsigned long *)ptr;
189 (g_slice_free1)(s + sizeof (unsigned long), p); 180 (g_slice_free1)(s + sizeof (unsigned long), p);
190 } 181 }
191} 182}
192 183
193#endif 184#endif
185
186/******************************************************************************/
187
188refcnt_buf::refcnt_buf (size_t size)
189{
190 static uint32_t empty_buf [2] = { 0, 1 }; // 2 == never deallocated
191 data = (char *)empty_buf + overhead;
192 assert (overhead == sizeof (empty_buf));
193 inc ();
194}
195
196refcnt_buf::refcnt_buf (void *data, size_t size)
197{
198 _alloc (size);
199 memcpy (this->data, data, size);
200}
201
202refcnt_buf::~refcnt_buf ()
203{
204 dec ();
205}
206
207void
208refcnt_buf::_dealloc ()
209{
210 sfree<char> (data - overhead, size () + overhead);
211}
212
213refcnt_buf &
214refcnt_buf::operator =(const refcnt_buf &src)
215{
216 dec ();
217 data = src.data;
218 inc ();
219 return *this;
220}
194 221
195/******************************************************************************/ 222/******************************************************************************/
196 223
197int 224int
198assign (char *dst, const char *src, int maxsize) 225assign (char *dst, const char *src, int maxsize)
245 va_end (ap); 272 va_end (ap);
246 273
247 return buf; 274 return buf;
248} 275}
249 276
250tstamp now () 277tstamp
278now ()
251{ 279{
252 struct timeval tv; 280 struct timeval tv;
253 281
254 gettimeofday (&tv, 0); 282 gettimeofday (&tv, 0);
255 return tstamp (tv.tv_sec) + tstamp (tv.tv_usec) * tstamp (1e-6); 283 return tstamp (tv.tv_sec) + tstamp (tv.tv_usec) * tstamp (1e-6);
320 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL, 348 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL,
321 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL, 349 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL,
322 0x2d02ef8dL 350 0x2d02ef8dL
323}; 351};
324 352
353void
325void thread::start (void *(*start_routine)(void *), void *arg) 354thread::start (void *(*start_routine)(void *), void *arg)
326{ 355{
327 pthread_attr_t attr; 356 pthread_attr_t attr;
328 357
329 pthread_attr_init (&attr); 358 pthread_attr_init (&attr);
330 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); 359 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines