ua_nodestore_default.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 2017 (c) Julian Grothoff
  5. * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
  6. */
  7. #include "ua_nodestore_default.h"
  8. /* container_of */
  9. #define container_of(ptr, type, member) \
  10. (type *)((uintptr_t)ptr - offsetof(type,member))
  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)
  17. #define END_CRITSECT(NODEMAP)
  18. #endif
  19. /* The default Nodestore is simply a hash-map from NodeIds to Nodes. To find an
  20. * entry, iterate over candidate positions according to the NodeId hash.
  21. *
  22. * - Tombstone or non-matching NodeId: continue searching
  23. * - Matching NodeId: Return the entry
  24. * - NULL: Abort the search */
  25. typedef struct UA_NodeMapEntry {
  26. struct UA_NodeMapEntry *orig; /* the version this is a copy from (or NULL) */
  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. UA_Node node;
  30. } UA_NodeMapEntry;
  31. #define UA_NODEMAP_MINSIZE 64
  32. #define UA_NODEMAP_TOMBSTONE ((UA_NodeMapEntry*)0x01)
  33. typedef struct {
  34. UA_NodeMapEntry **entries;
  35. UA_UInt32 size;
  36. UA_UInt32 count;
  37. UA_UInt32 sizePrimeIndex;
  38. #ifdef UA_ENABLE_MULTITHREADING
  39. pthread_mutex_t mutex; /* Protect access */
  40. #endif
  41. } UA_NodeMap;
  42. /*********************/
  43. /* HashMap Utilities */
  44. /*********************/
  45. /* The size of the hash-map is always a prime number. They are chosen to be
  46. * close to the next power of 2. So the size ca. doubles with each prime. */
  47. static UA_UInt32 const primes[] = {
  48. 7, 13, 31, 61, 127, 251,
  49. 509, 1021, 2039, 4093, 8191, 16381,
  50. 32749, 65521, 131071, 262139, 524287, 1048573,
  51. 2097143, 4194301, 8388593, 16777213, 33554393, 67108859,
  52. 134217689, 268435399, 536870909, 1073741789, 2147483647, 4294967291
  53. };
  54. static UA_UInt32 mod(UA_UInt32 h, UA_UInt32 size) { return h % size; }
  55. static UA_UInt32 mod2(UA_UInt32 h, UA_UInt32 size) { return 1 + (h % (size - 2)); }
  56. static UA_UInt16
  57. higher_prime_index(UA_UInt32 n) {
  58. UA_UInt16 low = 0;
  59. UA_UInt16 high = (UA_UInt16)(sizeof(primes) / sizeof(UA_UInt32));
  60. while(low != high) {
  61. UA_UInt16 mid = (UA_UInt16)(low + ((high - low) / 2));
  62. if(n > primes[mid])
  63. low = (UA_UInt16)(mid + 1);
  64. else
  65. high = mid;
  66. }
  67. return low;
  68. }
  69. /* returns an empty slot or null if the nodeid exists */
  70. static UA_NodeMapEntry **
  71. findFreeSlot(const UA_NodeMap *ns, const UA_NodeId *nodeid) {
  72. UA_UInt32 h = UA_NodeId_hash(nodeid);
  73. UA_UInt32 size = ns->size;
  74. UA_UInt32 idx = mod(h, size);
  75. UA_UInt32 hash2 = mod2(h, size);
  76. while(true) {
  77. UA_NodeMapEntry *e = ns->entries[idx];
  78. if(e > UA_NODEMAP_TOMBSTONE &&
  79. UA_NodeId_equal(&e->node.nodeId, nodeid))
  80. return NULL;
  81. if(ns->entries[idx] <= UA_NODEMAP_TOMBSTONE)
  82. return &ns->entries[idx];
  83. idx += hash2;
  84. if(idx >= size)
  85. idx -= size;
  86. }
  87. /* NOTREACHED */
  88. return NULL;
  89. }
  90. /* The occupancy of the table after the call will be about 50% */
  91. static UA_StatusCode
  92. expand(UA_NodeMap *ns) {
  93. UA_UInt32 osize = ns->size;
  94. UA_UInt32 count = ns->count;
  95. /* Resize only when table after removal of unused elements is either too
  96. full or too empty */
  97. if(count * 2 < osize && (count * 8 > osize || osize <= UA_NODEMAP_MINSIZE))
  98. return UA_STATUSCODE_GOOD;
  99. UA_NodeMapEntry **oentries = ns->entries;
  100. UA_UInt32 nindex = higher_prime_index(count * 2);
  101. UA_UInt32 nsize = primes[nindex];
  102. UA_NodeMapEntry **nentries = (UA_NodeMapEntry **)UA_calloc(nsize, sizeof(UA_NodeMapEntry*));
  103. if(!nentries)
  104. return UA_STATUSCODE_BADOUTOFMEMORY;
  105. ns->entries = nentries;
  106. ns->size = nsize;
  107. ns->sizePrimeIndex = nindex;
  108. /* recompute the position of every entry and insert the pointer */
  109. for(size_t i = 0, j = 0; i < osize && j < count; ++i) {
  110. if(oentries[i] <= UA_NODEMAP_TOMBSTONE)
  111. continue;
  112. UA_NodeMapEntry **e = findFreeSlot(ns, &oentries[i]->node.nodeId);
  113. UA_assert(e);
  114. *e = oentries[i];
  115. ++j;
  116. }
  117. UA_free(oentries);
  118. return UA_STATUSCODE_GOOD;
  119. }
  120. static UA_NodeMapEntry *
  121. newEntry(UA_NodeClass nodeClass) {
  122. size_t size = sizeof(UA_NodeMapEntry) - sizeof(UA_Node);
  123. switch(nodeClass) {
  124. case UA_NODECLASS_OBJECT:
  125. size += sizeof(UA_ObjectNode);
  126. break;
  127. case UA_NODECLASS_VARIABLE:
  128. size += sizeof(UA_VariableNode);
  129. break;
  130. case UA_NODECLASS_METHOD:
  131. size += sizeof(UA_MethodNode);
  132. break;
  133. case UA_NODECLASS_OBJECTTYPE:
  134. size += sizeof(UA_ObjectTypeNode);
  135. break;
  136. case UA_NODECLASS_VARIABLETYPE:
  137. size += sizeof(UA_VariableTypeNode);
  138. break;
  139. case UA_NODECLASS_REFERENCETYPE:
  140. size += sizeof(UA_ReferenceTypeNode);
  141. break;
  142. case UA_NODECLASS_DATATYPE:
  143. size += sizeof(UA_DataTypeNode);
  144. break;
  145. case UA_NODECLASS_VIEW:
  146. size += sizeof(UA_ViewNode);
  147. break;
  148. default:
  149. return NULL;
  150. }
  151. UA_NodeMapEntry *entry = (UA_NodeMapEntry*)UA_calloc(1, size);
  152. if(!entry)
  153. return NULL;
  154. entry->node.nodeClass = nodeClass;
  155. return entry;
  156. }
  157. static void
  158. deleteEntry(UA_NodeMapEntry *entry) {
  159. UA_Node_deleteMembers(&entry->node);
  160. UA_free(entry);
  161. }
  162. static void
  163. cleanupEntry(UA_NodeMapEntry *entry) {
  164. if(entry->deleted && entry->refCount == 0)
  165. deleteEntry(entry);
  166. }
  167. static UA_StatusCode
  168. clearSlot(UA_NodeMap *ns, UA_NodeMapEntry **slot) {
  169. (*slot)->deleted = true;
  170. cleanupEntry(*slot);
  171. *slot = UA_NODEMAP_TOMBSTONE;
  172. --ns->count;
  173. /* Downsize the hashmap if it is very empty */
  174. if(ns->count * 8 < ns->size && ns->size > 32)
  175. expand(ns); /* Can fail. Just continue with the bigger hashmap. */
  176. return UA_STATUSCODE_GOOD;
  177. }
  178. static UA_NodeMapEntry **
  179. findOccupiedSlot(const UA_NodeMap *ns, const UA_NodeId *nodeid) {
  180. UA_UInt32 h = UA_NodeId_hash(nodeid);
  181. UA_UInt32 size = ns->size;
  182. UA_UInt32 idx = mod(h, size);
  183. UA_UInt32 hash2 = mod2(h, size);
  184. while(true) {
  185. UA_NodeMapEntry *e = ns->entries[idx];
  186. if(!e)
  187. return NULL;
  188. if(e > UA_NODEMAP_TOMBSTONE &&
  189. UA_NodeId_equal(&e->node.nodeId, nodeid))
  190. return &ns->entries[idx];
  191. idx += hash2;
  192. if(idx >= size)
  193. idx -= size;
  194. }
  195. /* NOTREACHED */
  196. return NULL;
  197. }
  198. /***********************/
  199. /* Interface functions */
  200. /***********************/
  201. static UA_Node *
  202. UA_NodeMap_newNode(void *context, UA_NodeClass nodeClass) {
  203. UA_NodeMapEntry *entry = newEntry(nodeClass);
  204. if(!entry)
  205. return NULL;
  206. return &entry->node;
  207. }
  208. static void
  209. UA_NodeMap_deleteNode(void *context, UA_Node *node) {
  210. #ifdef UA_ENABLE_MULTITHREADING
  211. UA_NodeMap *ns = (UA_NodeMap*)context;
  212. #endif
  213. BEGIN_CRITSECT(ns);
  214. UA_NodeMapEntry *entry = container_of(node, UA_NodeMapEntry, node);
  215. UA_assert(&entry->node == node);
  216. deleteEntry(entry);
  217. END_CRITSECT(ns);
  218. }
  219. static const UA_Node *
  220. UA_NodeMap_getNode(void *context, const UA_NodeId *nodeid) {
  221. UA_NodeMap *ns = (UA_NodeMap*)context;
  222. BEGIN_CRITSECT(ns);
  223. UA_NodeMapEntry **entry = findOccupiedSlot(ns, nodeid);
  224. if(!entry) {
  225. END_CRITSECT(ns);
  226. return NULL;
  227. }
  228. ++(*entry)->refCount;
  229. END_CRITSECT(ns);
  230. return (const UA_Node*)&(*entry)->node;
  231. }
  232. static void
  233. UA_NodeMap_releaseNode(void *context, const UA_Node *node) {
  234. if (!node)
  235. return;
  236. #ifdef UA_ENABLE_MULTITHREADING
  237. UA_NodeMap *ns = (UA_NodeMap*)context;
  238. #endif
  239. BEGIN_CRITSECT(ns);
  240. UA_NodeMapEntry *entry = container_of(node, UA_NodeMapEntry, node);
  241. UA_assert(&entry->node == node);
  242. UA_assert(entry->refCount > 0);
  243. --entry->refCount;
  244. cleanupEntry(entry);
  245. END_CRITSECT(ns);
  246. }
  247. static UA_StatusCode
  248. UA_NodeMap_getNodeCopy(void *context, const UA_NodeId *nodeid,
  249. UA_Node **outNode) {
  250. UA_NodeMap *ns = (UA_NodeMap*)context;
  251. BEGIN_CRITSECT(ns);
  252. UA_NodeMapEntry **slot = findOccupiedSlot(ns, nodeid);
  253. if(!slot) {
  254. END_CRITSECT(ns);
  255. return UA_STATUSCODE_BADNODEIDUNKNOWN;
  256. }
  257. UA_NodeMapEntry *entry = *slot;
  258. UA_NodeMapEntry *newItem = newEntry(entry->node.nodeClass);
  259. if(!newItem) {
  260. END_CRITSECT(ns);
  261. return UA_STATUSCODE_BADOUTOFMEMORY;
  262. }
  263. UA_StatusCode retval = UA_Node_copy(&entry->node, &newItem->node);
  264. if(retval == UA_STATUSCODE_GOOD) {
  265. newItem->orig = entry; // store the pointer to the original
  266. *outNode = &newItem->node;
  267. } else {
  268. deleteEntry(newItem);
  269. }
  270. END_CRITSECT(ns);
  271. return retval;
  272. }
  273. static UA_StatusCode
  274. UA_NodeMap_removeNode(void *context, const UA_NodeId *nodeid) {
  275. UA_NodeMap *ns = (UA_NodeMap*)context;
  276. BEGIN_CRITSECT(ns);
  277. UA_NodeMapEntry **slot = findOccupiedSlot(ns, nodeid);
  278. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  279. if(slot)
  280. retval = clearSlot(ns, slot);
  281. else
  282. retval = UA_STATUSCODE_BADNODEIDUNKNOWN;
  283. END_CRITSECT(ns);
  284. return retval;
  285. }
  286. static UA_StatusCode
  287. UA_NodeMap_insertNode(void *context, UA_Node *node,
  288. UA_NodeId *addedNodeId) {
  289. UA_NodeMap *ns = (UA_NodeMap*)context;
  290. BEGIN_CRITSECT(ns);
  291. if(ns->size * 3 <= ns->count * 4) {
  292. if(expand(ns) != UA_STATUSCODE_GOOD) {
  293. END_CRITSECT(ns);
  294. return UA_STATUSCODE_BADINTERNALERROR;
  295. }
  296. }
  297. UA_NodeMapEntry **slot;
  298. if(node->nodeId.identifierType == UA_NODEIDTYPE_NUMERIC &&
  299. node->nodeId.identifier.numeric == 0) {
  300. /* create a random nodeid */
  301. /* start at least with 50,000 to make sure we don not conflict with nodes from the spec */
  302. /* E.g. adding a nodeset will create children while there are still other nodes which need to be created */
  303. /* Thus the node id's may collide */
  304. UA_UInt32 identifier = 50000 + ns->count+1; // start value
  305. UA_UInt32 size = ns->size;
  306. UA_UInt32 increase = mod2(ns->count+1, size);
  307. while(true) {
  308. node->nodeId.identifier.numeric = identifier;
  309. slot = findFreeSlot(ns, &node->nodeId);
  310. if(slot)
  311. break;
  312. identifier += increase;
  313. if(identifier >= size)
  314. identifier -= size;
  315. }
  316. } else {
  317. slot = findFreeSlot(ns, &node->nodeId);
  318. if(!slot) {
  319. deleteEntry(container_of(node, UA_NodeMapEntry, node));
  320. END_CRITSECT(ns);
  321. return UA_STATUSCODE_BADNODEIDEXISTS;
  322. }
  323. }
  324. *slot = container_of(node, UA_NodeMapEntry, node);
  325. ++ns->count;
  326. UA_assert(&(*slot)->node == node);
  327. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  328. if(addedNodeId) {
  329. retval = UA_NodeId_copy(&node->nodeId, addedNodeId);
  330. if(retval != UA_STATUSCODE_GOOD)
  331. clearSlot(ns, slot);
  332. }
  333. END_CRITSECT(ns);
  334. return retval;
  335. }
  336. static UA_StatusCode
  337. UA_NodeMap_replaceNode(void *context, UA_Node *node) {
  338. UA_NodeMap *ns = (UA_NodeMap*)context;
  339. BEGIN_CRITSECT(ns);
  340. UA_NodeMapEntry **slot = findOccupiedSlot(ns, &node->nodeId);
  341. if(!slot) {
  342. END_CRITSECT(ns);
  343. return UA_STATUSCODE_BADNODEIDUNKNOWN;
  344. }
  345. UA_NodeMapEntry *newEntryContainer = container_of(node, UA_NodeMapEntry, node);
  346. if(*slot != newEntryContainer->orig) {
  347. /* The node was updated since the copy was made */
  348. deleteEntry(newEntryContainer);
  349. END_CRITSECT(ns);
  350. return UA_STATUSCODE_BADINTERNALERROR;
  351. }
  352. (*slot)->deleted = true;
  353. cleanupEntry(*slot);
  354. *slot = newEntryContainer;
  355. END_CRITSECT(ns);
  356. return UA_STATUSCODE_GOOD;
  357. }
  358. static void
  359. UA_NodeMap_iterate(void *context, void *visitorContext,
  360. UA_NodestoreVisitor visitor) {
  361. UA_NodeMap *ns = (UA_NodeMap*)context;
  362. BEGIN_CRITSECT(ns);
  363. for(UA_UInt32 i = 0; i < ns->size; ++i) {
  364. if(ns->entries[i] > UA_NODEMAP_TOMBSTONE) {
  365. END_CRITSECT(ns);
  366. UA_NodeMapEntry *entry = ns->entries[i];
  367. entry->refCount++;
  368. visitor(visitorContext, &entry->node);
  369. entry->refCount--;
  370. cleanupEntry(entry);
  371. BEGIN_CRITSECT(ns);
  372. }
  373. }
  374. END_CRITSECT(ns);
  375. }
  376. static void
  377. UA_NodeMap_delete(void *context) {
  378. UA_NodeMap *ns = (UA_NodeMap*)context;
  379. #ifdef UA_ENABLE_MULTITHREADING
  380. pthread_mutex_destroy(&ns->mutex);
  381. #endif
  382. UA_UInt32 size = ns->size;
  383. UA_NodeMapEntry **entries = ns->entries;
  384. for(UA_UInt32 i = 0; i < size; ++i) {
  385. if(entries[i] > UA_NODEMAP_TOMBSTONE) {
  386. /* On debugging builds, check that all nodes were release */
  387. UA_assert(entries[i]->refCount == 0);
  388. /* Delete the node */
  389. deleteEntry(entries[i]);
  390. }
  391. }
  392. UA_free(ns->entries);
  393. UA_free(ns);
  394. }
  395. UA_StatusCode
  396. UA_Nodestore_default_new(UA_Nodestore *ns) {
  397. /* Allocate and initialize the nodemap */
  398. UA_NodeMap *nodemap = (UA_NodeMap*)UA_malloc(sizeof(UA_NodeMap));
  399. if(!nodemap)
  400. return UA_STATUSCODE_BADOUTOFMEMORY;
  401. nodemap->sizePrimeIndex = higher_prime_index(UA_NODEMAP_MINSIZE);
  402. nodemap->size = primes[nodemap->sizePrimeIndex];
  403. nodemap->count = 0;
  404. nodemap->entries = (UA_NodeMapEntry**)
  405. UA_calloc(nodemap->size, sizeof(UA_NodeMapEntry*));
  406. if(!nodemap->entries) {
  407. UA_free(nodemap);
  408. return UA_STATUSCODE_BADOUTOFMEMORY;
  409. }
  410. #ifdef UA_ENABLE_MULTITHREADING
  411. pthread_mutex_init(&nodemap->mutex, NULL);
  412. #endif
  413. /* Populate the nodestore */
  414. ns->context = nodemap;
  415. ns->deleteNodestore = UA_NodeMap_delete;
  416. ns->inPlaceEditAllowed = true;
  417. ns->newNode = UA_NodeMap_newNode;
  418. ns->deleteNode = UA_NodeMap_deleteNode;
  419. ns->getNode = UA_NodeMap_getNode;
  420. ns->releaseNode = UA_NodeMap_releaseNode;
  421. ns->getNodeCopy = UA_NodeMap_getNodeCopy;
  422. ns->insertNode = UA_NodeMap_insertNode;
  423. ns->replaceNode = UA_NodeMap_replaceNode;
  424. ns->removeNode = UA_NodeMap_removeNode;
  425. ns->iterate = UA_NodeMap_iterate;
  426. return UA_STATUSCODE_GOOD;
  427. }