kustore.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /** @file kustore.c
  2. */
  3. #include <assert.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <faux/list.h>
  8. #include <klish/khelper.h>
  9. #include <klish/kudata.h>
  10. #include <klish/kustore.h>
  11. struct kustore_s {
  12. faux_list_t *udatas;
  13. };
  14. // User data blobs list
  15. KCMP_NESTED(ustore, udata, name);
  16. KCMP_NESTED_BY_KEY(ustore, udata, name);
  17. KADD_NESTED(ustore, kudata_t *, udatas);
  18. KFIND_NESTED(ustore, udata);
  19. kustore_t *kustore_new()
  20. {
  21. kustore_t *ustore = NULL;
  22. ustore = faux_zmalloc(sizeof(*ustore));
  23. assert(ustore);
  24. if (!ustore)
  25. return NULL;
  26. // User data blobs list
  27. ustore->udatas = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  28. kustore_udata_compare, kustore_udata_kcompare,
  29. (void (*)(void *))kudata_free);
  30. assert(ustore->udatas);
  31. return ustore;
  32. }
  33. void kustore_free(kustore_t *ustore)
  34. {
  35. if (!ustore)
  36. return;
  37. faux_list_free(ustore->udatas);
  38. free(ustore);
  39. }
  40. kudata_t *kustore_slot_new(kustore_t *ustore,
  41. const char *name, void *data, kudata_data_free_fn free_fn)
  42. {
  43. kudata_t *udata = NULL;
  44. assert(ustore);
  45. if (!ustore)
  46. return NULL;
  47. if (!name)
  48. return NULL;
  49. udata = kudata_new(name);
  50. if (!udata)
  51. return NULL;
  52. kudata_set_data(udata, data);
  53. kudata_set_free_fn(udata, free_fn);
  54. return udata;
  55. }
  56. void *kustore_slot_data(kustore_t *ustore, const char *udata_name)
  57. {
  58. kudata_t *udata = NULL;
  59. assert(ustore);
  60. if (!ustore)
  61. return NULL;
  62. if (!udata_name)
  63. return NULL;
  64. udata = kustore_find_udata(ustore, udata_name);
  65. if (!udata)
  66. return NULL;
  67. return kudata_data(udata);
  68. }