string_table.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #ifndef _FS_CEPH_STRING_TABLE_H
  2. #define _FS_CEPH_STRING_TABLE_H
  3. #include <linux/types.h>
  4. #include <linux/kref.h>
  5. #include <linux/rbtree.h>
  6. #include <linux/rcupdate.h>
  7. struct ceph_string {
  8. struct kref kref;
  9. union {
  10. struct rb_node node;
  11. struct rcu_head rcu;
  12. };
  13. size_t len;
  14. char str[];
  15. };
  16. extern void ceph_release_string(struct kref *ref);
  17. extern struct ceph_string *ceph_find_or_create_string(const char *str,
  18. size_t len);
  19. extern bool ceph_strings_empty(void);
  20. static inline struct ceph_string *ceph_get_string(struct ceph_string *str)
  21. {
  22. kref_get(&str->kref);
  23. return str;
  24. }
  25. static inline void ceph_put_string(struct ceph_string *str)
  26. {
  27. if (!str)
  28. return;
  29. kref_put(&str->kref, ceph_release_string);
  30. }
  31. static inline int ceph_compare_string(struct ceph_string *cs,
  32. const char* str, size_t len)
  33. {
  34. size_t cs_len = cs ? cs->len : 0;
  35. if (cs_len != len)
  36. return cs_len - len;
  37. if (len == 0)
  38. return 0;
  39. return strncmp(cs->str, str, len);
  40. }
  41. #define ceph_try_get_string(x) \
  42. ({ \
  43. struct ceph_string *___str; \
  44. rcu_read_lock(); \
  45. for (;;) { \
  46. ___str = rcu_dereference(x); \
  47. if (!___str || \
  48. kref_get_unless_zero(&___str->kref)) \
  49. break; \
  50. } \
  51. rcu_read_unlock(); \
  52. (___str); \
  53. })
  54. #endif