store.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "store.h"
  2. #define ENTRY(a, b) case LSUP_STORE_##a: return &b;
  3. static inline const LSUP_StoreInt *
  4. select_interface (LSUP_StoreType be) {
  5. switch (be) {
  6. BACKEND_TBL
  7. default: return NULL;
  8. }
  9. }
  10. #undef ENTRY
  11. const LSUP_StoreInt *
  12. LSUP_store_int (LSUP_StoreType type) {
  13. switch (type) {
  14. #define ENTRY(a, b) \
  15. case LSUP_STORE_##a: return &b;
  16. BACKEND_TBL
  17. #undef ENTRY
  18. default: return NULL;
  19. }
  20. }
  21. LSUP_Store *
  22. LSUP_store_new (
  23. const LSUP_StoreType store_type, const char *store_id, size_t size)
  24. {
  25. if (UNLIKELY (!LSUP_IS_INIT)) {
  26. log_error (
  27. "Environment is not initialized. Did you call LSUP_init()?");
  28. return NULL;
  29. }
  30. const LSUP_StoreInt *sif = select_interface (store_type);
  31. if (UNLIKELY (!sif)) {
  32. log_error ("Not a valid store type: %d", store_type);
  33. return NULL;
  34. }
  35. LSUP_Store *store;
  36. MALLOC_GUARD (store, NULL);
  37. store->type = store_type;
  38. store->sif = sif;
  39. store->id = store_id ? strdup (store_id) : NULL;
  40. // TODO implement custom default context.
  41. store->data = store->sif->new_fn (store_id, size);
  42. return store;
  43. }
  44. void
  45. LSUP_store_free (LSUP_Store *store)
  46. {
  47. store->sif->free_fn (store->data);
  48. free (store);
  49. }