2 * @file frescan_packets.h
4 * @brief FRESCAN packets definition and pool
15 * This file contains the FRESCAN packets definition and functions to
16 * allocate and free them from a global pool of packets statically
21 * See MaRTE OS license
25 #include "frescan_packets.h"
26 #include "frescan_debug.h"
27 #include "frescan_config.h"
28 #include <misc/freelist.h>
31 * the_packet_pool - pool of frescan packets
33 * Like in the CAN driver, in FRESCAN we have a statically preallocated pool
34 * of packets that we will manage through a freelist in O(1) time.
37 static frescan_packet_t the_packet_pool[FRESCAN_MX_PACKETS];
38 static freelist_t the_packet_pool_freelist;
40 #ifdef FRESCAN_PACKETPOOL_ENABLE_DEBUG
41 static int allocated_total = 0;
45 * frescan_packets_init
47 * Initializes a pool of packets that will be managed internally
48 * TODO: initalization flag
51 int frescan_packets_init() {
52 DEBUG(FRESCAN_PACKETPOOL_ENABLE_DEBUG, "initialize freelist\n");
53 return freelist_init(&the_packet_pool_freelist, FRESCAN_MX_PACKETS);
57 * frescan_packets_alloc
59 * Allocates a frame from the pool of packets. On error it returns NULL
62 frescan_packet_t *frescan_packets_alloc() {
65 pos = freelist_alloc(&the_packet_pool_freelist);
67 ERROR("could not allocate packet\n");
71 #ifdef FRESCAN_PACKETPOOL_ENABLE_DEBUG
75 DEBUG(FRESCAN_PACKETPOOL_ENABLE_DEBUG,
76 "allocating packet, pos:%d, total:%d\n", pos, allocated_total);
78 the_packet_pool[pos].pool_pos = pos; // to know how to free it
79 return &the_packet_pool[pos];
83 * frescan_packets_free
85 * Frees a frame and returns it to the pool of packets.
88 int frescan_packets_free(frescan_packet_t *packet)
90 #ifdef FRESCAN_PACKETPOOL_ENABLE_DEBUG
93 DEBUG(FRESCAN_PACKETPOOL_ENABLE_DEBUG,
94 "freeing packet, pos:%d, total:%d\n",
95 packet->pool_pos, allocated_total);
97 return freelist_free(&the_packet_pool_freelist, packet->pool_pos);