flex_array.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. #define FLEX_ARRAY_INIT(size, total) { { {\
  29. .element_size = (size), \
  30. .total_nr_elements = (total), \
  31. } } }
  32. struct flex_array *flex_array_alloc(int element_size, unsigned int total,
  33. gfp_t flags);
  34. int flex_array_prealloc(struct flex_array *fa, unsigned int start,
  35. unsigned int end, gfp_t flags);
  36. void flex_array_free(struct flex_array *fa);
  37. void flex_array_free_parts(struct flex_array *fa);
  38. int flex_array_put(struct flex_array *fa, unsigned int element_nr, void *src,
  39. gfp_t flags);
  40. void *flex_array_get(struct flex_array *fa, unsigned int element_nr);
  41. #endif /* _FLEX_ARRAY_H */