aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/memory/tls_scratch-inl.h
blob: 0b4ef29bea74a337a4fb770a6a08c916fb968595 (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
#ifndef TLS_SCRATH_INL_H_
#error "Direct inclusion of this file is not allowed, include tls_scratch.h"
// For the sake of sane code completion.
#include "tls_scratch.h"
#endif

#include <library/cpp/yt/misc/tls.h>

#include <util/generic/bitops.h>

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

template <class T>
YT_PREVENT_TLS_CACHING TMutableRange<T> GetTlsScratchBuffer(size_t size)
{
    thread_local std::unique_ptr<T[]> scratchBuffer;
    thread_local size_t scratchBufferSize;
    if (scratchBufferSize < size) {
        scratchBufferSize = FastClp2(size);
        scratchBuffer = std::unique_ptr<T[]>(new T[scratchBufferSize]);
    }
    std::fill(scratchBuffer.get(), scratchBuffer.get() + size, T());
    return TMutableRange(scratchBuffer.get(), size);
}

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT