1 /*
2     array.h:
3 
4     Copyright (C) 2011, 2017 John ffitch and Stephen Kyne
5 
6     This file is part of Csound.
7 
8     The Csound Library is free software; you can redistribute it
9     and/or modify it under the terms of the GNU Lesser General Public
10     License as published by the Free Software Foundation; either
11     version 2.1 of the License, or (at your option) any later version.
12 
13     Csound is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU Lesser General Public License for more details.
17 
18     You should have received a copy of the GNU Lesser General Public
19     License along with Csound; if not, write to the Free Software
20     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21     02110-1301 USA
22 */
23 
24 #ifndef __ARRAY_H__
25 #define __ARRAY_H__
26 
tabinit(CSOUND * csound,ARRAYDAT * p,int size)27 static inline void tabinit(CSOUND *csound, ARRAYDAT *p, int size)
28 {
29     size_t ss;
30     if (p->dimensions==0) {
31         p->dimensions = 1;
32         p->sizes = (int32_t*)csound->Calloc(csound, sizeof(int32_t));
33     }
34     if (p->data == NULL) {
35         CS_VARIABLE* var = p->arrayType->createVariable(csound, NULL);
36         p->arrayMemberSize = var->memBlockSize;
37         ss = p->arrayMemberSize*size;
38         p->data = (MYFLT*)csound->Calloc(csound, ss);
39         p->allocated = ss;
40     } else if( (ss = p->arrayMemberSize*size) > p->allocated) {
41         p->data = (MYFLT*) csound->ReAlloc(csound, p->data, ss);
42         memset((char*)(p->data)+p->allocated, '\0', ss-p->allocated);
43         p->allocated = ss;
44     }
45     if (p->dimensions==1) p->sizes[0] = size;
46     //p->dimensions = 1;
47 }
tabinit_like(CSOUND * csound,ARRAYDAT * p,ARRAYDAT * tp)48 static inline void tabinit_like(CSOUND *csound, ARRAYDAT *p, ARRAYDAT *tp)
49 {
50     uint32_t ss = 1;
51     if (p->dimensions != tp->dimensions) {
52       int i;
53       p->sizes = (int32_t*)csound->ReAlloc(csound, p->sizes,
54                                            sizeof(int32_t)*tp->dimensions);
55       for (i=0; i<tp->dimensions; i++) {
56         p->sizes[i] = tp->sizes[i];
57         ss *= tp->sizes[i];
58       }
59       p->dimensions = tp->dimensions;
60     }
61     if (p->data == NULL) {
62         CS_VARIABLE* var = p->arrayType->createVariable(csound, NULL);
63         p->arrayMemberSize = var->memBlockSize;
64         ss = p->arrayMemberSize*ss;
65         p->data = (MYFLT*)csound->Calloc(csound, ss);
66         p->allocated = ss;
67     } else if( (ss = p->arrayMemberSize*ss) > p->allocated) {
68         p->data = (MYFLT*) csound->ReAlloc(csound, p->data, ss);
69         p->allocated = ss;
70     }
71 }
72 
tabcheck(CSOUND * csound,ARRAYDAT * p,int size,OPDS * q)73 static inline int tabcheck(CSOUND *csound, ARRAYDAT *p, int size, OPDS *q)
74 {
75     if (p->data==NULL || p->dimensions == 0) {
76       return csound->PerfError(csound, q, "%s", Str("Array not initialised"));
77     }
78     size_t s = p->arrayMemberSize*size;
79     if (s > p->allocated) { /* was arr->allocate */
80       return csound->PerfError(csound, q,
81         Str("Array too small (allocated %zu < needed %zu), but cannot "
82             "allocate during performance pass. Allocate a bigger array at init time"),
83         p->allocated, s);
84       return NOTOK;
85     }
86     p->sizes[0] = size;
87     return OK;
88 }
89 
90 #if 0
91 static inline void tabensure(CSOUND *csound, ARRAYDAT *p, int size)
92 {
93     if (p->data==NULL || p->dimensions == 0 ||
94         (p->dimensions==1 && p->sizes[0] < size)) {
95       size_t ss;
96       if (p->data == NULL) {
97         CS_VARIABLE* var = p->arrayType->createVariable(csound, NULL);
98         p->arrayMemberSize = var->memBlockSize;
99       }
100       ss = p->arrayMemberSize*size;
101       if (p->data==NULL) {
102         p->data = (MYFLT*)csound->Calloc(csound, ss);
103         p->allocated = ss;
104       }
105       else if (ss > p->allocated) {
106         p->data = (MYFLT*) csound->ReAlloc(csound, p->data, ss);
107         p->allocated = ss;
108       }
109       if (p->dimensions==0) {
110         p->dimensions = 1;
111         p->sizes = (int32_t*)csound->Malloc(csound, sizeof(int32_t));
112       }
113       p->sizes[0] = size;
114     }
115     //p->sizes[0] = size;
116 }
117 #endif
118 
119 #endif /* end of include guard: __ARRAY_H__ */
120