1
// libTorrent - BitTorrent library
2
// Copyright (C) 2005-2007, Jari Sundell
4
// This program is free software; you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation; either version 2 of the License, or
7
// (at your option) any later version.
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
// GNU General Public License for more details.
14
// You should have received a copy of the GNU General Public License
15
// along with this program; if not, write to the Free Software
16
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
// In addition, as a special exception, the copyright holders give
19
// permission to link the code of portions of this program with the
20
// OpenSSL library under certain conditions as described in each
21
// individual source file, and distribute linked combinations
24
// You must obey the GNU General Public License in all respects for
25
// all of the code used other than OpenSSL. If you modify file(s)
26
// with this exception, you may extend this exception to your version
27
// of the file(s), but you are not obligated to do so. If you do not
28
// wish to do so, delete this exception statement from your version.
29
// If you delete this exception statement from all source files in the
30
// program, then also delete it here.
32
// Contact: Jari Sundell <jaris@ifi.uio.no>
35
// 3185 Skoppum, NORWAY
40
#include <sys/types.h>
42
#include <rak/error_number.h>
44
#include "torrent/exceptions.h"
45
#include "memory_chunk.h"
49
uint32_t MemoryChunk::m_pagesize = getpagesize();
52
MemoryChunk::align_pair(uint32_t* offset, uint32_t* length) const {
53
*offset += page_align();
55
*length += *offset % m_pagesize;
56
*offset -= *offset % m_pagesize;
59
MemoryChunk::MemoryChunk(char* ptr, char* begin, char* end, int prot, int flags) :
67
throw internal_error("MemoryChunk::MemoryChunk(...) received ptr == NULL");
69
if (page_align() >= m_pagesize)
70
throw internal_error("MemoryChunk::MemoryChunk(...) received an page alignment >= page size");
72
if ((ptrdiff_t)ptr % m_pagesize)
73
throw internal_error("MemoryChunk::MemoryChunk(...) is not aligned to a page");
77
MemoryChunk::unmap() {
79
throw internal_error("MemoryChunk::unmap() called on an invalid object");
81
if (munmap(m_ptr, m_end - m_ptr) != 0)
82
throw internal_error("MemoryChunk::unmap() system call failed: " + std::string(rak::error_number::current().c_str()));
86
MemoryChunk::incore(char* buf, uint32_t offset, uint32_t length) {
88
throw internal_error("Called MemoryChunk::incore(...) on an invalid object");
90
if (!is_valid_range(offset, length))
91
throw internal_error("MemoryChunk::incore(...) received out-of-range input");
93
align_pair(&offset, &length);
97
#if USE_MINCORE_UNSIGNED
98
if (mincore(m_ptr + offset, length, (unsigned char*)buf))
100
if (mincore(m_ptr + offset, length, (char*)buf))
102
throw storage_error("System call mincore failed: " + std::string(rak::error_number::current().c_str()));
104
#else // !USE_MINCORE
105
// Pretend all pages are in memory.
106
memset(buf, 1, pages_touched(offset, length));
112
MemoryChunk::advise(uint32_t offset, uint32_t length, int advice) {
114
throw internal_error("Called MemoryChunk::advise() on an invalid object");
116
if (!is_valid_range(offset, length))
117
throw internal_error("MemoryChunk::advise(...) received out-of-range input");
120
align_pair(&offset, &length);
122
if (madvise(m_ptr + offset, length, advice) == 0)
125
else if (errno == EINVAL || (errno == ENOMEM && advice != advice_willneed) || errno == EBADF)
126
throw internal_error("MemoryChunk::advise(...) " + std::string(strerror(errno)));
138
MemoryChunk::sync(uint32_t offset, uint32_t length, int flags) {
140
throw internal_error("Called MemoryChunk::sync() on an invalid object");
142
if (!is_valid_range(offset, length))
143
throw internal_error("MemoryChunk::sync(...) received out-of-range input");
145
align_pair(&offset, &length);
147
return msync(m_ptr + offset, length, flags) == 0;