ua_nodestore.c 8.7 KB

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