string_table.h 1.3 KB

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