1 /* $NetBSD: citrus_mapper_zone.c,v 1.4 2003/07/12 15:39:21 tshiozak Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c)2003 Citrus Project,
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 #include <sys/queue.h>
40 #include "citrus_namespace.h"
41 #include "citrus_types.h"
42 #include "citrus_bcs.h"
43 #include "citrus_module.h"
44 #include "citrus_region.h"
45 #include "citrus_memstream.h"
46 #include "citrus_mmap.h"
47 #include "citrus_hash.h"
48 #include "citrus_mapper.h"
49 #include "citrus_mapper_zone.h"
51 /* ---------------------------------------------------------------------- */
53 _CITRUS_MAPPER_DECLS(mapper_zone);
54 _CITRUS_MAPPER_DEF_OPS(mapper_zone);
57 /* ---------------------------------------------------------------------- */
64 struct _citrus_mapper_zone {
67 int32_t mz_col_offset;
68 int32_t mz_row_offset;
73 enum { S_BEGIN, S_OFFSET } ps_state;
79 #define ps_u_imm u.u_imm
80 #define ps_s_imm u.s_imm
81 #define ps_zone u.zone
86 _citrus_mapper_zone_mapper_getops(struct _citrus_mapper_ops *ops)
89 memcpy(ops, &_citrus_mapper_zone_mapper_ops,
90 sizeof(_citrus_mapper_zone_mapper_ops));
100 get_imm(struct _memstream *ms, struct _parse_state *ps)
103 char buf[BUFSIZE + 1];
106 for (i = 0; i < BUFSIZE; i++) {
108 c = _memstream_peek(ms);
110 if (sign == 0 && (c == '+' || c == '-')) {
114 } else if (!_bcs_isdigit(c))
116 } else if (!_bcs_isxdigit(c))
117 if (!(i == 1 && c == 'x'))
119 buf[i] = _memstream_getc(ms);
122 ps->ps_u_imm = strtoul(buf, &p, 0);
126 ps->ps_u_imm = (unsigned long)-(long)ps->ps_u_imm;
131 get_tok(struct _memstream *ms, struct _parse_state *ps)
136 c = _memstream_peek(ms);
139 if (_bcs_isspace(c)) {
144 switch (ps->ps_state) {
162 return (get_imm(ms, ps));
182 return (get_imm(ms, ps));
190 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 */
219 remain = (z->z_end == 0) ? 0xFFFFFFFF :
220 0xFFFFFFFF - z->z_end + 1;
222 remain = maxval - z->z_end;
223 if ((uint32_t)ofs > remain)
225 } else if (ofs < 0) {
226 if (z->z_begin < (uint32_t)-ofs)
234 parse_var(struct _citrus_mapper_zone *mz, struct _memstream *ms)
236 struct _parse_state ps;
237 uint32_t colmax, rowmax;
240 ps.ps_state = S_BEGIN;
242 if (parse_zone(ms, &ps, &mz->mz_col))
245 ret = get_tok(ms, &ps);
247 /* rowzone / colzone / bits */
249 mz->mz_row = mz->mz_col;
251 if (parse_zone(ms, &ps, &mz->mz_col))
253 if (get_tok(ms, &ps) != '/')
255 if (get_tok(ms, &ps) != T_IMM)
257 mz->mz_col_bits = ps.ps_u_imm;
258 if (mz->mz_col_bits < 0 || mz->mz_col_bits > 32)
260 ret = get_tok(ms, &ps);
264 mz->mz_col_bits = 32;
265 mz->mz_row.z_begin = mz->mz_row.z_end = 0;
269 ps.ps_state = S_OFFSET;
270 if (get_tok(ms, &ps) != T_IMM)
272 mz->mz_col_offset = ps.ps_s_imm;
275 mz->mz_row_offset = mz->mz_col_offset;
276 if (get_tok(ms, &ps) != '/')
278 if (get_tok(ms, &ps) != T_IMM)
280 mz->mz_col_offset = ps.ps_s_imm;
282 mz->mz_row_offset = 0;
283 ret = get_tok(ms, &ps);
289 colmax = (mz->mz_col_bits == 32) ? 0 : 1 << mz->mz_col_bits;
290 rowmax = (mz->mz_col_bits == 0) ? 0 : 1 << (32-mz->mz_col_bits);
291 if (check_rowcol(&mz->mz_col, mz->mz_col_offset, colmax))
293 if (check_rowcol(&mz->mz_row, mz->mz_row_offset, rowmax))
301 _citrus_mapper_zone_mapper_init(struct _citrus_mapper_area *__restrict ma __unused,
302 struct _citrus_mapper * __restrict cm, const char * __restrict dir __unused,
303 const void * __restrict var, size_t lenvar,
304 struct _citrus_mapper_traits * __restrict mt, size_t lenmt)
306 struct _citrus_mapper_zone *mz;
307 struct _memstream ms;
310 if (lenmt < sizeof(*mt))
313 mz = malloc(sizeof(*mz));
317 mz->mz_col.z_begin = mz->mz_col.z_end = 0;
318 mz->mz_row.z_begin = mz->mz_row.z_end = 0;
320 mz->mz_row_offset = 0;
321 mz->mz_col_offset = 0;
323 _region_init(&r, __DECONST(void *, var), lenvar);
324 _memstream_bind(&ms, &r);
325 if (parse_var(mz, &ms)) {
330 mt->mt_src_max = mt->mt_dst_max = 1; /* 1:1 converter */
331 mt->mt_state_size = 0; /* stateless */
338 _citrus_mapper_zone_mapper_uninit(struct _citrus_mapper *cm __unused)
345 _citrus_mapper_zone_mapper_convert(struct _citrus_mapper * __restrict cm,
346 _citrus_index_t * __restrict dst, _citrus_index_t src,
347 void * __restrict ps __unused)
349 struct _citrus_mapper_zone *mz = cm->cm_closure;
352 if (mz->mz_col_bits == 32) {
355 if (col < mz->mz_col.z_begin || col > mz->mz_col.z_end)
356 return (_CITRUS_MAPPER_CONVERT_NONIDENTICAL);
357 if (mz->mz_col_offset > 0)
358 col += (uint32_t)mz->mz_col_offset;
360 col -= (uint32_t)-mz->mz_col_offset;
363 col = src & (((uint32_t)1 << mz->mz_col_bits) - 1);
364 row = src >> mz->mz_col_bits;
365 if (row < mz->mz_row.z_begin || row > mz->mz_row.z_end ||
366 col < mz->mz_col.z_begin || col > mz->mz_col.z_end)
367 return (_CITRUS_MAPPER_CONVERT_NONIDENTICAL);
368 if (mz->mz_col_offset > 0)
369 col += (uint32_t)mz->mz_col_offset;
371 col -= (uint32_t)-mz->mz_col_offset;
372 if (mz->mz_row_offset > 0)
373 row += (uint32_t)mz->mz_row_offset;
375 row -= (uint32_t)-mz->mz_row_offset;
376 *dst = col | (row << mz->mz_col_bits);
378 return (_CITRUS_MAPPER_CONVERT_SUCCESS);
383 _citrus_mapper_zone_mapper_init_state(void)