18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * VMware VMCI Driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2012 VMware, Inc. All rights reserved.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#ifndef _VMCI_HANDLE_ARRAY_H_
98c2ecf20Sopenharmony_ci#define _VMCI_HANDLE_ARRAY_H_
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/vmw_vmci_defs.h>
128c2ecf20Sopenharmony_ci#include <linux/limits.h>
138c2ecf20Sopenharmony_ci#include <linux/types.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_cistruct vmci_handle_arr {
168c2ecf20Sopenharmony_ci	u32 capacity;
178c2ecf20Sopenharmony_ci	u32 max_capacity;
188c2ecf20Sopenharmony_ci	u32 size;
198c2ecf20Sopenharmony_ci	u32 pad;
208c2ecf20Sopenharmony_ci	struct vmci_handle entries[];
218c2ecf20Sopenharmony_ci};
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define VMCI_HANDLE_ARRAY_HEADER_SIZE				\
248c2ecf20Sopenharmony_ci	offsetof(struct vmci_handle_arr, entries)
258c2ecf20Sopenharmony_ci/* Select a default capacity that results in a 64 byte sized array */
268c2ecf20Sopenharmony_ci#define VMCI_HANDLE_ARRAY_DEFAULT_CAPACITY			6
278c2ecf20Sopenharmony_ci/* Make sure that the max array size can be expressed by a u32 */
288c2ecf20Sopenharmony_ci#define VMCI_HANDLE_ARRAY_MAX_CAPACITY				\
298c2ecf20Sopenharmony_ci	((U32_MAX - VMCI_HANDLE_ARRAY_HEADER_SIZE - 1) /	\
308c2ecf20Sopenharmony_ci	sizeof(struct vmci_handle))
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistruct vmci_handle_arr *vmci_handle_arr_create(u32 capacity, u32 max_capacity);
338c2ecf20Sopenharmony_civoid vmci_handle_arr_destroy(struct vmci_handle_arr *array);
348c2ecf20Sopenharmony_ciint vmci_handle_arr_append_entry(struct vmci_handle_arr **array_ptr,
358c2ecf20Sopenharmony_ci				 struct vmci_handle handle);
368c2ecf20Sopenharmony_cistruct vmci_handle vmci_handle_arr_remove_entry(struct vmci_handle_arr *array,
378c2ecf20Sopenharmony_ci						struct vmci_handle
388c2ecf20Sopenharmony_ci						entry_handle);
398c2ecf20Sopenharmony_cistruct vmci_handle vmci_handle_arr_remove_tail(struct vmci_handle_arr *array);
408c2ecf20Sopenharmony_cistruct vmci_handle
418c2ecf20Sopenharmony_civmci_handle_arr_get_entry(const struct vmci_handle_arr *array, u32 index);
428c2ecf20Sopenharmony_cibool vmci_handle_arr_has_entry(const struct vmci_handle_arr *array,
438c2ecf20Sopenharmony_ci			       struct vmci_handle entry_handle);
448c2ecf20Sopenharmony_cistruct vmci_handle *vmci_handle_arr_get_handles(struct vmci_handle_arr *array);
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic inline u32 vmci_handle_arr_get_size(
478c2ecf20Sopenharmony_ci	const struct vmci_handle_arr *array)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	return array->size;
508c2ecf20Sopenharmony_ci}
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#endif /* _VMCI_HANDLE_ARRAY_H_ */
54