object.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * netlink/object.c Generic Cacheable Object
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation version 2.1
  7. * of the License.
  8. *
  9. * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
  10. */
  11. #ifndef NETLINK_OBJECT_H_
  12. #define NETLINK_OBJECT_H_
  13. #include <netlink/netlink.h>
  14. #include <netlink/utils.h>
  15. #include <netlink/object-api.h>
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. #define NL_OBJ_MARK 1
  20. struct nl_cache;
  21. struct nl_object;
  22. struct nl_object_ops;
  23. struct nl_object
  24. {
  25. NLHDR_COMMON
  26. };
  27. #define OBJ_CAST(ptr) ((struct nl_object *) (ptr))
  28. /* General */
  29. extern struct nl_object * nl_object_alloc(struct nl_object_ops *);
  30. extern void nl_object_free(struct nl_object *);
  31. extern struct nl_object * nl_object_clone(struct nl_object *obj);
  32. #ifdef disabled
  33. extern int nl_object_alloc_name(const char *,
  34. struct nl_object **);
  35. extern void nl_object_dump(struct nl_object *,
  36. struct nl_dump_params *);
  37. extern uint32_t nl_object_diff(struct nl_object *,
  38. struct nl_object *);
  39. extern int nl_object_match_filter(struct nl_object *,
  40. struct nl_object *);
  41. extern int nl_object_identical(struct nl_object *,
  42. struct nl_object *);
  43. extern char * nl_object_attrs2str(struct nl_object *,
  44. uint32_t attrs, char *buf,
  45. size_t);
  46. #endif
  47. /**
  48. * Check whether this object is used by multiple users
  49. * @arg obj object to check
  50. * @return true or false
  51. */
  52. static inline int nl_object_shared(struct nl_object *obj)
  53. {
  54. return obj->ce_refcnt > 1;
  55. }
  56. static inline void nl_object_get(struct nl_object *obj)
  57. {
  58. obj->ce_refcnt++;
  59. }
  60. static inline void nl_object_put(struct nl_object *obj)
  61. {
  62. if (!obj)
  63. return;
  64. obj->ce_refcnt--;
  65. if (obj->ce_refcnt <= 0)
  66. nl_object_free(obj);
  67. }
  68. /**
  69. * @name Marks
  70. * @{
  71. */
  72. /**
  73. * Add mark to object
  74. * @arg obj Object to mark
  75. */
  76. static inline void nl_object_mark(struct nl_object *obj)
  77. {
  78. obj->ce_flags |= NL_OBJ_MARK;
  79. }
  80. /**
  81. * Remove mark from object
  82. * @arg obj Object to unmark
  83. */
  84. static inline void nl_object_unmark(struct nl_object *obj)
  85. {
  86. obj->ce_flags &= ~NL_OBJ_MARK;
  87. }
  88. /**
  89. * Return true if object is marked
  90. * @arg obj Object to check
  91. * @return true if object is marked, otherwise false
  92. */
  93. static inline int nl_object_is_marked(struct nl_object *obj)
  94. {
  95. return (obj->ce_flags & NL_OBJ_MARK);
  96. }
  97. /** @} */
  98. #ifdef disabled
  99. /**
  100. * Return list of attributes present in an object
  101. * @arg obj an object
  102. * @arg buf destination buffer
  103. * @arg len length of destination buffer
  104. *
  105. * @return destination buffer.
  106. */
  107. static inline char *nl_object_attr_list(struct nl_object *obj, char *buf, size_t len)
  108. {
  109. return nl_object_attrs2str(obj, obj->ce_mask, buf, len);
  110. }
  111. #endif
  112. /**
  113. * @name Attributes
  114. * @{
  115. */
  116. static inline int nl_object_get_refcnt(struct nl_object *obj)
  117. {
  118. return obj->ce_refcnt;
  119. }
  120. static inline struct nl_cache *nl_object_get_cache(struct nl_object *obj)
  121. {
  122. return obj->ce_cache;
  123. }
  124. static inline void * nl_object_priv(struct nl_object *obj)
  125. {
  126. return obj;
  127. }
  128. /** @} */
  129. #ifdef __cplusplus
  130. }
  131. #endif
  132. #endif