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 | |
41 | refcnt_base::refcnt_t refcnt_dummy; |
41 | refcnt_base::refcnt_t refcnt_dummy; |
42 | size_t slice_alloc; |
42 | ssize_t slice_alloc; |
43 | rand_gen rndm; |
43 | rand_gen rndm; |
44 | |
44 | |
45 | void |
45 | void |
46 | tausworthe_random_generator::seed (uint32_t seed) |
46 | tausworthe_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 | |
472 | void *salloc_ (int n) throw (std::bad_alloc) |
472 | void *salloc_ (int n) throw (std::bad_alloc) |
473 | { |
473 | { |
474 | #if 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 | |
487 | void *salloc_ (int n, void *src) throw (std::bad_alloc) |
483 | void *salloc_ (int n, void *src) throw (std::bad_alloc) |
488 | { |
484 | { |
… | |
… | |
515 | return memset (g_slice_alloc (size), 0, size); |
511 | return memset (g_slice_alloc (size), 0, size); |
516 | } |
512 | } |
517 | |
513 | |
518 | void g_slice_free1 (unsigned long size, void *ptr) |
514 | void 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 (size != (unsigned long)(*p ^ MAGIC)) |
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 | |
|
|
652 | void thread::start (void *(*start_routine)(void *), void *arg) |
|
|
653 | { |
|
|
654 | sigset_t fullsigset, oldsigset; |
|
|
655 | sigfillset (&fullsigset); |
|
|
656 | |
|
|
657 | pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset); |
|
|
658 | |
|
|
659 | if (pthread_create (&id, 0, start_routine, arg)) |
|
|
660 | cleanup ("unable to create a new thread"); |
|
|
661 | |
|
|
662 | pthread_sigmask (SIG_SETMASK, &oldsigset, 0); |
|
|
663 | } |