ua_nodestore.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #include "ua_nodestore.h"
  2. #include "ua_util.h"
  3. #ifndef UA_ENABLE_MULTITHREADING /* conditional compilation */
  4. #define UA_NODESTORE_MINSIZE 64
  5. typedef struct UA_NodeStoreEntry {
  6. struct UA_NodeStoreEntry *orig; // the version this is a copy from (or NULL)
  7. UA_Node node;
  8. } UA_NodeStoreEntry;
  9. #define UA_NODESTORE_TOMBSTONE ((UA_NodeStoreEntry*)0x01)
  10. struct UA_NodeStore {
  11. UA_NodeStoreEntry **entries;
  12. UA_UInt32 size;
  13. UA_UInt32 count;
  14. UA_UInt32 sizePrimeIndex;
  15. };
  16. #include "ua_nodestore_hash.inc"
  17. /* The size of the hash-map is always a prime number. They are chosen to be
  18. * close to the next power of 2. So the size ca. doubles with each prime. */
  19. static hash_t const primes[] = {
  20. 7, 13, 31, 61, 127, 251,
  21. 509, 1021, 2039, 4093, 8191, 16381,
  22. 32749, 65521, 131071, 262139, 524287, 1048573,
  23. 2097143, 4194301, 8388593, 16777213, 33554393, 67108859,
  24. 134217689, 268435399, 536870909, 1073741789, 2147483647, 4294967291
  25. };
  26. static UA_UInt16
  27. higher_prime_index(hash_t n) {
  28. UA_UInt16 low = 0;
  29. UA_UInt16 high = (UA_UInt16)(sizeof(primes) / sizeof(hash_t));
  30. while(low != high) {
  31. UA_UInt16 mid = (UA_UInt16)(low + ((high - low) / 2));
  32. if(n > primes[mid])
  33. low = (UA_UInt16)(mid + 1);
  34. else
  35. high = mid;
  36. }
  37. return low;
  38. }
  39. static UA_NodeStoreEntry *
  40. instantiateEntry(UA_NodeClass nodeClass) {
  41. size_t size = sizeof(UA_NodeStoreEntry) - sizeof(UA_Node);
  42. switch(nodeClass) {
  43. case UA_NODECLASS_OBJECT:
  44. size += sizeof(UA_ObjectNode);
  45. break;
  46. case UA_NODECLASS_VARIABLE:
  47. size += sizeof(UA_VariableNode);
  48. break;
  49. case UA_NODECLASS_METHOD:
  50. size += sizeof(UA_MethodNode);
  51. break;
  52. case UA_NODECLASS_OBJECTTYPE:
  53. size += sizeof(UA_ObjectTypeNode);
  54. break;
  55. case UA_NODECLASS_VARIABLETYPE:
  56. size += sizeof(UA_VariableTypeNode);
  57. break;
  58. case UA_NODECLASS_REFERENCETYPE:
  59. size += sizeof(UA_ReferenceTypeNode);
  60. break;
  61. case UA_NODECLASS_DATATYPE:
  62. size += sizeof(UA_DataTypeNode);
  63. break;
  64. case UA_NODECLASS_VIEW:
  65. size += sizeof(UA_ViewNode);
  66. break;
  67. default:
  68. return NULL;
  69. }
  70. UA_NodeStoreEntry *entry = UA_calloc(1, size);
  71. if(!entry)
  72. return NULL;
  73. entry->node.nodeClass = nodeClass;
  74. return entry;
  75. }
  76. static void
  77. deleteEntry(UA_NodeStoreEntry *entry) {
  78. UA_Node_deleteMembersAnyNodeClass(&entry->node);
  79. UA_free(entry);
  80. }
  81. /* returns slot of a valid node or null */
  82. static UA_NodeStoreEntry **
  83. findNode(const UA_NodeStore *ns, const UA_NodeId *nodeid) {
  84. hash_t h = hash(nodeid);
  85. UA_UInt32 size = ns->size;
  86. hash_t idx = mod(h, size);
  87. hash_t hash2 = mod2(h, size);
  88. while(true) {
  89. UA_NodeStoreEntry *e = ns->entries[idx];
  90. if(!e)
  91. return NULL;
  92. if(e > UA_NODESTORE_TOMBSTONE &&
  93. UA_NodeId_equal(&e->node.nodeId, nodeid))
  94. return &ns->entries[idx];
  95. idx += hash2;
  96. if(idx >= size)
  97. idx -= size;
  98. }
  99. /* NOTREACHED */
  100. return NULL;
  101. }
  102. /* returns an empty slot or null if the nodeid exists */
  103. static UA_NodeStoreEntry **
  104. findSlot(const UA_NodeStore *ns, const UA_NodeId *nodeid) {
  105. hash_t h = hash(nodeid);
  106. UA_UInt32 size = ns->size;
  107. hash_t idx = mod(h, size);
  108. hash_t hash2 = mod2(h, size);
  109. while(true) {
  110. UA_NodeStoreEntry *e = ns->entries[idx];
  111. if(e > UA_NODESTORE_TOMBSTONE &&
  112. UA_NodeId_equal(&e->node.nodeId, nodeid))
  113. return NULL;
  114. if(ns->entries[idx] <= UA_NODESTORE_TOMBSTONE)
  115. return &ns->entries[idx];
  116. idx += hash2;
  117. if(idx >= size)
  118. idx -= size;
  119. }
  120. /* NOTREACHED */
  121. return NULL;
  122. }
  123. /* The occupancy of the table after the call will be about 50% */
  124. static UA_StatusCode
  125. expand(UA_NodeStore *ns) {
  126. UA_UInt32 osize = ns->size;
  127. UA_UInt32 count = ns->count;
  128. /* Resize only when table after removal of unused elements is either too
  129. full or too empty */
  130. if(count * 2 < osize && (count * 8 > osize || osize <= UA_NODESTORE_MINSIZE))
  131. return UA_STATUSCODE_GOOD;
  132. UA_NodeStoreEntry **oentries = ns->entries;
  133. UA_UInt32 nindex = higher_prime_index(count * 2);
  134. UA_UInt32 nsize = primes[nindex];
  135. UA_NodeStoreEntry **nentries = UA_calloc(nsize, sizeof(UA_NodeStoreEntry*));
  136. if(!nentries)
  137. return UA_STATUSCODE_BADOUTOFMEMORY;
  138. ns->entries = nentries;
  139. ns->size = nsize;
  140. ns->sizePrimeIndex = nindex;
  141. /* recompute the position of every entry and insert the pointer */
  142. for(size_t i = 0, j = 0; i < osize && j < count; i++) {
  143. if(oentries[i] <= UA_NODESTORE_TOMBSTONE)
  144. continue;
  145. UA_NodeStoreEntry **e = findSlot(ns, &oentries[i]->node.nodeId);
  146. *e = oentries[i];
  147. j++;
  148. }
  149. UA_free(oentries);
  150. return UA_STATUSCODE_GOOD;
  151. }
  152. /**********************/
  153. /* Exported functions */
  154. /**********************/
  155. UA_NodeStore *
  156. UA_NodeStore_new(void) {
  157. UA_NodeStore *ns = UA_malloc(sizeof(UA_NodeStore));
  158. if(!ns)
  159. return NULL;
  160. ns->sizePrimeIndex = higher_prime_index(UA_NODESTORE_MINSIZE);
  161. ns->size = primes[ns->sizePrimeIndex];
  162. ns->count = 0;
  163. ns->entries = UA_calloc(ns->size, sizeof(UA_NodeStoreEntry*));
  164. if(!ns->entries) {
  165. UA_free(ns);
  166. return NULL;
  167. }
  168. return ns;
  169. }
  170. void
  171. UA_NodeStore_delete(UA_NodeStore *ns) {
  172. UA_UInt32 size = ns->size;
  173. UA_NodeStoreEntry **entries = ns->entries;
  174. for(UA_UInt32 i = 0; i < size; i++) {
  175. if(entries[i] > UA_NODESTORE_TOMBSTONE)
  176. deleteEntry(entries[i]);
  177. }
  178. UA_free(ns->entries);
  179. UA_free(ns);
  180. }
  181. UA_Node *
  182. UA_NodeStore_newNode(UA_NodeClass class) {
  183. UA_NodeStoreEntry *entry = instantiateEntry(class);
  184. if(!entry)
  185. return NULL;
  186. return (UA_Node*)&entry->node;
  187. }
  188. void
  189. UA_NodeStore_deleteNode(UA_Node *node) {
  190. deleteEntry(container_of(node, UA_NodeStoreEntry, node));
  191. }
  192. UA_StatusCode
  193. UA_NodeStore_insert(UA_NodeStore *ns, UA_Node *node) {
  194. if(ns->size * 3 <= ns->count * 4) {
  195. if(expand(ns) != UA_STATUSCODE_GOOD)
  196. return UA_STATUSCODE_BADINTERNALERROR;
  197. }
  198. UA_NodeId tempNodeid;
  199. tempNodeid = node->nodeId;
  200. tempNodeid.namespaceIndex = 0;
  201. UA_NodeStoreEntry **entry;
  202. if(UA_NodeId_isNull(&tempNodeid)) {
  203. /* create a random nodeid */
  204. if(node->nodeId.namespaceIndex == 0)
  205. node->nodeId.namespaceIndex = 1;
  206. UA_UInt32 identifier = ns->count+1; // start value
  207. UA_UInt32 size = ns->size;
  208. hash_t increase = mod2(identifier, size);
  209. while(true) {
  210. node->nodeId.identifier.numeric = identifier;
  211. entry = findSlot(ns, &node->nodeId);
  212. if(entry)
  213. break;
  214. identifier += increase;
  215. if(identifier >= size)
  216. identifier -= size;
  217. }
  218. } else {
  219. entry = findSlot(ns, &node->nodeId);
  220. if(!entry) {
  221. deleteEntry(container_of(node, UA_NodeStoreEntry, node));
  222. return UA_STATUSCODE_BADNODEIDEXISTS;
  223. }
  224. }
  225. *entry = container_of(node, UA_NodeStoreEntry, node);
  226. ns->count++;
  227. return UA_STATUSCODE_GOOD;
  228. }
  229. UA_StatusCode
  230. UA_NodeStore_replace(UA_NodeStore *ns, UA_Node *node) {
  231. UA_NodeStoreEntry **entry = findNode(ns, &node->nodeId);
  232. if(!entry)
  233. return UA_STATUSCODE_BADNODEIDUNKNOWN;
  234. UA_NodeStoreEntry *newEntry = container_of(node, UA_NodeStoreEntry, node);
  235. if(*entry != newEntry->orig) {
  236. // the node was replaced since the copy was made
  237. deleteEntry(newEntry);
  238. return UA_STATUSCODE_BADINTERNALERROR;
  239. }
  240. deleteEntry(*entry);
  241. *entry = newEntry;
  242. return UA_STATUSCODE_GOOD;
  243. }
  244. const UA_Node *
  245. UA_NodeStore_get(UA_NodeStore *ns, const UA_NodeId *nodeid) {
  246. UA_NodeStoreEntry **entry = findNode(ns, nodeid);
  247. if(!entry)
  248. return NULL;
  249. return (const UA_Node*)&(*entry)->node;
  250. }
  251. UA_Node *
  252. UA_NodeStore_getCopy(UA_NodeStore *ns, const UA_NodeId *nodeid) {
  253. UA_NodeStoreEntry **slot = findNode(ns, nodeid);
  254. if(!slot)
  255. return NULL;
  256. UA_NodeStoreEntry *entry = *slot;
  257. UA_NodeStoreEntry *new = instantiateEntry(entry->node.nodeClass);
  258. if(!new)
  259. return NULL;
  260. if(UA_Node_copyAnyNodeClass(&entry->node, &new->node) != UA_STATUSCODE_GOOD) {
  261. deleteEntry(new);
  262. return NULL;
  263. }
  264. new->orig = entry; // store the pointer to the original
  265. return &new->node;
  266. }
  267. UA_StatusCode
  268. UA_NodeStore_remove(UA_NodeStore *ns, const UA_NodeId *nodeid) {
  269. UA_NodeStoreEntry **slot = findNode(ns, nodeid);
  270. if(!slot)
  271. return UA_STATUSCODE_BADNODEIDUNKNOWN;
  272. deleteEntry(*slot);
  273. *slot = UA_NODESTORE_TOMBSTONE;
  274. ns->count--;
  275. /* Downsize the hashmap if it is very empty */
  276. if(ns->count * 8 < ns->size && ns->size > 32)
  277. expand(ns); // this can fail. we just continue with the bigger hashmap.
  278. return UA_STATUSCODE_GOOD;
  279. }
  280. void
  281. UA_NodeStore_iterate(UA_NodeStore *ns, UA_NodeStore_nodeVisitor visitor) {
  282. for(UA_UInt32 i = 0; i < ns->size; i++) {
  283. if(ns->entries[i] > UA_NODESTORE_TOMBSTONE)
  284. visitor((UA_Node*)&ns->entries[i]->node);
  285. }
  286. }
  287. #endif /* UA_ENABLE_MULTITHREADING */