… | |
… | |
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 |
242 | inline int |
242 | inline int |
243 | idistance (int dx, int dy) |
243 | idistance (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 | |
482 | protected: |
482 | protected: |
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 | |
511 | INTERFACE_CLASS (attachable) |
518 | INTERFACE_CLASS (attachable) |
512 | struct refcnt_base |
519 | struct 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 | |
602 | static inline uint32_t |
609 | static inline uint32_t |
603 | memhsh (const char *s, size_t len) |
610 | memhsh (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 | } |