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.98 by root, Sat Apr 23 04:56:47 2011 UTC vs.
Revision 1.99 by root, Tue Jan 3 11:23:41 2012 UTC

126 } 126 }
127 127
128 LOG (llevError, "fork abort: %s\n", msg); 128 LOG (llevError, "fork abort: %s\n", msg);
129} 129}
130 130
131void *
131void *salloc_ (int n) throw (std::bad_alloc) 132salloc_ (int n) throw (std::bad_alloc)
132{ 133{
133 void *ptr = g_slice_alloc (n); 134 void *ptr = g_slice_alloc (n);
134 135
135 if (!ptr) 136 if (!ptr)
136 throw std::bad_alloc (); 137 throw std::bad_alloc ();
137 138
138 slice_alloc += n; 139 slice_alloc += n;
139 return ptr; 140 return ptr;
140} 141}
141 142
143void *
142void *salloc_ (int n, void *src) throw (std::bad_alloc) 144salloc_ (int n, void *src) throw (std::bad_alloc)
143{ 145{
144 void *ptr = salloc_ (n); 146 void *ptr = salloc_ (n);
145 147
146 if (src) 148 if (src)
147 memcpy (ptr, src, n); 149 memcpy (ptr, src, n);
155 157
156#if DEBUG_SALLOC 158#if DEBUG_SALLOC
157 159
158#define MAGIC 0xa1b2c35543deadLL 160#define MAGIC 0xa1b2c35543deadLL
159 161
162void *
160void *g_slice_alloc (unsigned long size) 163g_slice_alloc (unsigned long size)
161{ 164{
162 unsigned long *p = (unsigned long *) (g_slice_alloc)(size + sizeof (unsigned long)); 165 unsigned long *p = (unsigned long *) (g_slice_alloc)(size + sizeof (unsigned long));
163 *p++ = size ^ MAGIC; 166 *p++ = size ^ MAGIC;
164 //fprintf (stderr, "g_slice_alloc %ld %p\n", size, p);//D 167 //fprintf (stderr, "g_slice_alloc %ld %p\n", size, p);//D
165 return (void *)p; 168 return (void *)p;
166} 169}
167 170
171void *
168void *g_slice_alloc0 (unsigned long size) 172g_slice_alloc0 (unsigned long size)
169{ 173{
170 return memset (g_slice_alloc (size), 0, size); 174 return memset (g_slice_alloc (size), 0, size);
171} 175}
172 176
177void
173void g_slice_free1 (unsigned long size, void *ptr) 178g_slice_free1 (unsigned long size, void *ptr)
174{ 179{
175 //fprintf (stderr, "g_slice_free %ld %p\n", size, ptr);//D 180 //fprintf (stderr, "g_slice_free %ld %p\n", size, ptr);//D
176 if (expect_true (ptr)) 181 if (expect_true (ptr))
177 { 182 {
178 unsigned long *p = (unsigned long *)ptr; 183 unsigned long *p = (unsigned long *)ptr;
189 (g_slice_free1)(s + sizeof (unsigned long), p); 194 (g_slice_free1)(s + sizeof (unsigned long), p);
190 } 195 }
191} 196}
192 197
193#endif 198#endif
199
200/******************************************************************************/
201
202refcnt_buf::refcnt_buf (size_t size)
203{
204 _alloc (size);
205}
206
207refcnt_buf::refcnt_buf (void *data, size_t size)
208{
209 _alloc (size);
210 memcpy (this->data, data, size);
211}
212
213refcnt_buf::~refcnt_buf ()
214{
215 dec ();
216}
217
218refcnt_buf &
219refcnt_buf::operator =(const refcnt_buf &src)
220{
221 dec ();
222 data = src.data;
223 ++_refcnt ();
224 return *this;
225}
194 226
195/******************************************************************************/ 227/******************************************************************************/
196 228
197int 229int
198assign (char *dst, const char *src, int maxsize) 230assign (char *dst, const char *src, int maxsize)
245 va_end (ap); 277 va_end (ap);
246 278
247 return buf; 279 return buf;
248} 280}
249 281
250tstamp now () 282tstamp
283now ()
251{ 284{
252 struct timeval tv; 285 struct timeval tv;
253 286
254 gettimeofday (&tv, 0); 287 gettimeofday (&tv, 0);
255 return tstamp (tv.tv_sec) + tstamp (tv.tv_usec) * tstamp (1e-6); 288 return tstamp (tv.tv_sec) + tstamp (tv.tv_usec) * tstamp (1e-6);
320 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL, 353 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL,
321 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL, 354 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL,
322 0x2d02ef8dL 355 0x2d02ef8dL
323}; 356};
324 357
358void
325void thread::start (void *(*start_routine)(void *), void *arg) 359thread::start (void *(*start_routine)(void *), void *arg)
326{ 360{
327 pthread_attr_t attr; 361 pthread_attr_t attr;
328 362
329 pthread_attr_init (&attr); 363 pthread_attr_init (&attr);
330 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); 364 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines