]> rtime.felk.cvut.cz Git - frescor/fna.git/blob - src_frescan/frescan_packets.c
error in mapping function corrected using ceil... packets constant moved to config...
[frescor/fna.git] / src_frescan / frescan_packets.c
1 /*!
2  * @file frescan_packets.h
3  *
4  * @brief FRESCAN packets definition and pool
5  *
6  * @version 0.01
7  *
8  * @date 27-Feb-2008
9  *
10  * @author
11  *      Daniel Sangorrin
12  *
13  * @comments
14  *
15  * This file contains the FRESCAN packets definition and functions to
16  * allocate and free them from a global pool of packets statically
17  * preallocated.
18  *
19  * @license
20  *
21  * See MaRTE OS license
22  *
23  */
24
25 #include "frescan_packets.h"
26 #include "frescan_debug.h"
27 #include "frescan_config.h"
28 #include <misc/freelist.h>
29
30 /**
31  * the_packet_pool - pool of frescan packets
32  *
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.
35  */
36
37 static frescan_packet_t the_packet_pool[FRESCAN_MX_PACKETS];
38 static freelist_t the_packet_pool_freelist;
39
40 #ifdef FRESCAN_PACKETPOOL_ENABLE_DEBUG
41         static int allocated_total = 0;
42 #endif
43
44 /**
45  * frescan_packets_init
46  *
47  * Initializes a pool of packets that will be managed internally
48  * TODO: initalization flag
49  */
50
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);
54 }
55
56 /**
57  * frescan_packets_alloc
58  *
59  * Allocates a frame from the pool of packets. On error it returns NULL
60  */
61
62 frescan_packet_t *frescan_packets_alloc() {
63         int pos;
64
65         pos = freelist_alloc(&the_packet_pool_freelist);
66         if (pos == -1) {
67                 ERROR("could not allocate packet\n");
68                 return NULL;
69         }
70
71 #ifdef FRESCAN_PACKETPOOL_ENABLE_DEBUG
72         allocated_total++;
73 #endif
74
75         DEBUG(FRESCAN_PACKETPOOL_ENABLE_DEBUG,
76               "allocating packet, pos:%d, total:%d\n", pos, allocated_total);
77
78         the_packet_pool[pos].pool_pos = pos; // to know how to free it
79         return &the_packet_pool[pos];
80 }
81
82 /**
83  * frescan_packets_free
84  *
85  * Frees a frame and returns it to the pool of packets.
86  */
87
88 int frescan_packets_free(frescan_packet_t *packet)
89 {
90 #ifdef FRESCAN_PACKETPOOL_ENABLE_DEBUG
91         allocated_total--;
92 #endif
93         DEBUG(FRESCAN_PACKETPOOL_ENABLE_DEBUG,
94               "freeing packet, pos:%d, total:%d\n",
95               packet->pool_pos, allocated_total);
96
97         return freelist_free(&the_packet_pool_freelist, packet->pool_pos);
98 }