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.70 by root, Fri Jan 25 18:09:23 2008 UTC vs.
Revision 1.75 by root, Tue Apr 15 03:16:02 2008 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 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 Frank Tore Johansen 6 * Copyright (©) 1992,2007 Frank Tore Johansen
7 * 7 *
8 * Deliantra is free software: you can redistribute it and/or modify 8 * Deliantra is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
37#include <material.h> 37#include <material.h>
38 38
39#include <glib.h> 39#include <glib.h>
40 40
41refcnt_base::refcnt_t refcnt_dummy; 41refcnt_base::refcnt_t refcnt_dummy;
42size_t slice_alloc; 42ssize_t slice_alloc;
43rand_gen rndm; 43rand_gen rndm;
44 44
45void 45void
46tausworthe_random_generator::seed (uint32_t seed) 46tausworthe_random_generator::seed (uint32_t seed)
47{ 47{
48 state [0] = seed * 69069U; if (state [0] < 2U) state [0] += 2U; 48 state [0] = seed * 69069U; if (state [0] < 2U) state [0] += 2U;
49 state [1] = state [0] * 69069U; if (state [0] < 8U) state [0] += 8U; 49 state [1] = state [0] * 69069U; if (state [0] < 8U) state [0] += 8U;
50 state [2] = state [1] * 69069U; if (state [0] < 16U) state [0] += 16U; 50 state [2] = state [1] * 69069U; if (state [0] < 16U) state [0] += 16U;
51 state [3] = state [2] * 69069U; if (state [0] < 128) state [0] += 128U; 51 state [3] = state [2] * 69069U; if (state [0] < 128U) state [0] += 128U;
52 52
53 for (int i = 11; --i; ) 53 for (int i = 11; --i; )
54 operator ()(); 54 operator ()();
55} 55}
56 56
469 LOG (llevError, "fork abort: %s\n", msg); 469 LOG (llevError, "fork abort: %s\n", msg);
470} 470}
471 471
472void *salloc_ (int n) throw (std::bad_alloc) 472void *salloc_ (int n) throw (std::bad_alloc)
473{ 473{
474#ifdef PREFER_MALLOC
475 void *ptr = malloc (n);
476#else
477 slice_alloc += n;
478 void *ptr = g_slice_alloc (n); 474 void *ptr = g_slice_alloc (n);
479#endif
480 475
481 if (!ptr) 476 if (!ptr)
482 throw std::bad_alloc (); 477 throw std::bad_alloc ();
483 478
479 slice_alloc += n;
484 return ptr; 480 return ptr;
485} 481}
486 482
487void *salloc_ (int n, void *src) throw (std::bad_alloc) 483void *salloc_ (int n, void *src) throw (std::bad_alloc)
488{ 484{
496 return ptr; 492 return ptr;
497} 493}
498 494
499/******************************************************************************/ 495/******************************************************************************/
500 496
501#ifdef DEBUG_SALLOC 497#if DEBUG_SALLOC
502 498
503#define MAGIC 0xa1b2c35543deadLL 499#define MAGIC 0xa1b2c35543deadLL
504 500
505void *g_slice_alloc (unsigned long size) 501void *g_slice_alloc (unsigned long size)
506{ 502{
515 return memset (g_slice_alloc (size), 0, size); 511 return memset (g_slice_alloc (size), 0, size);
516} 512}
517 513
518void g_slice_free1 (unsigned long size, void *ptr) 514void g_slice_free1 (unsigned long size, void *ptr)
519{ 515{
516 //fprintf (stderr, "g_slice_free %ld %p\n", size, ptr);//D
520 if (expect_true (ptr)) 517 if (expect_true (ptr))
521 { 518 {
522 //fprintf (stderr, "g_slice_free %ld %p\n", size, ptr);//D
523 unsigned long *p = (unsigned long *)ptr; 519 unsigned long *p = (unsigned long *)ptr;
524 unsigned long s = *--p ^ MAGIC; 520 unsigned long s = *--p ^ MAGIC;
525 521
526 if ((*p ^ MAGIC) != size) 522 if (size != (unsigned long)(*p ^ MAGIC))
523 {
527 LOG (logBacktrace | llevError, "slice free size (%lx) doesn't match alloc size (%lx)\n", size, s); 524 LOG (logBacktrace | llevError, "slice free size (%lx) doesn't match alloc size (%lx)\n", size, s);
525 abort ();
526 }
528 527
529 *p = MAGIC; 528 *p = MAGIC;
530 529
531 (g_slice_free1)(s + sizeof (unsigned long), p); 530 (g_slice_free1)(s + sizeof (unsigned long), p);
532 } 531 }
648 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL, 647 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL,
649 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL, 648 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL,
650 0x2d02ef8dL 649 0x2d02ef8dL
651}; 650};
652 651
652void thread::start (void *(*start_routine)(void *), void *arg)
653{
654 pthread_attr_t attr;
655
656 pthread_attr_init (&attr);
657 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
658 pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < sizeof (long) * 4096
659 ? sizeof (long) * 4096 : PTHREAD_STACK_MIN);
660#ifdef PTHREAD_SCOPE_PROCESS
661 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
662#endif
663
664 sigset_t fullsigset, oldsigset;
665 sigfillset (&fullsigset);
666
667 pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset);
668
669 if (pthread_create (&id, &attr, start_routine, arg))
670 cleanup ("unable to create a new thread");
671
672 pthread_sigmask (SIG_SETMASK, &oldsigset, 0);
673}
674

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines