2009-01-15 18:09:36 +01:00
|
|
|
/*
|
|
|
|
|
2016-01-18 00:57:46 +01:00
|
|
|
Copyright (c) 2009-2016, 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
|
2016-06-18 20:01:38 +02:00
|
|
|
#include <cstdint>
|
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
|
2016-07-10 05:17:55 +02:00
|
|
|
#include <cstdlib> // 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
|
|
|
|
{
|
2016-06-18 20:01:38 +02:00
|
|
|
std::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
|
|
|
|
|
2017-04-12 19:00:57 +02: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();
|
2017-01-13 14:44:08 +01:00
|
|
|
const int num_pages = (bytes + (page - 1)) / page + 2;
|
2015-04-27 02:56:58 +02:00
|
|
|
const int orig_bytes = bytes;
|
2014-01-20 04:55:24 +01:00
|
|
|
bytes = num_pages * page;
|
|
|
|
#endif
|
|
|
|
|
2015-09-02 07:30:40 +02:00
|
|
|
void* ret;
|
2014-01-20 04:55:24 +01:00
|
|
|
#if TORRENT_USE_POSIX_MEMALIGN
|
2017-03-09 13:46:52 +01:00
|
|
|
if (posix_memalign(&ret, std::size_t(page_size()), std::size_t(bytes))
|
2016-06-20 17:32:06 +02:00
|
|
|
!= 0) ret = nullptr;
|
2014-01-20 04:55:24 +01:00
|
|
|
#elif TORRENT_USE_MEMALIGN
|
2017-03-09 13:46:52 +01:00
|
|
|
ret = memalign(std::size_t(page_size()), std::size_t(bytes));
|
2014-01-20 04:55:24 +01:00
|
|
|
#elif defined TORRENT_WINDOWS
|
2017-03-09 13:46:52 +01:00
|
|
|
ret = _aligned_malloc(std::size_t(bytes), std::size_t(page_size()));
|
2014-01-20 04:55:24 +01:00
|
|
|
#elif defined TORRENT_BEOS
|
|
|
|
area_id id = create_area("", &ret, B_ANY_ADDRESS
|
2017-01-13 14:44:08 +01:00
|
|
|
, (bytes + page_size() - 1) & (page_size() - 1), B_NO_LOCK, B_READ_AREA | B_WRITE_AREA);
|
2016-06-20 17:32:06 +02:00
|
|
|
if (id < B_OK) return nullptr;
|
2014-01-20 04:55:24 +01:00
|
|
|
#else
|
2017-01-13 14:44:08 +01:00
|
|
|
ret = valloc(std::size_t(bytes));
|
2014-01-20 04:55:24 +01:00
|
|
|
#endif
|
2016-06-20 17:32:06 +02:00
|
|
|
if (ret == nullptr) return nullptr;
|
2014-01-20 04:55:24 +01:00
|
|
|
|
|
|
|
#ifdef TORRENT_DEBUG_BUFFERS
|
2010-07-18 21:28:22 +02:00
|
|
|
// make the two surrounding pages non-readable and -writable
|
2015-09-02 07:30:40 +02:00
|
|
|
alloc_header* h = static_cast<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
|
2016-06-20 17:32:06 +02:00
|
|
|
#define mprotect(buf, size, prot) VirtualProtect(buf, size, prot, nullptr)
|
2013-11-29 10:41:53 +01:00
|
|
|
#define PROT_READ PAGE_READONLY
|
2011-04-15 10:37:27 +02:00
|
|
|
#endif
|
2017-01-09 07:43:57 +01:00
|
|
|
mprotect(ret, std::size_t(page), PROT_READ);
|
2017-01-13 14:44:08 +01:00
|
|
|
mprotect(static_cast<char*>(ret) + (num_pages - 1) * page, std::size_t(page), PROT_READ);
|
2013-11-29 10:41:53 +01:00
|
|
|
|
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
#undef mprotect
|
|
|
|
#undef PROT_READ
|
|
|
|
#endif
|
2016-05-17 15:24:06 +02:00
|
|
|
// std::fprintf(stderr, "malloc: %p head: %p tail: %p size: %d\n", ret + page, ret, ret + page + bytes, int(bytes));
|
2010-07-18 21:28:22 +02:00
|
|
|
|
2015-09-02 07:30:40 +02:00
|
|
|
return static_cast<char*>(ret) + page;
|
|
|
|
#else
|
|
|
|
return static_cast<char*>(ret);
|
2014-01-06 06:45:36 +01:00
|
|
|
#endif // TORRENT_DEBUG_BUFFERS
|
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
|
|
|
{
|
2016-07-09 22:26:26 +02:00
|
|
|
if (block == nullptr) return;
|
2010-07-18 21:28:22 +02:00
|
|
|
|
|
|
|
#ifdef TORRENT_DEBUG_BUFFERS
|
2013-11-29 10:41:53 +01:00
|
|
|
|
|
|
|
#ifdef TORRENT_WINDOWS
|
2016-06-20 17:32:06 +02:00
|
|
|
#define mprotect(buf, size, prot) VirtualProtect(buf, size, prot, nullptr)
|
2013-11-29 10:41:53 +01:00
|
|
|
#define PROT_READ PAGE_READONLY
|
|
|
|
#define PROT_WRITE PAGE_READWRITE
|
|
|
|
#endif
|
2016-12-11 02:51:55 +01:00
|
|
|
int const page = page_size();
|
2010-07-18 21:28:22 +02:00
|
|
|
// make the two surrounding pages non-readable and -writable
|
2017-01-09 07:43:57 +01:00
|
|
|
mprotect(block - page, std::size_t(page), PROT_READ | PROT_WRITE);
|
2015-09-02 07:30:40 +02:00
|
|
|
alloc_header* h = reinterpret_cast<alloc_header*>(block - page);
|
2016-12-11 02:51:55 +01:00
|
|
|
int const num_pages = int((h->size + (page - 1)) / page + 2);
|
2010-07-18 21:28:22 +02:00
|
|
|
TORRENT_ASSERT(h->magic == 0x1337);
|
2017-01-09 07:43:57 +01:00
|
|
|
mprotect(block + (num_pages - 2) * page, std::size_t(page), PROT_READ | PROT_WRITE);
|
2016-05-17 15:24:06 +02:00
|
|
|
// std::fprintf(stderr, "free: %p head: %p tail: %p size: %d\n", block, block - page, block + h->size, int(h->size));
|
2010-07-18 21:28:22 +02:00
|
|
|
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();
|
2015-09-02 07:30:40 +02:00
|
|
|
alloc_header const* h = reinterpret_cast<alloc_header const*>(block - page);
|
2014-07-06 21:18:00 +02:00
|
|
|
return h->magic == 0x1337;
|
|
|
|
}
|
|
|
|
#endif
|
2009-01-15 18:09:36 +01:00
|
|
|
|
|
|
|
}
|