flex_array.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #ifndef _FLEX_ARRAY_H
  2. #define _FLEX_ARRAY_H
  3. #include <linux/types.h>
  4. #include <linux/reciprocal_div.h>
  5. #include <asm/page.h>
  6. #define FLEX_ARRAY_PART_SIZE PAGE_SIZE
  7. #define FLEX_ARRAY_BASE_SIZE PAGE_SIZE
  8. struct flex_array_part;
  9. /*
  10. * This is meant to replace cases where an array-like
  11. * structure has gotten too big to fit into kmalloc()
  12. * and the developer is getting tempted to use
  13. * vmalloc().
  14. */
  15. struct flex_array {
  16. union {
  17. struct {
  18. int element_size;
  19. int total_nr_elements;
  20. int elems_per_part;
  21. struct reciprocal_value reciprocal_elems;
  22. struct flex_array_part *parts[];
  23. };
  24. /*
  25. * This little trick makes sure that
  26. * sizeof(flex_array) == PAGE_SIZE
  27. */
  28. char padding[FLEX_ARRAY_BASE_SIZE];
  29. };
  30. };
  31. /* Number of bytes left in base struct flex_array, excluding metadata */
  32. #define FLEX_ARRAY_BASE_BYTES_LEFT \
  33. (FLEX_ARRAY_BASE_SIZE - offsetof(struct flex_array, parts))
  34. /* Number of pointers in base to struct flex_array_part pages */
  35. #define FLEX_ARRAY_NR_BASE_PTRS \
  36. (FLEX_ARRAY_BASE_BYTES_LEFT / sizeof(struct flex_array_part *))
  37. /* Number of elements of size that fit in struct flex_array_part */
  38. #define FLEX_ARRAY_ELEMENTS_PER_PART(size) \
  39. (FLEX_ARRAY_PART_SIZE / size)
  40. /*
  41. * Defines a statically allocated flex array and ensures its parameters are
  42. * valid.
  43. */
  44. #define DEFINE_FLEX_ARRAY(__arrayname, __element_size, __total) \
  45. struct flex_array __arrayname = { { { \
  46. .element_size = (__element_size), \
  47. .total_nr_elements = (__total), \
  48. } } }; \
  49. static inline void __arrayname##_invalid_parameter(void) \
  50. { \
  51. BUILD_BUG_ON((__total) > FLEX_ARRAY_NR_BASE_PTRS * \
  52. FLEX_ARRAY_ELEMENTS_PER_PART(__element_size)); \
  53. }
  54. /**
  55. * flex_array_alloc() - Creates a flexible array.
  56. * @element_size: individual object size.
  57. * @total: maximum number of objects which can be stored.
  58. * @flags: GFP flags
  59. *
  60. * Return: Returns an object of structure flex_array.
  61. */
  62. struct flex_array *flex_array_alloc(int element_size, unsigned int total,
  63. gfp_t flags);
  64. /**
  65. * flex_array_prealloc() - Ensures that memory for the elements indexed in the
  66. * range defined by start and nr_elements has been allocated.
  67. * @fa: array to allocate memory to.
  68. * @start: start address
  69. * @nr_elements: number of elements to be allocated.
  70. * @flags: GFP flags
  71. *
  72. */
  73. int flex_array_prealloc(struct flex_array *fa, unsigned int start,
  74. unsigned int nr_elements, gfp_t flags);
  75. /**
  76. * flex_array_free() - Removes all elements of a flexible array.
  77. * @fa: array to be freed.
  78. */
  79. void flex_array_free(struct flex_array *fa);
  80. /**
  81. * flex_array_free_parts() - Removes all elements of a flexible array, but
  82. * leaves the array itself in place.
  83. * @fa: array to be emptied.
  84. */
  85. void flex_array_free_parts(struct flex_array *fa);
  86. /**
  87. * flex_array_put() - Stores data into a flexible array.
  88. * @fa: array where element is to be stored.
  89. * @element_nr: position to copy, must be less than the maximum specified when
  90. * the array was created.
  91. * @src: data source to be copied into the array.
  92. * @flags: GFP flags
  93. *
  94. * Return: Returns zero on success, a negative error code otherwise.
  95. */
  96. int flex_array_put(struct flex_array *fa, unsigned int element_nr, void *src,
  97. gfp_t flags);
  98. /**
  99. * flex_array_clear() - Clears an individual element in the array, sets the
  100. * given element to FLEX_ARRAY_FREE.
  101. * @element_nr: element position to clear.
  102. * @fa: array to which element to be cleared belongs.
  103. *
  104. * Return: Returns zero on success, -EINVAL otherwise.
  105. */
  106. int flex_array_clear(struct flex_array *fa, unsigned int element_nr);
  107. /**
  108. * flex_array_get() - Retrieves data into a flexible array.
  109. *
  110. * @element_nr: Element position to retrieve data from.
  111. * @fa: array from which data is to be retrieved.
  112. *
  113. * Return: Returns a pointer to the data element, or NULL if that
  114. * particular element has never been allocated.
  115. */
  116. void *flex_array_get(struct flex_array *fa, unsigned int element_nr);
  117. /**
  118. * flex_array_shrink() - Reduces the allocated size of an array.
  119. * @fa: array to shrink.
  120. *
  121. * Return: Returns number of pages of memory actually freed.
  122. *
  123. */
  124. int flex_array_shrink(struct flex_array *fa);
  125. #define flex_array_put_ptr(fa, nr, src, gfp) \
  126. flex_array_put(fa, nr, (void *)&(src), gfp)
  127. void *flex_array_get_ptr(struct flex_array *fa, unsigned int element_nr);
  128. #endif /* _FLEX_ARRAY_H */