File: sdif-mem.c

package info (click to toggle)
csound 1%3A6.18.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 63,220 kB
  • sloc: ansic: 192,643; cpp: 14,149; javascript: 9,654; objc: 9,181; python: 3,376; java: 3,337; sh: 1,840; yacc: 1,255; xml: 985; perl: 635; lisp: 411; tcl: 341; lex: 217; makefile: 128
file content (319 lines) | stat: -rw-r--r-- 8,272 bytes parent folder | download | duplicates (5)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
Copyright (c) 1999.  The Regents of the University of California (Regents).
All Rights Reserved.

Permission to use, copy, modify, and distribute this software and its
documentation, without fee and without a signed licensing agreement, is hereby
granted, provided that the above copyright notice, this paragraph and the
following two paragraphs appear in all copies, modifications, and
distributions.  Contact The Office of Technology Licensing, UC Berkeley, 2150
Shattuck Avenue, Suite 510, Berkeley, CA 94720-1620, (510) 643-7201, for
commercial licensing opportunities.

Written by Sami Khoury and Matt Wright, The Center for New Music and Audio
Technologies, University of California, Berkeley.

     IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
     SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
     ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
     REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

     REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     FOR A PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING
     DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
     REGENTS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
     ENHANCEMENTS, OR MODIFICATIONS.
*/

/*
  sdif-mem.c
  Implementation of routines for representing SDIF data in memory

  by Sami Khoury and Matt Wright
  Split from sdif.c 9/22/99 by Matt Wright
  Updated 10/13/99 by Matt for new SDIF library

  4:8:2000 RWD: func calls test explicitly for ESDIF_SUCCESS

*/

#include "sysdep.h"
#include <assert.h>
#include <errno.h>
#include "sdif-mem.h"

/* Global variables local to this file */
static void *(*my_malloc)(int32_t numBytes);
static void (*my_free)(void *memory, int32_t numBytes);

SDIFresult SDIFmem_Init(void *(*MemoryAllocator)(int32_t numBytes),
                        void (*MemoryFreer)(void *memory, int32_t numBytes))
{
    my_malloc = MemoryAllocator;
    my_free = MemoryFreer;

    return ESDIF_SUCCESS;
}

SDIFmem_Frame SDIFmem_CreateEmptyFrame(void)
{
    SDIFmem_Frame f;

    f = (*my_malloc)(sizeof(*f));

    if (f == NULL) {
      return NULL;
    }

    f->header.frameType[0] = '\0';
    f->header.frameType[1] = '\0';
    f->header.frameType[2] = '\0';
    f->header.frameType[3] = '\0';
    f->header.size = 16;
    f->header.time = 0;
    f->header.streamID = 0;
    f->header.matrixCount = 0;
    f->matrices = NULL;

    f->prev = NULL;
    f->next = NULL;
    return f;
}

void SDIFmem_FreeFrame(SDIFmem_Frame f)
{
    SDIFmem_Matrix m, next;

    if (f == 0) return;

    m = f->matrices;
    while (m != NULL) {
      next = m->next;
      SDIFmem_FreeMatrix(m);
      m = next;
    }

    (*my_free)(f, sizeof(*f));
}

SDIFmem_Matrix SDIFmem_CreateEmptyMatrix(void)
{

    SDIFmem_Matrix result;

    result = (*my_malloc)(sizeof(*result));

    if (result == NULL) {
      return NULL;
    }

    SDIF_Copy4Bytes(result->header.matrixType, "\0\0\0\0");
    result->header.matrixDataType = SDIF_NO_TYPE;
    result->header.rowCount = result->header.columnCount = 0;
    result->data = 0;

    return result;
}

void SDIFmem_FreeMatrix(SDIFmem_Matrix m)
{
    if (m->data != 0) {
      (*my_free)(m->data, SDIF_GetMatrixDataSize(&(m->header)));
    }
    (*my_free)(m, sizeof(*m));
}

void SDIFmem_RepairFrameHeader(SDIFmem_Frame f)
{
    sdif_int32 numBytes;
    sdif_int32 numMatrices;

    SDIFmem_Matrix m;

    /* The rest of the frame header: */
    numBytes = sizeof(SDIF_FrameHeader) - 8;
    numMatrices = 0;

    for (m = f->matrices; m != 0; m=m->next) {
      ++numMatrices;
      numBytes += sizeof(SDIF_MatrixHeader);
      numBytes += SDIF_GetMatrixDataSize(&(m->header));
    }

    f->header.size = numBytes;
    f->header.matrixCount = numMatrices;
}

