shm_table.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #include "precompile.h"
  2. #include <errno.h>
  3. #include "shm_table.h"
  4. #include "shm_mem.h"
  5. #include "list.h"
  6. #include "hash.h"
  7. #include <winpr/synch.h>
  8. #define HTABLE_SLOT_NUM 1023
  9. typedef struct shm_table_entry_t
  10. {
  11. struct hlist_node entry;
  12. char *key;
  13. void *data;
  14. int len;
  15. }shm_table_entry_t;
  16. typedef struct shm_table_core
  17. {
  18. struct hlist_head hlist[HTABLE_SLOT_NUM];
  19. int cnt;
  20. }shm_table_core;
  21. struct shm_table_t
  22. {
  23. shm_table_core *core;
  24. HANDLE hMutex;
  25. int first;
  26. };
  27. static HANDLE create_mutex(int shmkey)
  28. {
  29. char name[32];
  30. sprintf(name, "shm_table%08d", shmkey);
  31. return CreateMutexA(NULL, FALSE, name);
  32. }
  33. static shm_table_entry_t *find_entry(struct hlist_head *phead, const char *key)
  34. {
  35. shm_table_entry_t *tpos;
  36. struct hlist_node *pos;
  37. hlist_for_each_entry(tpos, pos, phead, shm_table_entry_t, entry) {
  38. if (strcmp(key, tpos->key) == 0)
  39. return tpos;
  40. }
  41. return NULL;
  42. }
  43. TOOLKIT_API int shm_table_create(shm_table_t **p_table)
  44. {
  45. int i;
  46. shm_table_t *table;
  47. shm_table_core *core;
  48. if (!p_table)
  49. return -1;
  50. table = (shm_table_t*)malloc(sizeof(shm_table_t));
  51. memset(table, 0, sizeof(shm_table_t));
  52. table->first = TRUE;
  53. core = shm_malloc(sizeof(shm_table_core));
  54. memset(core, 0, sizeof(shm_table_core));
  55. for (i = 0; i < HTABLE_SLOT_NUM; ++i)
  56. INIT_HLIST_HEAD(&core->hlist[i]);
  57. table->core = core;
  58. table->hMutex = create_mutex((int)core);
  59. if (!table->hMutex) {
  60. shm_table_destroy(table);
  61. return -1;
  62. }
  63. *p_table = table;
  64. return 0;
  65. }
  66. TOOLKIT_API int shm_table_open(void *core, shm_table_t **p_table)
  67. {
  68. shm_table_t *table;
  69. if (!p_table || !core)
  70. return -1;
  71. table = (shm_table_t*)malloc(sizeof(shm_table_t));
  72. memset(table, 0, sizeof(shm_table_t));
  73. table->first = FALSE;
  74. table->core = core;
  75. table->hMutex = create_mutex((int)core);
  76. if (!table->hMutex) {
  77. shm_table_destroy(table);
  78. return -1;
  79. }
  80. *p_table = table;
  81. return 0;
  82. }
  83. TOOLKIT_API void *shm_table_get_core_mem(shm_table_t *table)
  84. {
  85. return table->core;
  86. }
  87. TOOLKIT_API int shm_table_lock(shm_table_t *table, int timeout)
  88. {
  89. DWORD dwRet;
  90. dwRet = WaitForSingleObject(table->hMutex, (DWORD)timeout);
  91. return dwRet == WAIT_OBJECT_0 ? 0 : -1;
  92. }
  93. TOOLKIT_API int shm_table_unlock(shm_table_t *table)
  94. {
  95. return ReleaseMutex(table->hMutex) ? 0 : -1;
  96. }
  97. TOOLKIT_API int shm_table_destroy(shm_table_t *table)
  98. {
  99. if (table) {
  100. if (table->hMutex)
  101. CloseHandle(table->hMutex);
  102. if (table->first) {
  103. shm_free(table->core);
  104. }
  105. free(table);
  106. return 0;
  107. }
  108. return -1;
  109. }
  110. TOOLKIT_API int shm_table_set(shm_table_t *table, const char *key, const char *buf, int n)
  111. {
  112. int idx;
  113. shm_table_entry_t *e;
  114. shm_table_core *core;
  115. if (!table)
  116. return -1;
  117. if (n == -1 && buf)
  118. n = strlen(buf) + 1;
  119. core = table->core;
  120. idx = hash32_str(key, HASH32_STR_INIT) % HTABLE_SLOT_NUM;
  121. e = find_entry(&core->hlist[idx], key);
  122. if (e) { // update
  123. if (!buf) { // delete
  124. shm_free(e->key);
  125. shm_free(e->data);
  126. hlist_del(&e->entry);
  127. shm_free(e);
  128. } else {
  129. char *new_data = shm_malloc(n);
  130. if (!new_data)
  131. return -1;
  132. memcpy(new_data, buf, n);
  133. shm_free(e->data);
  134. e->data = new_data;
  135. e->len = n;
  136. }
  137. } else { // insert new
  138. if (!buf)
  139. return -1;
  140. e = (shm_table_entry_t*)shm_malloc(sizeof(shm_table_entry_t));
  141. if (!e)
  142. return -1;
  143. e->key = shm_strdup(key);
  144. if (!e->key) {
  145. shm_free(e);
  146. return -1;
  147. }
  148. e->data = shm_malloc(n);
  149. if (!e->data) {
  150. shm_free(e->key);
  151. shm_free(e);
  152. return -1;
  153. }
  154. memcpy(e->data, buf, n);
  155. e->len = n;
  156. hlist_add_head(&e->entry, &core->hlist[idx]);
  157. }
  158. return 0;
  159. }
  160. TOOLKIT_API int shm_table_get(shm_table_t *table, const char *key, char *buf, int *n)
  161. {
  162. int idx;
  163. shm_table_entry_t *e;
  164. shm_table_core *core;
  165. if (!table)
  166. return -1;
  167. core = table->core;
  168. idx = hash32_str(key, HASH32_STR_INIT) % HTABLE_SLOT_NUM;
  169. e = find_entry(&core->hlist[idx], key);
  170. if (!e)
  171. return -1;
  172. if (buf) {
  173. if (n) {
  174. if (*n < e->len) {
  175. *n = e->len;
  176. return -1;
  177. }
  178. }
  179. memcpy(buf, e->data, e->len);
  180. } else {
  181. if (n) {
  182. *n = e->len;
  183. } else {
  184. return -1;
  185. }
  186. }
  187. return 0;
  188. }
  189. TOOLKIT_API int shm_table_set_int(shm_table_t *table, const char *key, int val)
  190. {
  191. return shm_table_set(table, key, (char*)&val, sizeof(int));
  192. }
  193. TOOLKIT_API int shm_table_get_int(shm_table_t *table, const char *key, int* val)
  194. {
  195. if (!table || !val)
  196. return -1;
  197. return shm_table_get(table, key, (char*)val, NULL);
  198. }
  199. TOOLKIT_API int shm_table_incr_int(shm_table_t *table, const char *key, int *newvalue)
  200. {
  201. int idx;
  202. shm_table_entry_t *e;
  203. shm_table_core *core;
  204. if (!table)
  205. return -1;
  206. core = table->core;
  207. idx = hash32_str(key, HASH32_STR_INIT) % HTABLE_SLOT_NUM;
  208. e = find_entry(&core->hlist[idx], key);
  209. if (!e)
  210. return -1;
  211. if (e->len != sizeof(int))
  212. return -1;
  213. *(int*)e->data = *(int*)e->data + 1;
  214. if (newvalue)
  215. *newvalue = *(int*)e->data;
  216. return 0;
  217. }
  218. TOOLKIT_API int shm_table_decr_int(shm_table_t *table, const char *key, int *newvalue)
  219. {
  220. int idx;
  221. shm_table_entry_t *e;
  222. shm_table_core *core;
  223. if (!table)
  224. return -1;
  225. core = table->core;
  226. idx = hash32_str(key, HASH32_STR_INIT) % HTABLE_SLOT_NUM;
  227. e = find_entry(&core->hlist[idx], key);
  228. if (!e)
  229. return -1;
  230. if (e->len != sizeof(int))
  231. return -1;
  232. *(int*)e->data = *(int*)e->data - 1;
  233. if (newvalue)
  234. *newvalue = *(int*)e->data;
  235. return 0;
  236. }