b99a0d14d639e2fe7c87d920d881cbc9b6be24c2
[openwrt/svn-archive/archive.git] / libs / libtorrent / patches / 110-fix-no-posix_memalign.patch
1 Index: libtorrent-0.12.6_r1209/rak/allocators.h
2 ===================================================================
3 --- libtorrent-0.12.6_r1209.orig/rak/allocators.h 2010-11-10 12:40:28.000000000 +0100
4 +++ libtorrent-0.12.6_r1209/rak/allocators.h 2011-04-13 16:33:28.179339669 +0200
5 @@ -74,17 +74,13 @@
6 size_type max_size () const throw() { return std::numeric_limits<size_t>::max() / sizeof(T); }
7
8 pointer allocate(size_type num, const_void_pointer hint = 0) { return alloc_size(num*sizeof(T)); }
9 + void deallocate (pointer p, size_type num) { dealloc_size(p, num*sizeof(T)); }
10
11 - static pointer alloc_size(size_type size) {
12 - pointer ptr = NULL;
13 - int __UNUSED result = posix_memalign((void**)&ptr, LT_SMP_CACHE_BYTES, size);
14 -
15 - return ptr;
16 - }
17 + static pointer alloc_size(size_type size);
18 + static void dealloc_size(pointer p, size_type size);
19
20 void construct (pointer p, const T& value) { new((void*)p)T(value); }
21 void destroy (pointer p) { p->~T(); }
22 - void deallocate (pointer p, size_type num) { free((void*)p); }
23 };
24
25
26 @@ -98,6 +94,36 @@
27 return false;
28 }
29
30 +template <class T>
31 +inline typename cacheline_allocator<T>::pointer cacheline_allocator<T>::alloc_size(size_type size) {
32 + pointer ptr;
33 +
34 +#if HAVE_POSIX_MEMALIGN
35 + if (posix_memalign((void**)&ptr, LT_SMP_CACHE_BYTES, size))
36 + return NULL;
37 +#else
38 + char* org = (char*)malloc(size + sizeof(void*) + LT_SMP_CACHE_BYTES - 1);
39 + if (org == NULL)
40 + return NULL;
41 +
42 + ptr = (pointer)((uintptr_t)(org + LT_SMP_CACHE_BYTES - 1) & ~(LT_SMP_CACHE_BYTES - 1));
43 +
44 + // store originally allocated pointer for later free() at the end of the allocated data
45 + *(void**)((char*)ptr + size) = org;
46 +#endif
47 +
48 + return ptr;
49 +}
50 +
51 +template <class T>
52 +inline void cacheline_allocator<T>::dealloc_size(pointer p, size_type size) {
53 +#if HAVE_POSIX_MEMALIGN
54 + free(p);
55 +#else
56 + free(*(void**)((char*)p + size));
57 +#endif
58 +}
59 +
60 }
61
62 //