labcomm2014_renaming_encoder.c 8.24 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
  labcomm2014_renaming_encoder.c -- a stacked encoder that renames samples

  Copyright 2015 Anders Blomdell <anders.blomdell@control.lth.se>

  This file is part of LabComm.

  LabComm is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  LabComm is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <errno.h>
#include "labcomm2014_renaming_encoder.h"
#include "labcomm2014.h"
#include "labcomm2014_private.h"

struct encoder {
  struct labcomm2014_encoder encoder;
  struct labcomm2014_encoder *next;
  char *(*rename)(struct labcomm2014_memory *m, char *name, void *context);
  void *context;
  LABCOMM_SIGNATURE_ARRAY_DEF(renamed, struct labcomm2014_signature *);
};

static struct labcomm2014_signature *get_renamed(
  struct labcomm2014_encoder *e,
  const struct labcomm2014_signature *signature)
{
  struct labcomm2014_signature *result;
  struct encoder *ie = e->context;
  int index;

  index = labcomm2014_get_local_index(signature);
  labcomm2014_scheduler_writer_lock(e->scheduler);
  result = LABCOMM_SIGNATURE_ARRAY_GET(ie->renamed,
                                      struct labcomm2014_signature *,
                                      index, NULL);
  labcomm2014_scheduler_writer_unlock(e->scheduler);
  return result;
}
  
static struct labcomm2014_signature *set_renamed(
  struct labcomm2014_encoder *e,
  const struct labcomm2014_signature *signature)
{
  struct labcomm2014_signature *result;

  result = get_renamed(e, signature);
  if (result == NULL) {
    /* create a renamed sample */
    struct encoder *ie = e->context;
    int index;
    struct labcomm2014_signature **renamed;
  
    index = labcomm2014_get_local_index(signature);
    if (index <= 0) { goto out; /*result already NULL */}
    labcomm2014_scheduler_writer_lock(e->scheduler);
    renamed = LABCOMM_SIGNATURE_ARRAY_REF(e->memory, ie->renamed,
                                          struct labcomm2014_signature *,
                                          index);
    if (renamed == NULL) {
      labcomm2014_error_warning(e->error,
                                LABCOMM2014_ERROR_MEMORY,
                                "Could not allocate rename slot: %s\n",
                                signature->name);
      goto unlock;
    }
    if (*renamed != NULL) {
      /* Somebody beat as to allocation, this should never happen */
      goto unlock;
    }
    result = labcomm2014_memory_alloc(e->memory, 0, sizeof(*result));
    if (result == NULL) {
      labcomm2014_error_warning(e->error,
                                LABCOMM2014_ERROR_MEMORY,
                                "Could not allocate rename signature: %s\n",
                                signature->name);
      goto unlock;
    }
    result->name = ie->rename(e->memory, signature->name, ie->context);
    if (result->name == NULL) {
      labcomm2014_error_warning(e->error,
                                LABCOMM2014_ERROR_MEMORY,
                                "Could not allocate rename name: %s\n",
                                signature->name);
      goto unlock_free_result;
    }
    result->encoded_size = signature->encoded_size;
    result->size = signature->size;
    result->signature = signature->signature; 
    result->index = 0;
#ifndef LABCOMM_NO_TYPEDECL
    result->tdsize = signature->tdsize;
    result->treedata = signature->treedata;
#endif  
    labcomm2014_set_local_index(result);
    *renamed = result;
    goto unlock;
  unlock_free_result:
    labcomm2014_memory_free(e->memory, 0, result);
    result = NULL;
  unlock:
    labcomm2014_scheduler_writer_unlock(e->scheduler);
  out:
    ;
  }
  return result;
}
  
static int do_type_register(struct labcomm2014_encoder *e,
                            const struct labcomm2014_signature *signature)
{
  struct encoder *ie = e->context;
  
  return ie->next->type_register(ie->next, set_renamed(e, signature));
}

static int do_type_bind(struct labcomm2014_encoder *e,
                        const struct labcomm2014_signature *signature,
                        char has_deps)
{
  struct encoder *ie = e->context;
  
  return ie->next->type_bind(ie->next, set_renamed(e, signature), has_deps);
}

