VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/VBoxTAP/mem.c@ 13815

最後變更 在這個檔案從13815是 1,由 vboxsync 提交於 55 年 前

import

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 3.8 KB
 
1/*
2 * TAP-Win32 -- A kernel driver to provide virtual tap device functionality
3 * on Windows. Originally derived from the CIPE-Win32
4 * project by Damion K. Wilson, with extensive modifications by
5 * James Yonan.
6 *
7 * All source code which derives from the CIPE-Win32 project is
8 * Copyright (C) Damion K. Wilson, 2003, and is released under the
9 * GPL version 2 (see below).
10 *
11 * All other source code is Copyright (C) 2002-2005 OpenVPN Solutions LLC,
12 * and is released under the GPL version 2 (see below).
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2
16 * as published by the Free Software Foundation.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program (see the file COPYING included with this
25 * distribution); if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 */
28
29//------------------
30// Memory Management
31//------------------
32
33PVOID
34MemAlloc (ULONG p_Size, BOOLEAN zero)
35{
36 PVOID l_Return = NULL;
37
38 if (p_Size)
39 {
40 __try
41 {
42 if (NdisAllocateMemoryWithTag (&l_Return, p_Size, 'APAT')
43 == NDIS_STATUS_SUCCESS)
44 {
45 if (zero)
46 NdisZeroMemory (l_Return, p_Size);
47 }
48 else
49 l_Return = NULL;
50 }
51 __except (EXCEPTION_EXECUTE_HANDLER)
52 {
53 l_Return = NULL;
54 }
55 }
56
57 return l_Return;
58}
59
60VOID
61MemFree (PVOID p_Addr, ULONG p_Size)
62{
63 if (p_Addr && p_Size)
64 {
65 __try
66 {
67#if DEBUG
68 NdisZeroMemory (p_Addr, p_Size);
69#endif
70 NdisFreeMemory (p_Addr, p_Size, 0);
71 }
72 __except (EXCEPTION_EXECUTE_HANDLER)
73 {
74 }
75 }
76}
77
78/*
79 * Circular queue management routines.
80 */
81
82#define QUEUE_BYTE_ALLOCATION(size) \
83 (sizeof (Queue) + (size * sizeof (PVOID)))
84
85#define QUEUE_ADD_INDEX(var, inc) \
86{ \
87 var += inc; \
88 if (var >= q->capacity) \
89 var -= q->capacity; \
90 MYASSERT (var < q->capacity); \
91}
92
93#define QUEUE_SANITY_CHECK() \
94 MYASSERT (q != NULL && q->base < q->capacity && q->size <= q->capacity)
95
96#define QueueCount(q) (q->size)
97
98#define UPDATE_MAX_SIZE() \
99{ \
100 if (q->size > q->max_size) \
101 q->max_size = q->size; \
102}
103
104Queue *
105QueueInit (ULONG capacity)
106{
107 Queue *q;
108
109 MYASSERT (capacity > 0);
110 q = (Queue *) MemAlloc (QUEUE_BYTE_ALLOCATION (capacity), TRUE);
111 if (!q)
112 return NULL;
113
114 q->base = q->size = 0;
115 q->capacity = capacity;
116 q->max_size = 0;
117 return q;
118}
119
120VOID
121QueueFree (Queue *q)
122{
123 if (q)
124 {
125 QUEUE_SANITY_CHECK ();
126 MemFree (q, QUEUE_BYTE_ALLOCATION (q->capacity));
127 }
128}
129
130PVOID
131QueuePush (Queue *q, PVOID item)
132{
133 ULONG dest;
134 QUEUE_SANITY_CHECK ();
135 if (q->size == q->capacity)
136 return NULL;
137 dest = q->base;
138 QUEUE_ADD_INDEX (dest, q->size);
139 q->data[dest] = item;
140 ++q->size;
141 UPDATE_MAX_SIZE();
142 return item;
143}
144
145PVOID
146QueuePop (Queue *q)
147{
148 ULONG oldbase;
149 QUEUE_SANITY_CHECK ();
150 if (!q->size)
151 return NULL;
152 oldbase = q->base;
153 QUEUE_ADD_INDEX (q->base, 1);
154 --q->size;
155 UPDATE_MAX_SIZE();
156 return q->data[oldbase];
157}
158
159PVOID
160QueueExtract (Queue *q, PVOID item)
161{
162 ULONG src, dest, count, n;
163 QUEUE_SANITY_CHECK ();
164 n = 0;
165 src = dest = q->base;
166 count = q->size;
167 while (count--)
168 {
169 if (item == q->data[src])
170 {
171 ++n;
172 --q->size;
173 }
174 else
175 {
176 q->data[dest] = q->data[src];
177 QUEUE_ADD_INDEX (dest, 1);
178 }
179 QUEUE_ADD_INDEX (src, 1);
180 }
181 if (n)
182 return item;
183 else
184 return NULL;
185}
186
187#undef QUEUE_BYTE_ALLOCATION
188#undef QUEUE_ADD_INDEX
189#undef QUEUE_SANITY_CHECK
190#undef UPDATE_MAX_SIZE
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette