OpenLcbCLib 1.0 Alpha
OpenSource C Library to create OpenLcb/Lcc Nodes
Loading...
Searching...
No Matches
can_buffer_store.h File Reference

Pre-allocated pool of can_msg_t buffers (8-byte payload each). More...

Go to the source code of this file.

Functions

void CanBufferStore_initialize (void)
 Clears all buffers and resets telemetry counters.
 
can_msg_tCanBufferStore_allocate_buffer (void)
 Allocates one can_msg_t buffer from the pool.
 
void CanBufferStore_free_buffer (can_msg_t *msg)
 Returns a can_msg_t buffer to the pool.
 
uint16_t CanBufferStore_messages_allocated (void)
 Returns the number of can_msg_t buffers currently allocated.
 
uint16_t CanBufferStore_messages_max_allocated (void)
 Returns the peak allocation count since last reset.
 
void CanBufferStore_clear_max_allocated (void)
 Resets the peak counter without affecting current allocations.
 

Detailed Description

Pre-allocated pool of can_msg_t buffers (8-byte payload each).

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Pool size is set at compile time via USER_DEFINED_CAN_MSG_BUFFER_DEPTH. All memory is allocated at startup - no dynamic allocation at runtime. Telemetry counters support pool-size tuning during development.

Author
Jim Kueneman
Date
4 Mar 2026

Function Documentation

◆ CanBufferStore_initialize()

void CanBufferStore_initialize ( void )
extern

Clears all buffers and resets telemetry counters.

Warning
Must be called once at startup before any buffer operations.
NOT thread-safe.
See also
CanBufferStore_allocate_buffer

Algorithm:

  1. Iterate through all USER_DEFINED_CAN_MSG_BUFFER_DEPTH entries.
  2. Set allocated flag to false, clear identifier, payload_count, and all payload bytes.
  3. Reset both allocation counters to zero.

◆ CanBufferStore_allocate_buffer()

can_msg_t * CanBufferStore_allocate_buffer ( void )
extern

Allocates one can_msg_t buffer from the pool.

Finds the first free slot, clears it, marks it allocated, and updates the peak telemetry counter.

Returns
Pointer to the allocated can_msg_t, or NULL if the pool is exhausted.
Warning
Returns NULL when pool is full - caller MUST check before use.
NOT thread-safe.
See also
CanBufferStore_free_buffer

Algorithm:

  1. Iterate through pool looking for first unallocated buffer.
  2. When found: increment allocation counter, update peak counter if needed, clear the buffer, mark it allocated, and return pointer.
  3. If no free buffer found, return NULL.
Returns
Pointer to the allocated can_msg_t, or NULL if the pool is exhausted.

◆ CanBufferStore_free_buffer()

void CanBufferStore_free_buffer ( can_msg_t * msg)
extern

Returns a can_msg_t buffer to the pool.

Parameters
msgPointer to the buffer to free. NULL is safely ignored.
Warning
Do not access the buffer after freeing.
NOT thread-safe - use shared resource locking.
See also
CanBufferStore_allocate_buffer

Algorithm:

  1. If NULL pointer passed, return immediately.
  2. Decrement allocation counter and clear the allocated flag.
* @param msg Pointer to the buffer to free. NULL is safely ignored.
* 

◆ CanBufferStore_messages_allocated()

uint16_t CanBufferStore_messages_allocated ( void )
extern

Returns the number of can_msg_t buffers currently allocated.

◆ CanBufferStore_messages_max_allocated()

uint16_t CanBufferStore_messages_max_allocated ( void )
extern

Returns the peak allocation count since last reset.

◆ CanBufferStore_clear_max_allocated()

void CanBufferStore_clear_max_allocated ( void )
extern

Resets the peak counter without affecting current allocations.


Copyright (c) 2026 Jim Kueneman all rights reserved. See the License