vmci_handle_array.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * VMware VMCI Driver
  3. *
  4. * Copyright (C) 2012 VMware, Inc. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation version 2 and no later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #include <linux/slab.h>
  16. #include "vmci_handle_array.h"
  17. static size_t handle_arr_calc_size(size_t capacity)
  18. {
  19. return sizeof(struct vmci_handle_arr) +
  20. capacity * sizeof(struct vmci_handle);
  21. }
  22. struct vmci_handle_arr *vmci_handle_arr_create(size_t capacity)
  23. {
  24. struct vmci_handle_arr *array;
  25. if (capacity == 0)
  26. capacity = VMCI_HANDLE_ARRAY_DEFAULT_SIZE;
  27. array = kmalloc(handle_arr_calc_size(capacity), GFP_ATOMIC);
  28. if (!array)
  29. return NULL;
  30. array->capacity = capacity;
  31. array->size = 0;
  32. return array;
  33. }
  34. void vmci_handle_arr_destroy(struct vmci_handle_arr *array)
  35. {
  36. kfree(array);
  37. }
  38. void vmci_handle_arr_append_entry(struct vmci_handle_arr **array_ptr,
  39. struct vmci_handle handle)
  40. {
  41. struct vmci_handle_arr *array = *array_ptr;
  42. if (unlikely(array->size >= array->capacity)) {
  43. /* reallocate. */
  44. struct vmci_handle_arr *new_array;
  45. size_t new_capacity = array->capacity * VMCI_ARR_CAP_MULT;
  46. size_t new_size = handle_arr_calc_size(new_capacity);
  47. new_array = krealloc(array, new_size, GFP_ATOMIC);
  48. if (!new_array)
  49. return;
  50. new_array->capacity = new_capacity;
  51. *array_ptr = array = new_array;
  52. }
  53. array->entries[array->size] = handle;
  54. array->size++;
  55. }
  56. /*
  57. * Handle that was removed, VMCI_INVALID_HANDLE if entry not found.
  58. */
  59. struct vmci_handle vmci_handle_arr_remove_entry(struct vmci_handle_arr *array,
  60. struct vmci_handle entry_handle)
  61. {
  62. struct vmci_handle handle = VMCI_INVALID_HANDLE;
  63. size_t i;
  64. for (i = 0; i < array->size; i++) {
  65. if (vmci_handle_is_equal(array->entries[i], entry_handle)) {
  66. handle = array->entries[i];
  67. array->size--;
  68. array->entries[i] = array->entries[array->size];
  69. array->entries[array->size] = VMCI_INVALID_HANDLE;
  70. break;
  71. }
  72. }
  73. return handle;
  74. }
  75. /*
  76. * Handle that was removed, VMCI_INVALID_HANDLE if array was empty.
  77. */
  78. struct vmci_handle vmci_handle_arr_remove_tail(struct vmci_handle_arr *array)
  79. {
  80. struct vmci_handle handle = VMCI_INVALID_HANDLE;
  81. if (array->size) {
  82. array->size--;
  83. handle = array->entries[array->size];
  84. array->entries[array->size] = VMCI_INVALID_HANDLE;
  85. }
  86. return handle;
  87. }
  88. /*
  89. * Handle at given index, VMCI_INVALID_HANDLE if invalid index.
  90. */
  91. struct vmci_handle
  92. vmci_handle_arr_get_entry(const struct vmci_handle_arr *array, size_t index)
  93. {
  94. if (unlikely(index >= array->size))
  95. return VMCI_INVALID_HANDLE;
  96. return array->entries[index];
  97. }
  98. bool vmci_handle_arr_has_entry(const struct vmci_handle_arr *array,
  99. struct vmci_handle entry_handle)
  100. {
  101. size_t i;
  102. for (i = 0; i < array->size; i++)
  103. if (vmci_handle_is_equal(array->entries[i], entry_handle))
  104. return true;
  105. return false;
  106. }
  107. /*
  108. * NULL if the array is empty. Otherwise, a pointer to the array
  109. * of VMCI handles in the handle array.
  110. */
  111. struct vmci_handle *vmci_handle_arr_get_handles(struct vmci_handle_arr *array)
  112. {
  113. if (array->size)
  114. return array->entries;
  115. return NULL;
  116. }