SDIFresult SDIFmem_ReadFrameContents(SDIF_FrameHeader *head, FILE *f,
                     SDIFmem_Frame *putithere)
{
    /* The user has just read the header for this frame; now we have to read
       all the frame's matrices, put them in an SDIFmem_Matrix linked list,
       and then stuff everything into an SDIFmem_Frame. */
    SDIFresult r;
    SDIFmem_Frame result;
    SDIFmem_Matrix matrix;
    int32_t i, sz;
    SDIFmem_Matrix *prevNextPtr;

    result = (SDIFmem_Frame) (*my_malloc)(sizeof(*result));

    if (result == 0) {
      return ESDIF_OUT_OF_MEMORY;
    }

    result->header = *head;
    result->prev = 0;
    result->next = 0;
    result->matrices = 0;

    prevNextPtr = &(result->matrices);

    for (i = 0; i < head->matrixCount; ++i) {
      matrix = SDIFmem_CreateEmptyMatrix();
      if (matrix == 0) {
        SDIFmem_FreeFrame(result);
        return ESDIF_OUT_OF_MEMORY;
      }

      /* Get the linked list pointers right.  Now if we bomb out
         and call SDIFmem_FreeFrame(result) it will free the matrix
         we just allocated */
      *(prevNextPtr) = matrix;
      prevNextPtr = &(matrix->next);
      matrix->next = 0;

      if ((r = SDIF_ReadMatrixHeader(&(matrix->header), f))!=ESDIF_SUCCESS) {
        SDIFmem_FreeFrame(result);
        return r;
      }

      sz = SDIF_GetMatrixDataSize(&(matrix->header));

      if (sz == 0) {
        matrix->data = NULL;
      }
      else {
        matrix->data = (*my_malloc)(sz);
        if (matrix->data == NULL) {
          SDIFmem_FreeFrame(result);
          return ESDIF_OUT_OF_MEMORY;
        }
      }
      /* COVERITY: what if sz=0?  dereferences null */
      if (sz != 0)
        if ((r = SDIF_ReadMatrixData(matrix->data, f,
                                     &(matrix->header)))!=ESDIF_SUCCESS) {
          SDIFmem_FreeFrame(result);
          return r;
        }
    }
    *putithere = result;
    return ESDIF_SUCCESS;
}

SDIFresult SDIFmem_ReadFrame(FILE *f, SDIFmem_Frame *putithere)
{
    SDIFresult r;
    SDIF_FrameHeader fh;

    if ((r = SDIF_ReadFrameHeader(&fh, f))!=ESDIF_SUCCESS) {
      return r;
    }

    return SDIFmem_ReadFrameContents(&fh, f, putithere);
}

SDIFresult SDIFmem_AddMatrix(SDIFmem_Frame f, SDIFmem_Matrix m)
{
    int32_t sz;
    SDIFmem_Matrix p, last;

    assert(f != NULL);
    assert(m != NULL);

    m->next = NULL;

    p = f->matrices;
    if (p == NULL) {
      f->matrices = m;
    }
    else {
      while (p != NULL) {
        if (SDIF_Char4Eq(p->header.matrixType, m->header.matrixType)) {
          return ESDIF_DUPLICATE_MATRIX_TYPE_IN_FRAME;
        }
        last = p;
        p = p->next;
      }
      last->next = m;
    }

    sz = SDIF_GetMatrixDataSize(&(m->header));

    f->header.size += sz + sizeof(SDIF_MatrixHeader);
    f->header.matrixCount++;
    return ESDIF_SUCCESS;
}

SDIFresult SDIFmem_WriteMatrix(FILE *sdif_handle, SDIFmem_Matrix m)
{
    SDIFresult r;
    sdif_int32 sz, numElements;
    int32_t paddingNeeded;

    if ((r = SDIF_WriteMatrixHeader(&(m->header), sdif_handle))!=ESDIF_SUCCESS) {
      return r;
    }

    sz = SDIF_GetMatrixDataTypeSize(m->header.matrixDataType);
    numElements = (m->header.rowCount * m->header.columnCount);

    switch (sz) {
    case 1:
      r = SDIF_Write1(m->data, numElements, sdif_handle);
      break;
    case 2:
      r = SDIF_Write2(m->data, numElements, sdif_handle);
      break;
    case 4:
      r = SDIF_Write4(m->data, numElements, sdif_handle);
      break;
    case 8:
      r = SDIF_Write8(m->data, numElements, sdif_handle);
      break;

    default:
      return ESDIF_BAD_MATRIX_DATA_TYPE;
    }

    if (r !=ESDIF_SUCCESS) return r;
    paddingNeeded = SDIF_PaddingRequired(&(m->header));
    if (paddingNeeded) {
      char pad[8] = "\0\0\0\0\0\0\0\0";
      if ((r = SDIF_Write1(pad, paddingNeeded, sdif_handle))!=ESDIF_SUCCESS)
        return r;
    }
    return ESDIF_SUCCESS;
}

SDIFresult SDIFmem_WriteFrame(FILE *sdif_handle, SDIFmem_Frame f)
{
    SDIFresult r;
    SDIFmem_Matrix p;

    assert(f != NULL);
    assert(sdif_handle != NULL);

    if ((r = SDIF_WriteFrameHeader(&f->header, sdif_handle))!=ESDIF_SUCCESS) {
      return r;
    }

    for (p = f->matrices; p != NULL; p = p->next) {
      if ((r = SDIFmem_WriteMatrix(sdif_handle, p))!=ESDIF_SUCCESS)
        return r;
    }

    return ESDIF_SUCCESS;
}