2009-01-15 18:09:36 +01:00
|
|
|
/*
|
|
|
|
|
2015-06-03 07:18:48 +02:00
|
|
|
Copyright (c) 2009-2015, Arvid Norberg
|
2009-01-15 18:09:36 +01:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libtorrent/allocator.hpp"
|
|
|
|
#include "libtorrent/config.hpp"
|
2014-07-06 21:18:00 +02:00
|
|
|
#include "libtorrent/assert.hpp" // for print_backtrace
|
2014-12-03 05:32:50 +01:00
|
|
|
#include <boost/cstdint.hpp>
|
2009-01-15 18:09:36 +01:00
|
|
|
|
2013-09-22 21:19:45 +02:00
|
|
|
#if defined TORRENT_BEOS
|
2009-12-09 06:19:48 +01:00
|
|
|
#include <kernel/OS.h>
|
2009-12-09 10:55:19 +01:00
|
|
|
#include <stdlib.h> // malloc/free
|
2013-09-22 21:19:45 +02:00
|
|
|
#elif !defined TORRENT_WINDOWS
|
2014-07-06 21:18:00 +02:00
|
|
|
#include <stdlib.h> // posix_memalign/free
|
2009-12-09 02:55:30 +01:00
|
|
|
#include <unistd.h> // _SC_PAGESIZE
|
2009-01-15 18:09:36 +01:00
|
|
|
#endif
|
|
|
|
|
2013-09-05 09:14:42 +02:00
|
|
|
#if TORRENT_USE_MEMALIGN || TORRENT_USE_POSIX_MEMALIGN || defined TORRENT_WINDOWS
|
|
|
|
#include <malloc.h> // memalign and _aligned_malloc
|
2013-09-22 21:19:45 +02:00
|
|
|
#include <stdlib.h> // _aligned_malloc on mingw
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
// windows.h must be included after stdlib.h under mingw
|
2015-08-09 21:01:01 +02:00
|
|
|
#include <windows.h>
|
2013-09-22 21:19:45 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TORRENT_MINGW
|
|
|
|
#define _aligned_malloc __mingw_aligned_malloc
|
|
|
|
#define _aligned_free __mingw_aligned_free
|
2010-03-03 05:26:12 +01:00
|
|
|
#endif
|
2009-01-15 18:09:36 +01:00
|
|
|
|
2010-07-18 21:28:22 +02:00
|
|
|
#ifdef TORRENT_DEBUG_BUFFERS
|
2013-11-29 10:41:53 +01:00
|
|
|
#ifndef TORRENT_WINDOWS
|
2010-07-18 21:28:22 +02:00
|
|
|
#include <sys/mman.h>
|
2013-11-29 10:41:53 +01:00
|
|
|
#endif
|
2010-07-18 21:28:22 +02:00
|
|
|
|
|
|
|
struct alloc_header
|
|
|
|
{
|
2014-12-03 05:32:50 +01:00
|
|
|
boost::int64_t size;
|
2010-07-18 21:28:22 +02:00
|
|
|
int magic;
|
2011-04-15 10:37:27 +02:00
|
|
|
char stack[3072];
|
2010-07-18 21:28:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2009-01-15 18:09:36 +01:00
|
|
|
namespace libtorrent
|
|
|
|
{
|
2011-04-15 10:37:27 +02:00
|
|
|
|
2009-12-09 02:55:30 +01:00
|
|
|
int page_size()
|
|
|
|
{
|
|
|
|
static int s = 0;
|
|
|
|
if (s != 0) return s;
|
|
|
|
|
2015-08-09 21:01:01 +02:00
|
|
|
#ifdef TORRENT_BUILD_SIMULATOR
|
|
|
|
s = 4096;
|
|
|
|
#elif defined TORRENT_WINDOWS
|
2009-12-09 02:55:30 +01:00
|
|
|
SYSTEM_INFO si;
|
|
|
|
GetSystemInfo(&si);
|
|
|
|
s = si.dwPageSize;
|
2009-12-09 06:19:48 +01:00
|
|
|
#elif defined TORRENT_BEOS
|
|
|
|
s = B_PAGE_SIZE;
|
2009-12-09 02:55:30 +01:00
|
|
|
#else
|
2015-04-18 04:33:39 +02:00
|
|
|
s = int(sysconf(_SC_PAGESIZE));
|
2009-12-09 02:55:30 +01:00
|
|
|
#endif
|
|
|
|
// assume the page size is 4 kiB if we
|
|
|
|
// fail to query it
|
|
|
|
if (s <= 0) s = 4096;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2014-12-03 05:32:50 +01:00
|
|
|
char* page_aligned_allocator::malloc(page_aligned_allocator::size_type bytes)
|
2009-01-15 18:09:36 +01:00
|
|
|
{
|
2014-01-20 04:55:24 +01:00
|
|
|
TORRENT_ASSERT(bytes > 0);
|
|
|
|
// just sanity check (this needs to be pretty high
|
|
|
|
// for cases where the cache size is several gigabytes)
|
|
|
|
TORRENT_ASSERT(bytes < 0x30000000);
|
|
|
|
|
2014-07-05 01:40:31 +02:00
|
|
|
TORRENT_ASSERT(int(bytes) >= page_size());
|
2010-07-18 21:28:22 +02:00
|
|
|
#ifdef TORRENT_DEBUG_BUFFERS
|
2015-04-27 02:56:58 +02:00
|
|
|
const int page = page_size();
|
|
|
|
const int num_pages = (bytes + (page-1)) / page + 2;
|
|
|
|
const int orig_bytes = bytes;
|
2014-01-20 04:55:24 +01:00
|
|
|
bytes = num_pages * page;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
char* ret;
|
|
|
|
#if TORRENT_USE_POSIX_MEMALIGN
|
2015-04-18 04:33:39 +02:00
|
|
|
if (posix_memalign(reinterpret_cast<void**>(&ret), page_size(), bytes)
|
|
|
|
!= 0) ret = NULL;
|
2014-01-20 04:55:24 +01:00
|
|
|
#elif TORRENT_USE_MEMALIGN
|
2015-04-18 04:33:39 +02:00
|
|
|
ret = static_cast<char*>(memalign(page_size(), bytes));
|
2014-01-20 04:55:24 +01:00
|
|
|
#elif defined TORRENT_WINDOWS
|
2015-04-18 04:33:39 +02:00
|
|
|
ret = static_cast<char*>(_aligned_malloc(bytes, page_size()));
|
2014-01-20 04:55:24 +01:00
|
|
|
#elif defined TORRENT_BEOS
|
|
|
|
area_id id = create_area("", &ret, B_ANY_ADDRESS
|
|
|
|
, (bytes + page_size() - 1) & (page_size()-1), B_NO_LOCK, B_READ_AREA | B_WRITE_AREA);
|
|
|
|
if (id < B_OK) return NULL;
|
2015-04-18 04:33:39 +02:00
|
|
|
ret = static_cast<char*>(ret);
|
2014-01-20 04:55:24 +01:00
|
|
|
#else
|
2015-04-18 04:33:39 +02:00
|
|
|
ret = static_cast<char*>(valloc(size_t(bytes)));
|
2014-01-20 04:55:24 +01:00
|
|
|
#endif
|
|
|
|
if (ret == NULL) return NULL;
|
|
|
|
|
|
|
|
#ifdef TORRENT_DEBUG_BUFFERS
|
2010-07-18 21:28:22 +02:00
|
|
|
// make the two surrounding pages non-readable and -writable
|
|
|
|
alloc_header* h = (alloc_header*)ret;
|
2014-01-20 04:55:24 +01:00
|
|
|
h->size = orig_bytes;
|
2010-07-18 21:28:22 +02:00
|
|
|
h->magic = 0x1337;
|
2011-04-15 10:37:27 +02:00
|
|
|
print_backtrace(h->stack, sizeof(h->stack));
|
2013-11-29 10:41:53 +01:00
|
|
|
|
|
|
|
#ifdef TORRENT_WINDOWS
|
2014-01-20 04:55:24 +01:00
|
|
|
#define mprotect(buf, size, prot) VirtualProtect(buf, size, prot, NULL)
|
2013-11-29 10:41:53 +01:00
|
|
|
#define PROT_READ PAGE_READONLY
|
2011-04-15 10:37:27 +02:00
|
|
|
#endif
|
2010-07-18 21:28:22 +02:00
|
|
|
mprotect(ret, page, PROT_READ);
|
2011-04-15 10:37:27 +02:00
|
|
|
mprotect(ret + (num_pages-1) * page, page, PROT_READ);
|
2013-11-29 10:41:53 +01:00
|
|
|
|
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
#undef mprotect
|
|
|
|
#undef PROT_READ
|
|
|
|
#endif
|
2010-07-18 21:28:22 +02:00
|
|
|
// fprintf(stderr, "malloc: %p head: %p tail: %p size: %d\n", ret + page, ret, ret + page + bytes, int(bytes));
|
|
|
|
|
|
|
|
return ret + page;
|
2014-01-06 06:45:36 +01:00
|
|
|
#endif // TORRENT_DEBUG_BUFFERS
|
2014-01-20 04:55:24 +01:00
|
|
|
|
|
|
|
return ret;
|
2009-01-15 18:09:36 +01:00
|
|
|
}
|
|
|
|
|
2014-01-20 04:55:24 +01:00
|
|
|
void page_aligned_allocator::free(char* block)
|
2009-01-15 18:09:36 +01:00
|
|
|
{
|
2014-01-20 04:55:24 +01:00
|
|
|
if (block == 0) return;
|
2010-07-18 21:28:22 +02:00
|
|
|
|
|
|
|
#ifdef TORRENT_DEBUG_BUFFERS
|
2013-11-29 10:41:53 +01:00
|
|
|
|
|
|
|
#ifdef TORRENT_WINDOWS
|
2014-01-20 04:55:24 +01:00
|
|
|
#define mprotect(buf, size, prot) VirtualProtect(buf, size, prot, NULL)
|
2013-11-29 10:41:53 +01:00
|
|
|
#define PROT_READ PAGE_READONLY
|
|
|
|
#define PROT_WRITE PAGE_READWRITE
|
|
|
|
#endif
|
2015-04-27 02:56:58 +02:00
|
|
|
const int page = page_size();
|
2010-07-18 21:28:22 +02:00
|
|
|
// make the two surrounding pages non-readable and -writable
|
|
|
|
mprotect(block - page, page, PROT_READ | PROT_WRITE);
|
|
|
|
alloc_header* h = (alloc_header*)(block - page);
|
2015-04-27 02:56:58 +02:00
|
|
|
const int num_pages = (h->size + (page-1)) / page + 2;
|
2010-07-18 21:28:22 +02:00
|
|
|
TORRENT_ASSERT(h->magic == 0x1337);
|
2011-04-15 10:37:27 +02:00
|
|
|
mprotect(block + (num_pages-2) * page, page, PROT_READ | PROT_WRITE);
|
2010-07-18 21:28:22 +02:00
|
|
|
// fprintf(stderr, "free: %p head: %p tail: %p size: %d\n", block, block - page, block + h->size, int(h->size));
|
|
|
|
h->magic = 0;
|
2014-01-20 04:55:24 +01:00
|
|
|
block -= page;
|
2010-07-18 21:28:22 +02:00
|
|
|
|
2013-11-29 10:41:53 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
#undef mprotect
|
|
|
|
#undef PROT_READ
|
|
|
|
#undef PROT_WRITE
|
|
|
|
#endif
|
|
|
|
|
2011-04-15 10:37:27 +02:00
|
|
|
print_backtrace(h->stack, sizeof(h->stack));
|
2014-11-30 11:07:19 +01:00
|
|
|
|
2013-12-02 10:30:19 +01:00
|
|
|
#endif // TORRENT_DEBUG_BUFFERS
|
2010-07-18 21:28:22 +02:00
|
|
|
|
2009-01-15 18:09:36 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
2013-09-05 09:14:42 +02:00
|
|
|
_aligned_free(block);
|
2009-11-28 23:41:21 +01:00
|
|
|
#elif defined TORRENT_BEOS
|
2009-12-15 14:11:07 +01:00
|
|
|
area_id id = area_for(block);
|
|
|
|
if (id < B_OK) return;
|
|
|
|
delete_area(id);
|
2009-01-15 18:09:36 +01:00
|
|
|
#else
|
2009-01-15 19:25:27 +01:00
|
|
|
::free(block);
|
2013-12-02 10:30:19 +01:00
|
|
|
#endif // TORRENT_WINDOWS
|
2009-01-15 18:09:36 +01:00
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#ifdef TORRENT_DEBUG_BUFFERS
|
|
|
|
bool page_aligned_allocator::in_use(char const* block)
|
|
|
|
{
|
2015-04-27 02:56:58 +02:00
|
|
|
const int page = page_size();
|
2014-07-06 21:18:00 +02:00
|
|
|
alloc_header* h = (alloc_header*)(block - page);
|
|
|
|
return h->magic == 0x1337;
|
|
|
|
}
|
|
|
|
#endif
|
2009-01-15 18:09:36 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|