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