flex_array.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #ifndef _FLEX_ARRAY_H
  2. #define _FLEX_ARRAY_H
  3. #include <linux/types.h>
  4. #include <asm/page.h>
  5. #define FLEX_ARRAY_PART_SIZE PAGE_SIZE
  6. #define FLEX_ARRAY_BASE_SIZE PAGE_SIZE
  7. struct flex_array_part;
  8. /*
  9. * This is meant to replace cases where an array-like
  10. * structure has gotten too big to fit into kmalloc()
  11. * and the developer is getting tempted to use
  12. * vmalloc().
  13. */
  14. struct flex_array {
  15. union {
  16. struct {
  17. int element_size;
  18. int total_nr_elements;
  19. int elems_per_part;
  20. u32 reciprocal_elems;
  21. struct flex_array_part *parts[];
  22. };
  23. /*
  24. * This little trick makes sure that
  25. * sizeof(flex_array) == PAGE_SIZE
  26. */
  27. char padding[FLEX_ARRAY_BASE_SIZE];
  28. };
  29. };
  30. /* Number of bytes left in base struct flex_array, excluding metadata */
  31. #define FLEX_ARRAY_BASE_BYTES_LEFT \
  32. (FLEX_ARRAY_BASE_SIZE - offsetof(struct flex_array, parts))
  33. /* Number of pointers in base to struct flex_array_part pages */
  34. #define FLEX_ARRAY_NR_BASE_PTRS \
  35. (FLEX_ARRAY_BASE_BYTES_LEFT / sizeof(struct flex_array_part *))
  36. /* Number of elements of size that fit in struct flex_array_part */
  37. #define FLEX_ARRAY_ELEMENTS_PER_PART(size) \
  38. (FLEX_ARRAY_PART_SIZE / size)
  39. /*
  40. * Defines a statically allocated flex array and ensures its parameters are
  41. * valid.
  42. */
  43. #define DEFINE_FLEX_ARRAY(__arrayname, __element_size, __total) \
  44. struct flex_array __arrayname = { { { \
  45. .element_size = (__element_size), \
  46. .total_nr_elements = (__total), \
  47. } } }; \
  48. static inline void __arrayname##_invalid_parameter(void) \
  49. { \
  50. BUILD_BUG_ON((__total) > FLEX_ARRAY_NR_BASE_PTRS * \
  51. FLEX_ARRAY_ELEMENTS_PER_PART(__element_size)); \
  52. }
  53. struct flex_array *flex_array_alloc(int element_size, unsigned int total,
  54. gfp_t flags);
  55. int flex_array_prealloc(struct flex_array *fa, unsigned int start,
  56. unsigned int nr_elements, gfp_t flags);
  57. void flex_array_free(struct flex_array *fa);
  58. void flex_array_free_parts(struct flex_array *fa);
  59. int flex_array_put(struct flex_array *fa, unsigned int element_nr, void *src,
  60. gfp_t flags);
  61. int flex_array_clear(struct flex_array *fa, unsigned int element_nr);
  62. void *flex_array_get(struct flex_array *fa, unsigned int element_nr);
  63. int flex_array_shrink(struct flex_array *fa);
  64. #define flex_array_put_ptr(fa, nr, src, gfp) \
  65. flex_array_put(fa, nr, (void *)&(src), gfp)
  66. void *flex_array_get_ptr(struct flex_array *fa, unsigned int element_nr);
  67. #endif /* _FLEX_ARRAY_H */