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