aboutsummaryrefslogtreecommitdiff
path: root/src/tools.cpp
diff options
context:
space:
mode:
authorRichard Knight <q@1bpm.net>2022-08-25 18:14:53 +0100
committerRichard Knight <q@1bpm.net>2022-08-25 18:14:53 +0100
commit9ac4dfb0f0ba5be3f21bd3904c99ba67543b1833 (patch)
treeeda9aabe89979289d61f8ef40f605f18a3223438 /src/tools.cpp
parentdb5bc3227bf89c0eaf2f2498cf0f1a80cf5eead0 (diff)
downloadcsound-sqldb-9ac4dfb0f0ba5be3f21bd3904c99ba67543b1833.tar.gz
csound-sqldb-9ac4dfb0f0ba5be3f21bd3904c99ba67543b1833.tar.bz2
csound-sqldb-9ac4dfb0f0ba5be3f21bd3904c99ba67543b1833.zip
fixed string and array memory leaks
Diffstat (limited to 'src/tools.cpp')
-rw-r--r--src/tools.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/tools.cpp b/src/tools.cpp
new file mode 100644
index 0000000..ec8863b
--- /dev/null
+++ b/src/tools.cpp
@@ -0,0 +1,54 @@
+/*
+ tools.cpp
+ Copyright (C) 2022 Richard Knight
+
+
+ This program 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 3 of the License, or (at your option) any later version.
+
+ This program 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 this program; if not, write to the Free Software Foundation,
+ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+ */
+
+#include <plugin.h>
+
+STRINGDAT* arrayInit(csnd::Csound* csound, ARRAYDAT* array, int rows, int cols) {
+ int totalResults = rows * cols;
+ size_t totalAllocated;
+
+ if (array->data == NULL) {
+ array->sizes = (int32_t*) csound->calloc(sizeof(int32_t) * 2);
+ array->sizes[0] = rows;
+ array->sizes[1] = cols;
+ array->dimensions = 2;
+ CS_VARIABLE *var = array->arrayType->createVariable(csound->get_csound(), NULL);
+ array->arrayMemberSize = var->memBlockSize;
+ totalAllocated = array->arrayMemberSize * totalResults;
+ array->data = (MYFLT*) csound->calloc(totalAllocated);
+ } else if ((totalAllocated = array->arrayMemberSize * totalResults) > array->allocated) {
+ array->data = (MYFLT*) csound->realloc(array->data, totalAllocated);
+ memset((char*)(array->data)+array->allocated, '\0', totalAllocated - array->allocated);
+ array->allocated = totalAllocated;
+ }
+
+ // convenience return to be used if it is a string array
+ return (STRINGDAT*) array->data;
+}
+
+
+void insertArrayStringItem(csnd::Csound* csound, STRINGDAT* strings, int index, char* item) {
+ strings[index].size = strlen(item) + 1;
+ if (strings[index].data != NULL) {
+ csound->free(strings[index].data);
+ }
+ strings[index].data = csound->strdup(item);
+} \ No newline at end of file