Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 1 | /* |
| 2 | include/proto/port_range.h |
| 3 | This file defines everything needed to manage port ranges |
| 4 | |
| 5 | Copyright (C) 2000-2009 Willy Tarreau - w@1wt.eu |
| 6 | |
| 7 | This library is free software; you can redistribute it and/or |
| 8 | modify it under the terms of the GNU Lesser General Public |
| 9 | License as published by the Free Software Foundation, version 2.1 |
| 10 | exclusively. |
| 11 | |
| 12 | This library is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | Lesser General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU Lesser General Public |
| 18 | License along with this library; if not, write to the Free Software |
| 19 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #ifndef _PROTO_PORT_RANGE_H |
| 23 | #define _PROTO_PORT_RANGE_H |
| 24 | |
| 25 | #include <types/port_range.h> |
| 26 | |
| 27 | /* return an available port from range <range>, or zero if none is left */ |
| 28 | static inline int port_range_alloc_port(struct port_range *range) |
| 29 | { |
| 30 | int ret; |
| 31 | |
| 32 | if (!range->avail) |
| 33 | return 0; |
| 34 | ret = range->ports[range->get]; |
| 35 | range->get++; |
| 36 | if (range->get >= range->size) |
| 37 | range->get = 0; |
| 38 | range->avail--; |
| 39 | return ret; |
| 40 | } |
| 41 | |
| 42 | /* release port <port> into port range <range>. Does nothing if <port> is zero |
| 43 | * nor if <range> is null. The caller is responsible for marking the port |
| 44 | * unused by either setting the port to zero or the range to NULL. |
| 45 | */ |
| 46 | static inline void port_range_release_port(struct port_range *range, int port) |
| 47 | { |
| 48 | if (!port || !range) |
| 49 | return; |
| 50 | |
| 51 | range->ports[range->put] = port; |
| 52 | range->avail++; |
| 53 | range->put++; |
| 54 | if (range->put >= range->size) |
| 55 | range->put = 0; |
| 56 | } |
| 57 | |
| 58 | /* return a new initialized port range of N ports. The ports are not |
| 59 | * filled in, it's up to the caller to do it. |
| 60 | */ |
| 61 | static inline struct port_range *port_range_alloc_range(int n) |
| 62 | { |
| 63 | struct port_range *ret; |
| 64 | ret = calloc(1, sizeof(struct port_range) + |
| 65 | n * sizeof(((struct port_range *)0)->ports[0])); |
| 66 | ret->size = ret->avail = n; |
| 67 | return ret; |
| 68 | } |
| 69 | |
| 70 | #endif /* _PROTO_PORT_RANGE_H */ |
| 71 | |
| 72 | /* |
| 73 | * Local variables: |
| 74 | * c-indent-level: 8 |
| 75 | * c-basic-offset: 8 |
| 76 | * End: |
| 77 | */ |