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