VM: simplify slab allocator
[minix.git] / lib / libsys / ds.c
blobf5976d89fecee1666b7aad9efb6cbf3ed19584f4
2 #include <minix/ds.h>
3 #include <string.h>
5 #include "syslib.h"
7 static message m;
9 static int do_invoke_ds(int type, const char *ds_name)
11 cp_grant_id_t g_key;
12 size_t len_key;
13 int access, r;
15 if(type == DS_CHECK || type == DS_RETRIEVE_LABEL) {
16 len_key = DS_MAX_KEYLEN;
17 access = CPF_WRITE;
18 } else {
19 len_key = strlen(ds_name)+1;
20 access = CPF_READ;
23 /* Grant for key. */
24 g_key = cpf_grant_direct(DS_PROC_NR, (vir_bytes) ds_name,
25 len_key, access);
26 if(!GRANT_VALID(g_key))
27 return errno;
29 m.DS_KEY_GRANT = g_key;
30 m.DS_KEY_LEN = len_key;
32 r = _taskcall(DS_PROC_NR, type, &m);
34 cpf_revoke(g_key);
35 return r;
38 int ds_publish_label(const char *ds_name, endpoint_t endpoint, int flags)
40 m.DS_VAL = (u32_t) endpoint;
41 m.DS_FLAGS = DSF_TYPE_LABEL | flags;
42 return do_invoke_ds(DS_PUBLISH, ds_name);
45 int ds_publish_u32(const char *ds_name, u32_t value, int flags)
47 m.DS_VAL = value;
48 m.DS_FLAGS = DSF_TYPE_U32 | flags;
49 return do_invoke_ds(DS_PUBLISH, ds_name);
52 static int ds_publish_raw(const char *ds_name, void *vaddr, size_t length,
53 int flags)
55 cp_grant_id_t gid;
56 int r;
58 /* Grant for memory range. */
59 gid = cpf_grant_direct(DS_PROC_NR, (vir_bytes)vaddr, length, CPF_READ);
60 if(!GRANT_VALID(gid))
61 return errno;
63 m.DS_VAL = gid;
64 m.DS_VAL_LEN = length;
65 m.DS_FLAGS = flags;
67 r = do_invoke_ds(DS_PUBLISH, ds_name);
68 cpf_revoke(gid);
70 return r;
73 int ds_publish_str(const char *ds_name, char *value, int flags)
75 size_t length;
76 length = strlen(value) + 1;
77 value[length - 1] = '\0';
78 return ds_publish_raw(ds_name, value, length, flags | DSF_TYPE_STR);
81 int ds_publish_mem(const char *ds_name, void *vaddr, size_t length, int flags)
83 return ds_publish_raw(ds_name, vaddr, length, flags | DSF_TYPE_MEM);
86 int ds_publish_map(const char *ds_name, void *vaddr, size_t length, int flags)
88 cp_grant_id_t gid;
89 int r;
91 if(((vir_bytes)vaddr % CLICK_SIZE != 0) || (length % CLICK_SIZE != 0))
92 return EINVAL;
94 /* Grant for mapped memory range. */
95 gid = cpf_grant_direct(DS_PROC_NR, (vir_bytes)vaddr, length,
96 CPF_READ | CPF_MAP);
97 if(!GRANT_VALID(gid))
98 return errno;
100 m.DS_VAL = gid;
101 m.DS_VAL_LEN = length;
102 m.DS_FLAGS = DSF_TYPE_MAP | flags;
104 r = do_invoke_ds(DS_PUBLISH, ds_name);
106 return r;
109 int ds_snapshot_map(const char *ds_name, int *nr_snapshot)
111 int r;
112 r = do_invoke_ds(DS_SNAPSHOT, ds_name);
113 *nr_snapshot = m.DS_NR_SNAPSHOT;
114 return r;
117 int ds_retrieve_label_name(char *ds_name, endpoint_t endpoint)
119 int r;
120 m.DS_VAL = (u32_t) endpoint;
121 r = do_invoke_ds(DS_RETRIEVE_LABEL, ds_name);
122 return r;
125 int ds_retrieve_label_endpt(const char *ds_name, endpoint_t *endpoint)
127 int r;
128 m.DS_FLAGS = DSF_TYPE_LABEL;
129 r = do_invoke_ds(DS_RETRIEVE, ds_name);
130 *endpoint = (endpoint_t) m.DS_VAL;
131 return r;
134 int ds_retrieve_u32(const char *ds_name, u32_t *value)
136 int r;
137 m.DS_FLAGS = DSF_TYPE_U32;
138 r = do_invoke_ds(DS_RETRIEVE, ds_name);
139 *value = m.DS_VAL;
140 return r;
143 static int ds_retrieve_raw(const char *ds_name, char *vaddr, size_t *length,
144 int flags)
146 cp_grant_id_t gid;
147 int r;
149 /* Grant for memory range. */
150 gid = cpf_grant_direct(DS_PROC_NR, (vir_bytes)vaddr, *length, CPF_WRITE);
151 if(!GRANT_VALID(gid))
152 return errno;
154 m.DS_VAL = gid;
155 m.DS_VAL_LEN = *length;
156 m.DS_FLAGS = flags;
157 r = do_invoke_ds(DS_RETRIEVE, ds_name);
158 *length = m.DS_VAL_LEN;
159 cpf_revoke(gid);
161 return r;
164 int ds_retrieve_str(const char *ds_name, char *value, size_t len_str)
166 int r;
167 size_t length = len_str + 1;
168 r = ds_retrieve_raw(ds_name, value, &length, DSF_TYPE_STR);
169 value[length - 1] = '\0';
170 return r;
173 int ds_retrieve_mem(const char *ds_name, char *vaddr, size_t *length)
175 return ds_retrieve_raw(ds_name, vaddr, length, DSF_TYPE_MEM);
178 int ds_retrieve_map(const char *ds_name, char *vaddr, size_t *length,
179 int nr_snapshot, int flags)
181 cp_grant_id_t gid;
182 int r;
184 /* Map a mapped memory range. */
185 if(flags & DSMF_MAP_MAPPED) {
186 /* Request DS to grant. */
187 m.DS_FLAGS = DSF_TYPE_MAP | DSMF_MAP_MAPPED;
188 r = do_invoke_ds(DS_RETRIEVE, ds_name);
189 if(r != OK)
190 return r;
192 /* Do the safemap. */
193 if(*length > (size_t) m.DS_VAL_LEN)
194 *length = (size_t) m.DS_VAL_LEN;
195 *length = (size_t) CLICK_FLOOR(*length);
196 r = sys_safemap(DS_PROC_NR, m.DS_VAL, 0,
197 (vir_bytes)vaddr, *length, 0);
199 /* Copy mapped memory range or a snapshot. */
200 } else if(flags & (DSMF_COPY_MAPPED|DSMF_COPY_SNAPSHOT)) {
201 /* Grant for memory range first. */
202 gid = cpf_grant_direct(DS_PROC_NR, (vir_bytes)vaddr,
203 *length, CPF_WRITE);
204 if(!GRANT_VALID(gid))
205 return errno;
207 m.DS_VAL = gid;
208 m.DS_VAL_LEN = *length;
209 if(flags & DSMF_COPY_MAPPED) {
210 m.DS_FLAGS = DSF_TYPE_MAP | DSMF_COPY_MAPPED;
212 else {
213 m.DS_NR_SNAPSHOT = nr_snapshot;
214 m.DS_FLAGS = DSF_TYPE_MAP | DSMF_COPY_SNAPSHOT;
216 r = do_invoke_ds(DS_RETRIEVE, ds_name);
217 *length = m.DS_VAL_LEN;
218 cpf_revoke(gid);
220 else {
221 return EINVAL;
224 return r;
227 int ds_delete_u32(const char *ds_name)
229 m.DS_FLAGS = DSF_TYPE_U32;
230 return do_invoke_ds(DS_DELETE, ds_name);
233 int ds_delete_str(const char *ds_name)
235 m.DS_FLAGS = DSF_TYPE_STR;
236 return do_invoke_ds(DS_DELETE, ds_name);
239 int ds_delete_mem(const char *ds_name)
241 m.DS_FLAGS = DSF_TYPE_MEM;
242 return do_invoke_ds(DS_DELETE, ds_name);
245 int ds_delete_map(const char *ds_name)
247 m.DS_FLAGS = DSF_TYPE_MAP;
248 return do_invoke_ds(DS_DELETE, ds_name);
251 int ds_delete_label(const char *ds_name)
253 m.DS_FLAGS = DSF_TYPE_LABEL;
254 return do_invoke_ds(DS_DELETE, ds_name);
257 int ds_subscribe(const char *regexp, int flags)
259 m.DS_FLAGS = flags;
260 return do_invoke_ds(DS_SUBSCRIBE, regexp);
263 int ds_check(char *ds_key, int *type, endpoint_t *owner_e)
265 int r;
266 r = do_invoke_ds(DS_CHECK, ds_key);
267 if(type) *type = m.DS_FLAGS;
268 if(owner_e) *owner_e = m.DS_OWNER;
269 return r;