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.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,2011 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.
127 113
128 LOG (llevError, "fork abort: %s\n", msg); 114 LOG (llevError, "fork abort: %s\n", msg);
129} 115}
130 116
131void * 117void *
132salloc_ (int n) throw (std::bad_alloc) 118salloc_ (int n)
133{ 119{
134 void *ptr = g_slice_alloc (n); 120 void *ptr = g_slice_alloc (n);
135 121
136 if (!ptr) 122 if (!ptr)
137 throw std::bad_alloc (); 123 throw std::bad_alloc ();
139 slice_alloc += n; 125 slice_alloc += n;
140 return ptr; 126 return ptr;
141} 127}
142 128
143void * 129void *
144salloc_ (int n, void *src) throw (std::bad_alloc) 130salloc_ (int n, void *src)
145{ 131{
146 void *ptr = salloc_ (n); 132 void *ptr = salloc_ (n);
147 133
148 if (src) 134 if (src)
149 memcpy (ptr, src, n); 135 memcpy (ptr, src, n);
199 185
200/******************************************************************************/ 186/******************************************************************************/
201 187
202refcnt_buf::refcnt_buf (size_t size) 188refcnt_buf::refcnt_buf (size_t size)
203{ 189{
204 _alloc (size); 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 ();
205} 194}
206 195
207refcnt_buf::refcnt_buf (void *data, size_t size) 196refcnt_buf::refcnt_buf (void *data, size_t size)
208{ 197{
209 _alloc (size); 198 _alloc (size);
213refcnt_buf::~refcnt_buf () 202refcnt_buf::~refcnt_buf ()
214{ 203{
215 dec (); 204 dec ();
216} 205}
217 206
207void
208refcnt_buf::_dealloc ()
209{
210 sfree<char> (data - overhead, size () + overhead);
211}
212
218refcnt_buf & 213refcnt_buf &
219refcnt_buf::operator =(const refcnt_buf &src) 214refcnt_buf::operator =(const refcnt_buf &src)
220{ 215{
221 dec (); 216 dec ();
222 data = src.data; 217 data = src.data;
223 ++_refcnt (); 218 inc ();
224 return *this; 219 return *this;
225} 220}
226 221
227/******************************************************************************/ 222/******************************************************************************/
228 223

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines