vmci_handle_array.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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(u32 capacity)
  18. {
  19. return VMCI_HANDLE_ARRAY_HEADER_SIZE +
  20. capacity * sizeof(struct vmci_handle);
  21. }
  22. struct vmci_handle_arr *vmci_handle_arr_create(u32 capacity, u32 max_capacity)
  23. {
  24. struct vmci_handle_arr *array;
  25. if (max_capacity == 0 || capacity > max_capacity)
  26. return NULL;
  27. if (capacity == 0)
  28. capacity = min((u32)VMCI_HANDLE_ARRAY_DEFAULT_CAPACITY,
  29. max_capacity);
  30. array = kmalloc(handle_arr_calc_size(capacity), GFP_ATOMIC);
  31. if (!array)
  32. return NULL;
  33. array->capacity = capacity;
  34. array->max_capacity = max_capacity;
  35. array->size = 0;
  36. return array;
  37. }
  38. void vmci_handle_arr_destroy(struct vmci_handle_arr *array)
  39. {
  40. kfree(array);
  41. }
  42. int vmci_handle_arr_append_entry(struct vmci_handle_arr **array_ptr,
  43. struct vmci_handle handle)
  44. {
  45. struct vmci_handle_arr *array = *array_ptr;
  46. if (unlikely(array->size >= array->capacity)) {
  47. /* reallocate. */
  48. struct vmci_handle_arr *new_array;
  49. u32 capacity_bump = min(array->max_capacity - array->capacity,
  50. array->capacity);
  51. size_t new_size = handle_arr_calc_size(array->capacity +
  52. capacity_bump);
  53. if (array->size >= array->max_capacity)
  54. return VMCI_ERROR_NO_MEM;
  55. new_array = krealloc(array, new_size, GFP_ATOMIC);
  56. if (!new_array)
  57. return VMCI_ERROR_NO_MEM;
  58. new_array->capacity += capacity_bump;
  59. *array_ptr = array = new_array;
  60. }
  61. array->entries[array->size] = handle;
  62. array->size++;
  63. return VMCI_SUCCESS;
  64. }
  65. /*
  66. * Handle that was removed, VMCI_INVALID_HANDLE if entry not found.
  67. */
  68. struct vmci_handle vmci_handle_arr_remove_entry(struct vmci_handle_arr *array,
  69. struct vmci_handle entry_handle)
  70. {
  71. struct vmci_handle handle = VMCI_INVALID_HANDLE;
  72. u32 i;
  73. for (i = 0; i < array->size; i++) {
  74. if (vmci_handle_is_equal(array->entries[i], entry_handle)) {
  75. handle = array->entries[i];
  76. array->size--;
  77. array->entries[i] = array->entries[array->size];
  78. array->entries[array->size] = VMCI_INVALID_HANDLE;
  79. break;
  80. }
  81. }
  82. return handle;
  83. }
  84. /*
  85. * Handle that was removed, VMCI_INVALID_HANDLE if array was empty.
  86. */
  87. struct vmci_handle vmci_handle_arr_remove_tail(struct vmci_handle_arr *array)
  88. {
  89. struct vmci_handle handle = VMCI_INVALID_HANDLE;
  90. if (array->size) {
  91. array->size--;
  92. handle = array->entries[array->size];
  93. array->entries[array->size] = VMCI_INVALID_HANDLE;
  94. }
  95. return handle;
  96. }
  97. /*
  98. * Handle at given index, VMCI_INVALID_HANDLE if invalid index.
  99. */
  100. struct vmci_handle
  101. vmci_handle_arr_get_entry(const struct vmci_handle_arr *array, u32 index)
  102. {
  103. if (unlikely(index >= array->size))
  104. return VMCI_INVALID_HANDLE;
  105. return array->entries[index];
  106. }
  107. bool vmci_handle_arr_has_entry(const struct vmci_handle_arr *array,
  108. struct vmci_handle entry_handle)
  109. {
  110. u32 i;
  111. for (i = 0; i < array->size; i++)
  112. if (vmci_handle_is_equal(array->entries[i], entry_handle))
  113. return true;
  114. return false;
  115. }
  116. /*
  117. * NULL if the array is empty. Otherwise, a pointer to the array
  118. * of VMCI handles in the handle array.
  119. */
  120. struct vmci_handle *vmci_handle_arr_get_handles(struct vmci_handle_arr *array)
  121. {
  122. if (array->size)
  123. return array->entries;
  124. return NULL;
  125. }