2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2012 - Scilab Enterprises - Calixte DENIZET
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
13 #ifndef __SCILABABSTRACTMEMORYALLOCATOR_H__
14 #define __SCILABABSTRACTMEMORYALLOCATOR_H__
16 #include "ScilabAbstractEnvironmentWrapper.hxx"
17 #include "ScilabAbstractEnvironmentException.hxx"
20 #include "api_scilab.h"
23 namespace org_modules_external_objects
26 class ComplexDataPointers
30 ComplexDataPointers(double * _realPtr, double * _imagPtr) : realPtr(_realPtr), imagPtr(_imagPtr) { }
31 ComplexDataPointers() : realPtr(0), imagPtr(0) { }
32 ~ComplexDataPointers() { }
34 double * const realPtr;
35 double * const imagPtr;
38 class ScilabStackAllocator
43 ScilabStackAllocator(void * _pvApiCtx, int _position) : pvApiCtx(_pvApiCtx), position(_position) { }
45 ~ScilabStackAllocator() { }
52 inline static void create(void * pvApiCtx, const int position, const int rows, const int cols, double * ptr)
54 SciErr err = createMatrixOfDouble(pvApiCtx, position, rows, cols, ptr);
58 inline static double * alloc(void * pvApiCtx, const int position, const int rows, const int cols, double * ptr)
61 SciErr err = allocMatrixOfDouble(pvApiCtx, position, rows, cols, &_ptr);
67 inline static void create(void * pvApiCtx, const int position, const int rows, const int cols, float * ptr)
69 throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, "Invalid operation: cannot create a matrix of floats");
72 inline static float * alloc(void * pvApiCtx, const int position, const int rows, const int cols, float * ptr)
74 throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, "Invalid operation: cannot allocate a matrix of floats");
77 inline static void create(void * pvApiCtx, const int position, const int rows, const int cols, double * re, double * im)
79 SciErr err = createComplexMatrixOfDouble(pvApiCtx, position, rows, cols, re, im);
83 inline static ComplexDataPointers alloc(void * pvApiCtx, const int position, const int rows, const int cols, double * re, double * im)
85 double * _re = 0, * _im = 0;
86 SciErr err = allocComplexMatrixOfDouble(pvApiCtx, position, rows, cols, &_re, &_im);
89 return ComplexDataPointers(_re, _im);
92 inline static void create(void * pvApiCtx, const int position, const int rows, const int cols, char * ptr)
94 SciErr err = createMatrixOfInteger8(pvApiCtx, position, rows, cols, ptr);
98 inline static char * alloc(void * pvApiCtx, const int position, const int rows, const int cols, char * ptr)
101 SciErr err = allocMatrixOfInteger8(pvApiCtx, position, rows, cols, &_ptr);
107 inline static void create(void * pvApiCtx, const int position, const int rows, const int cols, unsigned char * ptr)
109 SciErr err = createMatrixOfUnsignedInteger8(pvApiCtx, position, rows, cols, ptr);
113 inline static unsigned char * alloc(void * pvApiCtx, const int position, const int rows, const int cols, unsigned char * ptr)
115 unsigned char * _ptr = 0;
116 SciErr err = allocMatrixOfUnsignedInteger8(pvApiCtx, position, rows, cols, &_ptr);
122 inline static void create(void * pvApiCtx, const int position, const int rows, const int cols, short * ptr)
124 SciErr err = createMatrixOfInteger16(pvApiCtx, position, rows, cols, ptr);
128 inline static short * alloc(void * pvApiCtx, const int position, const int rows, const int cols, short * ptr)
131 SciErr err = allocMatrixOfInteger16(pvApiCtx, position, rows, cols, &_ptr);
137 inline static void create(void * pvApiCtx, const int position, const int rows, const int cols, unsigned short * ptr)
139 SciErr err = createMatrixOfUnsignedInteger16(pvApiCtx, position, rows, cols, ptr);
143 inline static unsigned short * alloc(void * pvApiCtx, const int position, const int rows, const int cols, unsigned short * ptr)
145 unsigned short * _ptr = 0;
146 SciErr err = allocMatrixOfUnsignedInteger16(pvApiCtx, position, rows, cols, &_ptr);
152 inline static void create(void * pvApiCtx, const int position, const int rows, const int cols, int * ptr)
154 SciErr err = createMatrixOfInteger32(pvApiCtx, position, rows, cols, ptr);
158 inline static int * alloc(void * pvApiCtx, const int position, const int rows, const int cols, int * ptr)
161 SciErr err = allocMatrixOfInteger32(pvApiCtx, position, rows, cols, &_ptr);
167 inline static void create(void * pvApiCtx, const int position, const int rows, const int cols, unsigned int * ptr)
169 SciErr err = createMatrixOfUnsignedInteger32(pvApiCtx, position, rows, cols, ptr);
173 inline static unsigned int * alloc(void * pvApiCtx, const int position, const int rows, const int cols, unsigned int * ptr)
175 unsigned int * _ptr = 0;
176 SciErr err = allocMatrixOfUnsignedInteger32(pvApiCtx, position, rows, cols, &_ptr);
182 #ifdef __SCILAB_INT64__
184 inline static void create(void * pvApiCtx, const int position, const int rows, const int cols, long long * ptr)
186 SciErr err = createMatrixOfInteger64(pvApiCtx, position, rows, cols, ptr);
190 inline static long long * alloc(void * pvApiCtx, const int position, const int rows, const int cols, long long * ptr)
192 long long * _ptr = 0;
193 SciErr err = allocMatrixOfInteger64(pvApiCtx, position, rows, cols, &_ptr);
199 inline static void create(void * pvApiCtx, const int position, const int rows, const int cols, unsigned long long * ptr)
201 SciErr err = createMatrixOfUnsignedIntege64(pvApiCtx, position, rows, cols, ptr);
205 inline static unsigned long long * alloc(void * pvApiCtx, const int position, const int rows, const int cols, unsigned long long * ptr)
207 unsigned long long * _ptr = 0;
208 SciErr err = allocMatrixOfUnsignedInteger64(pvApiCtx, position, rows, cols, &_ptr);
216 inline static void create(void * pvApiCtx, const int position, const int rows, const int cols, long long * ptr)
219 alloc(pvApiCtx, position, rows, cols, dataPtr);
220 for (int i = 0; i < rows * cols; i++)
222 dataPtr[i] = static_cast<int>(ptr[i]);
226 inline static long long * alloc(void * pvApiCtx, const int position, const int rows, const int cols, long long * ptr)
228 throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, "Invalid operation: cannot allocate a matrix of Integer64");
231 inline static void create(void * pvApiCtx, const int position, const int rows, const int cols, unsigned long long * ptr)
233 unsigned int * dataPtr = 0;
234 alloc(pvApiCtx, position, rows, cols, dataPtr);
235 for (int i = 0; i < rows * cols; i++)
237 dataPtr[i] = static_cast<unsigned int>(ptr[i]);
241 inline static unsigned long long * alloc(void * pvApiCtx, const int position, const int rows, const int cols, unsigned long long * ptr)
243 throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, "Invalid operation: cannot allocate a matrix of UInteger64");
248 inline static void create(void * pvApiCtx, const int position, const int rows, const int cols, char ** ptr)
250 SciErr err = createMatrixOfString(pvApiCtx, position, rows, cols, const_cast<const char * const *>(ptr));
254 inline static char ** alloc(void * pvApiCtx, const int position, const int rows, const int cols, char ** ptr)
256 throw ScilabAbstractEnvironmentException("Invalid operation: cannot allocate a matrix of String");
259 inline static void createBool(void * pvApiCtx, const int position, const int rows, const int cols, int * ptr)
261 SciErr err = createMatrixOfBoolean(pvApiCtx, position, rows, cols, ptr);
265 inline static int * allocBool(void * pvApiCtx, const int position, const int rows, const int cols, int * ptr)
268 SciErr err = allocMatrixOfBoolean(pvApiCtx, position, rows, cols, &_ptr);
277 inline static void checkError(const SciErr & err)
281 throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, "Cannot allocate memory");
287 template <typename T>
288 class ScilabSingleTypeStackAllocator : public ScilabStackAllocator
293 ScilabSingleTypeStackAllocator(void * _pvApiCtx, int _position) : ScilabStackAllocator(_pvApiCtx, _position) { }
295 ~ScilabSingleTypeStackAllocator() { }
297 virtual T * allocate(const int rows, const int cols, T * dataPtr) const
301 createEmptyMatrix(pvApiCtx, position);
307 create(pvApiCtx, position, rows, cols, dataPtr);
312 return alloc(pvApiCtx, position, rows, cols, dataPtr);
317 typedef ScilabSingleTypeStackAllocator<double> ScilabDoubleStackAllocator;
318 typedef ScilabSingleTypeStackAllocator<char *> ScilabStringStackAllocator;
319 typedef ScilabSingleTypeStackAllocator<char> ScilabCharStackAllocator;
320 typedef ScilabSingleTypeStackAllocator<unsigned char> ScilabUCharStackAllocator;
321 typedef ScilabSingleTypeStackAllocator<short> ScilabShortStackAllocator;
322 typedef ScilabSingleTypeStackAllocator<unsigned short> ScilabUShortStackAllocator;
323 typedef ScilabSingleTypeStackAllocator<int> ScilabIntStackAllocator;
324 typedef ScilabSingleTypeStackAllocator<unsigned int> ScilabUIntStackAllocator;
325 typedef ScilabSingleTypeStackAllocator<long long> ScilabLongStackAllocator;
326 typedef ScilabSingleTypeStackAllocator<unsigned long long> ScilabULongStackAllocator;
327 typedef ScilabSingleTypeStackAllocator<float> ScilabFloatStackAllocator;
329 class ScilabComplexStackAllocator : public ScilabStackAllocator
334 ScilabComplexStackAllocator(void * _pvApiCtx, int _position) : ScilabStackAllocator(_pvApiCtx, _position) { }
336 ~ScilabComplexStackAllocator() { }
338 ComplexDataPointers allocate(const int rows, const int cols, double * realPtr, double * imagPtr) const
342 createEmptyMatrix(pvApiCtx, position);
343 return ComplexDataPointers();
346 if (realPtr && imagPtr)
348 create(pvApiCtx, position, rows, cols, realPtr, imagPtr);
349 return ComplexDataPointers();
353 return alloc(pvApiCtx, position, rows, cols, realPtr, imagPtr);
358 class ScilabBooleanStackAllocator : public ScilabSingleTypeStackAllocator<int>
363 ScilabBooleanStackAllocator(void * _pvApiCtx, int _position) : ScilabSingleTypeStackAllocator<int>(_pvApiCtx, _position) { }
365 ~ScilabBooleanStackAllocator() { }
367 int * allocate(const int rows, const int cols, int * dataPtr) const
371 createEmptyMatrix(pvApiCtx, position);
377 createBool(pvApiCtx, position, rows, cols, dataPtr);
382 return allocBool(pvApiCtx, position, rows, cols, dataPtr);
386 template <typename T>
387 int * allocate(const int rows, const int cols, T * dataPtr)
391 createEmptyMatrix(pvApiCtx, position);
398 allocBool(pvApiCtx, position, rows, cols, ptr);
399 for (int i = 0; i < rows * cols; i++)
401 ptr[i] = static_cast<int>(dataPtr[i]);
408 throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, "Invalid operation: cannot allocate a matrix of Boolean");
415 #endif // __SCILABABSTRACTMEMORYALLOCATOR_H__