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 //----------------------------------------------------------------------
22 // Copyright (C) 2006 - 2009 by the FRESCOR consortium:
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
31 // Thales Communication S.A. FRANCE
32 // Visual Tools S.A. SPAIN
33 // Rapita Systems Ltd UK
36 // See http://www.frescor.org
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
44 // based on previous work (FSF) done in the FIRST project
46 // Copyright (C) 2005 Mälardalen University, SWEDEN
47 // Scuola Superiore S.Anna, ITALY
48 // Universidad de Cantabria, SPAIN
49 // University of York, UK
51 // This file is part of FNA (Frescor Network Adaptation)
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.
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 // -----------------------------------------------------------------------
75 #include "frescan_packets.h"
76 #include "frescan_debug.h"
77 #include "frescan_config.h"
78 #include <misc/freelist.h>
81 * the_packet_pool - pool of frescan packets
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.
87 static frescan_packet_t the_packet_pool[FRESCAN_MX_PACKETS];
88 static freelist_t the_packet_pool_freelist;
90 #ifdef FRESCAN_PACKETPOOL_ENABLE_DEBUG
91 static int allocated_total = 0;
95 * frescan_packets_init
97 * Initializes a pool of packets that will be managed internally
98 * TODO: initalization flag
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);
107 * frescan_packets_alloc
109 * Allocates a frame from the pool of packets. On error it returns NULL
112 frescan_packet_t *frescan_packets_alloc() {
115 pos = freelist_alloc(&the_packet_pool_freelist);
117 FRESCAN_ERROR("could not allocate packet\n");
118 FRESCAN_ERROR("hint: configure FRESCAN_MX_PACKETS\n");
122 #ifdef FRESCAN_PACKETPOOL_ENABLE_DEBUG
126 DEBUG(FRESCAN_PACKETPOOL_ENABLE_DEBUG,
127 "allocating packet, pos:%d, total:%d\n", pos, allocated_total);
129 the_packet_pool[pos].pool_pos = pos; // to know how to free it
130 return &the_packet_pool[pos];
134 * frescan_packets_free
136 * Frees a frame and returns it to the pool of packets.
139 int frescan_packets_free(frescan_packet_t *packet)
141 #ifdef FRESCAN_PACKETPOOL_ENABLE_DEBUG
144 DEBUG(FRESCAN_PACKETPOOL_ENABLE_DEBUG,
145 "freeing packet, pos:%d, total:%d\n",
146 packet->pool_pos, allocated_total);
148 return freelist_free(&the_packet_pool_freelist, packet->pool_pos);