AlteriomPainlessMesh v2.1.1
Easy ESP32/ESP8266 mesh networking with Alteriom IoT extensions
Loading...
Searching...
No Matches
memory.hpp
Go to the documentation of this file.
1#ifndef _PAINLESS_MESH_MEMORY_HPP_
2#define _PAINLESS_MESH_MEMORY_HPP_
3
12#include <memory>
13#include <vector>
14
15namespace painlessmesh {
16namespace memory {
17
22template<typename T>
24public:
25 ObjectPool(size_t initial_size = 10) {
26 pool_.reserve(initial_size);
27 for (size_t i = 0; i < initial_size; ++i) {
28 pool_.emplace_back(std::make_unique<T>());
29 }
30 }
31
32 std::unique_ptr<T> acquire() {
33 if (!pool_.empty()) {
34 auto obj = std::move(pool_.back());
35 pool_.pop_back();
36 return obj;
37 }
38 return std::make_unique<T>();
39 }
40
41 void release(std::unique_ptr<T> obj) {
42 if (pool_.size() < max_pool_size_) {
43 // Reset object to default state before returning to pool
44 *obj = T{};
45 pool_.push_back(std::move(obj));
46 }
47 // If pool is full, let unique_ptr destroy the object automatically
48 }
49
50 size_t size() const { return pool_.size(); }
51
52private:
53 std::vector<std::unique_ptr<T>> pool_;
54 static constexpr size_t max_pool_size_ = 20; // Prevent unlimited growth
55};
56
61public:
62 explicit StringBuffer(size_t initial_capacity = 512) {
63 buffer_.reserve(initial_capacity);
64 }
65
66 void clear() { buffer_.clear(); }
67
68 void append(const TSTRING& str) {
69 if (buffer_.capacity() < buffer_.size() + str.length()) {
70 buffer_.reserve(std::max(buffer_.capacity() * 2, buffer_.size() + str.length()));
71 }
72 buffer_ += str;
73 }
74
75 const TSTRING& str() const { return buffer_; }
76 TSTRING& str() { return buffer_; }
77
78 size_t capacity() const { return buffer_.capacity(); }
79 size_t size() const { return buffer_.size(); }
80
81private:
82 TSTRING buffer_;
83};
84
89 size_t total_allocated = 0;
90 size_t total_freed = 0;
91 size_t current_usage = 0;
92 size_t peak_usage = 0;
93 size_t allocation_count = 0;
94
95 void record_allocation(size_t size) {
96 total_allocated += size;
97 current_usage += size;
100 }
101
102 void record_deallocation(size_t size) {
103 total_freed += size;
104 current_usage = (current_usage >= size) ? current_usage - size : 0;
105 }
106
107 void reset() {
108 *this = MemoryStats{};
109 }
110};
111
112// Global memory statistics (optional, can be disabled for production)
113#ifdef PAINLESS_MESH_ENABLE_MEMORY_STATS
114extern MemoryStats global_memory_stats;
115#endif
116
117} // namespace memory
118} // namespace painlessmesh
119
120#endif // _PAINLESS_MESH_MEMORY_HPP_
Definition memory.hpp:23
size_t size() const
Definition memory.hpp:50
void release(std::unique_ptr< T > obj)
Definition memory.hpp:41
ObjectPool(size_t initial_size=10)
Definition memory.hpp:25
std::unique_ptr< T > acquire()
Definition memory.hpp:32
Definition memory.hpp:60
const TSTRING & str() const
Definition memory.hpp:75
TSTRING & str()
Definition memory.hpp:76
size_t size() const
Definition memory.hpp:79
size_t capacity() const
Definition memory.hpp:78
void append(const TSTRING &str)
Definition memory.hpp:68
StringBuffer(size_t initial_capacity=512)
Definition memory.hpp:62
void clear()
Definition memory.hpp:66
String TSTRING
Definition configuration.hpp:132
Definition wifi.hpp:34
Definition memory.hpp:88
void reset()
Definition memory.hpp:107
void record_allocation(size_t size)
Definition memory.hpp:95
size_t peak_usage
Definition memory.hpp:92
size_t current_usage
Definition memory.hpp:91
size_t total_allocated
Definition memory.hpp:89
size_t allocation_count
Definition memory.hpp:93
void record_deallocation(size_t size)
Definition memory.hpp:102
size_t total_freed
Definition memory.hpp:90