ewmscp
..
src
block.cpp
Go to the documentation of this file.
1
#include "
block.h
"
2
#include <sys/mman.h>
3
#include <
throwcall.h
>
4
8
block::block
(
size_t
aBlockSize) :
9
bytes(0),
10
blockSize(aBlockSize),
11
startOffset(0),
12
hole(false) {
13
buffer
=
throwcall::badval
(mmap(
nullptr
,
blockSize
,
14
PROT_READ | PROT_WRITE,
15
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0),
16
MAP_FAILED,
"can't allocate buffer with size "
,
blockSize
);
17
}
18
block::~block
() {
19
throwcall::good0
(munmap(
buffer
,
blockSize
),
"can't unmap buffer"
);
20
}
21
22
const
void
*
block::bufferAt
(
size_t
aOffset)
const
{
23
if
(__builtin_expect(
hole
,
false
)) {
24
return
nullBuffer
();
25
}
26
return
static_cast<
const
void
*
>
(
static_cast<
char
*
>
(
buffer
) + aOffset);
27
}
28
void
*
block::bufferAt
(
size_t
aOffset) {
29
if
(__builtin_expect(
hole
,
false
)) {
30
throw
std::runtime_error(
"non const bufferAt called for hole block"
);
31
}
32
return
static_cast<
void
*
>
(
static_cast<
char
*
>
(
buffer
) + aOffset);
33
}
34
size_t
block::nullBufferSize
() {
35
return
1024 * 1024 * 16;
36
}
37
const
void
*
block::nullBuffer
() {
38
static
void
* nullBuf =
nullptr
;
39
if
(nullBuf ==
nullptr
) {
40
nullBuf =
throwcall::badval
(mmap(
nullptr
,
nullBufferSize
(),
41
PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0),
42
MAP_FAILED,
"can't allocate buffer with size "
,
nullBufferSize
());
43
// according no mmap(2) the buffer is zero-filled
44
}
45
return
nullBuf;
46
}
block.h
throwcall::badval
T badval(T call, t badvalue, const Args &... args)
template function to wrap system calls that return a special bad value on failure
Definition:
throwcall.h:54
block::nullBuffer
static const void * nullBuffer()
Definition:
block.cpp:37
block::blockSize
size_t blockSize
capacity of the block
Definition:
block.h:10
block::bufferAt
void * bufferAt(size_t offset)
only way to access the data in the block
Definition:
block.cpp:28
throwcall.h
block::buffer
void * buffer
Definition:
block.h:8
block::nullBufferSize
static size_t nullBufferSize()
Definition:
block.cpp:34
throwcall::good0
void good0(T call, const Args &... args)
template function to wrap system calls that return 0 on success
Definition:
throwcall.h:40
block::hole
bool hole
represents a hole in the file with bytes bytes.
Definition:
block.h:12
block::~block
~block()
Definition:
block.cpp:18
block::block
block(size_t aBlockSize)
Allocate block via mmap to ensure proper alignment along page borders, needed for efficient read/writ...
Definition:
block.cpp:8
Generated by
1.8.17