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.99 by root, Tue Jan 3 11:23:41 2012 UTC vs.
Revision 1.109 by root, Wed Dec 5 19:03:26 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 (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
4 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011 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.
41#include <glib.h> 42#include <glib.h>
42 43
43refcnt_base::refcnt_t refcnt_dummy; 44refcnt_base::refcnt_t refcnt_dummy;
44ssize_t slice_alloc; 45ssize_t slice_alloc;
45 46
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/******************************************************************************/ 47/******************************************************************************/
64 48
65/* 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
66 * an object for dangerous input. 50 * an object for dangerous input.
67 */ 51 */
68bool 52bool
69msg_is_safe (const char *msg) 53msg_is_safe (const char *msg)
70{ 54{
71 bool safe = true; 55 bool safe = true;
72 56
73 /* Trying to cheat by getting data into the object */ 57 /* Trying to cheat by getting data into the object */
92 signal (SIGINT , SIG_IGN); 76 signal (SIGINT , SIG_IGN);
93 signal (SIGTERM, SIG_IGN); 77 signal (SIGTERM, SIG_IGN);
94 signal (SIGABRT, SIG_IGN); 78 signal (SIGABRT, SIG_IGN);
95 79
96 signal (SIGSEGV, SIG_DFL); 80 signal (SIGSEGV, SIG_DFL);
81 signal (SIGFPE , SIG_DFL);
82#ifdef SIGBUS
97 signal (SIGBUS , SIG_DFL); 83 signal (SIGBUS , SIG_DFL);
84#endif
98 signal (SIGILL , SIG_DFL); 85 signal (SIGILL , SIG_DFL);
99 signal (SIGTRAP, SIG_DFL); 86 signal (SIGTRAP, SIG_DFL);
100 87
101 // try to put corefiles into a subdirectory, if existing, to allow 88 // try to put corefiles into a subdirectory, if existing, to allow
102 // an administrator to reduce the I/O load. 89 // an administrator to reduce the I/O load.
127 114
128 LOG (llevError, "fork abort: %s\n", msg); 115 LOG (llevError, "fork abort: %s\n", msg);
129} 116}
130 117
131void * 118void *
132salloc_ (int n) throw (std::bad_alloc) 119salloc_ (int n)
133{ 120{
134 void *ptr = g_slice_alloc (n); 121 void *ptr = g_slice_alloc (n);
135 122
136 if (!ptr) 123 if (!ptr)
137 throw std::bad_alloc (); 124 throw std::bad_alloc ();
139 slice_alloc += n; 126 slice_alloc += n;
140 return ptr; 127 return ptr;
141} 128}
142 129
143void * 130void *
144salloc_ (int n, void *src) throw (std::bad_alloc) 131salloc_ (int n, void *src)
145{ 132{
146 void *ptr = salloc_ (n); 133 void *ptr = salloc_ (n);
147 134
148 if (src) 135 if (src)
149 memcpy (ptr, src, n); 136 memcpy (ptr, src, n);
176 163
177void 164void
178g_slice_free1 (unsigned long size, void *ptr) 165g_slice_free1 (unsigned long size, void *ptr)
179{ 166{
180 //fprintf (stderr, "g_slice_free %ld %p\n", size, ptr);//D 167 //fprintf (stderr, "g_slice_free %ld %p\n", size, ptr);//D
181 if (expect_true (ptr)) 168 if (ecb_expect_true (ptr))
182 { 169 {
183 unsigned long *p = (unsigned long *)ptr; 170 unsigned long *p = (unsigned long *)ptr;
184 unsigned long s = *--p ^ MAGIC; 171 unsigned long s = *--p ^ MAGIC;
185 172
186 if (size != (unsigned long)(*p ^ MAGIC)) 173 if (size != (unsigned long)(*p ^ MAGIC))
199 186
200/******************************************************************************/ 187/******************************************************************************/
201 188
202refcnt_buf::refcnt_buf (size_t size) 189refcnt_buf::refcnt_buf (size_t size)
203{ 190{
204 _alloc (size); 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 ();
205} 195}
206 196
207refcnt_buf::refcnt_buf (void *data, size_t size) 197refcnt_buf::refcnt_buf (void *data, size_t size)
208{ 198{
209 _alloc (size); 199 _alloc (size);
213refcnt_buf::~refcnt_buf () 203refcnt_buf::~refcnt_buf ()
214{ 204{
215 dec (); 205 dec ();
216} 206}
217 207
208void
209refcnt_buf::_dealloc ()
210{
211 sfree<char> (data - overhead, size () + overhead);
212}
213
218refcnt_buf & 214refcnt_buf &
219refcnt_buf::operator =(const refcnt_buf &src) 215refcnt_buf::operator =(const refcnt_buf &src)
220{ 216{
221 dec (); 217 dec ();
222 data = src.data; 218 data = src.data;
223 ++_refcnt (); 219 inc ();
224 return *this; 220 return *this;
225} 221}
226 222
227/******************************************************************************/ 223/******************************************************************************/
228 224

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines