nuclear@5: #include nuclear@5: #include nuclear@5: #include nuclear@5: #include nuclear@5: #include "resman.h" nuclear@5: #include "dynarr.h" nuclear@5: #include "threadpool.h" nuclear@5: nuclear@5: struct resource { nuclear@5: char *name; nuclear@5: void *data; nuclear@5: }; nuclear@5: nuclear@5: struct resman { nuclear@5: struct resource *res; nuclear@5: nuclear@5: resman_load_func load_func; nuclear@5: resman_create_func create_func; nuclear@5: resman_update_func update_func; nuclear@5: resman_destroy_func destroy_func; nuclear@5: nuclear@5: void *load_func_cls; nuclear@5: void *create_func_cls; nuclear@5: void *update_func_cls; nuclear@5: void *destroy_func_cls; nuclear@5: }; nuclear@5: nuclear@5: nuclear@5: static int find_resource(struct resman *rman, const char *fname); nuclear@5: static int add_resource(struct resman *rman, const char *fname, void *data); nuclear@5: nuclear@5: struct resman *resman_create(void) nuclear@5: { nuclear@5: struct resman *rman = malloc(sizeof *rman); nuclear@5: if(resman_init(rman) == -1) { nuclear@5: free(rman); nuclear@5: return 0; nuclear@5: } nuclear@5: return rman; nuclear@5: } nuclear@5: nuclear@5: void resman_free(struct resman *rman) nuclear@5: { nuclear@5: resman_destroy(rman); nuclear@5: free(rman); nuclear@5: } nuclear@5: nuclear@5: int resman_init(struct resman *rman) nuclear@5: { nuclear@5: memset(rman, 0, sizeof *rman); nuclear@5: nuclear@5: if(!(rman->res = dynarr_alloc(0, sizeof *rman->res))) { nuclear@5: return -1; nuclear@5: } nuclear@5: nuclear@5: return 0; nuclear@5: } nuclear@5: nuclear@5: void resman_destroy(struct resman *rman) nuclear@5: { nuclear@5: int i; nuclear@5: if(!rman) return; nuclear@5: nuclear@5: for(i=0; ires); i++) { nuclear@5: if(rman->destroy_func) { nuclear@5: rman->destroy_func(rman->res[i].data, rman->destroy_func_cls); nuclear@5: } nuclear@5: } nuclear@5: dynarr_free(rman->res); nuclear@5: } nuclear@5: nuclear@5: nuclear@5: void resman_set_load_func(struct resman *rman, resman_load_func func, void *cls) nuclear@5: { nuclear@5: rman->load_func = func; nuclear@5: rman->load_func_cls = cls; nuclear@5: } nuclear@5: nuclear@5: void resman_set_create_func(struct resman *rman, resman_create_func func, void *cls) nuclear@5: { nuclear@5: rman->create_func = func; nuclear@5: rman->create_func_cls = cls; nuclear@5: } nuclear@5: nuclear@5: void resman_set_update_func(struct resman *rman, resman_update_func func, void *cls) nuclear@5: { nuclear@5: rman->update_func = func; nuclear@5: rman->update_func_cls = cls; nuclear@5: } nuclear@5: nuclear@5: void resman_set_destroy_func(struct resman *rman, resman_destroy_func func, void *cls) nuclear@5: { nuclear@5: rman->destroy_func = func; nuclear@5: rman->destroy_func_cls = cls; nuclear@5: } nuclear@5: nuclear@5: int resman_lookup(struct resman *rman, const char *fname, void *data) nuclear@5: { nuclear@5: int ridx; nuclear@5: nuclear@5: if((ridx = find_resource(rman, fname)) != -1) { nuclear@5: return ridx; nuclear@5: } nuclear@5: nuclear@5: /* resource not found, create a new one and start a loading job */ nuclear@5: return add_resource(rman, fname, data); nuclear@5: } nuclear@5: nuclear@5: void resman_wait(struct resman *rman, int id) nuclear@5: { nuclear@5: /* TODO */ nuclear@5: } nuclear@5: nuclear@5: int resman_poll(struct resman *rman) nuclear@5: { nuclear@5: /* TODO */ nuclear@5: return 0; nuclear@5: } nuclear@5: nuclear@5: nuclear@5: void resman_set_res_data(struct resman *rman, int res_id, void *data) nuclear@5: { nuclear@5: if(res_id >= 0 && res_id < dynarr_size(rman->res)) { nuclear@5: rman->res[res_id].data = data; nuclear@5: } nuclear@5: } nuclear@5: nuclear@5: void *resman_get_res_data(struct resman *rman, int res_id) nuclear@5: { nuclear@5: if(res_id >= 0 && res_id < dynarr_size(rman->res)) { nuclear@5: return rman->res[res_id].data; nuclear@5: } nuclear@5: return 0; nuclear@5: } nuclear@5: nuclear@5: static int find_resource(struct resman *rman, const char *fname) nuclear@5: { nuclear@5: int i, sz = dynarr_size(rman->res); nuclear@5: nuclear@5: for(i=0; ires[i].name, fname) == 0) { nuclear@5: return i; nuclear@5: } nuclear@5: } nuclear@5: return -1; nuclear@5: } nuclear@5: nuclear@5: static int add_resource(struct resman *rman, const char *fname, void *data) nuclear@5: { nuclear@5: int idx = dynarr_size(rman->res); nuclear@5: nuclear@5: struct resource *tmp = dynarr_push(rman->res, 0); nuclear@5: if(!tmp) { nuclear@5: return -1; nuclear@5: } nuclear@5: rman->res = tmp; nuclear@5: nuclear@5: rman->res[idx].name = strdup(fname); nuclear@5: assert(rman->res[idx].name); nuclear@5: nuclear@5: rman->res[idx].data = data; nuclear@5: nuclear@5: /* TODO start a loading job ... */ nuclear@5: nuclear@5: return idx; nuclear@5: }