ua_nodestore.c 8.7 KB

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