libresman

annotate 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
rev   line source
nuclear@5 1 #include <stdio.h>
nuclear@5 2 #include <stdlib.h>
nuclear@5 3 #include <string.h>
nuclear@5 4 #include <assert.h>
nuclear@5 5 #include "resman.h"
nuclear@5 6 #include "dynarr.h"
nuclear@5 7 #include "threadpool.h"
nuclear@5 8
nuclear@5 9 struct resource {
nuclear@5 10 char *name;
nuclear@5 11 void *data;
nuclear@5 12 };
nuclear@5 13
nuclear@5 14 struct resman {
nuclear@5 15 struct resource *res;
nuclear@5 16
nuclear@5 17 resman_load_func load_func;
nuclear@5 18 resman_create_func create_func;
nuclear@5 19 resman_update_func update_func;
nuclear@5 20 resman_destroy_func destroy_func;
nuclear@5 21
nuclear@5 22 void *load_func_cls;
nuclear@5 23 void *create_func_cls;
nuclear@5 24 void *update_func_cls;
nuclear@5 25 void *destroy_func_cls;
nuclear@5 26 };
nuclear@5 27
nuclear@5 28
nuclear@5 29 static int find_resource(struct resman *rman, const char *fname);
nuclear@5 30 static int add_resource(struct resman *rman, const char *fname, void *data);
nuclear@5 31
nuclear@5 32 struct resman *resman_create(void)
nuclear@5 33 {
nuclear@5 34 struct resman *rman = malloc(sizeof *rman);
nuclear@5 35 if(resman_init(rman) == -1) {
nuclear@5 36 free(rman);
nuclear@5 37 return 0;
nuclear@5 38 }
nuclear@5 39 return rman;
nuclear@5 40 }
nuclear@5 41
nuclear@5 42 void resman_free(struct resman *rman)
nuclear@5 43 {
nuclear@5 44 resman_destroy(rman);
nuclear@5 45 free(rman);
nuclear@5 46 }
nuclear@5 47
nuclear@5 48 int resman_init(struct resman *rman)
nuclear@5 49 {
nuclear@5 50 memset(rman, 0, sizeof *rman);
nuclear@5 51
nuclear@5 52 if(!(rman->res = dynarr_alloc(0, sizeof *rman->res))) {
nuclear@5 53 return -1;
nuclear@5 54 }
nuclear@5 55
nuclear@5 56 return 0;
nuclear@5 57 }
nuclear@5 58
nuclear@5 59 void resman_destroy(struct resman *rman)
nuclear@5 60 {
nuclear@5 61 int i;
nuclear@5 62 if(!rman) return;
nuclear@5 63
nuclear@5 64 for(i=0; i<dynarr_size(rman->res); i++) {
nuclear@5 65 if(rman->destroy_func) {
nuclear@5 66 rman->destroy_func(rman->res[i].data, rman->destroy_func_cls);
nuclear@5 67 }
nuclear@5 68 }
nuclear@5 69 dynarr_free(rman->res);
nuclear@5 70 }
nuclear@5 71
nuclear@5 72
nuclear@5 73 void resman_set_load_func(struct resman *rman, resman_load_func func, void *cls)
nuclear@5 74 {
nuclear@5 75 rman->load_func = func;
nuclear@5 76 rman->load_func_cls = cls;
nuclear@5 77 }
nuclear@5 78
nuclear@5 79 void resman_set_create_func(struct resman *rman, resman_create_func func, void *cls)
nuclear@5 80 {
nuclear@5 81 rman->create_func = func;
nuclear@5 82 rman->create_func_cls = cls;
nuclear@5 83 }
nuclear@5 84
nuclear@5 85 void resman_set_update_func(struct resman *rman, resman_update_func func, void *cls)
nuclear@5 86 {
nuclear@5 87 rman->update_func = func;
nuclear@5 88 rman->update_func_cls = cls;
nuclear@5 89 }
nuclear@5 90
nuclear@5 91 void resman_set_destroy_func(struct resman *rman, resman_destroy_func func, void *cls)
nuclear@5 92 {
nuclear@5 93 rman->destroy_func = func;
nuclear@5 94 rman->destroy_func_cls = cls;
nuclear@5 95 }
nuclear@5 96
nuclear@5 97 int resman_lookup(struct resman *rman, const char *fname, void *data)
nuclear@5 98 {
nuclear@5 99 int ridx;
nuclear@5 100
nuclear@5 101 if((ridx = find_resource(rman, fname)) != -1) {
nuclear@5 102 return ridx;
nuclear@5 103 }
nuclear@5 104
nuclear@5 105 /* resource not found, create a new one and start a loading job */
nuclear@5 106 return add_resource(rman, fname, data);
nuclear@5 107 }
nuclear@5 108
nuclear@5 109 void resman_wait(struct resman *rman, int id)
nuclear@5 110 {
nuclear@5 111 /* TODO */
nuclear@5 112 }
nuclear@5 113
nuclear@5 114 int resman_poll(struct resman *rman)
nuclear@5 115 {
nuclear@5 116 /* TODO */
nuclear@5 117 return 0;
nuclear@5 118 }
nuclear@5 119
nuclear@5 120
nuclear@5 121 void resman_set_res_data(struct resman *rman, int res_id, void *data)
nuclear@5 122 {
nuclear@5 123 if(res_id >= 0 && res_id < dynarr_size(rman->res)) {
nuclear@5 124 rman->res[res_id].data = data;
nuclear@5 125 }
nuclear@5 126 }
nuclear@5 127
nuclear@5 128 void *resman_get_res_data(struct resman *rman, int res_id)
nuclear@5 129 {
nuclear@5 130 if(res_id >= 0 && res_id < dynarr_size(rman->res)) {
nuclear@5 131 return rman->res[res_id].data;
nuclear@5 132 }
nuclear@5 133 return 0;
nuclear@5 134 }
nuclear@5 135
nuclear@5 136 static int find_resource(struct resman *rman, const char *fname)
nuclear@5 137 {
nuclear@5 138 int i, sz = dynarr_size(rman->res);
nuclear@5 139
nuclear@5 140 for(i=0; i<sz; i++) {
nuclear@5 141 if(strcmp(rman->res[i].name, fname) == 0) {
nuclear@5 142 return i;
nuclear@5 143 }
nuclear@5 144 }
nuclear@5 145 return -1;
nuclear@5 146 }
nuclear@5 147
nuclear@5 148 static int add_resource(struct resman *rman, const char *fname, void *data)
nuclear@5 149 {
nuclear@5 150 int idx = dynarr_size(rman->res);
nuclear@5 151
nuclear@5 152 struct resource *tmp = dynarr_push(rman->res, 0);
nuclear@5 153 if(!tmp) {
nuclear@5 154 return -1;
nuclear@5 155 }
nuclear@5 156 rman->res = tmp;
nuclear@5 157
nuclear@5 158 rman->res[idx].name = strdup(fname);
nuclear@5 159 assert(rman->res[idx].name);
nuclear@5 160
nuclear@5 161 rman->res[idx].data = data;
nuclear@5 162
nuclear@5 163 /* TODO start a loading job ... */
nuclear@5 164
nuclear@5 165 return idx;
nuclear@5 166 }