ua_nodestore_default.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information.
  3. *
  4. * Copyright 2014-2018 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  5. * Copyright 2017 (c) Julian Grothoff
  6. * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
  7. */
  8. #include <open62541/plugin/nodestore.h>
  9. #include "ziptree.h"
  10. #ifndef UA_ENABLE_CUSTOM_NODESTORE
  11. #ifdef UA_ENABLE_MULTITHREADING
  12. #include <pthread.h>
  13. #define BEGIN_CRITSECT(NODEMAP) pthread_mutex_lock(&(NODEMAP)->mutex)
  14. #define END_CRITSECT(NODEMAP) pthread_mutex_unlock(&(NODEMAP)->mutex)
  15. #else
  16. #define BEGIN_CRITSECT(NODEMAP) do {} while(0)
  17. #define END_CRITSECT(NODEMAP) do {} while(0)
  18. #endif
  19. /* container_of */
  20. #define container_of(ptr, type, member) \
  21. (type *)((uintptr_t)ptr - offsetof(type,member))
  22. struct NodeEntry;
  23. typedef struct NodeEntry NodeEntry;
  24. struct NodeEntry {
  25. ZIP_ENTRY(NodeEntry) zipfields;
  26. UA_UInt32 nodeIdHash;
  27. UA_UInt16 refCount; /* How many consumers have a reference to the node? */
  28. UA_Boolean deleted; /* Node was marked as deleted and can be deleted when refCount == 0 */
  29. NodeEntry *orig; /* If a copy is made to replace a node, track that we
  30. * replace only the node from which the copy was made.
  31. * Important for concurrent operations. */
  32. UA_NodeId nodeId; /* This is actually a UA_Node that also starts with a NodeId */
  33. };
  34. /* Absolute ordering for NodeIds */
  35. static enum ZIP_CMP
  36. cmpNodeId(const void *a, const void *b) {
  37. const NodeEntry *aa = (const NodeEntry*)a;
  38. const NodeEntry *bb = (const NodeEntry*)b;
  39. /* Compare hash */
  40. if(aa->nodeIdHash < bb->nodeIdHash)
  41. return ZIP_CMP_LESS;
  42. if(aa->nodeIdHash > bb->nodeIdHash)
  43. return ZIP_CMP_MORE;
  44. /* Compore nodes in detail */
  45. return (enum ZIP_CMP)UA_NodeId_order(&aa->nodeId, &bb->nodeId);
  46. }
  47. ZIP_HEAD(NodeTree, NodeEntry);
  48. typedef struct NodeTree NodeTree;
  49. typedef struct {
  50. NodeTree root;
  51. #ifdef UA_ENABLE_MULTITHREADING
  52. pthread_mutex_t mutex; /* Protect access */
  53. #endif
  54. } NodeMap;
  55. ZIP_PROTTYPE(NodeTree, NodeEntry, NodeEntry)
  56. ZIP_IMPL(NodeTree, NodeEntry, zipfields, NodeEntry, zipfields, cmpNodeId)
  57. static NodeEntry *
  58. newEntry(UA_NodeClass nodeClass) {
  59. size_t size = sizeof(NodeEntry) - sizeof(UA_NodeId);
  60. switch(nodeClass) {
  61. case UA_NODECLASS_OBJECT:
  62. size += sizeof(UA_ObjectNode);
  63. break;
  64. case UA_NODECLASS_VARIABLE:
  65. size += sizeof(UA_VariableNode);
  66. break;
  67. case UA_NODECLASS_METHOD:
  68. size += sizeof(UA_MethodNode);
  69. break;
  70. case UA_NODECLASS_OBJECTTYPE:
  71. size += sizeof(UA_ObjectTypeNode);
  72. break;
  73. case UA_NODECLASS_VARIABLETYPE:
  74. size += sizeof(UA_VariableTypeNode);
  75. break;
  76. case UA_NODECLASS_REFERENCETYPE:
  77. size += sizeof(UA_ReferenceTypeNode);
  78. break;
  79. case UA_NODECLASS_DATATYPE:
  80. size += sizeof(UA_DataTypeNode);
  81. break;
  82. case UA_NODECLASS_VIEW:
  83. size += sizeof(UA_ViewNode);
  84. break;
  85. default:
  86. return NULL;
  87. }
  88. NodeEntry *entry = (NodeEntry*)UA_calloc(1, size);
  89. if(!entry)
  90. return NULL;
  91. UA_Node *node = (UA_Node*)&entry->nodeId;
  92. node->nodeClass = nodeClass;
  93. return entry;
  94. }
  95. static void
  96. deleteEntry(NodeEntry *entry) {
  97. UA_Node_deleteMembers((UA_Node*)&entry->nodeId);
  98. UA_free(entry);
  99. }
  100. static void
  101. cleanupEntry(NodeEntry *entry) {
  102. if(entry->deleted && entry->refCount == 0)
  103. deleteEntry(entry);
  104. }
  105. /***********************/
  106. /* Interface functions */
  107. /***********************/
  108. /* Not yet inserted into the NodeMap */
  109. UA_Node *
  110. UA_Nodestore_newNode(void *nsCtx, UA_NodeClass nodeClass) {
  111. NodeEntry *entry = newEntry(nodeClass);
  112. if(!entry)
  113. return NULL;
  114. return (UA_Node*)&entry->nodeId;
  115. }
  116. /* Not yet inserted into the NodeMap */
  117. void
  118. UA_Nodestore_deleteNode(void *nsCtx, UA_Node *node) {
  119. deleteEntry(container_of(node, NodeEntry, nodeId));
  120. }
  121. const UA_Node *
  122. UA_Nodestore_getNode(void *nsCtx, const UA_NodeId *nodeId) {
  123. NodeMap *ns = (NodeMap*)nsCtx;
  124. BEGIN_CRITSECT(ns);
  125. NodeEntry dummy;
  126. dummy.nodeIdHash = UA_NodeId_hash(nodeId);
  127. dummy.nodeId = *nodeId;
  128. NodeEntry *entry = ZIP_FIND(NodeTree, &ns->root, &dummy);
  129. if(!entry) {
  130. END_CRITSECT(ns);
  131. return NULL;
  132. }
  133. ++entry->refCount;
  134. END_CRITSECT(ns);
  135. return (const UA_Node*)&entry->nodeId;
  136. }
  137. void
  138. UA_Nodestore_releaseNode(void *nsCtx, const UA_Node *node) {
  139. if(!node)
  140. return;
  141. #ifdef UA_ENABLE_MULTITHREADING
  142. NodeMap *ns = (NodeMap*)nsCtx;
  143. #endif
  144. BEGIN_CRITSECT(ns);
  145. NodeEntry *entry = container_of(node, NodeEntry, nodeId);
  146. UA_assert(entry->refCount > 0);
  147. --entry->refCount;
  148. cleanupEntry(entry);
  149. END_CRITSECT(ns);
  150. }
  151. UA_StatusCode
  152. UA_Nodestore_getNodeCopy(void *nsCtx, const UA_NodeId *nodeId,
  153. UA_Node **outNode) {
  154. /* Find the node */
  155. const UA_Node *node = UA_Nodestore_getNode(nsCtx, nodeId);
  156. if(!node)
  157. return UA_STATUSCODE_BADNODEIDUNKNOWN;
  158. /* Create the new entry */
  159. NodeEntry *ne = newEntry(node->nodeClass);
  160. if(!ne) {
  161. UA_Nodestore_releaseNode(nsCtx, node);
  162. return UA_STATUSCODE_BADOUTOFMEMORY;
  163. }
  164. /* Copy the node content */
  165. UA_Node *nnode = (UA_Node*)&ne->nodeId;
  166. UA_StatusCode retval = UA_Node_copy(node, nnode);
  167. UA_Nodestore_releaseNode(nsCtx, node);
  168. if(retval != UA_STATUSCODE_GOOD) {
  169. deleteEntry(ne);
  170. return retval;
  171. }
  172. ne->orig = container_of(node, NodeEntry, nodeId);
  173. *outNode = nnode;
  174. return UA_STATUSCODE_GOOD;
  175. }
  176. UA_StatusCode
  177. UA_Nodestore_insertNode(void *nsCtx, UA_Node *node, UA_NodeId *addedNodeId) {
  178. NodeEntry *entry = container_of(node, NodeEntry, nodeId);
  179. NodeMap *ns = (NodeMap*)nsCtx;
  180. BEGIN_CRITSECT(ns);
  181. /* Ensure that the NodeId is unique */
  182. NodeEntry dummy;
  183. dummy.nodeId = node->nodeId;
  184. if(node->nodeId.identifierType == UA_NODEIDTYPE_NUMERIC &&
  185. node->nodeId.identifier.numeric == 0) {
  186. do { /* Create a random nodeid until we find an unoccupied id */
  187. node->nodeId.identifier.numeric = UA_UInt32_random();
  188. dummy.nodeId.identifier.numeric = node->nodeId.identifier.numeric;
  189. dummy.nodeIdHash = UA_NodeId_hash(&node->nodeId);
  190. } while(ZIP_FIND(NodeTree, &ns->root, &dummy));
  191. } else {
  192. dummy.nodeIdHash = UA_NodeId_hash(&node->nodeId);
  193. if(ZIP_FIND(NodeTree, &ns->root, &dummy)) { /* The nodeid exists */
  194. deleteEntry(entry);
  195. END_CRITSECT(ns);
  196. return UA_STATUSCODE_BADNODEIDEXISTS;
  197. }
  198. }
  199. /* Copy the NodeId */
  200. if(addedNodeId) {
  201. UA_StatusCode retval = UA_NodeId_copy(&node->nodeId, addedNodeId);
  202. if(retval != UA_STATUSCODE_GOOD) {
  203. deleteEntry(entry);
  204. END_CRITSECT(ns);
  205. return retval;
  206. }
  207. }
  208. /* Insert the node */
  209. entry->nodeIdHash = dummy.nodeIdHash;
  210. ZIP_INSERT(NodeTree, &ns->root, entry, ZIP_FFS32(UA_UInt32_random()));
  211. END_CRITSECT(ns);
  212. return UA_STATUSCODE_GOOD;
  213. }
  214. UA_StatusCode
  215. UA_Nodestore_replaceNode(void *nsCtx, UA_Node *node) {
  216. /* Find the node */
  217. const UA_Node *oldNode = UA_Nodestore_getNode(nsCtx, &node->nodeId);
  218. if(!oldNode)
  219. return UA_STATUSCODE_BADNODEIDUNKNOWN;
  220. /* Test if the copy is current */
  221. NodeEntry *entry = container_of(node, NodeEntry, nodeId);
  222. NodeEntry *oldEntry = container_of(oldNode, NodeEntry, nodeId);
  223. if(oldEntry != entry->orig) {
  224. /* The node was already updated since the copy was made */
  225. deleteEntry(entry);
  226. UA_Nodestore_releaseNode(nsCtx, oldNode);
  227. return UA_STATUSCODE_BADINTERNALERROR;
  228. }
  229. /* Replace */
  230. NodeMap *ns = (NodeMap*)nsCtx;
  231. BEGIN_CRITSECT(ns);
  232. ZIP_REMOVE(NodeTree, &ns->root, oldEntry);
  233. entry->nodeIdHash = oldEntry->nodeIdHash;
  234. ZIP_INSERT(NodeTree, &ns->root, entry, ZIP_RANK(entry, zipfields));
  235. oldEntry->deleted = true;
  236. END_CRITSECT(ns);
  237. UA_Nodestore_releaseNode(nsCtx, oldNode);
  238. return UA_STATUSCODE_GOOD;
  239. }
  240. UA_StatusCode
  241. UA_Nodestore_removeNode(void *nsCtx, const UA_NodeId *nodeId) {
  242. NodeMap *ns = (NodeMap*)nsCtx;
  243. BEGIN_CRITSECT(ns);
  244. NodeEntry dummy;
  245. dummy.nodeIdHash = UA_NodeId_hash(nodeId);
  246. dummy.nodeId = *nodeId;
  247. NodeEntry *entry = ZIP_FIND(NodeTree, &ns->root, &dummy);
  248. if(!entry) {
  249. END_CRITSECT(ns);
  250. return UA_STATUSCODE_BADNODEIDUNKNOWN;
  251. }
  252. ZIP_REMOVE(NodeTree, &ns->root, entry);
  253. entry->deleted = true;
  254. cleanupEntry(entry);
  255. END_CRITSECT(ns);
  256. return UA_STATUSCODE_GOOD;
  257. }
  258. struct VisitorData {
  259. UA_NodestoreVisitor visitor;
  260. void *visitorContext;
  261. };
  262. static void
  263. nodeVisitor(NodeEntry *entry, void *data) {
  264. struct VisitorData *d = (struct VisitorData*)data;
  265. d->visitor(d->visitorContext, (UA_Node*)&entry->nodeId);
  266. }
  267. void
  268. UA_Nodestore_iterate(void *nsCtx, UA_NodestoreVisitor visitor,
  269. void *visitorCtx) {
  270. struct VisitorData d;
  271. d.visitor = visitor;
  272. d.visitorContext = visitorCtx;
  273. NodeMap *ns = (NodeMap*)nsCtx;
  274. BEGIN_CRITSECT(ns);
  275. ZIP_ITER(NodeTree, &ns->root, nodeVisitor, &d);
  276. END_CRITSECT(ns);
  277. }
  278. static void
  279. deleteNodeVisitor(NodeEntry *entry, void *data) {
  280. deleteEntry(entry);
  281. }
  282. /***********************/
  283. /* Nodestore Lifecycle */
  284. /***********************/
  285. const UA_Boolean inPlaceEditAllowed = true;
  286. UA_StatusCode
  287. UA_Nodestore_new(void **nsCtx) {
  288. /* Allocate and initialize the nodemap */
  289. NodeMap *nodemap = (NodeMap*)UA_malloc(sizeof(NodeMap));
  290. if(!nodemap)
  291. return UA_STATUSCODE_BADOUTOFMEMORY;
  292. #ifdef UA_ENABLE_MULTITHREADING
  293. pthread_mutex_init(&nodemap->mutex, NULL);
  294. #endif
  295. ZIP_INIT(&nodemap->root);
  296. /* Populate the nodestore */
  297. *nsCtx = (void*)nodemap;
  298. return UA_STATUSCODE_GOOD;
  299. }
  300. void
  301. UA_Nodestore_delete(void *nsCtx) {
  302. if (!nsCtx)
  303. return;
  304. NodeMap *ns = (NodeMap*)nsCtx;
  305. #ifdef UA_ENABLE_MULTITHREADING
  306. pthread_mutex_destroy(&ns->mutex);
  307. #endif
  308. ZIP_ITER(NodeTree, &ns->root, deleteNodeVisitor, NULL);
  309. UA_free(ns);
  310. }
  311. #endif /* UA_ENABLE_CUSTOM_NODESTORE */