static int do_sample_register(struct labcomm2014_encoder *e, 
                         const struct labcomm2014_signature *signature, 
                         labcomm2014_encoder_function encode)
{
  struct encoder *ie = e->context;
  
  return ie->next->sample_register(ie->next, set_renamed(e, signature), encode);
}

static int do_ref_register(struct labcomm2014_encoder *e, 
                      const struct labcomm2014_signature *signature)
{
  struct encoder *ie = e->context;
  
  return ie->next->ref_register(ie->next, set_renamed(e, signature));
}

static int do_encode(struct labcomm2014_encoder *e, 
                     const struct labcomm2014_signature *signature, 
                     labcomm2014_encoder_function encode,
                     void *value)
{
  struct encoder *ie = e->context;
  
  return ie->next->encode(ie->next, get_renamed(e, signature), encode, value);
}

static int do_ioctl(struct labcomm2014_encoder *e, 
                    const struct labcomm2014_signature *signature,
                    uint32_t ioctl_action, va_list args)
{
  struct encoder *ie = e->context;
  
  return ie->next->ioctl(ie->next, get_renamed(e, signature),
                         ioctl_action, args);
}

174
175
176
static int do_sample_ref_to_index(
  struct labcomm2014_encoder *e,
  const struct labcomm2014_sample_ref *sample_ref)
177
178
{
  struct encoder *ie = e->context;
179
180

  return ie->next->sample_ref_to_index(ie->next,sample_ref);
181
182
}

183
static const struct labcomm2014_sample_ref *do_ref_get(
Anders Blomdell's avatar
Anders Blomdell committed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
  struct labcomm2014_encoder *e,
  const struct labcomm2014_signature *signature)
{
  const struct labcomm2014_signature *renamed;
  struct encoder *ie = e->context;

  renamed = get_renamed(e, signature);
  if (renamed == NULL) {
    return ie->next->ref_get(ie->next, signature);
  } else {
    return ie->next->ref_get(ie->next, renamed);
  }
}

198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
static void do_free(struct labcomm2014_encoder *e)
{
  struct encoder *ie = e->context;
  int i;

  LABCOMM_SIGNATURE_ARRAY_FOREACH(ie->renamed, struct labcomm2014_signature *,
                                  i) {
    struct labcomm2014_signature *s;
    s = LABCOMM_SIGNATURE_ARRAY_GET(ie->renamed,
                                    struct labcomm2014_signature *, i, NULL);
    if (s) {
      labcomm2014_memory_free(e->memory, 0, s->name);
      labcomm2014_memory_free(e->memory, 0, s);
    }
  }
  LABCOMM_SIGNATURE_ARRAY_FREE(e->memory, ie->renamed,
                               struct labcomm2014_signature *);
  labcomm2014_memory_free(e->memory, 0, ie);
}

struct labcomm2014_encoder *labcomm2014_renaming_encoder_new(
  struct labcomm2014_encoder *e,
  char *(*rename)(struct labcomm2014_memory *m, char *name, void *context),
  void *context)
{
  struct encoder *result;
  result = labcomm2014_memory_alloc(e->memory, 0, sizeof(*result));
  if (!result) {
    return NULL;
  } else {
      result->encoder.context = result;
      result->encoder.writer = NULL;
      result->encoder.error = e->error;
      result->encoder.memory = e->memory;
      result->encoder.scheduler = e->scheduler;
      result->encoder.free = do_free;
      result->encoder.type_register = do_type_register;
      result->encoder.type_bind = do_type_bind;
      result->encoder.sample_register = do_sample_register;
      result->encoder.ref_register = do_ref_register;
      result->encoder.encode = do_encode;
      result->encoder.ioctl = do_ioctl;
240
      result->encoder.sample_ref_to_index = do_sample_ref_to_index;
Anders Blomdell's avatar
Anders Blomdell committed
241
      result->encoder.ref_get = do_ref_get;
242
243
244
245
246
247
248
249
      result->next = e;
      result->rename = rename;
      result->context = context;
      LABCOMM_SIGNATURE_ARRAY_INIT(result->renamed,
                                   struct labcomm2014_signature *);
      return &(result->encoder);
  }
}