[package] update rtorrent to r1189
[openwrt/svn-archive/archive.git] / net / rtorrent / patches / 110-fix-no-posix_memalign.diff
1 diff --git a/rak/allocators.h b/rak/allocators.h
2 index 0a1b711..2d7b98e 100644
3 --- a/rak/allocators.h
4 +++ b/rak/allocators.h
5 @@ -42,6 +42,7 @@
6 #include <cstddef>
7 #include <limits>
8 #include <stdlib.h>
9 +#include <stdint.h>
10 #include <sys/types.h>
11
12 namespace rak {
13 @@ -74,17 +75,13 @@ public:
14 size_type max_size () const throw() { return std::numeric_limits<size_t>::max() / sizeof(T); }
15
16 pointer allocate(size_type num, const_void_pointer hint = 0) { return alloc_size(num*sizeof(T)); }
17 + void deallocate (pointer p, size_type num) { dealloc_size(p, num*sizeof(T)); }
18
19 - static pointer alloc_size(size_type size) {
20 - pointer ptr = NULL;
21 - int __UNUSED result = posix_memalign((void**)&ptr, LT_SMP_CACHE_BYTES, size);
22 -
23 - return ptr;
24 - }
25 + static pointer alloc_size(size_type size);
26 + static void dealloc_size(pointer p, size_type size);
27
28 void construct (pointer p, const T& value) { new((void*)p)T(value); }
29 void destroy (pointer p) { p->~T(); }
30 - void deallocate (pointer p, size_type num) { free((void*)p); }
31 };
32
33
34 @@ -98,6 +95,36 @@ bool operator!= (const cacheline_allocator<T1>&, const cacheline_allocator<T2>&)
35 return false;
36 }
37
38 +template <class T>
39 +inline typename cacheline_allocator<T>::pointer cacheline_allocator<T>::alloc_size(size_type size) {
40 + pointer ptr;
41 +
42 +#if HAVE_POSIX_MEMALIGN
43 + if (posix_memalign((void**)&ptr, LT_SMP_CACHE_BYTES, size))
44 + return NULL;
45 +#else
46 + char* org = (char*)malloc(size + sizeof(void*) + LT_SMP_CACHE_BYTES - 1);
47 + if (org == NULL)
48 + return NULL;
49 +
50 + ptr = (pointer)((uintptr_t)(org + LT_SMP_CACHE_BYTES - 1) & ~(LT_SMP_CACHE_BYTES - 1));
51 +
52 + // store originally allocated pointer for later free() at the end of the allocated data
53 + *(void**)((char*)ptr + size) = org;
54 +#endif
55 +
56 + return ptr;
57 +}
58 +
59 +template <class T>
60 +inline void cacheline_allocator<T>::dealloc_size(pointer p, size_type size) {
61 +#if HAVE_POSIX_MEMALIGN
62 + free(p);
63 +#else
64 + free(*(void**)((char*)p + size));
65 +#endif
66 +}
67 +
68 }
69
70 //