1 /* $NetBSD: src/lib/libc/citrus/modules/citrus_mapper_zone.c,v 1.4 2003/07/12 15:39:21 tshiozak Exp $ */
2 /* $DragonFly: src/lib/libc/citrus/modules/citrus_mapper_zone.c,v 1.2 2008/04/10 10:21:02 hasso Exp $ */
5 * Copyright (c)2003 Citrus Project,
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/types.h>
31 #include <sys/queue.h>
38 #include "citrus_namespace.h"
39 #include "citrus_types.h"
40 #include "citrus_bcs.h"
41 #include "citrus_module.h"
42 #include "citrus_region.h"
43 #include "citrus_memstream.h"
44 #include "citrus_mmap.h"
45 #include "citrus_hash.h"
46 #include "citrus_mapper.h"
47 #include "citrus_mapper_zone.h"
49 /* ---------------------------------------------------------------------- */
51 _CITRUS_MAPPER_DECLS(mapper_zone);
52 _CITRUS_MAPPER_DEF_OPS(mapper_zone);
55 /* ---------------------------------------------------------------------- */
62 struct _citrus_mapper_zone {
66 int32_t mz_row_offset;
67 int32_t mz_col_offset;
71 enum { S_BEGIN, S_OFFSET } ps_state;
77 #define ps_u_imm u.u_imm
78 #define ps_s_imm u.s_imm
79 #define ps_zone u.zone
84 _citrus_mapper_zone_mapper_getops(struct _citrus_mapper_ops *ops,
85 size_t lenops, uint32_t expected_version)
87 if (expected_version<_CITRUS_MAPPER_ABI_VERSION || lenops<sizeof(*ops))
90 memcpy(ops, &_citrus_mapper_zone_mapper_ops,
91 sizeof(_citrus_mapper_zone_mapper_ops));
101 get_imm(struct _memstream *ms, struct _parse_state *ps)
105 char buf[BUFSIZE+1], *p;
107 for (i=0; i<BUFSIZE; i++) {
109 c = _memstream_peek(ms);
111 if (sign == 0 && (c == '+' || c == '-')) {
115 } else if (!_bcs_isdigit(c))
117 } else if (!_bcs_isxdigit(c))
118 if (!(i==1 && c == 'x'))
120 buf[i] = _memstream_getc(ms);
123 ps->ps_u_imm = strtoul(buf, &p, 0);
127 ps->ps_u_imm = (unsigned long)-(long)ps->ps_u_imm;
132 get_tok(struct _memstream *ms, struct _parse_state *ps)
137 c = _memstream_peek(ms);
140 if (_bcs_isspace(c)) {
145 switch (ps->ps_state) {
163 return get_imm(ms, ps);
183 return get_imm(ms, ps);
191 parse_zone(struct _memstream *ms, struct _parse_state *ps, struct _zone *z)
193 if (get_tok(ms, ps) != T_IMM)
195 z->z_begin = ps->ps_u_imm;
196 if (get_tok(ms, ps) != '-')
198 if (get_tok(ms, ps) != T_IMM)
200 z->z_end = ps->ps_u_imm;
202 if (z->z_begin > z->z_end)
209 check_rowcol(struct _zone *z, int32_t ofs, uint32_t maxval)
213 if (maxval != 0 && z->z_end >= maxval)
218 /* this should 0x100000000 - z->z_end */
222 remain = 0xFFFFFFFF - z->z_end + 1;
225 remain = maxval - z->z_end;
226 if ((u_int32_t)ofs > remain)
228 } else if (ofs < 0) {
229 if (z->z_begin < (u_int32_t)-ofs)
237 parse_var(struct _citrus_mapper_zone *mz, struct _memstream *ms)
239 struct _parse_state ps;
241 uint32_t rowmax, colmax;
243 ps.ps_state = S_BEGIN;
245 if (parse_zone(ms, &ps, &mz->mz_col))
248 ret = get_tok(ms, &ps);
250 /* rowzone / colzone / bits */
252 mz->mz_row = mz->mz_col;
254 if (parse_zone(ms, &ps, &mz->mz_col))
256 if (get_tok(ms, &ps) != '/')
258 if (get_tok(ms, &ps) != T_IMM)
260 mz->mz_col_bits = ps.ps_u_imm;
261 if (mz->mz_col_bits<0 || mz->mz_col_bits>32)
263 ret = get_tok(ms, &ps);
267 mz->mz_col_bits = 32;
268 mz->mz_row.z_begin = mz->mz_row.z_end = 0;
272 ps.ps_state = S_OFFSET;
273 if (get_tok(ms, &ps) != T_IMM)
275 mz->mz_col_offset = ps.ps_s_imm;
278 mz->mz_row_offset = mz->mz_col_offset;
279 if (get_tok(ms, &ps) != '/')
281 if (get_tok(ms, &ps) != T_IMM)
283 mz->mz_col_offset = ps.ps_s_imm;
285 mz->mz_row_offset = 0;
286 ret = get_tok(ms, &ps);
292 if (mz->mz_col_bits==32)
295 colmax = 1 << mz->mz_col_bits;
296 if (mz->mz_col_bits==0)
299 rowmax = 1 << (32-mz->mz_col_bits);
300 if (check_rowcol(&mz->mz_col, mz->mz_col_offset, colmax))
302 if (check_rowcol(&mz->mz_row, mz->mz_row_offset, rowmax))
310 _citrus_mapper_zone_mapper_init(struct _citrus_mapper_area *__restrict ma,
311 struct _citrus_mapper * __restrict cm,
312 const char * __restrict dir,
313 const void * __restrict var, size_t lenvar,
314 struct _citrus_mapper_traits * __restrict mt,
317 struct _citrus_mapper_zone *mz;
318 struct _memstream ms;
321 _DIAGASSERT(cm && dir && mt);
323 if (lenmt<sizeof(*mt))
326 mz = malloc(sizeof(*mz));
330 mz->mz_col.z_begin = mz->mz_col.z_end = 0;
331 mz->mz_row.z_begin = mz->mz_row.z_end = 0;
333 mz->mz_row_offset = 0;
334 mz->mz_col_offset = 0;
336 _region_init(&r, (void *)var, lenvar);
337 _memstream_bind(&ms, &r);
338 if (parse_var(mz, &ms)) {
343 mt->mt_src_max = mt->mt_dst_max = 1; /* 1:1 converter */
344 mt->mt_state_size = 0; /* stateless */
351 _citrus_mapper_zone_mapper_uninit(struct _citrus_mapper *cm)
357 _citrus_mapper_zone_mapper_convert(struct _citrus_mapper * __restrict cm,
358 _citrus_index_t * __restrict dst,
359 _citrus_index_t src, void * __restrict ps)
362 struct _citrus_mapper_zone *mz = cm->cm_closure;
364 if (mz->mz_col_bits == 32) {
367 if (col < mz->mz_col.z_begin || col > mz->mz_col.z_end)
368 return _CITRUS_MAPPER_CONVERT_NONIDENTICAL;
369 if (mz->mz_col_offset>0)
370 col += (u_int32_t)mz->mz_col_offset;
372 col -= (u_int32_t)-mz->mz_col_offset;
375 col = src & (((u_int32_t)1<<mz->mz_col_bits)-1);
376 row = src >> mz->mz_col_bits;
377 if (row < mz->mz_row.z_begin || row > mz->mz_row.z_end ||
378 col < mz->mz_col.z_begin || col > mz->mz_col.z_end)
379 return _CITRUS_MAPPER_CONVERT_NONIDENTICAL;
380 if (mz->mz_col_offset>0)
381 col += (u_int32_t)mz->mz_col_offset;
383 col -= (u_int32_t)-mz->mz_col_offset;
384 if (mz->mz_row_offset>0)
385 row += (u_int32_t)mz->mz_row_offset;
387 row -= (u_int32_t)-mz->mz_row_offset;
388 *dst = col | (row << mz->mz_col_bits);
390 return _CITRUS_MAPPER_CONVERT_SUCCESS;
395 _citrus_mapper_zone_mapper_init_state(struct _citrus_mapper * __restrict cm,
396 void * __restrict ps)