flex_array.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. struct flex_array_part *parts[];
  20. };
  21. /*
  22. * This little trick makes sure that
  23. * sizeof(flex_array) == PAGE_SIZE
  24. */
  25. char padding[FLEX_ARRAY_BASE_SIZE];
  26. };
  27. };
  28. /* Number of bytes left in base struct flex_array, excluding metadata */
  29. #define FLEX_ARRAY_BASE_BYTES_LEFT \
  30. (FLEX_ARRAY_BASE_SIZE - offsetof(struct flex_array, parts))
  31. /* Number of pointers in base to struct flex_array_part pages */
  32. #define FLEX_ARRAY_NR_BASE_PTRS \
  33. (FLEX_ARRAY_BASE_BYTES_LEFT / sizeof(struct flex_array_part *))
  34. /* Number of elements of size that fit in struct flex_array_part */
  35. #define FLEX_ARRAY_ELEMENTS_PER_PART(size) \
  36. (FLEX_ARRAY_PART_SIZE / size)
  37. /*
  38. * Defines a statically allocated flex array and ensures its parameters are
  39. * valid.
  40. */
  41. #define DEFINE_FLEX_ARRAY(__arrayname, __element_size, __total) \
  42. struct flex_array __arrayname = { { { \
  43. .element_size = (__element_size), \
  44. .total_nr_elements = (__total), \
  45. } } }; \
  46. static inline void __arrayname##_invalid_parameter(void) \
  47. { \
  48. BUILD_BUG_ON((__total) > FLEX_ARRAY_NR_BASE_PTRS * \
  49. FLEX_ARRAY_ELEMENTS_PER_PART(__element_size)); \
  50. }
  51. struct flex_array *flex_array_alloc(int element_size, unsigned int total,
  52. gfp_t flags);
  53. int flex_array_prealloc(struct flex_array *fa, unsigned int start,
  54. unsigned int end, gfp_t flags);
  55. void flex_array_free(struct flex_array *fa);
  56. void flex_array_free_parts(struct flex_array *fa);
  57. int flex_array_put(struct flex_array *fa, unsigned int element_nr, void *src,
  58. gfp_t flags);
  59. int flex_array_clear(struct flex_array *fa, unsigned int element_nr);
  60. void *flex_array_get(struct flex_array *fa, unsigned int element_nr);
  61. int flex_array_shrink(struct flex_array *fa);
  62. #define flex_array_put_ptr(fa, nr, src, gfp) \
  63. flex_array_put(fa, nr, (void *)&(src), gfp)
  64. void *flex_array_get_ptr(struct flex_array *fa, unsigned int element_nr);
  65. #endif /* _FLEX_ARRAY_H */