obs-data.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /******************************************************************************
  2. Copyright (C) 2014 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "util/bmem.h"
  15. #include "util/darray.h"
  16. #include "obs-data.h"
  17. struct obs_data_item {
  18. volatile int ref;
  19. struct obs_data *parent;
  20. struct obs_data_item *next;
  21. enum obs_data_type type;
  22. size_t name_len;
  23. size_t data_len;
  24. size_t capacity;
  25. };
  26. struct obs_data {
  27. volatile int ref;
  28. char *json;
  29. struct obs_data_item *first_item;
  30. };
  31. struct obs_data_array {
  32. volatile int ref;
  33. DARRAY(obs_data_t) objects;
  34. };
  35. /* ------------------------------------------------------------------------- */
  36. /* Item structure, designed to be one allocation only */
  37. /* ensures data after the name has 16 byte alignment (in case of SSE) */
  38. static inline size_t get_name_align_size(const char *name)
  39. {
  40. size_t name_size = strlen(name) + 1;
  41. size_t total_size = sizeof(struct obs_data_item) + (name_size + 15);
  42. return (total_size & 0xFFFFFFF0) - sizeof(struct obs_data_item);
  43. }
  44. static inline char *get_item_name(struct obs_data_item *item)
  45. {
  46. return (char*)item + sizeof(struct obs_data_item);
  47. }
  48. static inline void *get_item_data(struct obs_data_item *item)
  49. {
  50. return (uint8_t*)get_item_name(item) + item->name_len;
  51. }
  52. static inline size_t obs_data_item_total_size(struct obs_data_item *item)
  53. {
  54. return sizeof(struct obs_data_item) + item->data_len + item->name_len;
  55. }
  56. static inline obs_data_t get_item_obj(struct obs_data_item *item)
  57. {
  58. return *(obs_data_t*)get_item_data(item);
  59. }
  60. static inline obs_data_array_t get_item_array(struct obs_data_item *item)
  61. {
  62. return *(obs_data_array_t*)get_item_data(item);
  63. }
  64. static inline void item_data_release(struct obs_data_item *item)
  65. {
  66. if (item->type == OBS_DATA_OBJECT) {
  67. obs_data_t obj = get_item_obj(item);
  68. obs_data_release(obj);
  69. } else if (item->type == OBS_DATA_ARRAY) {
  70. obs_data_array_t array = get_item_array(item);
  71. obs_data_array_release(array);
  72. }
  73. }
  74. static inline void item_data_addref(struct obs_data_item *item)
  75. {
  76. if (item->type == OBS_DATA_OBJECT) {
  77. obs_data_t obj = get_item_obj(item);
  78. obs_data_addref(obj);
  79. } else if (item->type == OBS_DATA_ARRAY) {
  80. obs_data_array_t array = get_item_array(item);
  81. obs_data_array_addref(array);
  82. }
  83. }
  84. static struct obs_data_item *obs_data_item_create(const char *name,
  85. const void *data, size_t size, enum obs_data_type type)
  86. {
  87. struct obs_data_item *item;
  88. size_t name_size, total_size;
  89. if (!name || !data)
  90. return NULL;
  91. name_size = get_name_align_size(name);
  92. total_size = name_size + sizeof(struct obs_data_item) + size;
  93. item = bmalloc(total_size);
  94. memset(item, 0, total_size);
  95. item->capacity = total_size;
  96. item->type = type;
  97. item->name_len = name_size;
  98. item->data_len = size;
  99. item->ref = 1;
  100. strcpy(get_item_name(item), name);
  101. memcpy(get_item_data(item), data, size);
  102. item_data_addref(item);
  103. return item;
  104. }
  105. static struct obs_data_item **get_item_prev_next(struct obs_data *data,
  106. struct obs_data_item *current)
  107. {
  108. if (!current || !data)
  109. return NULL;
  110. struct obs_data_item **prev_next = &data->first_item;
  111. struct obs_data_item *item = data->first_item;
  112. while (item) {
  113. if (item == current)
  114. return prev_next;
  115. prev_next = &item->next;
  116. item = item->next;
  117. }
  118. return NULL;
  119. }
  120. static inline void obs_data_item_detach(struct obs_data_item *item)
  121. {
  122. struct obs_data_item **prev_next = get_item_prev_next(item->parent,
  123. item);
  124. if (prev_next) {
  125. *prev_next = item->next;
  126. item->next = NULL;
  127. }
  128. }
  129. static inline void obs_data_item_reattach(struct obs_data_item *old_ptr,
  130. struct obs_data_item *new_ptr)
  131. {
  132. struct obs_data_item **prev_next = get_item_prev_next(new_ptr->parent,
  133. old_ptr);
  134. if (prev_next)
  135. *prev_next = new_ptr;
  136. }
  137. static struct obs_data_item *obs_data_item_ensure_capacity(
  138. struct obs_data_item *item)
  139. {
  140. size_t new_size = obs_data_item_total_size(item);
  141. struct obs_data_item *new_item;
  142. if (item->capacity >= new_size)
  143. return item;
  144. new_item = brealloc(item, new_size);
  145. new_item->capacity = new_size;
  146. obs_data_item_reattach(item, new_item);
  147. return new_item;
  148. }
  149. static inline void obs_data_item_destroy(struct obs_data_item *item)
  150. {
  151. item_data_release(item);
  152. obs_data_item_detach(item);
  153. bfree(item);
  154. }
  155. static inline void obs_data_item_setdata(
  156. struct obs_data_item **p_item, const void *data, size_t size,
  157. enum obs_data_type type)
  158. {
  159. if (!p_item || !*p_item)
  160. return;
  161. struct obs_data_item *item = *p_item;
  162. item_data_release(item);
  163. item->data_len = size;
  164. item->type = type;
  165. item = obs_data_item_ensure_capacity(item);
  166. if (size) {
  167. memcpy(get_item_data(item), data, size);
  168. item_data_addref(item);
  169. }
  170. *p_item = item;
  171. }
  172. /* ------------------------------------------------------------------------- */
  173. obs_data_t obs_data_create()
  174. {
  175. struct obs_data *data = bmalloc(sizeof(struct obs_data));
  176. memset(data, 0, sizeof(struct obs_data));
  177. data->ref = 1;
  178. return data;
  179. }
  180. obs_data_t obs_data_create_from_json(const char *json_string)
  181. {
  182. /* TODO */
  183. return NULL;
  184. }
  185. int obs_data_addref(obs_data_t data)
  186. {
  187. return ++data->ref;
  188. }
  189. static inline obs_data_destroy(struct obs_data *data)
  190. {
  191. struct obs_data_item *item = data->first_item;
  192. while (item) {
  193. struct obs_data_item *next = item->next;
  194. obs_data_item_release(&item);
  195. item = next;
  196. }
  197. bfree(data->json);
  198. bfree(data);
  199. }
  200. int obs_data_release(obs_data_t data)
  201. {
  202. if (!data)
  203. return 0;
  204. int ref = --data->ref;
  205. if (!ref)
  206. obs_data_destroy(data);
  207. return ref;
  208. }
  209. const char *obs_data_getjson(obs_data_t data)
  210. {
  211. /* TODO */
  212. return data->json;
  213. }
  214. static struct obs_data_item *get_item(struct obs_data *data, const char *name)
  215. {
  216. struct obs_data_item *item = data->first_item;
  217. while (item) {
  218. if (strcmp(get_item_name(item), name) == 0)
  219. return item;
  220. item = item->next;
  221. }
  222. return NULL;
  223. }
  224. static inline struct obs_data_item *get_item_of(struct obs_data *data,
  225. const char *name, enum obs_data_type type)
  226. {
  227. struct obs_data_item *item = get_item(data, name);
  228. return (item->type == type) ? item : NULL;
  229. }
  230. static void set_item(struct obs_data *data, const char *name, const void *ptr,
  231. size_t size, enum obs_data_type type)
  232. {
  233. struct obs_data_item *item = get_item(data, name);
  234. if (!item) {
  235. item = obs_data_item_create(name, ptr, size, type);
  236. item->next = data->first_item;
  237. item->parent = data;
  238. data->first_item = item;
  239. } else {
  240. obs_data_item_setdata(&item, ptr, size, type);
  241. }
  242. }
  243. void obs_data_erase(obs_data_t data, const char *name)
  244. {
  245. struct obs_data_item *item = get_item(data, name);
  246. obs_data_item_detach(item);
  247. obs_data_item_release(&item);
  248. }
  249. void obs_data_setstring(obs_data_t data, const char *name, const char *val)
  250. {
  251. if (!val) val = "";
  252. set_item(data, name, val, strlen(val)+1, OBS_DATA_STRING);
  253. }
  254. void obs_data_setint(obs_data_t data, const char *name, long long val)
  255. {
  256. double f_val = (double)val;
  257. set_item(data, name, &f_val, sizeof(double), OBS_DATA_NUMBER);
  258. }
  259. void obs_data_setdouble(obs_data_t data, const char *name, double val)
  260. {
  261. set_item(data, name, &val, sizeof(double), OBS_DATA_NUMBER);
  262. }
  263. void obs_data_setbool(obs_data_t data, const char *name, bool val)
  264. {
  265. set_item(data, name, &val, sizeof(bool), OBS_DATA_BOOLEAN);
  266. }
  267. void obs_data_setobj(obs_data_t data, const char *name, obs_data_t obj)
  268. {
  269. set_item(data, name, &obj, sizeof(obs_data_t), OBS_DATA_OBJECT);
  270. }
  271. void obs_data_setarray(obs_data_t data, const char *name,
  272. obs_data_array_t array)
  273. {
  274. set_item(data, name, &array, sizeof(obs_data_t), OBS_DATA_ARRAY);
  275. }
  276. const char *obs_data_getstring(obs_data_t data, const char *name,
  277. const char *def)
  278. {
  279. struct obs_data_item *item = get_item_of(data, name, OBS_DATA_STRING);
  280. return item ? get_item_data(item) : def;
  281. }
  282. long long obs_data_getint(obs_data_t data, const char *name, long long def)
  283. {
  284. struct obs_data_item *item = get_item_of(data, name, OBS_DATA_NUMBER);
  285. return item ? (long long)*(double*)get_item_data(item) : def;
  286. }
  287. double obs_data_getdouble(obs_data_t data, const char *name, double def)
  288. {
  289. struct obs_data_item *item = get_item_of(data, name, OBS_DATA_NUMBER);
  290. return item ? *(double*)get_item_data(item) : def;
  291. }
  292. bool obs_data_getbool(obs_data_t data, const char *name, bool def)
  293. {
  294. struct obs_data_item *item = get_item_of(data, name, OBS_DATA_BOOLEAN);
  295. return item ? *(bool*)get_item_data(item) : def;
  296. }
  297. obs_data_t obs_data_getobj(obs_data_t data, const char *name)
  298. {
  299. struct obs_data_item *item = get_item_of(data, name, OBS_DATA_OBJECT);
  300. obs_data_t obj = get_item_obj(item);
  301. if (obj)
  302. obj->ref++;
  303. return obj;
  304. }
  305. obs_data_array_t obs_data_getarray(obs_data_t data, const char *name)
  306. {
  307. struct obs_data_item *item = get_item_of(data, name, OBS_DATA_ARRAY);
  308. obs_data_array_t array = get_item_array(item);
  309. if (array)
  310. array->ref++;
  311. return array;
  312. }
  313. obs_data_array_t obs_data_array_create()
  314. {
  315. struct obs_data_array *array = bmalloc(sizeof(struct obs_data_array));
  316. memset(array, 0, sizeof(struct obs_data_array));
  317. array->ref = 1;
  318. return array;
  319. }
  320. int obs_data_array_addref(obs_data_array_t array)
  321. {
  322. return ++array->ref;
  323. }
  324. static void obs_data_array_destroy(obs_data_array_t array)
  325. {
  326. for (size_t i = 0; i < array->objects.num; i++)
  327. obs_data_release(array->objects.array[i]);
  328. da_free(array->objects);
  329. bfree(array);
  330. }
  331. int obs_data_array_release(obs_data_array_t array)
  332. {
  333. int ref = --array->ref;
  334. if (!ref)
  335. obs_data_array_destroy(array);
  336. return ref;
  337. }
  338. size_t obs_data_array_count(obs_data_array_t array)
  339. {
  340. return array->objects.num;
  341. }
  342. obs_data_t obs_data_array_item(obs_data_array_t array, size_t idx)
  343. {
  344. obs_data_t data;
  345. data = (idx < array->objects.num) ? array->objects.array[idx] : NULL;
  346. if (data)
  347. data->ref++;
  348. return data;
  349. }
  350. size_t obs_data_array_push_back(obs_data_array_t array, obs_data_t obj)
  351. {
  352. obj->ref++;
  353. return da_push_back(array->objects, &obj);
  354. }
  355. void obs_data_array_insert(obs_data_array_t array, size_t idx, obs_data_t obj)
  356. {
  357. obj->ref++;
  358. da_insert(array->objects, idx, &obj);
  359. }
  360. void obs_data_array_erase(obs_data_array_t array, size_t idx)
  361. {
  362. obs_data_release(array->objects.array[idx]);
  363. da_erase(array->objects, idx);
  364. }
  365. /* ------------------------------------------------------------------------- */
  366. /* Item iteration */
  367. obs_data_item_t obs_data_first(obs_data_t data)
  368. {
  369. if (data->first_item)
  370. data->first_item->ref++;
  371. return data->first_item;
  372. }
  373. obs_data_item_t obs_data_item_byname(obs_data_t data, const char *name)
  374. {
  375. struct obs_data_item *item = get_item(data, name);
  376. if (item)
  377. item->ref++;
  378. return item;
  379. }
  380. bool obs_data_item_next(obs_data_item_t *item)
  381. {
  382. if (item && *item) {
  383. (*item)->ref--;
  384. *item = (*item)->next;
  385. if (*item) {
  386. (*item)->ref++;
  387. return true;
  388. }
  389. }
  390. return false;
  391. }
  392. void obs_data_item_release(obs_data_item_t *item)
  393. {
  394. if (item && *item) {
  395. int ref = --(*item)->ref;
  396. if (!ref) {
  397. obs_data_item_destroy(*item);
  398. *item = NULL;
  399. }
  400. }
  401. }
  402. void obs_data_item_remove(obs_data_item_t *item)
  403. {
  404. if (item && *item) {
  405. obs_data_item_detach(*item);
  406. obs_data_item_release(item);
  407. }
  408. }
  409. enum obs_data_type obs_data_item_gettype(obs_data_item_t item)
  410. {
  411. return item->type;
  412. }
  413. void obs_data_item_setstring(obs_data_item_t *item, const char *val)
  414. {
  415. obs_data_item_setdata(item, val, strlen(val)+1, OBS_DATA_STRING);
  416. }
  417. void obs_data_item_setint(obs_data_item_t *item, long long val)
  418. {
  419. double f_val = (double)val;
  420. obs_data_item_setdata(item, &f_val, sizeof(double), OBS_DATA_NUMBER);
  421. }
  422. void obs_data_item_setdouble(obs_data_item_t *item, double val)
  423. {
  424. obs_data_item_setdata(item, &val, sizeof(double), OBS_DATA_NUMBER);
  425. }
  426. void obs_data_item_setbool(obs_data_item_t *item, bool val)
  427. {
  428. obs_data_item_setdata(item, &val, sizeof(bool), OBS_DATA_BOOLEAN);
  429. }
  430. void obs_data_item_setobj(obs_data_item_t *item, obs_data_t val)
  431. {
  432. obs_data_item_setdata(item, &val, sizeof(obs_data_t), OBS_DATA_OBJECT);
  433. }
  434. void obs_data_item_setarray(obs_data_item_t *item, obs_data_array_t val)
  435. {
  436. obs_data_item_setdata(item, &val, sizeof(obs_data_array_t),
  437. OBS_DATA_ARRAY);
  438. }
  439. static inline bool item_valid(struct obs_data_item *item,
  440. enum obs_data_type type)
  441. {
  442. return item && item->type == type;
  443. }
  444. const char *obs_data_item_getstring(obs_data_item_t item, const char *def)
  445. {
  446. return item_valid(item, OBS_DATA_STRING) ? get_item_data(item) : def;
  447. }
  448. long long obs_data_item_getint(obs_data_item_t item, long long def)
  449. {
  450. return item_valid(item, OBS_DATA_NUMBER) ?
  451. (long long)*(double*)get_item_data(item) : def;
  452. }
  453. double obs_data_item_getdouble(obs_data_item_t item, double def)
  454. {
  455. return item_valid(item, OBS_DATA_NUMBER) ?
  456. *(double*)get_item_data(item) : def;
  457. }
  458. bool obs_data_item_getbool(obs_data_item_t item, bool def)
  459. {
  460. return item_valid(item, OBS_DATA_BOOLEAN) ?
  461. *(bool*)get_item_data(item) : def;
  462. }
  463. obs_data_t obs_data_item_getobj(obs_data_item_t item)
  464. {
  465. obs_data_t obj = item_valid(item, OBS_DATA_OBJECT) ?
  466. get_item_obj(item) : NULL;
  467. if (obj)
  468. obj->ref++;
  469. return obj;
  470. }
  471. obs_data_array_t obs_data_item_getarray(obs_data_item_t item)
  472. {
  473. obs_data_array_t array = item_valid(item, OBS_DATA_ARRAY) ?
  474. get_item_array(item) : NULL;
  475. if (array)
  476. array->ref++;
  477. return array;
  478. }