flex_array.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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[0];
  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, int total, gfp_t flags);
  33. int flex_array_prealloc(struct flex_array *fa, int start, int end, gfp_t flags);
  34. void flex_array_free(struct flex_array *fa);
  35. void flex_array_free_parts(struct flex_array *fa);
  36. int flex_array_put(struct flex_array *fa, int element_nr, void *src,
  37. gfp_t flags);
  38. void *flex_array_get(struct flex_array *fa, int element_nr);
  39. #endif /* _FLEX_ARRAY_H */