File: csound_type_system.c

package info (click to toggle)
csound 1%3A6.18.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, 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 (320 lines) | stat: -rw-r--r-- 9,109 bytes parent folder | download
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
320
/*
 csound_type_system.c:

 Copyright (C) 2012,2013 Steven Yi

 This file is part of Csound.

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

 Csound 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 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with Csound; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 02110-1301 USA
 */

#include "csound_type_system.h"
#include <string.h>
#include <stdio.h>
#include "csoundCore.h"

int csTypeExistsWithSameName(TYPE_POOL* pool, CS_TYPE* typeInstance) {
    CS_TYPE_ITEM* current = pool->head;
    while (current != NULL) {

      /* printf("Search if type [%s] == [%s]",
         current->varTypeName, typeInstance->varTypeName); */

      if (strcmp(current->cstype->varTypeName,
                 typeInstance->varTypeName) == 0) {
        return 1;
      }
      current = current->next;
    }

    return 0;
}

CS_TYPE* csoundGetTypeWithVarTypeName(TYPE_POOL* pool, char* typeName) {
    CS_TYPE_ITEM* current = pool->head;
    while (current != NULL) {
      if (strcmp(typeName, current->cstype->varTypeName) == 0) {
        return current->cstype;
      }
      current = current->next;
    }
    return NULL;
}

CS_TYPE* csoundGetTypeForVarName(TYPE_POOL* pool, char* varName) {
    CS_TYPE_ITEM* current = pool->head;
    char temp[2];
    temp[0] = varName[0];
    temp[1] = 0;
    while (current != NULL) {
      if (strcmp(temp, current->cstype->varTypeName) == 0) {
        return current->cstype;
      }
      current = current->next;
    }
    return NULL;
}

int csoundAddVariableType(CSOUND* csound, TYPE_POOL* pool, CS_TYPE* typeInstance)
{
    CS_TYPE_ITEM* item;
    if (csTypeExistsWithSameName(pool, typeInstance)) {
      return 0;
    }

    item = (CS_TYPE_ITEM*)csound->Calloc(csound, sizeof(CS_TYPE_ITEM));
    item->cstype = typeInstance;

    if (pool->head == NULL) {
      pool->head = item;
    } else {
      CS_TYPE_ITEM* current = pool->head;
      while (current->next) {
        current = current->next;
      }
      current->next = item;
      item->next = NULL;
    }

    /* printf("Adding type with type name: %s\n", typeInstance->varTypeName); */

    return 1;
}

/* VAR POOL FUNCTIONS */


CS_VAR_POOL* csoundCreateVarPool(CSOUND* csound) {
    CS_VAR_POOL* varPool = csound->Calloc(csound, sizeof(CS_VAR_POOL));
    varPool->table = cs_hash_table_create(csound);
    return varPool;
}

void csoundFreeVarPool(CSOUND* csound, CS_VAR_POOL* pool) {
    if(pool->table) cs_hash_table_mfree_complete(csound, pool->table);
    csound->Free(csound, pool);
}

char* getVarSimpleName(CSOUND* csound, const char* varName) {
    char* retVal;

    if (varName[0] != '[') {
      retVal = (char*)csound->Calloc(csound, sizeof(char) * (strlen(varName) + 1));
      strcpy(retVal, varName);
    } else {
      int start = 0;
      int typeEnd = 0;
      int len = strlen(varName);
      int newFirstLen, newSecondLen, newTotalLen;
      char* t = (char*) varName;
      char* t2;

      while(*t == '[') {
        t++;
        start++;
      }
      typeEnd = start;
      t2 = t;
      while(*t2 != ']' && *t2 != (char)0) {
        t2++;
        typeEnd++;
      }
      t2++;
      typeEnd++;

      newFirstLen = (typeEnd - start - 1);
      newSecondLen = (len - typeEnd);
      newTotalLen = newFirstLen + newSecondLen;

      retVal = (char*)csound->Calloc(csound, sizeof(char) * (newTotalLen + 1));
      strncpy(retVal, t, newFirstLen);
      strncpy(retVal + newFirstLen, t2, newSecondLen);
    }

    return retVal;
}

CS_VARIABLE* csoundCreateVariable(void* csound, TYPE_POOL* pool,
                                  CS_TYPE* type, char* name, void* typeArg)
{
    CS_TYPE_ITEM* current = pool->head;
    if (LIKELY(type != NULL))
      while (current != NULL) {
        if (strcmp(type->varTypeName, current->cstype->varTypeName) == 0) {
          CS_VARIABLE* var = current->cstype->createVariable(csound, typeArg);
          var->varType = type;
          var->varName = cs_strdup(csound, name);
          return var;
        }
        current = current->next;
      }
    else ((CSOUND *)csound)->ErrorMsg(csound,
                                      Str("cannot create variable %s: NULL type"),
                                      name);
    return NULL;
}

