libresman
diff src/resman.c @ 5:bd9b4ff19c93
more stuff
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 01 Feb 2014 08:02:08 +0200 |
parents | |
children | 4d18498a0078 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/resman.c Sat Feb 01 08:02:08 2014 +0200 1.3 @@ -0,0 +1,166 @@ 1.4 +#include <stdio.h> 1.5 +#include <stdlib.h> 1.6 +#include <string.h> 1.7 +#include <assert.h> 1.8 +#include "resman.h" 1.9 +#include "dynarr.h" 1.10 +#include "threadpool.h" 1.11 + 1.12 +struct resource { 1.13 + char *name; 1.14 + void *data; 1.15 +}; 1.16 + 1.17 +struct resman { 1.18 + struct resource *res; 1.19 + 1.20 + resman_load_func load_func; 1.21 + resman_create_func create_func; 1.22 + resman_update_func update_func; 1.23 + resman_destroy_func destroy_func; 1.24 + 1.25 + void *load_func_cls; 1.26 + void *create_func_cls; 1.27 + void *update_func_cls; 1.28 + void *destroy_func_cls; 1.29 +}; 1.30 + 1.31 + 1.32 +static int find_resource(struct resman *rman, const char *fname); 1.33 +static int add_resource(struct resman *rman, const char *fname, void *data); 1.34 + 1.35 +struct resman *resman_create(void) 1.36 +{ 1.37 + struct resman *rman = malloc(sizeof *rman); 1.38 + if(resman_init(rman) == -1) { 1.39 + free(rman); 1.40 + return 0; 1.41 + } 1.42 + return rman; 1.43 +} 1.44 + 1.45 +void resman_free(struct resman *rman) 1.46 +{ 1.47 + resman_destroy(rman); 1.48 + free(rman); 1.49 +} 1.50 + 1.51 +int resman_init(struct resman *rman) 1.52 +{ 1.53 + memset(rman, 0, sizeof *rman); 1.54 + 1.55 + if(!(rman->res = dynarr_alloc(0, sizeof *rman->res))) { 1.56 + return -1; 1.57 + } 1.58 + 1.59 + return 0; 1.60 +} 1.61 + 1.62 +void resman_destroy(struct resman *rman) 1.63 +{ 1.64 + int i; 1.65 + if(!rman) return; 1.66 + 1.67 + for(i=0; i<dynarr_size(rman->res); i++) { 1.68 + if(rman->destroy_func) { 1.69 + rman->destroy_func(rman->res[i].data, rman->destroy_func_cls); 1.70 + } 1.71 + } 1.72 + dynarr_free(rman->res); 1.73 +} 1.74 + 1.75 + 1.76 +void resman_set_load_func(struct resman *rman, resman_load_func func, void *cls) 1.77 +{ 1.78 + rman->load_func = func; 1.79 + rman->load_func_cls = cls; 1.80 +} 1.81 + 1.82 +void resman_set_create_func(struct resman *rman, resman_create_func func, void *cls) 1.83 +{ 1.84 + rman->create_func = func; 1.85 + rman->create_func_cls = cls; 1.86 +} 1.87 + 1.88 +void resman_set_update_func(struct resman *rman, resman_update_func func, void *cls) 1.89 +{ 1.90 + rman->update_func = func; 1.91 + rman->update_func_cls = cls; 1.92 +} 1.93 + 1.94 +void resman_set_destroy_func(struct resman *rman, resman_destroy_func func, void *cls) 1.95 +{ 1.96 + rman->destroy_func = func; 1.97 + rman->destroy_func_cls = cls; 1.98 +} 1.99 + 1.100 +int resman_lookup(struct resman *rman, const char *fname, void *data) 1.101 +{ 1.102 + int ridx; 1.103 + 1.104 + if((ridx = find_resource(rman, fname)) != -1) { 1.105 + return ridx; 1.106 + } 1.107 + 1.108 + /* resource not found, create a new one and start a loading job */ 1.109 + return add_resource(rman, fname, data); 1.110 +} 1.111 + 1.112 +void resman_wait(struct resman *rman, int id) 1.113 +{ 1.114 + /* TODO */ 1.115 +} 1.116 + 1.117 +int resman_poll(struct resman *rman) 1.118 +{ 1.119 + /* TODO */ 1.120 + return 0; 1.121 +} 1.122 + 1.123 + 1.124 +void resman_set_res_data(struct resman *rman, int res_id, void *data) 1.125 +{ 1.126 + if(res_id >= 0 && res_id < dynarr_size(rman->res)) { 1.127 + rman->res[res_id].data = data; 1.128 + } 1.129 +} 1.130 + 1.131 +void *resman_get_res_data(struct resman *rman, int res_id) 1.132 +{ 1.133 + if(res_id >= 0 && res_id < dynarr_size(rman->res)) { 1.134 + return rman->res[res_id].data; 1.135 + } 1.136 + return 0; 1.137 +} 1.138 + 1.139 +static int find_resource(struct resman *rman, const char *fname) 1.140 +{ 1.141 + int i, sz = dynarr_size(rman->res); 1.142 + 1.143 + for(i=0; i<sz; i++) { 1.144 + if(strcmp(rman->res[i].name, fname) == 0) { 1.145 + return i; 1.146 + } 1.147 + } 1.148 + return -1; 1.149 +} 1.150 + 1.151 +static int add_resource(struct resman *rman, const char *fname, void *data) 1.152 +{ 1.153 + int idx = dynarr_size(rman->res); 1.154 + 1.155 + struct resource *tmp = dynarr_push(rman->res, 0); 1.156 + if(!tmp) { 1.157 + return -1; 1.158 + } 1.159 + rman->res = tmp; 1.160 + 1.161 + rman->res[idx].name = strdup(fname); 1.162 + assert(rman->res[idx].name); 1.163 + 1.164 + rman->res[idx].data = data; 1.165 + 1.166 + /* TODO start a loading job ... */ 1.167 + 1.168 + return idx; 1.169 +}