]> rtime.felk.cvut.cz Git - frescor/fna.git/blob - src_frescan/frescan_packets.c
changes to use the FRSH FSA module to do the analysis and spare capacity. TODO: finis...
[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  * -----------------------------------------------------------------------
22  *  Copyright (C) 2006 - 2008 FRESCOR consortium partners:
23  *
24  *    Universidad de Cantabria,              SPAIN
25  *    University of York,                    UK
26  *    Scuola Superiore Sant'Anna,            ITALY
27  *    Kaiserslautern University,             GERMANY
28  *    Univ. Politécnica  Valencia,           SPAIN
29  *    Czech Technical University in Prague,  CZECH REPUBLIC
30  *    ENEA                                   SWEDEN
31  *    Thales Communication S.A.              FRANCE
32  *    Visual Tools S.A.                      SPAIN
33  *    Rapita Systems Ltd                     UK
34  *    Evidence                               ITALY
35  *
36  *    See http://www.frescor.org for a link to partners' websites
37  *
38  *           FRESCOR project (FP6/2005/IST/5-034026) is funded
39  *        in part by the European Union Sixth Framework Programme
40  *        The European Union is not liable of any use that may be
41  *        made of this code.
42  *
43  *  This file is part of FRESCAN
44  *
45  *  FRESCAN is free software; you can  redistribute it and/or  modify
46  *  it under the terms of  the GNU General Public License as published by
47  *  the Free Software Foundation;  either  version 2, or (at  your option)
48  *  any later version.
49  *
50  *  FRESCAN  is distributed  in  the hope  that  it  will  be useful,  but
51  *  WITHOUT  ANY  WARRANTY;     without  even the   implied   warranty  of
52  *  MERCHANTABILITY  or  FITNESS FOR  A  PARTICULAR PURPOSE. See  the  GNU
53  *  General Public License for more details.
54  *
55  *  You should have  received a  copy of  the  GNU  General Public License
56  *  distributed  with  FRESCAN;  see file COPYING.   If not,  write to the
57  *  Free Software  Foundation,  59 Temple Place  -  Suite 330,  Boston, MA
58  *  02111-1307, USA.
59  *
60  * As a special exception, including FRESCAN header files in a file,
61  * instantiating FRESCAN generics or templates, or linking other files
62  * with FRESCAN objects to produce an executable application, does not
63  * by itself cause the resulting executable application to be covered
64  * by the GNU General Public License. This exception does not
65  * however invalidate any other reasons why the executable file might be
66  * covered by the GNU Public License.
67  * -----------------------------------------------------------------------
68  *
69  */
70
71 #include "frescan_packets.h"
72 #include "frescan_debug.h"
73 #include "frescan_config.h"
74 #include <misc/freelist.h>
75
76 /**
77  * the_packet_pool - pool of frescan packets
78  *
79  * Like in the CAN driver, in FRESCAN we have a statically preallocated pool
80  * of packets that we will manage through a freelist in O(1) time.
81  */
82
83 static frescan_packet_t the_packet_pool[FRESCAN_MX_PACKETS];
84 static freelist_t the_packet_pool_freelist;
85
86 #ifdef FRESCAN_PACKETPOOL_ENABLE_DEBUG
87         static int allocated_total = 0;
88 #endif
89
90 /**
91  * frescan_packets_init
92  *
93  * Initializes a pool of packets that will be managed internally
94  * TODO: initalization flag
95  */
96
97 int frescan_packets_init() {
98         DEBUG(FRESCAN_PACKETPOOL_ENABLE_DEBUG, "initialize freelist\n");
99         return freelist_init(&the_packet_pool_freelist, FRESCAN_MX_PACKETS);
100 }
101
102 /**
103  * frescan_packets_alloc
104  *
105  * Allocates a frame from the pool of packets. On error it returns NULL
106  */
107
108 frescan_packet_t *frescan_packets_alloc() {
109         int pos;
110
111         pos = freelist_alloc(&the_packet_pool_freelist);
112         if (pos == -1) {
113                 FRESCAN_ERROR("could not allocate packet\n");
114                 return NULL;
115         }
116
117 #ifdef FRESCAN_PACKETPOOL_ENABLE_DEBUG
118         allocated_total++;
119 #endif
120
121         DEBUG(FRESCAN_PACKETPOOL_ENABLE_DEBUG,
122               "allocating packet, pos:%d, total:%d\n", pos, allocated_total);
123
124         the_packet_pool[pos].pool_pos = pos; // to know how to free it
125         return &the_packet_pool[pos];
126 }
127
128 /**
129  * frescan_packets_free
130  *
131  * Frees a frame and returns it to the pool of packets.
132  */
133
134 int frescan_packets_free(frescan_packet_t *packet)
135 {
136 #ifdef FRESCAN_PACKETPOOL_ENABLE_DEBUG
137         allocated_total--;
138 #endif
139         DEBUG(FRESCAN_PACKETPOOL_ENABLE_DEBUG,
140               "freeing packet, pos:%d, total:%d\n",
141               packet->pool_pos, allocated_total);
142
143         return freelist_free(&the_packet_pool_freelist, packet->pool_pos);
144 }