CS_VARIABLE* csoundFindVariableWithName(CSOUND* csound, CS_VAR_POOL* pool,
                                        const char* name)
{

    CS_VARIABLE* returnValue = cs_hash_table_get(csound, pool->table, (char*)name);

    if (returnValue == NULL && pool->parent != NULL) {
      returnValue = csoundFindVariableWithName(csound, pool->parent, name);
    }

    return returnValue;
}

CS_VARIABLE* csoundGetVariable(CS_VAR_POOL* pool, int index) {

    CS_VARIABLE* current = pool->head;
    int i;

    for(i = 0; i < index || current != NULL; i++) {
      /* THIS WAS WRONG!! && or || meant foR , ?? */
      current = current->next;
    }

    return current;
}

//int csoundGetVariableIndex(CS_VAR_POOL* pool, CS_VARIABLE* var) {
//    CS_VARIABLE* current = pool->head;
//    int index = 0;
//
//    if (current == NULL) {
//        return -1;
//    }
//
//    for (index = 0; current != NULL; index++) {
//        if (current == var) {
//            return index;
//        }
//    }
//    return -1;
//}

int csoundAddVariable(CSOUND* csound, CS_VAR_POOL* pool, CS_VARIABLE* var) {
  if(var != NULL) {
    if(pool->head == NULL) {
      pool->head = var;
      pool->tail = var;
    } else {
      pool->tail->next = var;
      pool->tail = var;
    }
    cs_hash_table_put(csound, pool->table, var->varName, var);
    // may need to revise this; var pools are accessed as MYFLT*,
    // so need to ensure all memory is aligned to sizeof(MYFLT)
    var->memBlockIndex = (pool->poolSize / sizeof(MYFLT)) +
      ((pool->varCount + 1) * (CS_FLOAT_ALIGN(CS_VAR_TYPE_OFFSET) / sizeof(MYFLT)));
    pool->poolSize += var->memBlockSize;
    pool->varCount += 1;
    return 0;
  } else return -1;
}

void recalculateVarPoolMemory(void* csound, CS_VAR_POOL* pool)
{
    CS_VARIABLE* current = pool->head;
    int varCount = 1;
    pool->poolSize = 0;

    while (current != NULL) {
      /* VL 26-12-12: had to revert these lines to avoid memory crashes
         with higher ksmps */
      if(current->updateMemBlockSize != NULL) {
        current->updateMemBlockSize(csound, current);
      }

      current->memBlockIndex = (pool->poolSize / sizeof(MYFLT)) +
        (varCount * CS_FLOAT_ALIGN(CS_VAR_TYPE_OFFSET) / sizeof(MYFLT));
      pool->poolSize += current->memBlockSize;

      current = current->next;
      varCount++;
    }
}

void reallocateVarPoolMemory(void* csound, CS_VAR_POOL* pool) {
    CS_VARIABLE* current = pool->head;
    CS_VAR_MEM* varMem = NULL;
    size_t memSize;
    pool->poolSize = 0;

    while (current != NULL) {
      varMem = current->memBlock;
      memSize = current->memBlockSize;

      if(current->updateMemBlockSize != NULL) {
        current->updateMemBlockSize(csound, current);
      }
      // VL 14-3-2015 only realloc if we need to
      if(memSize < (size_t)current->memBlockSize) {
          memSize = CS_VAR_TYPE_OFFSET + current->memBlockSize;
          varMem =
               (CS_VAR_MEM *)((CSOUND *)csound)->ReAlloc(csound,varMem,
                                             memSize);
          current->memBlock = varMem;
       }
       pool->poolSize += current->memBlockSize;
       current = current->next;
    }
}

void deleteVarPoolMemory(void* csnd, CS_VAR_POOL* pool) {
    CS_VARIABLE* current = pool->head, *tmp;
    CSOUND *csound = (CSOUND *)csnd;
    CS_TYPE* type;
    while (current != NULL) {
      tmp = current;
      type = current->subType;
      if (type->freeVariableMemory != NULL) {
        type->freeVariableMemory(csound, current->memBlock);
      }
      csound->Free(csound, current->memBlock);
      current = current->next;
      csound->Free(csound, tmp);
    }
}



void initializeVarPool(void *csound, MYFLT* memBlock, CS_VAR_POOL* pool) {
    CS_VARIABLE* current = pool->head;
    //int varNum = 1;

    while (current != NULL) {
      if (current->initializeVariableMemory != NULL) {
        current->initializeVariableMemory(csound, current,
                                          memBlock + current->memBlockIndex);
      }
      //x varNum++;
      current = current->next;
    }
}

void debug_print_varpool(CSOUND* csound, CS_VAR_POOL* pool) {
    CS_VARIABLE* gVar = pool->head;
    int count = 0;
    while(gVar != NULL) {
      csound->Message(csound, "  %d) %s:%s\n", count++,
                      gVar->varName, gVar->varType->varTypeName);
      gVar = gVar->next;
    }
}