aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/util/filealign.h
blob: 73ca03926a038c99cedbf3c91370929890db5bc2 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <cstddef>
#include <cstdint>

class FastOS_FileInterface;

namespace search {

class FileAlign
{
private:
    size_t _directIOFileAlign;
    size_t _preferredFileAlign;
    size_t _minDirectIOSize;
    size_t _minAlignedSize;
    size_t _elemSize;
    size_t _directIOMemAlign;
    bool   _directio;


public:
    FileAlign();
    ~FileAlign();

    /**
     * Adjust number of bytes for IO (read or write), reducing
     * number of bytes if it helps making end of IO matching
     * an alignment boundary.
     *
     * @param offset position of start of IO, measured in bytes
     * @param size   number of bytes for IO
     *
     * @return adjusted number of bytes for IO
     */
    size_t adjustSize(int64_t offset, size_t size) const;

    /**
     * Adjust number of elements for IO (read or write), reducing
     * number of elements if it helps making end of IO matching
     * an alignment boundary.
     *
     * @param eoffset position of start of IO, measured in elements
     * @param esize   number of elements for IO
     *
     * @return adjusted number of elements for IO
     */
    size_t adjustElements(int64_t eoffset, size_t esize) const;

    /**
     * Setup alignment
     *
     * @param elements  suggested number of elements in buffer
     * @param elemSize  size of each elements
     * @param file      File interface for IO
     * @param preferredFileAlignment prefered alignment for IO
     *
     * @return adjusted number of elements in buffer
     */
    size_t setupAlign(size_t elements, size_t elemSize, FastOS_FileInterface *file, size_t preferredFileAlignment);
    bool getDirectIO() const { return _directio; }
    size_t getDirectIOFileAlign() const { return _directIOFileAlign; }
    size_t getDirectIOMemAlign() const { return _directIOMemAlign; }
    size_t getMinDirectIOSize() const { return _minDirectIOSize; }
    size_t getMinAlignedSize() const { return _minAlignedSize; }
    size_t getPreferredFileAlign() const { return _preferredFileAlign; }
    size_t getElemSize() const { return _elemSize; }
};

}