xref: /minix3/lib/libc/citrus/modules/citrus_mapper_serial.c (revision 2fe8fb192fe7e8720e3e7a77f928da545e872a6a)
1*2fe8fb19SBen Gras /*	$NetBSD: citrus_mapper_serial.c,v 1.2 2003/07/12 15:39:20 tshiozak Exp $	*/
2*2fe8fb19SBen Gras 
3*2fe8fb19SBen Gras /*-
4*2fe8fb19SBen Gras  * Copyright (c)2003 Citrus Project,
5*2fe8fb19SBen Gras  * All rights reserved.
6*2fe8fb19SBen Gras  *
7*2fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
8*2fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
9*2fe8fb19SBen Gras  * are met:
10*2fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
11*2fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
12*2fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
13*2fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
14*2fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
15*2fe8fb19SBen Gras  *
16*2fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*2fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*2fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*2fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*2fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*2fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*2fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*2fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*2fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*2fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*2fe8fb19SBen Gras  * SUCH DAMAGE.
27*2fe8fb19SBen Gras  */
28*2fe8fb19SBen Gras 
29*2fe8fb19SBen Gras #include <sys/cdefs.h>
30*2fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
31*2fe8fb19SBen Gras __RCSID("$NetBSD: citrus_mapper_serial.c,v 1.2 2003/07/12 15:39:20 tshiozak Exp $");
32*2fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
33*2fe8fb19SBen Gras 
34*2fe8fb19SBen Gras #include <assert.h>
35*2fe8fb19SBen Gras #include <errno.h>
36*2fe8fb19SBen Gras #include <stdio.h>
37*2fe8fb19SBen Gras #include <stdlib.h>
38*2fe8fb19SBen Gras #include <string.h>
39*2fe8fb19SBen Gras #include <limits.h>
40*2fe8fb19SBen Gras #include <sys/queue.h>
41*2fe8fb19SBen Gras 
42*2fe8fb19SBen Gras #include "citrus_namespace.h"
43*2fe8fb19SBen Gras #include "citrus_types.h"
44*2fe8fb19SBen Gras #include "citrus_bcs.h"
45*2fe8fb19SBen Gras #include "citrus_module.h"
46*2fe8fb19SBen Gras #include "citrus_region.h"
47*2fe8fb19SBen Gras #include "citrus_memstream.h"
48*2fe8fb19SBen Gras #include "citrus_mmap.h"
49*2fe8fb19SBen Gras #include "citrus_hash.h"
50*2fe8fb19SBen Gras #include "citrus_mapper.h"
51*2fe8fb19SBen Gras #include "citrus_mapper_serial.h"
52*2fe8fb19SBen Gras 
53*2fe8fb19SBen Gras /* ---------------------------------------------------------------------- */
54*2fe8fb19SBen Gras 
55*2fe8fb19SBen Gras _CITRUS_MAPPER_DECLS(mapper_serial);
56*2fe8fb19SBen Gras _CITRUS_MAPPER_DEF_OPS(mapper_serial);
57*2fe8fb19SBen Gras 
58*2fe8fb19SBen Gras #define _citrus_mapper_parallel_mapper_init		\
59*2fe8fb19SBen Gras 	_citrus_mapper_serial_mapper_init
60*2fe8fb19SBen Gras #define _citrus_mapper_parallel_mapper_uninit		\
61*2fe8fb19SBen Gras 	_citrus_mapper_serial_mapper_uninit
62*2fe8fb19SBen Gras #define _citrus_mapper_parallel_mapper_init_state	\
63*2fe8fb19SBen Gras 	_citrus_mapper_serial_mapper_init_state
64*2fe8fb19SBen Gras static int	_citrus_mapper_parallel_mapper_convert(
65*2fe8fb19SBen Gras 	struct _citrus_mapper * __restrict, _index_t * __restrict, _index_t,
66*2fe8fb19SBen Gras 	void * __restrict);
67*2fe8fb19SBen Gras _CITRUS_MAPPER_DEF_OPS(mapper_parallel);
68*2fe8fb19SBen Gras #undef _citrus_mapper_parallel_mapper_init
69*2fe8fb19SBen Gras #undef _citrus_mapper_parallel_mapper_uninit
70*2fe8fb19SBen Gras #undef _citrus_mapper_parallel_mapper_init_state
71*2fe8fb19SBen Gras 
72*2fe8fb19SBen Gras 
73*2fe8fb19SBen Gras /* ---------------------------------------------------------------------- */
74*2fe8fb19SBen Gras 
75*2fe8fb19SBen Gras struct maplink {
76*2fe8fb19SBen Gras 	SIMPLEQ_ENTRY(maplink)	ml_entry;
77*2fe8fb19SBen Gras 	struct _mapper		*ml_mapper;
78*2fe8fb19SBen Gras };
79*2fe8fb19SBen Gras SIMPLEQ_HEAD(maplist, maplink);
80*2fe8fb19SBen Gras 
81*2fe8fb19SBen Gras struct _citrus_mapper_serial {
82*2fe8fb19SBen Gras 	struct maplist	sr_mappers;
83*2fe8fb19SBen Gras };
84*2fe8fb19SBen Gras 
85*2fe8fb19SBen Gras int
_citrus_mapper_serial_mapper_getops(struct _citrus_mapper_ops * ops,size_t lenops,uint32_t expected_version)86*2fe8fb19SBen Gras _citrus_mapper_serial_mapper_getops(struct _citrus_mapper_ops *ops,
87*2fe8fb19SBen Gras 				    size_t lenops, uint32_t expected_version)
88*2fe8fb19SBen Gras {
89*2fe8fb19SBen Gras 	if (expected_version<_CITRUS_MAPPER_ABI_VERSION || lenops<sizeof(*ops))
90*2fe8fb19SBen Gras 		return EINVAL;
91*2fe8fb19SBen Gras 
92*2fe8fb19SBen Gras 	memcpy(ops, &_citrus_mapper_serial_mapper_ops,
93*2fe8fb19SBen Gras 	       sizeof(_citrus_mapper_serial_mapper_ops));
94*2fe8fb19SBen Gras 
95*2fe8fb19SBen Gras 	return 0;
96*2fe8fb19SBen Gras }
97*2fe8fb19SBen Gras 
98*2fe8fb19SBen Gras int
_citrus_mapper_parallel_mapper_getops(struct _citrus_mapper_ops * ops,size_t lenops,uint32_t expected_version)99*2fe8fb19SBen Gras _citrus_mapper_parallel_mapper_getops(struct _citrus_mapper_ops *ops,
100*2fe8fb19SBen Gras 				      size_t lenops, uint32_t expected_version)
101*2fe8fb19SBen Gras {
102*2fe8fb19SBen Gras 	if (expected_version<_CITRUS_MAPPER_ABI_VERSION || lenops<sizeof(*ops))
103*2fe8fb19SBen Gras 		return EINVAL;
104*2fe8fb19SBen Gras 
105*2fe8fb19SBen Gras 	memcpy(ops, &_citrus_mapper_parallel_mapper_ops,
106*2fe8fb19SBen Gras 	       sizeof(_citrus_mapper_parallel_mapper_ops));
107*2fe8fb19SBen Gras 
108*2fe8fb19SBen Gras 	return 0;
109*2fe8fb19SBen Gras }
110*2fe8fb19SBen Gras 
111*2fe8fb19SBen Gras static void
uninit(struct _citrus_mapper_serial * sr)112*2fe8fb19SBen Gras uninit(struct _citrus_mapper_serial *sr)
113*2fe8fb19SBen Gras {
114*2fe8fb19SBen Gras 	struct maplink *ml;
115*2fe8fb19SBen Gras 
116*2fe8fb19SBen Gras 	while ((ml = SIMPLEQ_FIRST(&sr->sr_mappers)) != NULL) {
117*2fe8fb19SBen Gras 		SIMPLEQ_REMOVE_HEAD(&sr->sr_mappers, ml_entry);
118*2fe8fb19SBen Gras 		_mapper_close(ml->ml_mapper);
119*2fe8fb19SBen Gras 		free(ml);
120*2fe8fb19SBen Gras 	}
121*2fe8fb19SBen Gras }
122*2fe8fb19SBen Gras 
123*2fe8fb19SBen Gras static int
parse_var(struct _citrus_mapper_area * __restrict ma,struct _citrus_mapper_serial * sr,struct _memstream * ms)124*2fe8fb19SBen Gras parse_var(struct _citrus_mapper_area *__restrict ma,
125*2fe8fb19SBen Gras 	  struct _citrus_mapper_serial *sr, struct _memstream *ms)
126*2fe8fb19SBen Gras {
127*2fe8fb19SBen Gras 	int ret;
128*2fe8fb19SBen Gras 	struct _region r;
129*2fe8fb19SBen Gras 	char mapname[PATH_MAX];
130*2fe8fb19SBen Gras 	struct maplink *ml;
131*2fe8fb19SBen Gras 
132*2fe8fb19SBen Gras 	SIMPLEQ_INIT(&sr->sr_mappers);
133*2fe8fb19SBen Gras 	while (1) {
134*2fe8fb19SBen Gras 		/* remove beginning white spaces */
135*2fe8fb19SBen Gras 		_memstream_skip_ws(ms);
136*2fe8fb19SBen Gras 		if (_memstream_iseof(ms))
137*2fe8fb19SBen Gras 			break;
138*2fe8fb19SBen Gras 		/* cut down a mapper name */
139*2fe8fb19SBen Gras 		_memstream_chr(ms, &r, ',');
140*2fe8fb19SBen Gras 		snprintf(mapname, sizeof(mapname), "%.*s",
141*2fe8fb19SBen Gras 			 (int)_region_size(&r), (char *)_region_head(&r));
142*2fe8fb19SBen Gras 		/* remove trailing white spaces */
143*2fe8fb19SBen Gras 		mapname[_bcs_skip_nonws(mapname)-mapname] = '\0';
144*2fe8fb19SBen Gras 		/* create a new mapper record */
145*2fe8fb19SBen Gras 		ml = malloc(sizeof(*ml));
146*2fe8fb19SBen Gras 		if (ml == NULL)
147*2fe8fb19SBen Gras 			return errno;
148*2fe8fb19SBen Gras 		ret = _mapper_open(ma, &ml->ml_mapper, mapname);
149*2fe8fb19SBen Gras 		if (ret) {
150*2fe8fb19SBen Gras 			free(ml);
151*2fe8fb19SBen Gras 			return ret;
152*2fe8fb19SBen Gras 		}
153*2fe8fb19SBen Gras 		/* support only 1:1 and stateless converter */
154*2fe8fb19SBen Gras 		if (_mapper_get_src_max(ml->ml_mapper) != 1 ||
155*2fe8fb19SBen Gras 		    _mapper_get_dst_max(ml->ml_mapper) != 1 ||
156*2fe8fb19SBen Gras 		    _mapper_get_state_size(ml->ml_mapper) != 0) {
157*2fe8fb19SBen Gras 			free(ml);
158*2fe8fb19SBen Gras 			return EINVAL;
159*2fe8fb19SBen Gras 		}
160*2fe8fb19SBen Gras 		SIMPLEQ_INSERT_TAIL(&sr->sr_mappers, ml, ml_entry);
161*2fe8fb19SBen Gras 	}
162*2fe8fb19SBen Gras 	return 0;
163*2fe8fb19SBen Gras }
164*2fe8fb19SBen Gras 
165*2fe8fb19SBen Gras static int
166*2fe8fb19SBen Gras /*ARGSUSED*/
_citrus_mapper_serial_mapper_init(struct _citrus_mapper_area * __restrict ma,struct _citrus_mapper * __restrict cm,const char * __restrict dir,const void * __restrict var,size_t lenvar,struct _citrus_mapper_traits * __restrict mt,size_t lenmt)167*2fe8fb19SBen Gras _citrus_mapper_serial_mapper_init(struct _citrus_mapper_area *__restrict ma,
168*2fe8fb19SBen Gras 				  struct _citrus_mapper * __restrict cm,
169*2fe8fb19SBen Gras 				  const char * __restrict dir,
170*2fe8fb19SBen Gras 				  const void * __restrict var, size_t lenvar,
171*2fe8fb19SBen Gras 				  struct _citrus_mapper_traits * __restrict mt,
172*2fe8fb19SBen Gras 				  size_t lenmt)
173*2fe8fb19SBen Gras {
174*2fe8fb19SBen Gras 	struct _citrus_mapper_serial *sr;
175*2fe8fb19SBen Gras 	struct _memstream ms;
176*2fe8fb19SBen Gras 	struct _region r;
177*2fe8fb19SBen Gras 
178*2fe8fb19SBen Gras 	_DIAGASSERT(cm && dir && mt);
179*2fe8fb19SBen Gras 
180*2fe8fb19SBen Gras 	if (lenmt<sizeof(*mt))
181*2fe8fb19SBen Gras 		return EINVAL;
182*2fe8fb19SBen Gras 
183*2fe8fb19SBen Gras 	sr = malloc(sizeof(*sr));
184*2fe8fb19SBen Gras 	if (sr == NULL)
185*2fe8fb19SBen Gras 		return errno;
186*2fe8fb19SBen Gras 
187*2fe8fb19SBen Gras 	_region_init(&r, (void *)var, lenvar);
188*2fe8fb19SBen Gras 	_memstream_bind(&ms, &r);
189*2fe8fb19SBen Gras 	if (parse_var(ma, sr, &ms)) {
190*2fe8fb19SBen Gras 		uninit(sr);
191*2fe8fb19SBen Gras 		free(sr);
192*2fe8fb19SBen Gras 		return EINVAL;
193*2fe8fb19SBen Gras 	}
194*2fe8fb19SBen Gras 	cm->cm_closure = sr;
195*2fe8fb19SBen Gras 	mt->mt_src_max = mt->mt_dst_max = 1;	/* 1:1 converter */
196*2fe8fb19SBen Gras 	mt->mt_state_size = 0;			/* stateless */
197*2fe8fb19SBen Gras 
198*2fe8fb19SBen Gras 	return 0;
199*2fe8fb19SBen Gras }
200*2fe8fb19SBen Gras 
201*2fe8fb19SBen Gras static void
202*2fe8fb19SBen Gras /*ARGSUSED*/
_citrus_mapper_serial_mapper_uninit(struct _citrus_mapper * cm)203*2fe8fb19SBen Gras _citrus_mapper_serial_mapper_uninit(struct _citrus_mapper *cm)
204*2fe8fb19SBen Gras {
205*2fe8fb19SBen Gras 	if (cm && cm->cm_closure) {
206*2fe8fb19SBen Gras 		uninit(cm->cm_closure);
207*2fe8fb19SBen Gras 		free(cm->cm_closure);
208*2fe8fb19SBen Gras 	}
209*2fe8fb19SBen Gras }
210*2fe8fb19SBen Gras 
211*2fe8fb19SBen Gras static int
212*2fe8fb19SBen Gras /*ARGSUSED*/
_citrus_mapper_serial_mapper_convert(struct _citrus_mapper * __restrict cm,_index_t * __restrict dst,_index_t src,void * __restrict ps)213*2fe8fb19SBen Gras _citrus_mapper_serial_mapper_convert(struct _citrus_mapper * __restrict cm,
214*2fe8fb19SBen Gras 				     _index_t * __restrict dst, _index_t src,
215*2fe8fb19SBen Gras 				     void * __restrict ps)
216*2fe8fb19SBen Gras {
217*2fe8fb19SBen Gras 	int ret;
218*2fe8fb19SBen Gras 	struct _citrus_mapper_serial *sr;
219*2fe8fb19SBen Gras 	struct maplink *ml;
220*2fe8fb19SBen Gras 
221*2fe8fb19SBen Gras 	_DIAGASSERT(cm && cm->cm_closure);
222*2fe8fb19SBen Gras 
223*2fe8fb19SBen Gras 	sr = cm->cm_closure;
224*2fe8fb19SBen Gras 	SIMPLEQ_FOREACH(ml, &sr->sr_mappers, ml_entry) {
225*2fe8fb19SBen Gras 		ret = _mapper_convert(ml->ml_mapper, &src, src, NULL);
226*2fe8fb19SBen Gras 		if (ret != _MAPPER_CONVERT_SUCCESS)
227*2fe8fb19SBen Gras 			return ret;
228*2fe8fb19SBen Gras 	}
229*2fe8fb19SBen Gras 	*dst = src;
230*2fe8fb19SBen Gras 	return _MAPPER_CONVERT_SUCCESS;
231*2fe8fb19SBen Gras }
232*2fe8fb19SBen Gras 
233*2fe8fb19SBen Gras static int
234*2fe8fb19SBen Gras /*ARGSUSED*/
_citrus_mapper_parallel_mapper_convert(struct _citrus_mapper * __restrict cm,_index_t * __restrict dst,_index_t src,void * __restrict ps)235*2fe8fb19SBen Gras _citrus_mapper_parallel_mapper_convert(struct _citrus_mapper * __restrict cm,
236*2fe8fb19SBen Gras 				       _index_t * __restrict dst, _index_t src,
237*2fe8fb19SBen Gras 				       void * __restrict ps)
238*2fe8fb19SBen Gras {
239*2fe8fb19SBen Gras 	int ret;
240*2fe8fb19SBen Gras 	struct _citrus_mapper_serial *sr;
241*2fe8fb19SBen Gras 	struct maplink *ml;
242*2fe8fb19SBen Gras 	_index_t tmp;
243*2fe8fb19SBen Gras 
244*2fe8fb19SBen Gras 	_DIAGASSERT(cm && cm->cm_closure);
245*2fe8fb19SBen Gras 
246*2fe8fb19SBen Gras 	sr = cm->cm_closure;
247*2fe8fb19SBen Gras 	SIMPLEQ_FOREACH(ml, &sr->sr_mappers, ml_entry) {
248*2fe8fb19SBen Gras 		ret = _mapper_convert(ml->ml_mapper, &tmp, src, NULL);
249*2fe8fb19SBen Gras 		if (ret == _MAPPER_CONVERT_SUCCESS) {
250*2fe8fb19SBen Gras 			*dst = tmp;
251*2fe8fb19SBen Gras 			return _MAPPER_CONVERT_SUCCESS;
252*2fe8fb19SBen Gras 		} else if (ret == _MAPPER_CONVERT_ILSEQ)
253*2fe8fb19SBen Gras 			return _MAPPER_CONVERT_ILSEQ;
254*2fe8fb19SBen Gras 	}
255*2fe8fb19SBen Gras 	return _MAPPER_CONVERT_NONIDENTICAL;
256*2fe8fb19SBen Gras }
257*2fe8fb19SBen Gras 
258*2fe8fb19SBen Gras static void
259*2fe8fb19SBen Gras /*ARGSUSED*/
_citrus_mapper_serial_mapper_init_state(struct _citrus_mapper * __restrict cm,void * __restrict ps)260*2fe8fb19SBen Gras _citrus_mapper_serial_mapper_init_state(struct _citrus_mapper * __restrict cm,
261*2fe8fb19SBen Gras 					void * __restrict ps)
262*2fe8fb19SBen Gras {
263*2fe8fb19SBen Gras }
264