blob: b95f8ac72e873418912c82ec7b501cc4e5aea56b (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once
#include <cctype>
#include <cstdlib>
#include <unistd.h>
#include <cassert>
#include <cstring>
#include <algorithm>
namespace vespamalloc {
class Memory
{
public:
Memory(size_t blockSize) : _blockSize(std::max(blockSize, size_t(getpagesize()))), _start(NULL), _end(NULL) { }
virtual ~Memory() { }
void * getStart() const { return _start; }
void * getEnd() const { return _end; }
size_t getMinBlockSize() const { return _blockSize; }
static size_t getMinPreferredStartAddress() { return 0x10000000000; } // 1T
static size_t getBlockAlignment() { return 0x200000; } //2M
protected:
void setStart(void * v) { _start = v; }
void setEnd(void * v) { _end = v; }
size_t _blockSize;
void * _start;
void * _end;
};
class MmapMemory : public Memory
{
public:
MmapMemory(size_t blockSize);
virtual ~MmapMemory();
void *reserve(size_t & len);
void *get(size_t len);
bool release(void * mem, size_t len);
bool reclaim(void * mem, size_t len);
bool freeTail(void * mem, size_t len);
private:
void * getHugePages(size_t len);
void * getNormalPages(size_t len);
void * getBasePages(size_t len, int mmapOpt, int fd, size_t offset);
void setupFAdvise();
void setupHugePages();
size_t _useMAdvLimit;
int _hugePagesFd;
size_t _hugePagesOffset;
size_t _hugePageSize;
char _hugePagesFileName[256];
};
}
|