15f49333f9d07a687382cbb2d678b43e8be9d1cc
[scilab.git] / scilab / modules / scicos_blocks / src / c / scoMemoryScope.c
1 /*  Scicos
2 *
3 *  Copyright (C) INRIA - METALAU Project <scicos@inria.fr>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 *
19 * See the file ./license.txt
20 */
21 /**
22    \file scoMemoryScope.c
23    \author Benoit Bayol
24    \version 1.0
25    \date September 2006 - January 2007
26    \brief Source Code of all functions interacting with the memory of the computer for the ScopeMemory structure
27 */
28 #include <stdio.h>
29 #include "scoBase.h"
30 #include "scoMisc.h"
31 #include "scoMemoryScope.h"
32 #include "scoGetProperty.h"
33 #include "scoSetProperty.h"
34 #include "sciprint.h"
35
36 /** This function use scicos_malloc on the whole structure and set up 0 for whole properties*/
37 void scoInitScopeMemory(void ** block_work, ScopeMemory ** pScopeMemory, int number_of_subwin, int * number_of_curves_by_subwin)
38 {
39   int i,j;
40   *block_work = (ScopeMemory*)scicos_malloc(sizeof(ScopeMemory));
41   if(*block_work == NULL)
42     {
43       scoScopeError(*pScopeMemory,1);
44     }
45   *pScopeMemory = (ScopeMemory*) *block_work;
46
47   scoSetHandleScopeWindow((*pScopeMemory),0);
48
49   scoSetNumberOfSubwin((*pScopeMemory),number_of_subwin);
50
51   (*pScopeMemory)->number_of_curves_by_subwin = (scoLineOfInteger)scicos_malloc(number_of_subwin*sizeof(scoInteger));
52   if((*pScopeMemory)->number_of_curves_by_subwin == NULL)
53     {
54       scoScopeError(*pScopeMemory,1);
55     }
56   for(i=0 ; i < number_of_subwin ; i++)
57     {
58       scoSetNumberOfCurvesBySubwin((*pScopeMemory),i,number_of_curves_by_subwin[i]);
59     }
60
61   (*pScopeMemory)->new_draw = (scoLineOfInteger)scicos_malloc(number_of_subwin*sizeof(scoInteger));
62   if((*pScopeMemory)->new_draw == NULL)
63     {
64       scoScopeError(*pScopeMemory,1);
65     }
66   for(i=0; i < number_of_subwin ; i++)
67     {
68       scoSetNewDraw(*pScopeMemory,i,0);
69     }
70  
71   (*pScopeMemory)->shortdraw_size = (scoLineOfInteger)scicos_malloc(number_of_subwin*sizeof(scoInteger));
72   if((*pScopeMemory)->shortdraw_size == NULL)
73     {
74       scoScopeError(*pScopeMemory, 1);
75     }
76   for(i=0; i < number_of_subwin ; i++)
77     {
78       scoSetShortDrawSize(*pScopeMemory,i,0);
79     }
80
81   (*pScopeMemory)->period = (scoLineOfDouble)scicos_malloc(number_of_subwin*sizeof(scoDouble));
82   if((*pScopeMemory)->period == NULL)
83     {
84       scoScopeError(*pScopeMemory, 1);
85     }
86   for(i=0; i < number_of_subwin ; i++)
87     {
88       scoSetPeriod(*pScopeMemory,i,0);
89     }
90
91   (*pScopeMemory)->period_counter = (scoLineOfInteger)scicos_malloc(number_of_subwin*sizeof(scoInteger));
92   if((*pScopeMemory)->period_counter == NULL)
93     {
94       scoScopeError(*pScopeMemory, 1);
95     }
96   for(i=0; i < number_of_subwin ; i++)
97     {
98       scoSetPeriodCounter(*pScopeMemory,i,0);
99     }
100
101   (*pScopeMemory)->longdraw_size = (scoLineOfInteger)scicos_malloc(number_of_subwin*sizeof(scoInteger));
102   if((*pScopeMemory)->longdraw_size == NULL)
103     {
104       scoScopeError(*pScopeMemory,1);
105     }
106   for(i=0; i < number_of_subwin ; i++)
107     {
108       scoSetLongDrawSize(*pScopeMemory,i,0);
109     }
110
111   (*pScopeMemory)->hAxes = (scoLineOfLongInteger)scicos_malloc(number_of_subwin*sizeof(scoLongInteger));
112   if((*pScopeMemory)->hAxes == NULL)
113     {
114       scoScopeError(*pScopeMemory, 1);
115     }
116   for(i=0; i < number_of_subwin ; i++)
117     {
118       scoSetHandleAxes(*pScopeMemory,i,0);
119     }
120
121
122   (*pScopeMemory)->hShortDraw = (scoMatrixOfLongInteger)scicos_malloc(number_of_subwin*sizeof(scoLineOfLongInteger));
123   if((*pScopeMemory)->hShortDraw == NULL)
124     {
125       scoScopeError(*pScopeMemory, 1);
126     }
127
128   (*pScopeMemory)->hLongDraw = (scoMatrixOfLongInteger)scicos_malloc(number_of_subwin*sizeof(scoLineOfLongInteger));
129   if((*pScopeMemory)->hLongDraw == NULL)
130     {
131       scoScopeError(*pScopeMemory, 1);
132     }
133
134   for(i = 0 ; i < number_of_subwin ; i++)
135     {
136       (*pScopeMemory)->hShortDraw[i] = (scoLineOfLongInteger)scicos_malloc(number_of_curves_by_subwin[i]*sizeof(scoLongInteger));
137       if((*pScopeMemory)->hShortDraw[i] == NULL)
138         {
139           scoScopeError(*pScopeMemory, 1);
140         }
141       for(j=0; j < number_of_curves_by_subwin[i] ; j++)
142         {
143           scoSetHandleShortDraw(*pScopeMemory,i,j,0);
144         }
145   
146       (*pScopeMemory)->hLongDraw[i]  = (scoLineOfLongInteger)scicos_malloc(number_of_curves_by_subwin[i]*sizeof(scoLongInteger));
147       if((*pScopeMemory)->hLongDraw[i] == NULL)
148         {
149           scoScopeError(*pScopeMemory, 1);
150         }
151       for(j=0; j < number_of_curves_by_subwin[i] ; j++)
152         {
153           scoSetHandleLongDraw(*pScopeMemory,i,j,0);
154         }
155     }
156   scoSetScopeActivation(*pScopeMemory,0); //Not activated by default
157 }
158
159
160 void scoFreeScopeMemory(void ** block_work, ScopeMemory ** pScopeMemory)
161 {
162   int i;
163
164   if (*pScopeMemory!=NULL) {
165     scicos_free((*pScopeMemory)->new_draw);
166     scicos_free((*pScopeMemory)->number_of_curves_by_subwin);
167     scicos_free((*pScopeMemory)->period_counter);
168     scicos_free((*pScopeMemory)->period);
169     scicos_free((*pScopeMemory)->longdraw_size);
170     scicos_free((*pScopeMemory)->shortdraw_size);
171     for(i = 0; i < (*pScopeMemory)->number_of_subwin ; i++) {
172       scicos_free((*pScopeMemory)->hShortDraw[i]);
173       scicos_free((*pScopeMemory)->hLongDraw[i]);
174     }
175     scicos_free((*pScopeMemory)->hShortDraw);
176     scicos_free((*pScopeMemory)->hLongDraw);
177     scicos_free((*pScopeMemory)->hAxes);
178     scicos_free(*block_work);
179   }
180
181 }
182
183 void scoRetrieveScopeMemory(void ** block_work, ScopeMemory ** pScopeMemory)
184 {
185   *pScopeMemory = (ScopeMemory*)*block_work;
186 }
187
188 /** If I have a longdraw of size 200, the next buffer size is 20 and to be sure I add 5000 more points so I ve got a 7020 pts longdraw at the end. Watch out the function use REALLOC there is no scicos_realloc() existing in scicos*/
189 void scoReallocLongDraw(scoGraphicalObject pLongDraw, int NbrPtsLong, int shortdraw_size, int plus)
190 {
191   switch(sciGetEntityType(pLongDraw))
192     {
193     case SCI_POLYLINE:
194       pPOLYLINE_FEATURE(pLongDraw)->pvx = REALLOC((pPOLYLINE_FEATURE(pLongDraw)->pvx),(NbrPtsLong+shortdraw_size+plus)*sizeof(double));
195       pPOLYLINE_FEATURE(pLongDraw)->pvy = REALLOC((pPOLYLINE_FEATURE(pLongDraw)->pvy),(NbrPtsLong+shortdraw_size+plus)*sizeof(double));
196       //If we are in 2D mode in the window at init then pvz is not initialized
197       if(pPOLYLINE_FEATURE(pLongDraw)->pvz != NULL)
198         {
199           pPOLYLINE_FEATURE(pLongDraw)->pvz = REALLOC((pPOLYLINE_FEATURE(pLongDraw)->pvz),(NbrPtsLong+shortdraw_size+plus)*sizeof(double));
200         }
201       break;
202     case SCI_SEGS:
203       pSEGS_FEATURE(pLongDraw)->vx = REALLOC((pSEGS_FEATURE(pLongDraw)->vx),(NbrPtsLong+shortdraw_size+plus)*sizeof(double));
204       pSEGS_FEATURE(pLongDraw)->vy = REALLOC((pSEGS_FEATURE(pLongDraw)->vy),(NbrPtsLong+shortdraw_size+plus)*sizeof(double));
205       //If we are in 2D mode in the window at init then vz is not initialized
206       if(pSEGS_FEATURE(pLongDraw)->vz != NULL)
207         {
208           pSEGS_FEATURE(pLongDraw)->vz = REALLOC((pSEGS_FEATURE(pLongDraw)->vz),(NbrPtsLong+shortdraw_size+plus)*sizeof(double));
209         }
210       break;
211     default:
212       sciprint("SCOPE ERROR : Error in scoReallocLongDraw()");
213       break;
214     }
215 }