buffer.h 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #ifndef __FS_CEPH_BUFFER_H
  2. #define __FS_CEPH_BUFFER_H
  3. #include <linux/mm.h>
  4. #include <linux/vmalloc.h>
  5. #include <linux/types.h>
  6. #include <linux/uio.h>
  7. /*
  8. * a simple reference counted buffer.
  9. *
  10. * use kmalloc for small sizes (<= one page), vmalloc for larger
  11. * sizes.
  12. */
  13. struct ceph_buffer {
  14. atomic_t nref;
  15. struct kvec vec;
  16. size_t alloc_len;
  17. bool is_vmalloc;
  18. };
  19. struct ceph_buffer *ceph_buffer_new(gfp_t gfp);
  20. int ceph_buffer_alloc(struct ceph_buffer *b, int len, gfp_t gfp);
  21. static inline struct ceph_buffer *ceph_buffer_get(struct ceph_buffer *b)
  22. {
  23. atomic_inc(&b->nref);
  24. return b;
  25. }
  26. static inline void ceph_buffer_put(struct ceph_buffer *b)
  27. {
  28. if (b && atomic_dec_and_test(&b->nref)) {
  29. if (b->vec.iov_base) {
  30. if (b->is_vmalloc)
  31. vfree(b->vec.iov_base);
  32. else
  33. kfree(b->vec.iov_base);
  34. }
  35. kfree(b);
  36. }
  37. }
  38. static inline struct ceph_buffer *ceph_buffer_new_alloc(int len, gfp_t gfp)
  39. {
  40. struct ceph_buffer *b = ceph_buffer_new(gfp);
  41. if (b && ceph_buffer_alloc(b, len, gfp) < 0) {
  42. ceph_buffer_put(b);
  43. b = NULL;
  44. }
  45. return b;
  46. }
  47. #endif