aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/util/file_area_freelist.h
blob: 5f557b31510bdb1735e259bd004acdabfc778dbe (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <vespa/vespalib/stllike/hash_set.h>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <map>
#include <set>

namespace vespalib::alloc {

/*
 * Class that tracks free areas in a file.
 */
class FileAreaFreeList {
    std::map<uint64_t, size_t> _free_areas; // map from offset to size
    std::map<size_t, std::set<uint64_t>> _free_sizes; // map from size to set of offsets
    vespalib::hash_set<uint64_t> _fences;
    void remove_from_size_set(uint64_t offset, size_t size);
    std::pair<uint64_t, size_t> prepare_reuse_area(size_t size);
public:
    FileAreaFreeList();
    ~FileAreaFreeList();
    uint64_t alloc(size_t size);
    void free(uint64_t offset, size_t size);
    void add_premmapped_area(uint64_t offset, size_t size);
    void remove_premmapped_area(uint64_t offset, size_t size);
    static constexpr uint64_t bad_offset = std::numeric_limits<uint64_t>::max();
};

}