~ubuntu-branches/ubuntu/oneiric/libtorrent/oneiric

« back to all changes in this revision

Viewing changes to src/data/memory_chunk.cc

  • Committer: Bazaar Package Importer
  • Author(s): Rogério Brito
  • Date: 2011-03-20 01:06:18 UTC
  • mfrom: (1.1.13 upstream) (4.1.9 sid)
  • Revision ID: james.westby@ubuntu.com-20110320010618-g3wyylccqzqko73c
Tags: 0.12.7-5
* Use Steinar's "real" patch for IPv6. Addresses #490277, #618275,
  and Closes: #617791.
* Adapt libtorrent-0.12.6-ipv6-07.patch. It FTBFS otherwise.
* Add proper attibution to the IPv6 patch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// libTorrent - BitTorrent library
 
2
// Copyright (C) 2005-2007, Jari Sundell
 
3
//
 
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.
 
8
// 
 
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.
 
13
// 
 
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
 
17
//
 
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
 
22
// including the two.
 
23
//
 
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.
 
31
//
 
32
// Contact:  Jari Sundell <jaris@ifi.uio.no>
 
33
//
 
34
//           Skomakerveien 33
 
35
//           3185 Skoppum, NORWAY
 
36
 
 
37
#include "config.h"
 
38
 
 
39
#include <unistd.h>
 
40
#include <sys/types.h>
 
41
#include <sys/mman.h>
 
42
#include <rak/error_number.h>
 
43
 
 
44
#include "torrent/exceptions.h"
 
45
#include "memory_chunk.h"
 
46
 
 
47
namespace torrent {
 
48
 
 
49
uint32_t MemoryChunk::m_pagesize = getpagesize();
 
50
 
 
51
inline void
 
52
MemoryChunk::align_pair(uint32_t* offset, uint32_t* length) const {
 
53
  *offset += page_align();
 
54
  
 
55
  *length += *offset % m_pagesize;
 
56
  *offset -= *offset % m_pagesize;
 
57
}
 
58
 
 
59
MemoryChunk::MemoryChunk(char* ptr, char* begin, char* end, int prot, int flags) :
 
60
  m_ptr(ptr),
 
61
  m_begin(begin),
 
62
  m_end(end),
 
63
  m_prot(prot),
 
64
  m_flags(flags) {
 
65
 
 
66
  if (ptr == NULL)
 
67
    throw internal_error("MemoryChunk::MemoryChunk(...) received ptr == NULL");
 
68
 
 
69
  if (page_align() >= m_pagesize)
 
70
    throw internal_error("MemoryChunk::MemoryChunk(...) received an page alignment >= page size");
 
71
 
 
72
  if ((ptrdiff_t)ptr % m_pagesize)
 
73
    throw internal_error("MemoryChunk::MemoryChunk(...) is not aligned to a page");
 
74
}
 
75
 
 
76
void
 
77
MemoryChunk::unmap() {
 
78
  if (!is_valid())
 
79
    throw internal_error("MemoryChunk::unmap() called on an invalid object");
 
80
 
 
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()));
 
83
}  
 
84
 
 
85
void
 
86
MemoryChunk::incore(char* buf, uint32_t offset, uint32_t length) {
 
87
  if (!is_valid())
 
88
    throw internal_error("Called MemoryChunk::incore(...) on an invalid object");
 
89
 
 
90
  if (!is_valid_range(offset, length))
 
91
    throw internal_error("MemoryChunk::incore(...) received out-of-range input");
 
92
 
 
93
  align_pair(&offset, &length);
 
94
 
 
95
#if USE_MINCORE
 
96
 
 
97
#if USE_MINCORE_UNSIGNED
 
98
  if (mincore(m_ptr + offset, length, (unsigned char*)buf))
 
99
#else
 
100
  if (mincore(m_ptr + offset, length, (char*)buf))
 
101
#endif
 
102
    throw storage_error("System call mincore failed: " + std::string(rak::error_number::current().c_str()));
 
103
 
 
104
#else // !USE_MINCORE
 
105
  // Pretend all pages are in memory.
 
106
  memset(buf, 1, pages_touched(offset, length));
 
107
 
 
108
#endif
 
109
}
 
110
 
 
111
bool
 
112
MemoryChunk::advise(uint32_t offset, uint32_t length, int advice) {
 
113
  if (!is_valid())
 
114
    throw internal_error("Called MemoryChunk::advise() on an invalid object");
 
115
 
 
116
  if (!is_valid_range(offset, length))
 
117
    throw internal_error("MemoryChunk::advise(...) received out-of-range input");
 
118
 
 
119
#if USE_MADVISE
 
120
  align_pair(&offset, &length);
 
121
 
 
122
  if (madvise(m_ptr + offset, length, advice) == 0)
 
123
    return true;
 
124
 
 
125
  else if (errno == EINVAL || (errno == ENOMEM && advice != advice_willneed) || errno == EBADF)
 
126
    throw internal_error("MemoryChunk::advise(...) " + std::string(strerror(errno)));
 
127
  
 
128
  else
 
129
    return false;
 
130
 
 
131
#else
 
132
  return true;
 
133
 
 
134
#endif
 
135
}
 
136
 
 
137
bool
 
138
MemoryChunk::sync(uint32_t offset, uint32_t length, int flags) {
 
139
  if (!is_valid())
 
140
    throw internal_error("Called MemoryChunk::sync() on an invalid object");
 
141
 
 
142
  if (!is_valid_range(offset, length))
 
143
    throw internal_error("MemoryChunk::sync(...) received out-of-range input");
 
144
 
 
145
  align_pair(&offset, &length);
 
146
  
 
147
  return msync(m_ptr + offset, length, flags) == 0;
 
148
}    
 
149
 
 
150
}