1 |
/* |
2 |
device.C -- include the correct low-level implementation. |
3 |
Copyright (C) 2003-2008 Marc Lehmann <gvpe@schmorp.de> |
4 |
|
5 |
This file is part of GVPE. |
6 |
|
7 |
GVPE is free software; you can redistribute it and/or modify it |
8 |
under the terms of the GNU General Public License as published by the |
9 |
Free Software Foundation; either version 3 of the License, or (at your |
10 |
option) any later version. |
11 |
|
12 |
This program is distributed in the hope that it will be useful, but |
13 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
15 |
Public License for more details. |
16 |
|
17 |
You should have received a copy of the GNU General Public License along |
18 |
with this program; if not, see <http://www.gnu.org/licenses/>. |
19 |
|
20 |
Additional permission under GNU GPL version 3 section 7 |
21 |
|
22 |
If you modify this Program, or any covered work, by linking or |
23 |
combining it with the OpenSSL project's OpenSSL library (or a modified |
24 |
version of that library), containing parts covered by the terms of the |
25 |
OpenSSL or SSLeay licenses, the licensors of this Program grant you |
26 |
additional permission to convey the resulting work. Corresponding |
27 |
Source for a non-source form of such a combination shall include the |
28 |
source code for the parts of OpenSSL used as well as that of the |
29 |
covered work. |
30 |
*/ |
31 |
|
32 |
#include "config.h" |
33 |
|
34 |
#include <cstring> |
35 |
#include <cstdlib> |
36 |
#include <stdexcept> |
37 |
|
38 |
#include "slog.h" |
39 |
#include "device.h" |
40 |
|
41 |
static void *pkt_cachep[PKTCACHESIZE]; |
42 |
static int pkt_cachen = 0; |
43 |
|
44 |
void * |
45 |
net_packet::operator new(size_t s) |
46 |
{ |
47 |
if (s > sizeof (data_packet)) |
48 |
{ |
49 |
slog (L_ERR, _("FATAL: allocation for network packet larger than max supported packet size (%d > %d)."), |
50 |
s, sizeof (data_packet)); |
51 |
abort (); |
52 |
} |
53 |
|
54 |
if (pkt_cachen) |
55 |
return pkt_cachep[--pkt_cachen]; |
56 |
else |
57 |
{ |
58 |
void *p = malloc (sizeof (data_packet)); |
59 |
|
60 |
if (!p) |
61 |
throw (std::bad_alloc ()); |
62 |
|
63 |
memset (p, 0, sizeof (data_packet)); |
64 |
return p; |
65 |
} |
66 |
} |
67 |
|
68 |
void |
69 |
net_packet::operator delete(void *p) |
70 |
{ |
71 |
if (p) |
72 |
{ |
73 |
if (pkt_cachen < PKTCACHESIZE) |
74 |
{ |
75 |
memset (p, 0, sizeof (data_packet)); |
76 |
pkt_cachep[pkt_cachen++] = p; |
77 |
} |
78 |
else |
79 |
free (p); |
80 |
} |
81 |
} |
82 |
|
83 |
#if IFTYPE_tincd |
84 |
# include "device-tincd.C" |
85 |
#elif IFTYPE_native && IF_linux |
86 |
# include "device-linux.C" |
87 |
#elif IFTYPE_native && IF_cygwin |
88 |
# include "device-cygwin.C" |
89 |
#elif IFTYPE_native && IF_darwin |
90 |
# include "device-darwin.C" |
91 |
#else |
92 |
# error No interface implementation for your IFTYPE/IFSUBTYPE combination. |
93 |
#endif |
94 |
|
95 |
|