source: mainline/uspace/app/bithenge/transform.h@ cb4a66d2

lfn serial ticket/834-toolchain-update topic/fix-logger-deadlock topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cb4a66d2 was cb4a66d2, checked in by Sean Bartell <wingedtachikoma@…>, 14 years ago

Bithenge: add transform_prefix_apply

  • makes struct nodes more efficient in some cases.
  • will make certain future transforms easier to implement.
  • Property mode set to 100644
File size: 9.1 KB
Line 
1/*
2 * Copyright (c) 2012 Sean Bartell
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup bithenge
30 * @{
31 */
32/**
33 * @file
34 * Transforms.
35 */
36
37#ifndef BITHENGE_TRANSFORM_H_
38#define BITHENGE_TRANSFORM_H_
39
40#include "blob.h"
41#include "tree.h"
42
43/** A transform that creates a new tree from an old tree. */
44typedef struct {
45 /** @privatesection */
46 const struct bithenge_transform_ops *ops;
47 unsigned int refs;
48 int num_params;
49} bithenge_transform_t;
50
51/** Context and parameters used when applying transforms. */
52typedef struct {
53 /** @privatesection */
54 int num_params;
55 bithenge_node_t **params;
56 bithenge_node_t *current_node;
57} bithenge_scope_t;
58
59/** Operations that may be provided by a transform. All transforms must provide
60 * apply and/or prefix_apply. To be used in struct transforms and repeat
61 * transforms, transforms must provide prefix_length and/or prefix_apply. */
62typedef struct bithenge_transform_ops {
63 /** @copydoc bithenge_transform_t::bithenge_transform_apply */
64 int (*apply)(bithenge_transform_t *self, bithenge_scope_t *scope,
65 bithenge_node_t *in, bithenge_node_t **out);
66 /** @copydoc bithenge_transform_t::bithenge_transform_prefix_length */
67 int (*prefix_length)(bithenge_transform_t *self,
68 bithenge_scope_t *scope, bithenge_blob_t *blob, aoff64_t *out);
69 /** @copydoc bithenge_transform_t::bithenge_transform_prefix_apply */
70 int (*prefix_apply)(bithenge_transform_t *self,
71 bithenge_scope_t *scope, bithenge_blob_t *blob,
72 bithenge_node_t **out_node, aoff64_t *out_size);
73 /** Destroy the transform.
74 * @param self The transform. */
75 void (*destroy)(bithenge_transform_t *self);
76} bithenge_transform_ops_t;
77
78/** Initialize a transform scope. It must be destroyed with @a
79 * bithenge_scope_destroy after it is used.
80 * @param[out] scope The scope to initialize. */
81static inline void bithenge_scope_init(bithenge_scope_t *scope)
82{
83 scope->num_params = 0;
84 scope->params = NULL;
85 scope->current_node = NULL;
86}
87
88/** Destroy a transform scope.
89 * @param scope The scope to destroy.
90 * @return EOK on success or an error code from errno.h. */
91static inline void bithenge_scope_destroy(bithenge_scope_t *scope)
92{
93 bithenge_node_dec_ref(scope->current_node);
94 for (int i = 0; i < scope->num_params; i++)
95 bithenge_node_dec_ref(scope->params[i]);
96 free(scope->params);
97}
98
99/** Copy a scope.
100 * @param[out] out The scope to fill in; must have been initialized with @a
101 * bithenge_scope_init.
102 * @param scope The scope to copy.
103 * @return EOK on success or an error code from errno.h. */
104static inline int bithenge_scope_copy(bithenge_scope_t *out,
105 bithenge_scope_t *scope)
106{
107 out->params = malloc(sizeof(*out->params) * scope->num_params);
108 if (!out->params)
109 return ENOMEM;
110 memcpy(out->params, scope->params, sizeof(*out->params) *
111 scope->num_params);
112 out->num_params = scope->num_params;
113 for (int i = 0; i < out->num_params; i++)
114 bithenge_node_inc_ref(out->params[i]);
115 out->current_node = scope->current_node;
116 if (out->current_node)
117 bithenge_node_inc_ref(out->current_node);
118 return EOK;
119}
120
121/** Set the current node being created. Takes a reference to @a node.
122 * @param scope The scope to set the current node in.
123 * @param node The current node being created.
124 * @return EOK on success or an error code from errno.h. */
125static inline void bithenge_scope_set_current_node(bithenge_scope_t *scope,
126 bithenge_node_t *node)
127{
128 bithenge_node_dec_ref(scope->current_node);
129 scope->current_node = node;
130}
131
132/** Get the current node being created, which may be NULL.
133 * @param scope The scope to get the current node from.
134 * @return The node being created, or NULL. */
135static inline bithenge_node_t *bithenge_scope_get_current_node(
136 bithenge_scope_t *scope)
137{
138 if (scope->current_node)
139 bithenge_node_inc_ref(scope->current_node);
140 return scope->current_node;
141}
142
143/** Allocate parameters. The parameters must then be set with @a
144 * bithenge_scope_set_param. This must not be called on a scope that already
145 * has parameters.
146 * @param scope The scope in which to allocate parameters.
147 * @param num_params The number of parameters to allocate.
148 * @return EOK on success or an error code from errno.h. */
149static inline int bithenge_scope_alloc_params(bithenge_scope_t *scope,
150 int num_params)
151{
152 scope->params = malloc(sizeof(*scope->params) * num_params);
153 if (!scope->params)
154 return ENOMEM;
155 scope->num_params = num_params;
156 for (int i = 0; i < num_params; i++)
157 scope->params[i] = NULL;
158 return EOK;
159}
160
161/** Set a parameter. Takes a reference to @a value. Note that range checking is
162 * not done in release builds.
163 * @param scope The scope in which to allocate parameters.
164 * @param i The index of the parameter to set.
165 * @param value The value to store in the parameter.
166 * @return EOK on success or an error code from errno.h. */
167static inline int bithenge_scope_set_param( bithenge_scope_t *scope, int i,
168 bithenge_node_t *node)
169{
170 assert(scope);
171 assert(i >= 0 && i < scope->num_params);
172 scope->params[i] = node;
173 return EOK;
174}
175
176/** Get a parameter. Note that range checking is not done in release builds.
177 * @param scope The scope to get the parameter from.
178 * @param i The index of the parameter to set.
179 * @param[out] out Stores a new reference to the parameter.
180 * @return EOK on success or an error code from errno.h. */
181static inline int bithenge_scope_get_param(bithenge_scope_t *scope, int i,
182 bithenge_node_t **out)
183{
184 assert(scope);
185 assert(i >= 0 && i < scope->num_params);
186 *out = scope->params[i];
187 bithenge_node_inc_ref(*out);
188 return EOK;
189}
190
191/** Get the number of parameters required by a transform. This number is used
192 * by the parser and param-wrapper. Takes ownership of nothing.
193 * @param self The transform.
194 * @return The number of parameters required. */
195static inline int bithenge_transform_num_params(bithenge_transform_t *self)
196{
197 assert(self);
198 return self->num_params;
199}
200
201/** Increment a transform's reference count.
202 * @param self The transform to reference. */
203static inline void bithenge_transform_inc_ref(bithenge_transform_t *self)
204{
205 assert(self);
206 self->refs++;
207}
208
209/** Decrement a transform's reference count and free it if appropriate.
210 * @param self The transform to dereference, or NULL. */
211static inline void bithenge_transform_dec_ref(bithenge_transform_t *self)
212{
213 if (!self)
214 return;
215 assert(self->ops);
216 if (--self->refs == 0)
217 self->ops->destroy(self);
218}
219
220/** A transform with a name. */
221typedef struct {
222 const char *name;
223 bithenge_transform_t *transform;
224} bithenge_named_transform_t;
225
226extern bithenge_transform_t bithenge_ascii_transform;
227extern bithenge_transform_t bithenge_known_length_transform;
228extern bithenge_transform_t bithenge_invalid_transform;
229extern bithenge_transform_t bithenge_uint8_transform;
230extern bithenge_transform_t bithenge_uint16le_transform;
231extern bithenge_transform_t bithenge_uint16be_transform;
232extern bithenge_transform_t bithenge_uint32le_transform;
233extern bithenge_transform_t bithenge_uint32be_transform;
234extern bithenge_transform_t bithenge_uint64le_transform;
235extern bithenge_transform_t bithenge_uint64be_transform;
236extern bithenge_transform_t bithenge_zero_terminated_transform;
237extern bithenge_named_transform_t *bithenge_primitive_transforms;
238
239int bithenge_init_transform(bithenge_transform_t *,
240 const bithenge_transform_ops_t *, int);
241int bithenge_transform_apply(bithenge_transform_t *, bithenge_scope_t *,
242 bithenge_node_t *, bithenge_node_t **);
243int bithenge_transform_prefix_length(bithenge_transform_t *,
244 bithenge_scope_t *, bithenge_blob_t *, aoff64_t *);
245int bithenge_transform_prefix_apply(bithenge_transform_t *, bithenge_scope_t *,
246 bithenge_blob_t *, bithenge_node_t **, aoff64_t *);
247int bithenge_new_param_transform(bithenge_transform_t **,
248 bithenge_transform_t *, int);
249int bithenge_new_struct(bithenge_transform_t **,
250 bithenge_named_transform_t *);
251int bithenge_new_composed_transform(bithenge_transform_t **,
252 bithenge_transform_t **, size_t);
253
254#endif
255
256/** @}
257 */
Note: See TracBrowser for help on using the repository browser.