ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/util.h
(Generate patch)

Comparing deliantra/server/include/util.h (file contents):
Revision 1.120 by root, Mon Oct 29 23:55:54 2012 UTC vs.
Revision 1.122 by root, Mon Nov 12 02:39:51 2012 UTC

237#if 0 237#if 0
238// and has a max. error of 6 in the range -100..+100. 238// and has a max. error of 6 in the range -100..+100.
239#else 239#else
240// and has a max. error of 9 in the range -100..+100. 240// and has a max. error of 9 in the range -100..+100.
241#endif 241#endif
242inline int 242inline int
243idistance (int dx, int dy) 243idistance (int dx, int dy)
244{ 244{
245 unsigned int dx_ = abs (dx); 245 unsigned int dx_ = abs (dx);
246 unsigned int dy_ = abs (dy); 246 unsigned int dy_ = abs (dy);
247 247
248#if 0 248#if 0
249 return dx_ > dy_ 249 return dx_ > dy_
407 typedef const Tp *const_pointer; 407 typedef const Tp *const_pointer;
408 typedef Tp &reference; 408 typedef Tp &reference;
409 typedef const Tp &const_reference; 409 typedef const Tp &const_reference;
410 typedef Tp value_type; 410 typedef Tp value_type;
411 411
412 template <class U> 412 template <class U>
413 struct rebind 413 struct rebind
414 { 414 {
415 typedef slice_allocator<U> other; 415 typedef slice_allocator<U> other;
416 }; 416 };
417 417
460 refcnt_buf (void *data, size_t size); 460 refcnt_buf (void *data, size_t size);
461 461
462 refcnt_buf (const refcnt_buf &src) 462 refcnt_buf (const refcnt_buf &src)
463 { 463 {
464 data = src.data; 464 data = src.data;
465 ++_refcnt (); 465 inc ();
466 } 466 }
467 467
468 ~refcnt_buf (); 468 ~refcnt_buf ();
469 469
470 refcnt_buf &operator =(const refcnt_buf &src); 470 refcnt_buf &operator =(const refcnt_buf &src);
479 return _size (); 479 return _size ();
480 } 480 }
481 481
482protected: 482protected:
483 enum { 483 enum {
484 overhead = sizeof (unsigned int) * 2 484 overhead = sizeof (uint32_t) * 2
485 }; 485 };
486 486
487 unsigned int &_size () const 487 uint32_t &_size () const
488 { 488 {
489 return ((unsigned int *)data)[-2]; 489 return ((unsigned int *)data)[-2];
490 } 490 }
491 491
492 unsigned int &_refcnt () const 492 uint32_t &_refcnt () const
493 { 493 {
494 return ((unsigned int *)data)[-1]; 494 return ((unsigned int *)data)[-1];
495 } 495 }
496 496
497 void _alloc (unsigned int size) 497 void _alloc (uint32_t size)
498 { 498 {
499 data = ((char *)salloc<char> (size + overhead)) + overhead; 499 data = ((char *)salloc<char> (size + overhead)) + overhead;
500 _size () = size; 500 _size () = size;
501 _refcnt () = 1; 501 _refcnt () = 1;
502 } 502 }
503 503
504 void _dealloc ();
505
506 void inc ()
507 {
508 ++_refcnt ();
509 }
510
504 void dec () 511 void dec ()
505 { 512 {
506 if (!--_refcnt ()) 513 if (!--_refcnt ())
507 sfree<char> (data - overhead, size () + overhead); 514 _dealloc ();
508 } 515 }
509}; 516};
510 517
511INTERFACE_CLASS (attachable) 518INTERFACE_CLASS (attachable)
512struct refcnt_base 519struct refcnt_base
590 // with good distribution. 597 // with good distribution.
591 // FNV-1a is faster on many cpus because the multiplication 598 // FNV-1a is faster on many cpus because the multiplication
592 // runs concurrently with the looping logic. 599 // runs concurrently with the looping logic.
593 // we modify the hash a bit to improve its distribution 600 // we modify the hash a bit to improve its distribution
594 uint32_t hash = STRHSH_NULL; 601 uint32_t hash = STRHSH_NULL;
595 602
596 while (*s) 603 while (*s)
597 hash = (hash ^ *s++) * 16777619U; 604 hash = (hash ^ *s++) * 16777619U;
598 605
599 return hash ^ (hash >> 16); 606 return hash ^ (hash >> 16);
600} 607}
601 608
602static inline uint32_t 609static inline uint32_t
603memhsh (const char *s, size_t len) 610memhsh (const char *s, size_t len)
604{ 611{
605 uint32_t hash = STRHSH_NULL; 612 uint32_t hash = STRHSH_NULL;
606 613
607 while (len--) 614 while (len--)
608 hash = (hash ^ *s++) * 16777619U; 615 hash = (hash ^ *s++) * 16777619U;
609 616
610 return hash; 617 return hash;
611} 618}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines