µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
Memory management

C API memory management definitions. More...

Classes

struct  os_memory_s
 Memory resource object storage. More...
 

Typedefs

typedef struct os_memory_s os_memory_t
 Memory resource object storage.
 

Memory Management Functions

os_memory_tos_memory_get_default (void)
 Get the application default memory resource (free store).
 
void * os_memory_allocate (os_memory_t *memory, size_t bytes, size_t alignment)
 Allocate a block of memory.
 
void os_memory_deallocate (os_memory_t *memory, void *addr, size_t bytes, size_t alignment)
 Deallocate the previously allocated block of memory.
 
void os_memory_reset (os_memory_t *memory)
 Reset the memory manager to the initial state.
 
bool os_memory_coalesce (os_memory_t *memory)
 Coalesce free blocks.
 
size_t os_memory_get_total_bytes (os_memory_t *memory)
 Get the total size of managed memory.
 
size_t os_memory_get_allocated_bytes (os_memory_t *memory)
 Get the total size of allocated chunks.
 
size_t os_memory_get_free_bytes (os_memory_t *memory)
 Get the total size of free chunks.
 
size_t os_memory_get_allocated_chunks (os_memory_t *memory)
 Get the number of allocated chunks.
 
size_t os_memory_get_free_chunks (os_memory_t *memory)
 Get the number of free chunks.
 

Standard functions

void * malloc (size_t bytes)
 Allocate a memory block (non-initialised).
 
void * calloc (size_t nelem, size_t elbytes)
 Allocate an array of memory blocks (initialised to zero).
 
void * realloc (void *ptr, size_t bytes)
 Reallocate the memory block (non-initialised).
 
void free (void *ptr)
 Free the allocated memory block.
 

Detailed Description

The µOS++ RTOS includes several advanced and flexible memory management features.

Access to these classes from C can be done using the os_memory_*() functions.

For the complete definition, see
RTOS C++ API
Examples
int
os_main (int argc, char* argv[])
{
// Get the memory resource used to manage the application free store.
void* p;
p = os_memory_allocate(mem, 123, 8);
os_memory_deallocate(mem, p, 123, 8);
// Print some memory resource statistics.
}
os_memory_t * os_memory_get_default(void)
Get the application default memory resource (free store).
size_t os_memory_get_free_bytes(os_memory_t *memory)
Get the total size of free chunks.
void * os_memory_allocate(os_memory_t *memory, size_t bytes, size_t alignment)
Allocate a block of memory.
void os_memory_deallocate(os_memory_t *memory, void *addr, size_t bytes, size_t alignment)
Deallocate the previously allocated block of memory.
int os_main(int argc, char *argv[])
Application entry point, running on the main thread context.
Memory resource object storage.
int trace_printf(const char *format,...)

Typedef Documentation

◆ os_memory_t

typedef struct os_memory_s os_memory_t

A pointer to this structure can be used as a pointer to the os::rtos::memory::memory_resource object.

The members of this structure are hidden and should not be used directly, but only through specific functions.

See also
os::rtos::memory::memory_resource

Function Documentation

◆ calloc()

void * calloc ( size_t  nelem,
size_t  elbytes 
)
Parameters
nelemNumber of elements.
elbytesElement size in bytes.
Returns
A pointer to the allocated memory or null and ENOMEM.

The calloc() function shall allocate unused space for an array of nelem elements each of whose size in bytes is elbytes. The space shall be initialised to all bits 0.

The order and contiguity of storage allocated by successive calls to calloc() is unspecified. The pointer returned if the allocation succeeds shall be suitably aligned so that it may be assigned to a pointer to any type of object and then used to access such an object or an array of such objects in the space allocated (until the space is explicitly freed or reallocated). Each such allocation shall yield a pointer to an object disjoint from any other object. The pointer returned shall point to the start (lowest byte address) of the allocated space. If the space cannot be allocated, a null pointer shall be returned. If the size of the space requested is 0, the behaviour is implementation-defined: the value returned shall be either a null pointer or a unique pointer.

Upon successful completion with both nelem and elbytes non-zero, calloc() shall return a pointer to the allocated space. If either nelem or elbytes is 0, then either a null pointer or a unique pointer value that can be successfully passed to free() shall be returned. Otherwise, it shall return a null pointer and set errno to indicate the error.

Note
In µOS++ this function uses a scheduler critical section and is thread safe.
POSIX compatibility
Inspired by calloc() (IEEE Std 1003.1, 2013 Edition).
Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 153 of file malloc.cpp.

154{
156
157 errno = 0;
158 if (nelem == 0 || elbytes == 0)
159 {
160 return nullptr;
161 }
162
163 void* mem;
164 {
165 // ----- Begin of critical section ----------------------------------------
167
168 mem = estd::pmr::get_default_resource ()->allocate (nelem * elbytes);
169
170#if defined(OS_TRACE_LIBC_MALLOC)
171 trace::printf ("::%s(%u,%u)=%p\n", __func__, nelem, elbytes, mem);
172#endif
173 // ----- End of critical section ------------------------------------------
174 }
175
176 if (mem != nullptr)
177 {
178 memset (mem, 0, nelem * elbytes);
179 }
180 else
181 {
182 errno = ENOMEM;
183 }
184
185 return mem;
186}
void * allocate(std::size_t bytes, std::size_t alignment=max_align)
Allocate a memory block.
Definition os-memory.h:1290
Scheduler critical section RAII helper.
Definition os-sched.h:171
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:59
memory_resource * get_default_resource(void) noexcept
Get the default application memory manager.
bool in_handler_mode(void)
Check if the CPU is in handler mode.
Definition os-sched.h:1101

References os::rtos::memory::memory_resource::allocate(), os::estd::pmr::get_default_resource(), os::rtos::interrupts::in_handler_mode(), and os::trace::printf().

◆ free()

void free ( void *  ptr)
Parameters
ptrPointer to previously allocated block.
Returns
Nothing.

The free() function shall cause the space pointed to by ptr to be deallocated; that is, made available for further allocation. If ptr is a null pointer, no action shall occur. Otherwise, if the argument does not match a pointer earlier returned by a function in POSIX.1-2008 that allocates memory as if by malloc(), or if the space has been deallocated by a call to free() or realloc(), the behaviour is undefined.

Any use of a pointer that refers to freed space results in undefined behaviour.

The free() function shall not return a value.

Note
In µOS++ this function uses a scheduler critical section and is thread safe.
POSIX compatibility
Inspired by free() (IEEE Std 1003.1, 2013 Edition).
Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 347 of file malloc.cpp.

348{
350
351 if (ptr == nullptr)
352 {
353 return;
354 }
355
356 // ----- Begin of critical section ------------------------------------------
358
359#if defined(OS_TRACE_LIBC_MALLOC)
360 trace::printf ("::%s(%p)\n", __func__, ptr);
361#endif
362
363 // Size unknown, pass 0.
365 // ----- End of critical section --------------------------------------------
366}
void deallocate(void *addr, std::size_t bytes, std::size_t alignment=max_align) noexcept
Deallocate the previously allocated memory block.
Definition os-memory.h:1312

References os::rtos::memory::memory_resource::deallocate(), os::estd::pmr::get_default_resource(), os::rtos::interrupts::in_handler_mode(), and os::trace::printf().

Referenced by os_mempool_free().

◆ malloc()

void * malloc ( size_t  bytes)
Parameters
bytesNumber of bytes to allocate.
Returns
A pointer to the allocated memory or null and ENOMEM.

The malloc() function shall allocate unused space for an object whose size is bytes and whose value is unspecified.

The order and contiguity of storage allocated by successive calls to malloc() is unspecified. The pointer returned if the allocation succeeds shall be suitably aligned so that it may be assigned to a pointer to any type of object and then used to access such an object in the space allocated (until the space is explicitly freed or reallocated). Each such allocation shall yield a pointer to an object disjoint from any other object. The pointer returned points to the start (lowest byte address) of the allocated space. If the space cannot be allocated, a null pointer shall be returned. If the size of the space requested is 0, the behaviour is implementation-defined: the value returned shall be either a null pointer or a unique pointer.

Upon successful completion with size not equal to 0, malloc() shall return a pointer to the allocated space. If size is 0, either a null pointer or a unique pointer that can be successfully passed to free() shall be returned. Otherwise, it shall return a null pointer and set errno to indicate the error.

Note
In µOS++ this function uses a scheduler critical section and is thread safe.
POSIX compatibility
Inspired by malloc() (IEEE Std 1003.1, 2013 Edition).
Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 84 of file malloc.cpp.

85{
87
88 void* mem;
89 {
90 // ----- Begin of critical section --------------------------------------
92
93 errno = 0;
95 if (mem == nullptr)
96 {
97 errno = ENOMEM;
98 }
99
100#if defined(OS_TRACE_LIBC_MALLOC)
101 trace::printf ("::%s(%d)=%p\n", __func__, bytes, mem);
102#endif
103 // ----- End of critical section ----------------------------------------
104 }
105
106 return mem;
107}

References os::rtos::memory::memory_resource::allocate(), os::estd::pmr::get_default_resource(), os::rtos::interrupts::in_handler_mode(), and os::trace::printf().

◆ os_memory_allocate()

void * os_memory_allocate ( os_memory_t memory,
size_t  bytes,
size_t  alignment 
)
Parameters
memoryPointer to a memory resource object instance.
bytesNumber of bytes to allocate.
alignmentInteger (power of 2) with alignment constraints.
Warning
Cannot be invoked from Interrupt Service Routines.
Not thread safe, use a scheduler critical section to protect it.
For the complete definition, see
os::rtos::memory::memory_resource::allocate()

Definition at line 3223 of file os-c-wrapper.cpp.

3224{
3225 assert (memory != nullptr);
3226#pragma GCC diagnostic push
3227#pragma GCC diagnostic ignored "-Wcast-align"
3228 return (reinterpret_cast<rtos::memory::memory_resource&> (*memory))
3229 .allocate (bytes, alignment);
3230#pragma GCC diagnostic pop
3231}
Memory resource manager (abstract class).
Definition os-memory.h:159

◆ os_memory_coalesce()

bool os_memory_coalesce ( os_memory_t memory)
Parameters
memoryPointer to a memory resource object instance.
Parameters
None.
Return values
trueif the operation resulted in larger blocks.
falseif the operation was ineffective.
Warning
Cannot be invoked from Interrupt Service Routines.
Not thread safe, use a scheduler critical section to protect it.
For the complete definition, see
os::rtos::memory::memory_resource::coalesce()

Definition at line 3277 of file os-c-wrapper.cpp.

3278{
3279 assert (memory != nullptr);
3280#pragma GCC diagnostic push
3281#pragma GCC diagnostic ignored "-Wcast-align"
3282 return (reinterpret_cast<rtos::memory::memory_resource&> (*memory))
3283 .coalesce ();
3284#pragma GCC diagnostic pop
3285}

◆ os_memory_deallocate()

void os_memory_deallocate ( os_memory_t memory,
void *  addr,
size_t  bytes,
size_t  alignment 
)
Parameters
memoryPointer to a memory resource object instance.
addrAddress of memory block to free.
bytesNumber of bytes to deallocate (may be 0 if unknown).
alignmentInteger (power of 2) with alignment constraints.
Warning
Cannot be invoked from Interrupt Service Routines.
Not thread safe, use a scheduler critical section to protect it.
For the complete definition, see
os::rtos::memory::memory_resource::deallocate()

Definition at line 3241 of file os-c-wrapper.cpp.

3243{
3244 assert (memory != nullptr);
3245#pragma GCC diagnostic push
3246#pragma GCC diagnostic ignored "-Wcast-align"
3247 (reinterpret_cast<rtos::memory::memory_resource&> (*memory))
3248 .deallocate (addr, bytes, alignment);
3249#pragma GCC diagnostic pop
3250}

◆ os_memory_get_allocated_bytes()

size_t os_memory_get_allocated_bytes ( os_memory_t memory)
Parameters
memoryPointer to a memory resource object instance.
Returns
Number of bytes.
Warning
Cannot be invoked from Interrupt Service Routines.
Not thread safe, use a scheduler critical section to protect it.
For the complete definition, see
os::rtos::memory::memory_resource::allocated_bytes()

Definition at line 3313 of file os-c-wrapper.cpp.

3314{
3315 assert (memory != nullptr);
3316#pragma GCC diagnostic push
3317#pragma GCC diagnostic ignored "-Wcast-align"
3318 return (reinterpret_cast<rtos::memory::memory_resource&> (*memory))
3319 .allocated_bytes ();
3320#pragma GCC diagnostic pop
3321}

◆ os_memory_get_allocated_chunks()

size_t os_memory_get_allocated_chunks ( os_memory_t memory)
Parameters
memoryPointer to a memory resource object instance.
Returns
Number of chunks.
Warning
Cannot be invoked from Interrupt Service Routines.
Not thread safe, use a scheduler critical section to protect it.
For the complete definition, see
os::rtos::memory::memory_resource::allocated_chunks()

Definition at line 3349 of file os-c-wrapper.cpp.

3350{
3351 assert (memory != nullptr);
3352#pragma GCC diagnostic push
3353#pragma GCC diagnostic ignored "-Wcast-align"
3354 return (reinterpret_cast<rtos::memory::memory_resource&> (*memory))
3355 .allocated_chunks ();
3356#pragma GCC diagnostic pop
3357}

◆ os_memory_get_default()

os_memory_t * os_memory_get_default ( void  )
Returns
Pointer to memory resource object instance.
Warning
Cannot be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::memory::get_default_resource()

Definition at line 3209 of file os-c-wrapper.cpp.

3210{
3211 return reinterpret_cast<os_memory_t*> (
3213}
memory_resource * get_default_resource(void) noexcept
Get the default RTOS system memory manager.
Definition os-memory.h:1136

References os::rtos::memory::get_default_resource().

◆ os_memory_get_free_bytes()

size_t os_memory_get_free_bytes ( os_memory_t memory)
Parameters
memoryPointer to a memory resource object instance.
Returns
Number of bytes.
Warning
Cannot be invoked from Interrupt Service Routines.
Not thread safe, use a scheduler critical section to protect it.
For the complete definition, see
os::rtos::memory::memory_resource::free_bytes()

Definition at line 3331 of file os-c-wrapper.cpp.

3332{
3333 assert (memory != nullptr);
3334#pragma GCC diagnostic push
3335#pragma GCC diagnostic ignored "-Wcast-align"
3336 return (reinterpret_cast<rtos::memory::memory_resource&> (*memory))
3337 .free_bytes ();
3338#pragma GCC diagnostic pop
3339}

◆ os_memory_get_free_chunks()

size_t os_memory_get_free_chunks ( os_memory_t memory)
Parameters
memoryPointer to a memory resource object instance.
Returns
Number of chunks.
Warning
Cannot be invoked from Interrupt Service Routines.
Not thread safe, use a scheduler critical section to protect it.
For the complete definition, see
os::rtos::memory::memory_resource::free_chunks()

Definition at line 3367 of file os-c-wrapper.cpp.

3368{
3369 assert (memory != nullptr);
3370#pragma GCC diagnostic push
3371#pragma GCC diagnostic ignored "-Wcast-align"
3372 return (reinterpret_cast<rtos::memory::memory_resource&> (*memory))
3373 .free_chunks ();
3374#pragma GCC diagnostic pop
3375}

◆ os_memory_get_total_bytes()

size_t os_memory_get_total_bytes ( os_memory_t memory)
Parameters
memoryPointer to a memory resource object instance.
Returns
Number of bytes.
Warning
Cannot be invoked from Interrupt Service Routines.
Not thread safe, use a scheduler critical section to protect it.
For the complete definition, see
os::rtos::memory::memory_resource::total_bytes()

Definition at line 3295 of file os-c-wrapper.cpp.

3296{
3297 assert (memory != nullptr);
3298#pragma GCC diagnostic push
3299#pragma GCC diagnostic ignored "-Wcast-align"
3300 return (reinterpret_cast<rtos::memory::memory_resource&> (*memory))
3301 .total_bytes ();
3302#pragma GCC diagnostic pop
3303}

◆ os_memory_reset()

void os_memory_reset ( os_memory_t memory)
Parameters
memoryPointer to a memory resource object instance.
Parameters
None.
Returns
Nothing.
Warning
Cannot be invoked from Interrupt Service Routines.
Not thread safe, use a scheduler critical section to protect it.
For the complete definition, see
os::rtos::memory::memory_resource::reset()

Definition at line 3260 of file os-c-wrapper.cpp.

3261{
3262 assert (memory != nullptr);
3263#pragma GCC diagnostic push
3264#pragma GCC diagnostic ignored "-Wcast-align"
3265 (reinterpret_cast<rtos::memory::memory_resource&> (*memory)).reset ();
3266#pragma GCC diagnostic pop
3267}

◆ realloc()

void * realloc ( void *  ptr,
size_t  bytes 
)
Parameters
ptrPointer to previously allocated block.
bytesNumber of bytes to re-allocate.
Returns
A pointer to the allocated memory or null and ENOMEM.

The realloc() function shall deallocate the old object pointed to by ptr and return a pointer to a new object that has the size specified by bytes. The contents of the new object shall be the same as that of the old object prior to deallocation, up to the lesser of the new and old sizes. Any bytes in the new object beyond the size of the old object have indeterminate values. If the size of the space requested is zero, the behaviour shall be implementation-defined: either a null pointer is returned, or the behavior shall be as if the size were some non-zero value, except that the returned pointer shall not be used to access an object. If the space cannot be allocated, the object shall remain unchanged.

If ptr is a null pointer, realloc() shall be equivalent to malloc() for the specified size.

If ptr does not match a pointer returned earlier by calloc(), malloc(), or realloc() or if the space has previously been deallocated by a call to free() or realloc(), the behaviour is undefined.

The order and contiguity of storage allocated by successive calls to realloc() is unspecified. The pointer returned if the allocation succeeds shall be suitably aligned so that it may be assigned to a pointer to any type of object and then used to access such an object in the space allocated (until the space is explicitly freed or reallocated). Each such allocation shall yield a pointer to an object disjoint from any other object. The pointer returned shall point to the start (lowest byte address) of the allocated space. If the space cannot be allocated, a null pointer shall be returned.

Upon successful completion, realloc() shall return a pointer to the (possibly moved) allocated space. If size is 0, either:

  • A null pointer shall be returned and errno set to an implementation-defined value.
  • A unique pointer that can be successfully passed to free() shall be returned, and the memory object pointed to by ptr shall be freed. The application shall ensure that the pointer is not used to access an object.

If there is not enough available memory, realloc() shall return a null pointer and set errno to ENOMEM. If realloc() returns a null pointer and errno has been set to ENOMEM, the memory referenced by ptr shall not be changed.

Note
In µOS++ this function uses a scheduler critical section and is thread safe.
POSIX compatibility
Inspired by realloc() (IEEE Std 1003.1, 2013 Edition).
Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 254 of file malloc.cpp.

255{
257
258 void* mem;
259
260 {
261 // ----- Begin of critical section ----------------------------------------
263
264 errno = 0;
265 if (ptr == nullptr)
266 {
268#if defined(OS_TRACE_LIBC_MALLOC)
269 trace::printf ("::%s(%p,%u)=%p\n", __func__, ptr, bytes, mem);
270#endif
271 if (mem == nullptr)
272 {
273 errno = ENOMEM;
274 }
275 return mem;
276 }
277
278 if (bytes == 0)
279 {
281#if defined(OS_TRACE_LIBC_MALLOC)
282 trace::printf ("::%s(%p,%u)=0\n", __func__, ptr, bytes);
283#endif
284 return nullptr;
285 }
286
287#if 0
288 /* TODO: There is chance to shrink the chunk if newly requested
289 * size is much small */
290 if (nano_malloc_usable_size (RCALL ptr) >= bytes)
291 return ptr;
292#endif
293
295 if (mem != nullptr)
296 {
297 memcpy (mem, ptr, bytes);
299 }
300 else
301 {
302 errno = ENOMEM;
303 }
304
305#if defined(OS_TRACE_LIBC_MALLOC)
306 trace::printf ("::%s(%p,%u)=%p", __func__, ptr, bytes, mem);
307#endif
308 // ----- End of critical section ------------------------------------------
309 }
310
311 return mem;
312}

References os::rtos::memory::memory_resource::allocate(), os::rtos::memory::memory_resource::deallocate(), os::estd::pmr::get_default_resource(), os::rtos::interrupts::in_handler_mode(), and os::trace::printf().