2 /* $NetBSD: citrus_mapper_zone.c,v 1.4 2003/07/12 15:39:21 tshiozak Exp $ */
5 * SPDX-License-Identifier: BSD-2-Clause
7 * Copyright (c)2003 Citrus Project,
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 #include <sys/queue.h>
41 #include "citrus_namespace.h"
42 #include "citrus_types.h"
43 #include "citrus_bcs.h"
44 #include "citrus_module.h"
45 #include "citrus_region.h"
46 #include "citrus_memstream.h"
47 #include "citrus_mmap.h"
48 #include "citrus_hash.h"
49 #include "citrus_mapper.h"
50 #include "citrus_mapper_zone.h"
52 /* ---------------------------------------------------------------------- */
54 _CITRUS_MAPPER_DECLS(mapper_zone);
55 _CITRUS_MAPPER_DEF_OPS(mapper_zone);
58 /* ---------------------------------------------------------------------- */
65 struct _citrus_mapper_zone {
68 int32_t mz_col_offset;
69 int32_t mz_row_offset;
74 enum { S_BEGIN, S_OFFSET } ps_state;
80 #define ps_u_imm u.u_imm
81 #define ps_s_imm u.s_imm
82 #define ps_zone u.zone
87 _citrus_mapper_zone_mapper_getops(struct _citrus_mapper_ops *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)
104 char buf[BUFSIZE + 1];
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)
194 if (get_tok(ms, ps) != T_IMM)
196 z->z_begin = ps->ps_u_imm;
197 if (get_tok(ms, ps) != '-')
199 if (get_tok(ms, ps) != T_IMM)
201 z->z_end = ps->ps_u_imm;
203 if (z->z_begin > z->z_end)
210 check_rowcol(struct _zone *z, int32_t ofs, uint32_t maxval)
214 if (maxval != 0 && z->z_end >= maxval)
219 /* this should 0x100000000 - z->z_end */
220 remain = (z->z_end == 0) ? 0xFFFFFFFF :
221 0xFFFFFFFF - z->z_end + 1;
223 remain = maxval - z->z_end;
224 if ((uint32_t)ofs > remain)
226 } else if (ofs < 0) {
227 if (z->z_begin < (uint32_t)-ofs)
235 parse_var(struct _citrus_mapper_zone *mz, struct _memstream *ms)
237 struct _parse_state ps;
238 uint32_t colmax, rowmax;
241 ps.ps_state = S_BEGIN;
243 if (parse_zone(ms, &ps, &mz->mz_col))
246 ret = get_tok(ms, &ps);
248 /* rowzone / colzone / bits */
250 mz->mz_row = mz->mz_col;
252 if (parse_zone(ms, &ps, &mz->mz_col))
254 if (get_tok(ms, &ps) != '/')
256 if (get_tok(ms, &ps) != T_IMM)
258 mz->mz_col_bits = ps.ps_u_imm;
259 if (mz->mz_col_bits < 0 || mz->mz_col_bits > 32)
261 ret = get_tok(ms, &ps);
265 mz->mz_col_bits = 32;
266 mz->mz_row.z_begin = mz->mz_row.z_end = 0;
270 ps.ps_state = S_OFFSET;
271 if (get_tok(ms, &ps) != T_IMM)
273 mz->mz_col_offset = ps.ps_s_imm;
276 mz->mz_row_offset = mz->mz_col_offset;
277 if (get_tok(ms, &ps) != '/')
279 if (get_tok(ms, &ps) != T_IMM)
281 mz->mz_col_offset = ps.ps_s_imm;
283 mz->mz_row_offset = 0;
284 ret = get_tok(ms, &ps);
290 colmax = (mz->mz_col_bits == 32) ? 0 : 1 << mz->mz_col_bits;
291 rowmax = (mz->mz_col_bits == 0) ? 0 : 1 << (32-mz->mz_col_bits);
292 if (check_rowcol(&mz->mz_col, mz->mz_col_offset, colmax))
294 if (check_rowcol(&mz->mz_row, mz->mz_row_offset, rowmax))
302 _citrus_mapper_zone_mapper_init(struct _citrus_mapper_area *__restrict ma __unused,
303 struct _citrus_mapper * __restrict cm, const char * __restrict dir __unused,
304 const void * __restrict var, size_t lenvar,
305 struct _citrus_mapper_traits * __restrict mt, size_t lenmt)
307 struct _citrus_mapper_zone *mz;
308 struct _memstream ms;
311 if (lenmt < sizeof(*mt))
314 mz = malloc(sizeof(*mz));
318 mz->mz_col.z_begin = mz->mz_col.z_end = 0;
319 mz->mz_row.z_begin = mz->mz_row.z_end = 0;
321 mz->mz_row_offset = 0;
322 mz->mz_col_offset = 0;
324 _region_init(&r, __DECONST(void *, var), lenvar);
325 _memstream_bind(&ms, &r);
326 if (parse_var(mz, &ms)) {
331 mt->mt_src_max = mt->mt_dst_max = 1; /* 1:1 converter */
332 mt->mt_state_size = 0; /* stateless */
339 _citrus_mapper_zone_mapper_uninit(struct _citrus_mapper *cm __unused)
346 _citrus_mapper_zone_mapper_convert(struct _citrus_mapper * __restrict cm,
347 _citrus_index_t * __restrict dst, _citrus_index_t src,
348 void * __restrict ps __unused)
350 struct _citrus_mapper_zone *mz = cm->cm_closure;
353 if (mz->mz_col_bits == 32) {
356 if (col < mz->mz_col.z_begin || col > mz->mz_col.z_end)
357 return (_CITRUS_MAPPER_CONVERT_NONIDENTICAL);
358 if (mz->mz_col_offset > 0)
359 col += (uint32_t)mz->mz_col_offset;
361 col -= (uint32_t)-mz->mz_col_offset;
364 col = src & (((uint32_t)1 << mz->mz_col_bits) - 1);
365 row = src >> mz->mz_col_bits;
366 if (row < mz->mz_row.z_begin || row > mz->mz_row.z_end ||
367 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 += (uint32_t)mz->mz_col_offset;
372 col -= (uint32_t)-mz->mz_col_offset;
373 if (mz->mz_row_offset > 0)
374 row += (uint32_t)mz->mz_row_offset;
376 row -= (uint32_t)-mz->mz_row_offset;
377 *dst = col | (row << mz->mz_col_bits);
379 return (_CITRUS_MAPPER_CONVERT_SUCCESS);
384 _citrus_mapper_zone_mapper_init_state(void)