mempool.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * memory buffer pool support
  3. */
  4. #ifndef _LINUX_MEMPOOL_H
  5. #define _LINUX_MEMPOOL_H
  6. #include <linux/wait.h>
  7. struct kmem_cache;
  8. typedef void * (mempool_alloc_t)(gfp_t gfp_mask, void *pool_data);
  9. typedef void (mempool_free_t)(void *element, void *pool_data);
  10. typedef struct mempool_s {
  11. spinlock_t lock;
  12. int min_nr; /* nr of elements at *elements */
  13. int curr_nr; /* Current nr of elements at *elements */
  14. void **elements;
  15. void *pool_data;
  16. mempool_alloc_t *alloc;
  17. mempool_free_t *free;
  18. wait_queue_head_t wait;
  19. } mempool_t;
  20. extern mempool_t *mempool_create(int min_nr, mempool_alloc_t *alloc_fn,
  21. mempool_free_t *free_fn, void *pool_data);
  22. extern mempool_t *mempool_create_node(int min_nr, mempool_alloc_t *alloc_fn,
  23. mempool_free_t *free_fn, void *pool_data,
  24. gfp_t gfp_mask, int nid);
  25. extern int mempool_resize(mempool_t *pool, int new_min_nr);
  26. extern void mempool_destroy(mempool_t *pool);
  27. extern void * mempool_alloc(mempool_t *pool, gfp_t gfp_mask);
  28. extern void mempool_free(void *element, mempool_t *pool);
  29. /*
  30. * A mempool_alloc_t and mempool_free_t that get the memory from
  31. * a slab cache that is passed in through pool_data.
  32. * Note: the slab cache may not have a ctor function.
  33. */
  34. void *mempool_alloc_slab(gfp_t gfp_mask, void *pool_data);
  35. void mempool_free_slab(void *element, void *pool_data);
  36. static inline mempool_t *
  37. mempool_create_slab_pool(int min_nr, struct kmem_cache *kc)
  38. {
  39. return mempool_create(min_nr, mempool_alloc_slab, mempool_free_slab,
  40. (void *) kc);
  41. }
  42. /*
  43. * a mempool_alloc_t and a mempool_free_t to kmalloc and kfree the
  44. * amount of memory specified by pool_data
  45. */
  46. void *mempool_kmalloc(gfp_t gfp_mask, void *pool_data);
  47. void mempool_kfree(void *element, void *pool_data);
  48. static inline mempool_t *mempool_create_kmalloc_pool(int min_nr, size_t size)
  49. {
  50. return mempool_create(min_nr, mempool_kmalloc, mempool_kfree,
  51. (void *) size);
  52. }
  53. /*
  54. * A mempool_alloc_t and mempool_free_t for a simple page allocator that
  55. * allocates pages of the order specified by pool_data
  56. */
  57. void *mempool_alloc_pages(gfp_t gfp_mask, void *pool_data);
  58. void mempool_free_pages(void *element, void *pool_data);
  59. static inline mempool_t *mempool_create_page_pool(int min_nr, int order)
  60. {
  61. return mempool_create(min_nr, mempool_alloc_pages, mempool_free_pages,
  62. (void *)(long)order);
  63. }
  64. #endif /* _LINUX_MEMPOOL_H */