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

Implementation of the pre-allocated CAN message buffer pool. More...

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

Implementation of the pre-allocated CAN message buffer pool.

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.

Single static array of can_msg_t buffers with first-fit allocation. Allocation and free operations toggle a per-slot flag and maintain running and peak counters for pool-size tuning. NOT thread-safe.

Author
Jim Kueneman
Date
4 Mar 2026

Function Documentation

◆ CanBufferStore_initialize()

void CanBufferStore_initialize ( void )

Clears all buffers and resets telemetry counters.

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 )

Allocates one can_msg_t buffer from the pool.

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)

Returns a can_msg_t buffer to the pool.

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 )

Returns the number of can_msg_t buffers currently allocated.

◆ CanBufferStore_messages_max_allocated()

uint16_t CanBufferStore_messages_max_allocated ( void )

Returns the peak allocation count since last reset.

◆ CanBufferStore_clear_max_allocated()

void CanBufferStore_clear_max_allocated ( void )

Resets the peak counter without affecting current allocations.


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