Merge from vendor branch CVS:
[dragonfly.git] / lib / libc / citrus / modules / citrus_mapper_serial.c
1 /*      $NetBSD: src/lib/libc/citrus/modules/citrus_mapper_serial.c,v 1.2 2003/07/12 15:39:20 tshiozak Exp $    */
2 /*      $DragonFly: src/lib/libc/citrus/modules/citrus_mapper_serial.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */
3
4 /*-
5  * Copyright (c)2003 Citrus Project,
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/types.h>
31 #include <sys/queue.h>
32 #include <assert.h>
33 #include <errno.h>
34 #include <limits.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include "citrus_namespace.h"
40 #include "citrus_types.h"
41 #include "citrus_bcs.h"
42 #include "citrus_module.h"
43 #include "citrus_region.h"
44 #include "citrus_memstream.h"
45 #include "citrus_mmap.h"
46 #include "citrus_hash.h"
47 #include "citrus_mapper.h"
48 #include "citrus_mapper_serial.h"
49
50 /* ---------------------------------------------------------------------- */
51
52 _CITRUS_MAPPER_DECLS(mapper_serial);
53 _CITRUS_MAPPER_DEF_OPS(mapper_serial);
54
55 #define _citrus_mapper_parallel_mapper_init             \
56         _citrus_mapper_serial_mapper_init
57 #define _citrus_mapper_parallel_mapper_uninit           \
58         _citrus_mapper_serial_mapper_uninit
59 #define _citrus_mapper_parallel_mapper_init_state       \
60         _citrus_mapper_serial_mapper_init_state
61 static int      _citrus_mapper_parallel_mapper_convert(
62         struct _citrus_mapper * __restrict, _index_t * __restrict, _index_t,
63         void * __restrict);
64 _CITRUS_MAPPER_DEF_OPS(mapper_parallel);
65 #undef _citrus_mapper_parallel_mapper_init
66 #undef _citrus_mapper_parallel_mapper_uninit
67 #undef _citrus_mapper_parallel_mapper_init_state
68
69
70 /* ---------------------------------------------------------------------- */
71
72 struct maplink {
73         STAILQ_ENTRY(maplink)   ml_entry;
74         struct _mapper          *ml_mapper;
75 };
76 STAILQ_HEAD(maplist, maplink);
77
78 struct _citrus_mapper_serial {
79         struct maplist  sr_mappers;
80 };
81
82 int
83 _citrus_mapper_serial_mapper_getops(struct _citrus_mapper_ops *ops,
84                                     size_t lenops, uint32_t expected_version)
85 {
86         if (expected_version<_CITRUS_MAPPER_ABI_VERSION || lenops<sizeof(*ops))
87                 return EINVAL;
88
89         memcpy(ops, &_citrus_mapper_serial_mapper_ops,
90                sizeof(_citrus_mapper_serial_mapper_ops));
91
92         return 0;
93 }
94
95 int
96 _citrus_mapper_parallel_mapper_getops(struct _citrus_mapper_ops *ops,
97                                       size_t lenops, uint32_t expected_version)
98 {
99         if (expected_version<_CITRUS_MAPPER_ABI_VERSION || lenops<sizeof(*ops))
100                 return EINVAL;
101
102         memcpy(ops, &_citrus_mapper_parallel_mapper_ops,
103                sizeof(_citrus_mapper_parallel_mapper_ops));
104
105         return 0;
106 }
107
108 static void
109 uninit(struct _citrus_mapper_serial *sr)
110 {
111         struct maplink *ml;
112
113         while ((ml = STAILQ_FIRST(&sr->sr_mappers)) != NULL) {
114                 STAILQ_REMOVE_HEAD(&sr->sr_mappers, ml_entry);
115                 _mapper_close(ml->ml_mapper);
116                 free(ml);
117         }
118 }
119
120 static int
121 parse_var(struct _citrus_mapper_area *__restrict ma,
122           struct _citrus_mapper_serial *sr, struct _memstream *ms)
123 {
124         int ret;
125         struct _region r;
126         char mapname[PATH_MAX];
127         struct maplink *ml;
128
129         STAILQ_INIT(&sr->sr_mappers);
130         while (1) {
131                 /* remove beginning white spaces */
132                 _memstream_skip_ws(ms);
133                 if (_memstream_iseof(ms))
134                         break;
135                 /* cut down a mapper name */
136                 _memstream_chr(ms, &r, ',');
137                 snprintf(mapname, sizeof(mapname), "%.*s",
138                          (int)_region_size(&r), (char *)_region_head(&r));
139                 /* remove trailing white spaces */
140                 mapname[_bcs_skip_nonws(mapname)-mapname] = '\0';
141                 /* create a new mapper record */
142                 ml = malloc(sizeof(*ml));
143                 if (ml == NULL)
144                         return errno;
145                 ret = _mapper_open(ma, &ml->ml_mapper, mapname);
146                 if (ret) {
147                         free(ml);
148                         return ret;
149                 }
150                 /* support only 1:1 and stateless converter */
151                 if (_mapper_get_src_max(ml->ml_mapper) != 1 ||
152                     _mapper_get_dst_max(ml->ml_mapper) != 1 ||
153                     _mapper_get_state_size(ml->ml_mapper) != 0) {
154                         free(ml);
155                         return EINVAL;
156                 }
157                 STAILQ_INSERT_TAIL(&sr->sr_mappers, ml, ml_entry);
158         }
159         return 0;
160 }
161
162 static int
163 /*ARGSUSED*/
164 _citrus_mapper_serial_mapper_init(struct _citrus_mapper_area *__restrict ma,
165                                   struct _citrus_mapper * __restrict cm,
166                                   const char * __restrict dir,
167                                   const void * __restrict var, size_t lenvar,
168                                   struct _citrus_mapper_traits * __restrict mt,
169                                   size_t lenmt)
170 {
171         struct _citrus_mapper_serial *sr;
172         struct _memstream ms;
173         struct _region r;
174
175         _DIAGASSERT(cm && dir && mt);
176
177         if (lenmt<sizeof(*mt))
178                 return EINVAL;
179
180         sr = malloc(sizeof(*sr));
181         if (sr == NULL)
182                 return errno;
183
184         _region_init(&r, (void *)var, lenvar);
185         _memstream_bind(&ms, &r);
186         if (parse_var(ma, sr, &ms)) {
187                 uninit(sr);
188                 free(sr);
189                 return EINVAL;
190         }
191         cm->cm_closure = sr;
192         mt->mt_src_max = mt->mt_dst_max = 1;    /* 1:1 converter */
193         mt->mt_state_size = 0;                  /* stateless */
194
195         return 0;
196 }
197
198 static void
199 /*ARGSUSED*/
200 _citrus_mapper_serial_mapper_uninit(struct _citrus_mapper *cm)
201 {
202         if (cm && cm->cm_closure) {
203                 uninit(cm->cm_closure);
204                 free(cm->cm_closure);
205         }
206 }
207
208 static int
209 /*ARGSUSED*/
210 _citrus_mapper_serial_mapper_convert(struct _citrus_mapper * __restrict cm,
211                                      _index_t * __restrict dst, _index_t src,
212                                      void * __restrict ps)
213 {
214         int ret;
215         struct _citrus_mapper_serial *sr;
216         struct maplink *ml;
217
218         _DIAGASSERT(cm && cm->cm_closure);
219
220         sr = cm->cm_closure;
221         STAILQ_FOREACH(ml, &sr->sr_mappers, ml_entry) {
222                 ret = _mapper_convert(ml->ml_mapper, &src, src, NULL);
223                 if (ret != _MAPPER_CONVERT_SUCCESS)
224                         return ret;
225         }
226         *dst = src;
227         return _MAPPER_CONVERT_SUCCESS;
228 }
229
230 static int
231 /*ARGSUSED*/
232 _citrus_mapper_parallel_mapper_convert(struct _citrus_mapper * __restrict cm,
233                                        _index_t * __restrict dst, _index_t src,
234                                        void * __restrict ps)
235 {
236         int ret;
237         struct _citrus_mapper_serial *sr;
238         struct maplink *ml;
239         _index_t tmp;
240
241         _DIAGASSERT(cm && cm->cm_closure);
242
243         sr = cm->cm_closure;
244         STAILQ_FOREACH(ml, &sr->sr_mappers, ml_entry) {
245                 ret = _mapper_convert(ml->ml_mapper, &tmp, src, NULL);
246                 if (ret == _MAPPER_CONVERT_SUCCESS) {
247                         *dst = tmp;
248                         return _MAPPER_CONVERT_SUCCESS;
249                 } else if (ret == _MAPPER_CONVERT_ILSEQ)
250                         return _MAPPER_CONVERT_ILSEQ;
251         }
252         return _MAPPER_CONVERT_NONIDENTICAL;
253 }
254
255 static void
256 /*ARGSUSED*/
257 _citrus_mapper_serial_mapper_init_state(struct _citrus_mapper * __restrict cm,
258                                         void * __restrict ps)
259 {
260 }