~ubuntu-branches/ubuntu/quantal/open-vm-tools/quantal-201210021442

« back to all changes in this revision

Viewing changes to modules/linux/vmci/shared/vmci_handle_array.h

  • Committer: Bazaar Package Importer
  • Author(s): Serge Hallyn
  • Date: 2011-03-31 14:20:05 UTC
  • mfrom: (1.4.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110331142005-3n9red91p7ogkweo
Tags: 2011.03.28-387002-0ubuntu1
* Merge latest upstream git tag.  This has the unlocked_ioctl change
  needed to fix dkms build failures (LP: #727342)
* Changes in debian/rules:
  - work around a bug in toolbox/Makefile, where install-exec-hook is
    not happening.  This needs to get fixed the right way.
  - don't install 'vmware-user' which seems to no longer exist
  - move /etc/xdg into open-vm-toolbox (which should be done using .install)
* debian/open-vm-tools.init: add 'modprobe [-r] vmblock'. (LP: #332323)
* debian/rules and debian/open-vm-toolbox.lintian-overrides:
  - Make vmware-user-suid-wrapper suid-root (LP: #332323)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*********************************************************
 
2
 * Copyright (C) 2006 VMware, Inc. All rights reserved.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify it
 
5
 * under the terms of the GNU General Public License as published by the
 
6
 * Free Software Foundation version 2 and no later version.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but
 
9
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
10
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 
11
 * for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License along
 
14
 * with this program; if not, write to the Free Software Foundation, Inc.,
 
15
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 
16
 *
 
17
 *********************************************************/
 
18
 
 
19
/*
 
20
 * vmci_handle_array.h --
 
21
 *
 
22
 *      Simple dynamic array.
 
23
 */
 
24
 
 
25
#ifndef _VMCI_HANDLE_ARRAY_H_
 
26
#define _VMCI_HANDLE_ARRAY_H_
 
27
 
 
28
#define INCLUDE_ALLOW_VMMON
 
29
#define INCLUDE_ALLOW_MODULE
 
30
#define INCLUDE_ALLOW_VMCORE
 
31
#define INCLUDE_ALLOW_VMKERNEL
 
32
#include "includeCheck.h"
 
33
 
 
34
#include "vmci_kernel_if.h"
 
35
#include "vmware.h"
 
36
 
 
37
#include "vmci_defs.h"
 
38
#include "vm_assert.h"
 
39
#ifdef VMKERNEL 
 
40
#include "vm_libc.h"
 
41
#endif // VMKERNEL
 
42
 
 
43
#ifdef SOLARIS
 
44
#include <sys/ddi.h>
 
45
#include <sys/kmem.h>
 
46
#include <sys/types.h>
 
47
#include <sys/systm.h>
 
48
#endif
 
49
 
 
50
#define VMCI_HANDLE_ARRAY_DEFAULT_SIZE 4
 
51
 
 
52
typedef struct VMCIHandleArray {
 
53
   uint32          capacity;
 
54
   uint32          size;
 
55
   VMCIHandle      entries[1];
 
56
} VMCIHandleArray;
 
57
 
 
58
 
 
59
/*
 
60
 *-----------------------------------------------------------------------------------
 
61
 *
 
62
 * VMCIHandleArray_Create --
 
63
 *
 
64
 * Results:
 
65
 *      Array if successful, NULL if not.
 
66
 *
 
67
 * Side effects:
 
68
 *      None.
 
69
 *
 
70
 *-----------------------------------------------------------------------------------
 
71
 */
 
72
 
 
73
static INLINE VMCIHandleArray *
 
74
VMCIHandleArray_Create(uint32 capacity) 
 
75
{
 
76
   VMCIHandleArray *array;
 
77
   
 
78
   if (capacity == 0) {
 
79
      capacity = VMCI_HANDLE_ARRAY_DEFAULT_SIZE;
 
80
   }
 
81
 
 
82
   array = (VMCIHandleArray *)VMCI_AllocKernelMem(sizeof array->capacity +
 
83
                                                  sizeof array->size +
 
84
                                                  capacity * sizeof(VMCIHandle),
 
85
                                                  VMCI_MEMORY_NONPAGED);
 
86
   if (array == NULL) {
 
87
      return NULL;
 
88
   }
 
89
   array->capacity = capacity;
 
90
   array->size = 0;
 
91
   
 
92
   return array;
 
93
}
 
94
 
 
95
 
 
96
/*
 
97
 *-----------------------------------------------------------------------------------
 
98
 *
 
99
 * VMCIHandleArray_Destroy --
 
100
 *
 
101
 * Results:
 
102
 *      None.
 
103
 *
 
104
 * Side effects:
 
105
 *      None.
 
106
 *
 
107
 *-----------------------------------------------------------------------------------
 
108
 */
 
109
 
 
110
static INLINE void
 
111
VMCIHandleArray_Destroy(VMCIHandleArray *array) 
 
112
{
 
113
   VMCI_FreeKernelMem(array,
 
114
                      sizeof array->capacity + sizeof array->size +
 
115
                      array->capacity * sizeof(VMCIHandle));
 
116
}
 
117
 
 
118
 
 
119
/*
 
120
 *-----------------------------------------------------------------------------------
 
121
 *
 
122
 * VMCIHandleArray_AppendEntry --
 
123
 *
 
124
 * Results:
 
125
 *      None.
 
126
 *
 
127
 * Side effects:
 
128
 *      Array may be reallocated.
 
129
 *
 
130
 *-----------------------------------------------------------------------------------
 
131
 */
 
132
 
 
133
static INLINE void
 
134
VMCIHandleArray_AppendEntry(VMCIHandleArray **arrayPtr,
 
135
                            VMCIHandle handle)
 
136
{
 
137
   VMCIHandleArray *array;
 
138
 
 
139
   ASSERT(arrayPtr && *arrayPtr);
 
140
   array = *arrayPtr;
 
141
 
 
142
   if (UNLIKELY(array->size >= array->capacity)) {
 
143
      /* reallocate. */
 
144
      uint32 arraySize = sizeof array->capacity + sizeof array->size +
 
145
         array->capacity * sizeof(VMCIHandle);
 
146
      VMCIHandleArray *newArray = (VMCIHandleArray *)
 
147
         VMCI_AllocKernelMem(arraySize + array->capacity * sizeof(VMCIHandle),
 
148
                             VMCI_MEMORY_NONPAGED | VMCI_MEMORY_ATOMIC);
 
149
      if (newArray == NULL) {
 
150
         return;
 
151
      }
 
152
      memcpy(newArray, array, arraySize);
 
153
      newArray->capacity *= 2;
 
154
      VMCI_FreeKernelMem(array, arraySize);
 
155
      *arrayPtr = newArray;
 
156
      array = newArray;
 
157
   }
 
158
   array->entries[array->size] = handle;
 
159
   array->size++;
 
160
}
 
161
 
 
162
 
 
163
/*
 
164
 *-----------------------------------------------------------------------------------
 
165
 *
 
166
 * VMCIHandleArray_RemoveEntry --
 
167
 *
 
168
 * Results:
 
169
 *      Handle that was removed, VMCI_INVALID_HANDLE if entry not found.
 
170
 *
 
171
 * Side effects:
 
172
 *      None.
 
173
 *
 
174
 *-----------------------------------------------------------------------------------
 
175
 */
 
176
 
 
177
static INLINE VMCIHandle
 
178
VMCIHandleArray_RemoveEntry(VMCIHandleArray *array,
 
179
                            VMCIHandle entryHandle)
 
180
{
 
181
   uint32 i;
 
182
   VMCIHandle handle = VMCI_INVALID_HANDLE;
 
183
 
 
184
   ASSERT(array);
 
185
   for (i = 0; i < array->size; i++) {
 
186
      if (VMCI_HANDLE_EQUAL(array->entries[i], entryHandle)) {
 
187
         handle = array->entries[i];
 
188
         array->entries[i] = array->entries[array->size-1];
 
189
         array->entries[array->size-1] = VMCI_INVALID_HANDLE;
 
190
         array->size--;
 
191
         break;
 
192
      }
 
193
   }
 
194
 
 
195
   return handle;
 
196
}
 
197
 
 
198
 
 
199
/*
 
200
 *-----------------------------------------------------------------------------------
 
201
 *
 
202
 * VMCIHandleArray_RemoveTail --
 
203
 *
 
204
 * Results:
 
205
 *      Handle that was removed, VMCI_INVALID_HANDLE if array was empty.
 
206
 *
 
207
 * Side effects:
 
208
 *      None.
 
209
 *
 
210
 *-----------------------------------------------------------------------------------
 
211
 */
 
212
 
 
213
static INLINE VMCIHandle
 
214
VMCIHandleArray_RemoveTail(VMCIHandleArray *array)
 
215
{
 
216
   VMCIHandle handle;
 
217
   
 
218
   if (array->size == 0) {
 
219
      return VMCI_INVALID_HANDLE;
 
220
   }
 
221
   handle = array->entries[array->size-1];
 
222
   array->entries[array->size-1] = VMCI_INVALID_HANDLE;
 
223
   array->size--;
 
224
   
 
225
   return handle;
 
226
}
 
227
 
 
228
 
 
229
/*
 
230
 *-----------------------------------------------------------------------------------
 
231
 *
 
232
 * VMCIHandleArray_GetEntry --
 
233
 *
 
234
 * Results:
 
235
 *      Handle at given index, VMCI_INVALID_HANDLE if invalid index.
 
236
 *
 
237
 * Side effects:
 
238
 *      None.
 
239
 *
 
240
 *-----------------------------------------------------------------------------------
 
241
 */
 
242
 
 
243
static INLINE VMCIHandle
 
244
VMCIHandleArray_GetEntry(const VMCIHandleArray *array,
 
245
                         uint32 index)
 
246
{
 
247
   ASSERT(array);
 
248
   if (UNLIKELY(index >= array->size)) {
 
249
      return VMCI_INVALID_HANDLE;
 
250
   }
 
251
 
 
252
  return array->entries[index];
 
253
}
 
254
 
 
255
 
 
256
/*
 
257
 *-----------------------------------------------------------------------------------
 
258
 *
 
259
 * VMCIHandleArray_GetSize --
 
260
 *
 
261
 * Results:
 
262
 *      Number of entries in array.
 
263
 *
 
264
 * Side effects:
 
265
 *      None.
 
266
 *
 
267
 *-----------------------------------------------------------------------------------
 
268
 */
 
269
 
 
270
static INLINE uint32
 
271
VMCIHandleArray_GetSize(const VMCIHandleArray *array)
 
272
{
 
273
   ASSERT(array);
 
274
   return array->size;
 
275
}
 
276
 
 
277
 
 
278
/*
 
279
 *-----------------------------------------------------------------------------------
 
280
 *
 
281
 * VMCIHandleArray_HasEntry --
 
282
 *
 
283
 * Results:
 
284
 *      TRUE is entry exists in array, FALSE if not.
 
285
 *
 
286
 * Side effects:
 
287
 *      None.
 
288
 *
 
289
 *-----------------------------------------------------------------------------------
 
290
 */
 
291
 
 
292
static INLINE Bool
 
293
VMCIHandleArray_HasEntry(const VMCIHandleArray *array,
 
294
                         VMCIHandle entryHandle)
 
295
{
 
296
   uint32 i;
 
297
 
 
298
   ASSERT(array);
 
299
   for (i = 0; i < array->size; i++) {
 
300
      if (VMCI_HANDLE_EQUAL(array->entries[i], entryHandle)) {
 
301
         return TRUE;
 
302
      }
 
303
   }
 
304
 
 
305
   return FALSE;
 
306
}
 
307
 
 
308
 
 
309
/*
 
310
 *-----------------------------------------------------------------------------------
 
311
 *
 
312
 * VMCIHandleArray_GetCopy --
 
313
 *
 
314
 * Results:
 
315
 *      Returns pointer to copy of array on success or NULL, if memory allocation
 
316
 *      fails.
 
317
 *
 
318
 * Side effects:
 
319
 *      Allocates nonpaged memory.
 
320
 *
 
321
 *-----------------------------------------------------------------------------------
 
322
 */
 
323
 
 
324
static INLINE VMCIHandleArray *
 
325
VMCIHandleArray_GetCopy(const VMCIHandleArray *array)
 
326
{
 
327
   VMCIHandleArray *arrayCopy;
 
328
 
 
329
   ASSERT(array);
 
330
   
 
331
   arrayCopy = (VMCIHandleArray *)VMCI_AllocKernelMem(sizeof array->capacity + 
 
332
                                                      sizeof array->size +
 
333
                                                      array->size * sizeof(VMCIHandle),
 
334
                                                      VMCI_MEMORY_NONPAGED |
 
335
                                                         VMCI_MEMORY_ATOMIC);
 
336
   if (arrayCopy != NULL) {
 
337
      memcpy(&arrayCopy->size, &array->size,
 
338
             sizeof array->size + array->size * sizeof(VMCIHandle));
 
339
      arrayCopy->capacity = array->size;
 
340
   }
 
341
 
 
342
   return arrayCopy;
 
343
}
 
344
 
 
345
 
 
346
/*
 
347
 *-----------------------------------------------------------------------------------
 
348
 *
 
349
 * VMCIHandleArray_GetHandles --
 
350
 *
 
351
 * Results:
 
352
 *      NULL if the array is empty. Otherwise, a pointer to the array
 
353
 *      of VMCI handles in the handle array.
 
354
 *
 
355
 * Side effects:
 
356
 *      None.
 
357
 *
 
358
 *-----------------------------------------------------------------------------------
 
359
 */
 
360
 
 
361
static INLINE VMCIHandle *
 
362
VMCIHandleArray_GetHandles(VMCIHandleArray *array) // IN
 
363
{
 
364
   ASSERT(array);
 
365
 
 
366
   if (array->size) {
 
367
      return array->entries;
 
368
   } else {
 
369
      return NULL;
 
370
   }
 
371
}
 
372
 
 
373
#endif // _VMCI_HANDLE_ARRAY_H_