From: George Zhang <georgezhang@vmware.com>
To: linux-kernel@vger.kernel.org, georgezhang@vmware.com,
virtualization@lists.linux-foundation.org
Cc: pv-drivers@vmware.com, gregkh@linuxfoundation.org
Subject: [PATCH 06/12] VMCI: handle array implementation.
Date: Wed, 07 Nov 2012 10:42:05 -0800 [thread overview]
Message-ID: <20121107184200.9658.72587.stgit@promb-2n-dhcp175.eng.vmware.com> (raw)
In-Reply-To: <20121107183624.9658.78903.stgit@promb-2n-dhcp175.eng.vmware.com>
VMCI handle code adds support for dynamic arrays that will grow if they need to.
Signed-off-by: George Zhang <georgezhang@vmware.com>
---
drivers/misc/vmw_vmci/vmci_handle_array.c | 142 +++++++++++++++++++++++++++++
drivers/misc/vmw_vmci/vmci_handle_array.h | 52 +++++++++++
2 files changed, 194 insertions(+), 0 deletions(-)
create mode 100644 drivers/misc/vmw_vmci/vmci_handle_array.c
create mode 100644 drivers/misc/vmw_vmci/vmci_handle_array.h
diff --git a/drivers/misc/vmw_vmci/vmci_handle_array.c b/drivers/misc/vmw_vmci/vmci_handle_array.c
new file mode 100644
index 0000000..9122373
--- /dev/null
+++ b/drivers/misc/vmw_vmci/vmci_handle_array.c
@@ -0,0 +1,142 @@
+/*
+ * VMware VMCI Driver
+ *
+ * Copyright (C) 2012 VMware, Inc. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation version 2 and no later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ */
+
+#include <linux/slab.h>
+#include "vmci_handle_array.h"
+
+static size_t handle_arr_calc_size(size_t capacity)
+{
+ return sizeof(struct vmci_handle_arr) +
+ capacity * sizeof(struct vmci_handle);
+}
+
+struct vmci_handle_arr *vmci_handle_arr_create(size_t capacity)
+{
+ struct vmci_handle_arr *array;
+
+ if (capacity == 0)
+ capacity = VMCI_HANDLE_ARRAY_DEFAULT_SIZE;
+
+ array = kmalloc(handle_arr_calc_size(capacity), GFP_ATOMIC);
+ if (!array)
+ return NULL;
+
+ array->capacity = capacity;
+ array->size = 0;
+
+ return array;
+}
+
+void vmci_handle_arr_destroy(struct vmci_handle_arr *array)
+{
+ kfree(array);
+}
+
+void vmci_handle_arr_append_entry(struct vmci_handle_arr **array_ptr,
+ struct vmci_handle handle)
+{
+ struct vmci_handle_arr *array = *array_ptr;
+
+ if (unlikely(array->size >= array->capacity)) {
+ /* reallocate. */
+ struct vmci_handle_arr *new_array;
+ size_t new_capacity = array->capacity * VMCI_ARR_CAP_MULT;
+ size_t new_size = handle_arr_calc_size(new_capacity);
+
+ new_array = krealloc(array, new_size, GFP_ATOMIC);
+ if (!new_array)
+ return;
+
+ new_array->capacity = new_capacity;
+ *array_ptr = array = new_array;
+ }
+
+ array->entries[array->size] = handle;
+ array->size++;
+}
+
+/*
+ * Handle that was removed, VMCI_INVALID_HANDLE if entry not found.
+ */
+struct vmci_handle vmci_handle_arr_remove_entry(struct vmci_handle_arr *array,
+ struct vmci_handle entry_handle)
+{
+ struct vmci_handle handle = VMCI_INVALID_HANDLE;
+ size_t i;
+
+ for (i = 0; i < array->size; i++) {
+ if (VMCI_HANDLE_EQUAL(array->entries[i], entry_handle)) {
+ handle = array->entries[i];
+ array->size--;
+ array->entries[i] = array->entries[array->size];
+ array->entries[array->size] = VMCI_INVALID_HANDLE;
+ break;
+ }
+ }
+
+ return handle;
+}
+
+/*
+ * Handle that was removed, VMCI_INVALID_HANDLE if array was empty.
+ */
+struct vmci_handle vmci_handle_arr_remove_tail(struct vmci_handle_arr *array)
+{
+ struct vmci_handle handle = VMCI_INVALID_HANDLE;
+
+ if (array->size) {
+ array->size--;
+ handle = array->entries[array->size];
+ array->entries[array->size] = VMCI_INVALID_HANDLE;
+ }
+
+ return handle;
+}
+
+/*
+ * Handle at given index, VMCI_INVALID_HANDLE if invalid index.
+ */
+struct vmci_handle
+vmci_handle_arr_get_entry(const struct vmci_handle_arr *array, size_t index)
+{
+ if (unlikely(index >= array->size))
+ return VMCI_INVALID_HANDLE;
+
+ return array->entries[index];
+}
+
+bool vmci_handle_arr_has_entry(const struct vmci_handle_arr *array,
+ struct vmci_handle entry_handle)
+{
+ size_t i;
+
+ for (i = 0; i < array->size; i++)
+ if (VMCI_HANDLE_EQUAL(array->entries[i], entry_handle))
+ return true;
+
+ return false;
+}
+
+/*
+ * NULL if the array is empty. Otherwise, a pointer to the array
+ * of VMCI handles in the handle array.
+ */
+struct vmci_handle *vmci_handle_arr_get_handles(struct vmci_handle_arr *array)
+{
+ if (array->size)
+ return array->entries;
+
+ return NULL;
+}
diff --git a/drivers/misc/vmw_vmci/vmci_handle_array.h b/drivers/misc/vmw_vmci/vmci_handle_array.h
new file mode 100644
index 0000000..b5f3a7f
--- /dev/null
+++ b/drivers/misc/vmw_vmci/vmci_handle_array.h
@@ -0,0 +1,52 @@
+/*
+ * VMware VMCI Driver
+ *
+ * Copyright (C) 2012 VMware, Inc. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation version 2 and no later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ */
+
+#ifndef _VMCI_HANDLE_ARRAY_H_
+#define _VMCI_HANDLE_ARRAY_H_
+
+#include <linux/vmw_vmci_defs.h>
+#include <linux/types.h>
+
+#define VMCI_HANDLE_ARRAY_DEFAULT_SIZE 4
+#define VMCI_ARR_CAP_MULT 2 /* Array capacity multiplier */
+
+struct vmci_handle_arr {
+ size_t capacity;
+ size_t size;
+ struct vmci_handle entries[];
+};
+
+struct vmci_handle_arr *vmci_handle_arr_create(size_t capacity);
+void vmci_handle_arr_destroy(struct vmci_handle_arr *array);
+void vmci_handle_arr_append_entry(struct vmci_handle_arr **array_ptr,
+ struct vmci_handle handle);
+struct vmci_handle vmci_handle_arr_remove_entry(struct vmci_handle_arr *array,
+ struct vmci_handle
+ entry_handle);
+struct vmci_handle vmci_handle_arr_remove_tail(struct vmci_handle_arr *array);
+struct vmci_handle
+vmci_handle_arr_get_entry(const struct vmci_handle_arr *array, size_t index);
+bool vmci_handle_arr_has_entry(const struct vmci_handle_arr *array,
+ struct vmci_handle entry_handle);
+struct vmci_handle *vmci_handle_arr_get_handles(struct vmci_handle_arr *array);
+
+static inline size_t vmci_handle_arr_get_size(
+ const struct vmci_handle_arr *array)
+{
+ return array->size;
+}
+
+
+#endif /* _VMCI_HANDLE_ARRAY_H_ */
next prev parent reply other threads:[~2012-11-07 18:42 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-11-07 18:40 [PATCH 00/12] VMCI for Linux upstreaming George Zhang
2012-11-07 18:41 ` [PATCH 01/12] VMCI: context implementation George Zhang
2012-11-07 18:41 ` [PATCH 02/12] VMCI: datagram implementation George Zhang
2012-11-15 23:47 ` Greg KH
2012-11-07 18:41 ` [PATCH 03/12] VMCI: doorbell implementation George Zhang
2012-11-07 18:41 ` [PATCH 04/12] VMCI: device driver implementaton George Zhang
2012-11-07 18:41 ` [PATCH 05/12] VMCI: event handling implementation George Zhang
2012-11-07 18:42 ` George Zhang [this message]
2012-11-07 18:42 ` [PATCH 07/12] VMCI: queue pairs implementation George Zhang
2012-11-15 23:48 ` Greg KH
2012-11-07 18:42 ` [PATCH 08/12] VMCI: resource object implementation George Zhang
2012-11-07 18:42 ` [PATCH 09/12] VMCI: routing implementation George Zhang
2012-11-07 18:42 ` [PATCH 10/12] VMCI: guest side driver implementation George Zhang
2012-11-07 18:42 ` [PATCH 11/12] VMCI: host " George Zhang
2012-11-16 0:03 ` Greg KH
2012-11-07 18:43 ` [PATCH 12/12] VMCI: Some header and config files George Zhang
2012-11-16 0:01 ` Greg KH
2012-11-27 0:03 ` Greg KH
[not found] ` <20121127000304.GA18680@kroah.com>
2012-11-27 0:23 ` Dmitry Torokhov
2012-11-27 0:32 ` Greg KH
2012-11-27 0:45 ` [Pv-drivers] " Dmitry Torokhov
2012-11-30 16:47 ` Andy King
[not found] ` <1481496655.36482563.1354294066563.JavaMail.root@vmware.com>
2012-11-30 17:09 ` Greg KH
2012-11-30 17:20 ` [Pv-drivers] " Dmitry Torokhov
2012-11-30 18:39 ` Greg KH
[not found] ` <20121130183918.GA22577@kroah.com>
2012-11-30 18:45 ` Dmitry Torokhov
2012-11-30 18:57 ` Greg KH
2012-11-30 20:09 ` Dmitry Torokhov
2012-11-30 20:44 ` Greg KH
[not found] ` <20121130204406.GB32321@kroah.com>
2012-11-30 20:58 ` Dmitry Torokhov
2012-11-30 21:17 ` Greg KH
-- strict thread matches above, loose matches on Subject: below --
2013-01-08 23:52 [PATCH 00/12] VMCI for Linux upstreaming George Zhang
2013-01-08 23:54 ` [PATCH 06/12] VMCI: handle array implementation George Zhang
2012-11-21 20:31 [PATCH 00/12] VMCI for Linux upstreaming George Zhang
2012-11-21 20:34 ` [PATCH 06/12] VMCI: handle array implementation George Zhang
2012-11-01 17:28 [PATCH 00/12] VMCI for Linux upstreaming George Zhang
2012-11-01 17:29 ` [PATCH 06/12] VMCI: handle array implementation George Zhang
2012-10-30 1:03 [PATCH 00/12] VMCI for Linux upstreaming George Zhang
2012-10-30 1:04 ` [PATCH 06/12] VMCI: handle array implementation George Zhang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20121107184200.9658.72587.stgit@promb-2n-dhcp175.eng.vmware.com \
--to=georgezhang@vmware.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pv-drivers@vmware.com \
--cc=virtualization@lists.linux-foundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).