shm_table.c 5.2 KB

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