Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
memory_limiter.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 Roc Streaming authors
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 */
8
9//! @file roc_core/memory_limiter.h
10//! @brief Memory limiter.
11
12#ifndef ROC_CORE_MEMORY_LIMITER_H_
13#define ROC_CORE_MEMORY_LIMITER_H_
14
15#include "roc_core/atomic.h"
16#include "roc_core/attributes.h"
18#include "roc_core/stddefs.h"
19
20namespace roc {
21namespace core {
22
23//! Memory limiter.
24//! This class can be used to keep track of memory being consumed. This is done through
25//! the acquire and release methods. The class is used within classes such as LimitedPool,
26//! LimitedArena.
27class MemoryLimiter : public NonCopyable<> {
28public:
29 //! Initialize memory limiter.
30 //! @p max_bytes is the maximum total amount of memory that can be acquired. If 0,
31 //! then there is no limit, in which case, only tracking will be performed.
32 explicit MemoryLimiter(const char* name, size_t max_bytes);
33
34 //! Destroy memory limiter.
35 //! This will panic if memory is still tracked as acquired.
37
38 //! Track acquired memory.
39 //! @returns
40 //! true if successful i.e. maximum limit not breached.
41 ROC_ATTR_NODISCARD bool acquire(size_t num_bytes);
42
43 //! Track released memory.
44 //! This will panic if we are releasing more than what is currently acquired.
45 void release(size_t num_bytes);
46
47 //! Get number of bytes currently acquired.
48 size_t num_acquired();
49
50private:
51 const char* name_;
52 const size_t max_bytes_;
53 Atomic<size_t> bytes_acquired_;
54};
55
56} // namespace core
57} // namespace roc
58
59#endif // ROC_CORE_MEMORY_LIMITER_H_
Atomic.
Compiler attributes.
#define ROC_ATTR_NODISCARD
Emit warning if function result is not checked.
Definition attributes.h:31
Memory limiter. This class can be used to keep track of memory being consumed. This is done through t...
size_t num_acquired()
Get number of bytes currently acquired.
bool acquire(size_t num_bytes)
Track acquired memory.
MemoryLimiter(const char *name, size_t max_bytes)
Initialize memory limiter. max_bytes is the maximum total amount of memory that can be acquired....
~MemoryLimiter()
Destroy memory limiter. This will panic if memory is still tracked as acquired.
void release(size_t num_bytes)
Track released memory. This will panic if we are releasing more than what is currently acquired.
Base class for non-copyable objects.
Definition noncopyable.h:23
Shared ownership intrusive pointer.
Definition shared_ptr.h:32
Root namespace.
Non-copyable object.
Commonly used types and functions.