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 - 2008 FRESCOR consortium partners:
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
31 * Thales Communication S.A. FRANCE
32 * Visual Tools S.A. SPAIN
33 * Rapita Systems Ltd UK
36 * See http://www.frescor.org for a link to partners' websites
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
43 * This file is part of FRESCAN
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)
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.
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
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 * -----------------------------------------------------------------------
71 #include "frescan_packets.h"
72 #include "frescan_debug.h"
73 #include "frescan_config.h"
74 #include <misc/freelist.h>
77 * the_packet_pool - pool of frescan packets
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.
83 static frescan_packet_t the_packet_pool[FRESCAN_MX_PACKETS];
84 static freelist_t the_packet_pool_freelist;
86 #ifdef FRESCAN_PACKETPOOL_ENABLE_DEBUG
87 static int allocated_total = 0;
91 * frescan_packets_init
93 * Initializes a pool of packets that will be managed internally
94 * TODO: initalization flag
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);
103 * frescan_packets_alloc
105 * Allocates a frame from the pool of packets. On error it returns NULL
108 frescan_packet_t *frescan_packets_alloc() {
111 pos = freelist_alloc(&the_packet_pool_freelist);
113 FRESCAN_ERROR("could not allocate packet\n");
117 #ifdef FRESCAN_PACKETPOOL_ENABLE_DEBUG
121 DEBUG(FRESCAN_PACKETPOOL_ENABLE_DEBUG,
122 "allocating packet, pos:%d, total:%d\n", pos, allocated_total);
124 the_packet_pool[pos].pool_pos = pos; // to know how to free it
125 return &the_packet_pool[pos];
129 * frescan_packets_free
131 * Frees a frame and returns it to the pool of packets.
134 int frescan_packets_free(frescan_packet_t *packet)
136 #ifdef FRESCAN_PACKETPOOL_ENABLE_DEBUG
139 DEBUG(FRESCAN_PACKETPOOL_ENABLE_DEBUG,
140 "freeing packet, pos:%d, total:%d\n",
141 packet->pool_pos, allocated_total);
143 return freelist_free(&the_packet_pool_freelist, packet->pool_pos);