flex_array.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. struct flex_array *flex_array_alloc(int element_size, unsigned int total,
  55. gfp_t flags);
  56. int flex_array_prealloc(struct flex_array *fa, unsigned int start,
  57. unsigned int nr_elements, gfp_t flags);
  58. void flex_array_free(struct flex_array *fa);
  59. void flex_array_free_parts(struct flex_array *fa);
  60. int flex_array_put(struct flex_array *fa, unsigned int element_nr, void *src,
  61. gfp_t flags);
  62. int flex_array_clear(struct flex_array *fa, unsigned int element_nr);
  63. void *flex_array_get(struct flex_array *fa, unsigned int element_nr);
  64. int flex_array_shrink(struct flex_array *fa);
  65. #define flex_array_put_ptr(fa, nr, src, gfp) \
  66. flex_array_put(fa, nr, (void *)&(src), gfp)
  67. void *flex_array_get_ptr(struct flex_array *fa, unsigned int element_nr);
  68. #endif /* _FLEX_ARRAY_H */