1
/*********************************************************
2
* Copyright (C) 2006 VMware, Inc. All rights reserved.
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.
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
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
17
*********************************************************/
20
* vmci_handle_array.h --
22
* Simple dynamic array.
25
#ifndef _VMCI_HANDLE_ARRAY_H_
26
#define _VMCI_HANDLE_ARRAY_H_
28
#define INCLUDE_ALLOW_VMMON
29
#define INCLUDE_ALLOW_MODULE
30
#define INCLUDE_ALLOW_VMCORE
31
#define INCLUDE_ALLOW_VMKERNEL
32
#include "includeCheck.h"
34
#include "vmci_kernel_if.h"
37
#include "vmci_defs.h"
38
#include "vm_assert.h"
46
#include <sys/types.h>
47
#include <sys/systm.h>
50
#define VMCI_HANDLE_ARRAY_DEFAULT_SIZE 4
52
typedef struct VMCIHandleArray {
55
VMCIHandle entries[1];
60
*-----------------------------------------------------------------------------------
62
* VMCIHandleArray_Create --
65
* Array if successful, NULL if not.
70
*-----------------------------------------------------------------------------------
73
static INLINE VMCIHandleArray *
74
VMCIHandleArray_Create(uint32 capacity)
76
VMCIHandleArray *array;
79
capacity = VMCI_HANDLE_ARRAY_DEFAULT_SIZE;
82
array = (VMCIHandleArray *)VMCI_AllocKernelMem(sizeof array->capacity +
84
capacity * sizeof(VMCIHandle),
85
VMCI_MEMORY_NONPAGED);
89
array->capacity = capacity;
97
*-----------------------------------------------------------------------------------
99
* VMCIHandleArray_Destroy --
107
*-----------------------------------------------------------------------------------
111
VMCIHandleArray_Destroy(VMCIHandleArray *array)
113
VMCI_FreeKernelMem(array,
114
sizeof array->capacity + sizeof array->size +
115
array->capacity * sizeof(VMCIHandle));
120
*-----------------------------------------------------------------------------------
122
* VMCIHandleArray_AppendEntry --
128
* Array may be reallocated.
130
*-----------------------------------------------------------------------------------
134
VMCIHandleArray_AppendEntry(VMCIHandleArray **arrayPtr,
137
VMCIHandleArray *array;
139
ASSERT(arrayPtr && *arrayPtr);
142
if (UNLIKELY(array->size >= array->capacity)) {
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) {
152
memcpy(newArray, array, arraySize);
153
newArray->capacity *= 2;
154
VMCI_FreeKernelMem(array, arraySize);
155
*arrayPtr = newArray;
158
array->entries[array->size] = handle;
164
*-----------------------------------------------------------------------------------
166
* VMCIHandleArray_RemoveEntry --
169
* Handle that was removed, VMCI_INVALID_HANDLE if entry not found.
174
*-----------------------------------------------------------------------------------
177
static INLINE VMCIHandle
178
VMCIHandleArray_RemoveEntry(VMCIHandleArray *array,
179
VMCIHandle entryHandle)
182
VMCIHandle handle = VMCI_INVALID_HANDLE;
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;
200
*-----------------------------------------------------------------------------------
202
* VMCIHandleArray_RemoveTail --
205
* Handle that was removed, VMCI_INVALID_HANDLE if array was empty.
210
*-----------------------------------------------------------------------------------
213
static INLINE VMCIHandle
214
VMCIHandleArray_RemoveTail(VMCIHandleArray *array)
218
if (array->size == 0) {
219
return VMCI_INVALID_HANDLE;
221
handle = array->entries[array->size-1];
222
array->entries[array->size-1] = VMCI_INVALID_HANDLE;
230
*-----------------------------------------------------------------------------------
232
* VMCIHandleArray_GetEntry --
235
* Handle at given index, VMCI_INVALID_HANDLE if invalid index.
240
*-----------------------------------------------------------------------------------
243
static INLINE VMCIHandle
244
VMCIHandleArray_GetEntry(const VMCIHandleArray *array,
248
if (UNLIKELY(index >= array->size)) {
249
return VMCI_INVALID_HANDLE;
252
return array->entries[index];
257
*-----------------------------------------------------------------------------------
259
* VMCIHandleArray_GetSize --
262
* Number of entries in array.
267
*-----------------------------------------------------------------------------------
271
VMCIHandleArray_GetSize(const VMCIHandleArray *array)
279
*-----------------------------------------------------------------------------------
281
* VMCIHandleArray_HasEntry --
284
* TRUE is entry exists in array, FALSE if not.
289
*-----------------------------------------------------------------------------------
293
VMCIHandleArray_HasEntry(const VMCIHandleArray *array,
294
VMCIHandle entryHandle)
299
for (i = 0; i < array->size; i++) {
300
if (VMCI_HANDLE_EQUAL(array->entries[i], entryHandle)) {
310
*-----------------------------------------------------------------------------------
312
* VMCIHandleArray_GetCopy --
315
* Returns pointer to copy of array on success or NULL, if memory allocation
319
* Allocates nonpaged memory.
321
*-----------------------------------------------------------------------------------
324
static INLINE VMCIHandleArray *
325
VMCIHandleArray_GetCopy(const VMCIHandleArray *array)
327
VMCIHandleArray *arrayCopy;
331
arrayCopy = (VMCIHandleArray *)VMCI_AllocKernelMem(sizeof array->capacity +
333
array->size * sizeof(VMCIHandle),
334
VMCI_MEMORY_NONPAGED |
336
if (arrayCopy != NULL) {
337
memcpy(&arrayCopy->size, &array->size,
338
sizeof array->size + array->size * sizeof(VMCIHandle));
339
arrayCopy->capacity = array->size;
347
*-----------------------------------------------------------------------------------
349
* VMCIHandleArray_GetHandles --
352
* NULL if the array is empty. Otherwise, a pointer to the array
353
* of VMCI handles in the handle array.
358
*-----------------------------------------------------------------------------------
361
static INLINE VMCIHandle *
362
VMCIHandleArray_GetHandles(VMCIHandleArray *array) // IN
367
return array->entries;
373
#endif // _VMCI_HANDLE_ARRAY_H_