Skip to content

Commit a02c3a1

Browse files
authored
Merge pull request #14537 from kjbracey-arm/mempool-doc
Better document MemoryPool behaviour
2 parents f2d73e1 + 537bee9 commit a02c3a1

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

rtos/include/rtos/MemoryPool.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ class MemoryPool : private mbed::NonCopyable<MemoryPool<T, pool_sz> > {
9797
}
9898

9999
/** Allocate a memory block from a memory pool, without blocking.
100+
101+
This method works like `std::malloc` or `std::allocator<T>::allocate` in that the
102+
returned memory block is not initialized. For types with a non-trivial constructor
103+
placement new must be used to construct an object in the returned storage.
104+
105+
Example:
106+
@code
107+
MyObject *obj = pool.alloc();
108+
if (obj) {
109+
new (obj) MyObject(1, 2);
110+
}
111+
@endcode
112+
100113
@return address of the allocated memory block or nullptr in case of no memory available.
101114
102115
@note You may call this function from ISR context.
@@ -120,6 +133,7 @@ class MemoryPool : private mbed::NonCopyable<MemoryPool<T, pool_sz> > {
120133
}
121134

122135
/** Allocate a memory block from a memory pool, optionally blocking.
136+
@see MemoryPool::try_alloc
123137
@param rel_time timeout value (Kernel::wait_for_u32_forever to wait forever)
124138
@return address of the allocated memory block or nullptr in case of no memory available.
125139
@@ -149,6 +163,7 @@ class MemoryPool : private mbed::NonCopyable<MemoryPool<T, pool_sz> > {
149163
}
150164

151165
/** Allocate a memory block from a memory pool, blocking.
166+
@see MemoryPool::try_alloc
152167
@param abs_time absolute timeout time, referenced to Kernel::Clock.
153168
@return address of the allocated memory block or nullptr in case of no memory available.
154169
@@ -264,6 +279,17 @@ class MemoryPool : private mbed::NonCopyable<MemoryPool<T, pool_sz> > {
264279
}
265280

266281
/** Free a memory block.
282+
283+
This method works like `std::free` or `std::allocator<T>::deallocate` in that any
284+
object in the memory is not destroyed. For types with a non-trivial destructor
285+
that destructor must be called manually before freeing the memory.
286+
287+
Example:
288+
@code
289+
obj->~MyObject();
290+
pool.free(obj);
291+
@endcode
292+
267293
@param block address of the allocated memory block to be freed.
268294
@return osOK on successful deallocation, osErrorParameter if given memory block id
269295
is nullptr or invalid, or osErrorResource if given memory block is in an

0 commit comments

Comments
 (0)