aboutsummaryrefslogtreecommitdiffstats
path: root/vespamalloc/src/vespamalloc/malloc/mmappool.h
blob: 0a3dd5d0a0abfb325e25ff52d5763042f929d146 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <atomic>
#include <cstdio>
#include <mutex>
#include <unordered_map>

namespace vespamalloc {

class MMapPool {
public:
    MMapPool();
    MMapPool(const MMapPool &) = delete;
    MMapPool & operator =(const MMapPool &) = delete;
    ~MMapPool();
    void * mmap(size_t sz);
    void unmap(void *);
    size_t get_size(void *) const;
    size_t getNumMappings() const;
    size_t getMmappedBytes() const;
    size_t getMmappedBytesPeak() const;
    void info(FILE * os, size_t level) const;
private:
    struct MMapInfo {
        MMapInfo(size_t id, size_t sz) noexcept : _id(id), _sz(sz) { }
        size_t _id;
        size_t _sz;
    };
    const size_t        _page_size;
    const int           _huge_flags;
    size_t              _peakBytes;
    size_t              _currentBytes;
    std::atomic<size_t> _count;
    std::atomic<bool>   _has_hugepage_failure_just_happened;
    mutable std::mutex  _mutex;
    std::unordered_map<const void *, MMapInfo> _mappings;
};

}