lf_dynarray.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License, version 2.0,
  4. as published by the Free Software Foundation.
  5. This program is also distributed with certain software (including
  6. but not limited to OpenSSL) that is licensed under separate terms,
  7. as designated in a particular file or component or in included license
  8. documentation. The authors of MySQL hereby grant you an additional
  9. permission to link the program and your derivative works with the
  10. separately licensed software that they have included with MySQL.
  11. Without limiting anything contained in the foregoing, this file,
  12. which is part of C Driver for MySQL (Connector/C), is also subject to the
  13. Universal FOSS Exception, version 1.0, a copy of which can be found at
  14. http://oss.oracle.com/licenses/universal-foss-exception.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License, version 2.0, for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  22. /**
  23. @file mysys/lf_dynarray.cc
  24. Analog of DYNAMIC_ARRAY that never reallocs
  25. (so no pointer into the array may ever become invalid).
  26. Memory is allocated in non-contiguous chunks.
  27. This data structure is not space efficient for sparse arrays.
  28. Every element is aligned to sizeof(element) boundary
  29. (to avoid false sharing if element is big enough).
  30. LF_DYNARRAY is a recursive structure. On the zero level
  31. LF_DYNARRAY::level[0] it's an array of LF_DYNARRAY_LEVEL_LENGTH elements,
  32. on the first level it's an array of LF_DYNARRAY_LEVEL_LENGTH pointers
  33. to arrays of elements, on the second level it's an array of pointers
  34. to arrays of pointers to arrays of elements. And so on.
  35. With four levels the number of elements is limited to 4311810304
  36. (but as in all functions index is uint, the real limit is 2^32-1)
  37. Actually, it's wait-free, not lock-free ;-)
  38. */
  39. #include <string.h>
  40. #include <sys/types.h>
  41. #include <algorithm>
  42. #include "lf.h"
  43. #include "my_compiler.h"
  44. #include "my_inttypes.h"
  45. #include "my_macros.h"
  46. #include "my_sys.h"
  47. #include "mysql/service_mysql_alloc.h"
  48. #include "mysys/mysys_priv.h"
  49. void lf_dynarray_init(LF_DYNARRAY *array, uint element_size) {
  50. std::fill(begin(array->level), end(array->level), nullptr);
  51. array->size_of_element = element_size;
  52. }
  53. static void recursive_free(std::atomic<void *> *alloc, int level) {
  54. if (!alloc) {
  55. return;
  56. }
  57. if (level) {
  58. int i;
  59. for (i = 0; i < LF_DYNARRAY_LEVEL_LENGTH; i++)
  60. recursive_free(static_cast<std::atomic<void *> *>(alloc[i].load()),
  61. level - 1);
  62. my_free(alloc);
  63. } else {
  64. my_free(alloc[-1]);
  65. }
  66. }
  67. void lf_dynarray_destroy(LF_DYNARRAY *array) {
  68. int i;
  69. for (i = 0; i < LF_DYNARRAY_LEVELS; i++)
  70. recursive_free(static_cast<std::atomic<void *> *>(array->level[i].load()),
  71. i);
  72. }
  73. static const ulong dynarray_idxes_in_prev_levels[LF_DYNARRAY_LEVELS] = {
  74. 0, /* +1 here to to avoid -1's below */
  75. LF_DYNARRAY_LEVEL_LENGTH,
  76. LF_DYNARRAY_LEVEL_LENGTH *LF_DYNARRAY_LEVEL_LENGTH +
  77. LF_DYNARRAY_LEVEL_LENGTH,
  78. LF_DYNARRAY_LEVEL_LENGTH *LF_DYNARRAY_LEVEL_LENGTH
  79. *LF_DYNARRAY_LEVEL_LENGTH +
  80. LF_DYNARRAY_LEVEL_LENGTH *LF_DYNARRAY_LEVEL_LENGTH +
  81. LF_DYNARRAY_LEVEL_LENGTH};
  82. static const ulong dynarray_idxes_in_prev_level[LF_DYNARRAY_LEVELS] = {
  83. 0, /* +1 here to to avoid -1's below */
  84. LF_DYNARRAY_LEVEL_LENGTH,
  85. LF_DYNARRAY_LEVEL_LENGTH *LF_DYNARRAY_LEVEL_LENGTH,
  86. LF_DYNARRAY_LEVEL_LENGTH *LF_DYNARRAY_LEVEL_LENGTH
  87. *LF_DYNARRAY_LEVEL_LENGTH,
  88. };
  89. /*
  90. Returns a valid lvalue pointer to the element number 'idx'.
  91. Allocates memory if necessary.
  92. */
  93. void *lf_dynarray_lvalue(LF_DYNARRAY *array, uint idx) {
  94. void *ptr;
  95. int i;
  96. for (i = LF_DYNARRAY_LEVELS - 1; idx < dynarray_idxes_in_prev_levels[i]; i--)
  97. /* no-op */;
  98. std::atomic<void *> *ptr_ptr = &array->level[i];
  99. idx -= dynarray_idxes_in_prev_levels[i];
  100. for (; i > 0; i--) {
  101. if (!(ptr = *ptr_ptr)) {
  102. void *alloc = my_malloc(key_memory_lf_dynarray,
  103. LF_DYNARRAY_LEVEL_LENGTH * sizeof(void *),
  104. MYF(MY_WME | MY_ZEROFILL));
  105. if (unlikely(!alloc)) {
  106. return (NULL);
  107. }
  108. if (atomic_compare_exchange_strong(ptr_ptr, &ptr, alloc)) {
  109. ptr = alloc;
  110. } else {
  111. my_free(alloc);
  112. }
  113. }
  114. ptr_ptr =
  115. ((std::atomic<void *> *)ptr) + idx / dynarray_idxes_in_prev_level[i];
  116. idx %= dynarray_idxes_in_prev_level[i];
  117. }
  118. if (!(ptr = *ptr_ptr)) {
  119. uchar *alloc, *data;
  120. alloc = static_cast<uchar *>(
  121. my_malloc(key_memory_lf_dynarray,
  122. LF_DYNARRAY_LEVEL_LENGTH * array->size_of_element +
  123. MY_MAX(array->size_of_element, sizeof(void *)),
  124. MYF(MY_WME | MY_ZEROFILL)));
  125. if (unlikely(!alloc)) {
  126. return (NULL);
  127. }
  128. /* reserve the space for free() address */
  129. data = alloc + sizeof(void *);
  130. {
  131. /* alignment */
  132. intptr mod = ((intptr)data) % array->size_of_element;
  133. if (mod) {
  134. data += array->size_of_element - mod;
  135. }
  136. }
  137. ((void **)data)[-1] = alloc; /* free() will need the original pointer */
  138. if (atomic_compare_exchange_strong(ptr_ptr, &ptr,
  139. static_cast<void *>(data))) {
  140. ptr = data;
  141. } else {
  142. my_free(alloc);
  143. }
  144. }
  145. return ((uchar *)ptr) + array->size_of_element * idx;
  146. }
  147. /*
  148. Returns a pointer to the element number 'idx'
  149. or NULL if an element does not exists
  150. */
  151. void *lf_dynarray_value(LF_DYNARRAY *array, uint idx) {
  152. void *ptr;
  153. int i;
  154. for (i = LF_DYNARRAY_LEVELS - 1; idx < dynarray_idxes_in_prev_levels[i]; i--)
  155. /* no-op */;
  156. std::atomic<void *> *ptr_ptr = &array->level[i];
  157. idx -= dynarray_idxes_in_prev_levels[i];
  158. for (; i > 0; i--) {
  159. if (!(ptr = *ptr_ptr)) {
  160. return (NULL);
  161. }
  162. ptr_ptr =
  163. ((std::atomic<void *> *)ptr) + idx / dynarray_idxes_in_prev_level[i];
  164. idx %= dynarray_idxes_in_prev_level[i];
  165. }
  166. if (!(ptr = *ptr_ptr)) {
  167. return (NULL);
  168. }
  169. return ((uchar *)ptr) + array->size_of_element * idx;
  170. }
  171. static int recursive_iterate(LF_DYNARRAY *array, void *ptr, int level,
  172. lf_dynarray_func func, void *arg) {
  173. int res, i;
  174. if (!ptr) {
  175. return 0;
  176. }
  177. if (!level) {
  178. return func(ptr, arg);
  179. }
  180. for (i = 0; i < LF_DYNARRAY_LEVEL_LENGTH; i++)
  181. if ((res = recursive_iterate(array, ((void **)ptr)[i], level - 1, func,
  182. arg))) {
  183. return res;
  184. }
  185. return 0;
  186. }
  187. /*
  188. Calls func(array, arg) on every array of LF_DYNARRAY_LEVEL_LENGTH elements
  189. in lf_dynarray.
  190. DESCRIPTION
  191. lf_dynarray consists of a set of arrays, LF_DYNARRAY_LEVEL_LENGTH elements
  192. each. lf_dynarray_iterate() calls user-supplied function on every array
  193. from the set. It is the fastest way to scan the array, faster than
  194. for (i=0; i < N; i++) { func(lf_dynarray_value(dynarray, i)); }
  195. NOTE
  196. if func() returns non-zero, the scan is aborted
  197. */
  198. int lf_dynarray_iterate(LF_DYNARRAY *array, lf_dynarray_func func, void *arg) {
  199. int i, res;
  200. for (i = 0; i < LF_DYNARRAY_LEVELS; i++)
  201. if ((res = recursive_iterate(array, array->level[i], i, func, arg))) {
  202. return res;
  203. }
  204. return 0;
  205. }