]> rtime.felk.cvut.cz Git - frescor/fna.git/blob - src_frescan/frescan_packets.c
Unified header for FNA
[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 - 2009 by the FRESCOR consortium:
23 //
24 //    Universidad de Cantabria,              SPAIN
25 //    University of York,                    UK
26 //    Scuola Superiore Sant'Anna,            ITALY
27 //    Kaiserslautern University,             GERMANY
28 //    Univ. Politecnica  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
37 //
38 //        The 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 //
44 //  based on previous work (FSF) done in the FIRST project
45 //
46 //   Copyright (C) 2005  Mälardalen University, SWEDEN
47 //                       Scuola Superiore S.Anna, ITALY
48 //                       Universidad de Cantabria, SPAIN
49 //                       University of York, UK
50 //
51 // This file is part of FNA (Frescor Network Adaptation)
52 //
53 // FNA is free software; you can redistribute it and/or modify it
54 // under terms of the GNU General Public License as published by the
55 // Free Software Foundation; either version 2, or (at your option) any
56 // later version.  FNA is distributed in the hope that it will be
57 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
58 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
59 // General Public License for more details. You should have received a
60 // copy of the GNU General Public License along with FNA; see file
61 // COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,
62 // Cambridge, MA 02139, USA.
63 //
64 // As a special exception, including FNA header files in a file,
65 // instantiating FNA generics or templates, or linking other files
66 // with FNA objects to produce an executable application, does not
67 // by itself cause the resulting executable application to be covered
68 // by the GNU General Public License. This exception does not
69 // however invalidate any other reasons why the executable file might be
70 // covered by the GNU Public License.
71 // -----------------------------------------------------------------------
72  *
73  */
74
75 #include "frescan_packets.h"
76 #include "frescan_debug.h"
77 #include "frescan_config.h"
78 #include <misc/freelist.h>
79
80 /**
81  * the_packet_pool - pool of frescan packets
82  *
83  * Like in the CAN driver, in FRESCAN we have a statically preallocated pool
84  * of packets that we will manage through a freelist in O(1) time.
85  */
86
87 static frescan_packet_t the_packet_pool[FRESCAN_MX_PACKETS];
88 static freelist_t the_packet_pool_freelist;
89
90 #ifdef FRESCAN_PACKETPOOL_ENABLE_DEBUG
91         static int allocated_total = 0;
92 #endif
93
94 /**
95  * frescan_packets_init
96  *
97  * Initializes a pool of packets that will be managed internally
98  * TODO: initalization flag
99  */
100
101 int frescan_packets_init() {
102         DEBUG(FRESCAN_PACKETPOOL_ENABLE_DEBUG, "initialize freelist\n");
103         return freelist_init(&the_packet_pool_freelist, FRESCAN_MX_PACKETS);
104 }
105
106 /**
107  * frescan_packets_alloc
108  *
109  * Allocates a frame from the pool of packets. On error it returns NULL
110  */
111
112 frescan_packet_t *frescan_packets_alloc() {
113         int pos;
114
115         pos = freelist_alloc(&the_packet_pool_freelist);
116         if (pos == -1) {
117                 FRESCAN_ERROR("could not allocate packet\n");
118                 FRESCAN_ERROR("hint: configure FRESCAN_MX_PACKETS\n");
119                 return NULL;
120         }
121
122 #ifdef FRESCAN_PACKETPOOL_ENABLE_DEBUG
123         allocated_total++;
124 #endif
125
126         DEBUG(FRESCAN_PACKETPOOL_ENABLE_DEBUG,
127               "allocating packet, pos:%d, total:%d\n", pos, allocated_total);
128
129         the_packet_pool[pos].pool_pos = pos; // to know how to free it
130         return &the_packet_pool[pos];
131 }
132
133 /**
134  * frescan_packets_free
135  *
136  * Frees a frame and returns it to the pool of packets.
137  */
138
139 int frescan_packets_free(frescan_packet_t *packet)
140 {
141 #ifdef FRESCAN_PACKETPOOL_ENABLE_DEBUG
142         allocated_total--;
143 #endif
144         DEBUG(FRESCAN_PACKETPOOL_ENABLE_DEBUG,
145               "freeing packet, pos:%d, total:%d\n",
146               packet->pool_pos, allocated_total);
147
148         return freelist_free(&the_packet_pool_freelist, packet->pool_pos);
149 }