YAMI4 Core Library 2.0.0
Messaging Solution for Distributed Systems
Loading...
Searching...
No Matches
locked_allocator.h
1// Copyright Maciej Sobczak 2008-2022.
2// This file is part of YAMI4.
3// See the package-level LICENSE.txt file.
4
5#ifndef YAMICORE_LOCKED_ALLOCATOR_H_INCLUDED
6#define YAMICORE_LOCKED_ALLOCATOR_H_INCLUDED
7
8#include "allocator.h"
9#include "dll.h"
10
11#include <mutex>
12
13namespace yami
14{
15
16namespace core
17{
18
20class DLL locked_allocator : public allocator
21{
22public:
24
25 void set_working_area(void * buf, std::size_t size)
26 {
27 alloc_.set_working_area(buf, size);
28 }
29
30 virtual void * allocate(std::size_t requested_size)
31 {
32 std::lock_guard<std::mutex> lock(mtx_);
33
34 void * result = alloc_.allocate(requested_size);
35
36 return result;
37 }
38
39 virtual void deallocate(const void * p)
40 {
41 std::lock_guard<std::mutex> lock(mtx_);
42
43 alloc_.deallocate(p);
44 }
45
46 void get_free_size(std::size_t & biggest, std::size_t & all) const
47 {
48 std::lock_guard<std::mutex> lock(mtx_);
49
50 alloc_.get_free_size(biggest, all);
51 }
52
53private:
55 void operator=(const locked_allocator &);
56
58 mutable std::mutex mtx_;
59};
60
61} // namespace core
62
63} // namespace yami
64
65#endif // YAMICORE_LOCKED_ALLOCATOR_H_INCLUDED
Common interface for the custom memory allocator.
Definition: allocator.h:20
Locking (thread-safe) wrapper for the block-based allocator.
Definition: locked_allocator.h:21
Non-locking (thread-unsafe) block-based allocator.
Definition: allocator.h:46
result
General type for reporting success and error states.
Definition: core.h:21
Namespace devoted for everything related to YAMI4.
Definition: agent.h:14