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,2012 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. |
… | |
… | |
41 | #include <glib.h> |
42 | #include <glib.h> |
42 | |
43 | |
43 | refcnt_base::refcnt_t refcnt_dummy; |
44 | refcnt_base::refcnt_t refcnt_dummy; |
44 | ssize_t slice_alloc; |
45 | ssize_t slice_alloc; |
45 | |
46 | |
46 | #if !GCC_VERSION(3,4) |
|
|
47 | int 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 | */ |
68 | bool |
52 | bool |
69 | msg_is_safe (const char *msg) |
53 | msg_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 | |
131 | void * |
118 | void * |
132 | salloc_ (int n) throw (std::bad_alloc) |
119 | salloc_ (int n) noexcept |
133 | { |
120 | { |
134 | void *ptr = g_slice_alloc (n); |
121 | void *ptr = g_slice_alloc (n); |
135 | |
122 | |
136 | if (!ptr) |
123 | if (ecb_expect_false (!ptr)) |
137 | throw std::bad_alloc (); |
124 | { |
|
|
125 | LOG (llevError, "out of memory allocating %d bytes, aborting.\n", n); |
|
|
126 | abort (); |
|
|
127 | } |
138 | |
128 | |
139 | slice_alloc += n; |
129 | slice_alloc += n; |
140 | return ptr; |
130 | return ptr; |
141 | } |
131 | } |
142 | |
132 | |
|
|
133 | // thos noinline works around a bug in gcc10, which otherwise inlines |
|
|
134 | // this fucntion and, only in the inlined versions, optimises out |
|
|
135 | // the memset, leasving memory uninitialised. |
|
|
136 | ecb_noinline |
143 | void * |
137 | void * |
144 | salloc_ (int n, void *src) throw (std::bad_alloc) |
138 | salloc_ (int n, void *src) noexcept |
145 | { |
139 | { |
146 | void *ptr = salloc_ (n); |
140 | void *ptr = salloc_ (n); |
147 | |
141 | |
148 | if (src) |
142 | if (src) |
149 | memcpy (ptr, src, n); |
143 | memcpy (ptr, src, n); |
… | |
… | |
176 | |
170 | |
177 | void |
171 | void |
178 | g_slice_free1 (unsigned long size, void *ptr) |
172 | g_slice_free1 (unsigned long size, void *ptr) |
179 | { |
173 | { |
180 | //fprintf (stderr, "g_slice_free %ld %p\n", size, ptr);//D |
174 | //fprintf (stderr, "g_slice_free %ld %p\n", size, ptr);//D |
181 | if (expect_true (ptr)) |
175 | if (ecb_expect_true (ptr)) |
182 | { |
176 | { |
183 | unsigned long *p = (unsigned long *)ptr; |
177 | unsigned long *p = (unsigned long *)ptr; |
184 | unsigned long s = *--p ^ MAGIC; |
178 | unsigned long s = *--p ^ MAGIC; |
185 | |
179 | |
186 | if (size != (unsigned long)(*p ^ MAGIC)) |
180 | if (size != (unsigned long)(*p ^ MAGIC)) |
… | |
… | |
199 | |
193 | |
200 | /******************************************************************************/ |
194 | /******************************************************************************/ |
201 | |
195 | |
202 | refcnt_buf::refcnt_buf (size_t size) |
196 | refcnt_buf::refcnt_buf (size_t size) |
203 | { |
197 | { |
204 | _alloc (size); |
198 | static uint32_t empty_buf [2] = { 0, 1 }; // 2 == never deallocated |
|
|
199 | data = (char *)empty_buf + overhead; |
|
|
200 | assert (overhead == sizeof (empty_buf)); |
|
|
201 | inc (); |
205 | } |
202 | } |
206 | |
203 | |
207 | refcnt_buf::refcnt_buf (void *data, size_t size) |
204 | refcnt_buf::refcnt_buf (void *data, size_t size) |
208 | { |
205 | { |
209 | _alloc (size); |
206 | _alloc (size); |
… | |
… | |
213 | refcnt_buf::~refcnt_buf () |
210 | refcnt_buf::~refcnt_buf () |
214 | { |
211 | { |
215 | dec (); |
212 | dec (); |
216 | } |
213 | } |
217 | |
214 | |
|
|
215 | void |
|
|
216 | refcnt_buf::_dealloc () |
|
|
217 | { |
|
|
218 | sfree<char> (data - overhead, size () + overhead); |
|
|
219 | } |
|
|
220 | |
218 | refcnt_buf & |
221 | refcnt_buf & |
219 | refcnt_buf::operator =(const refcnt_buf &src) |
222 | refcnt_buf::operator =(const refcnt_buf &src) |
220 | { |
223 | { |
221 | dec (); |
224 | dec (); |
222 | data = src.data; |
225 | data = src.data; |
223 | ++_refcnt (); |
226 | inc (); |
224 | return *this; |
227 | return *this; |
225 | } |
228 | } |
226 | |
229 | |
227 | /******************************************************************************/ |
230 | /******************************************************************************/ |
228 | |
231 | |