Avoid overloading of a Windows API function making compilation fail under Windows
[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 /**
23    \file scoMemoryScope.c
24    \author Benoit Bayol
25    \version 1.0
26    \date September 2006 - January 2007
27    \brief Source Code of all functions interacting with the memory of the computer for the ScopeMemory structure
28 */
29 /*--------------------------------------------------------------------------*/ 
30 #include <stdio.h>
31 #include "scoBase.h"
32 #include "scoMisc.h"
33 #include "scoMemoryScope.h"
34 #include "scoGetProperty.h"
35 #include "scoSetProperty.h"
36 #include "sciprint.h"
37 #include "scicos_malloc.h"
38 #include "scicos_free.h"
39 #include "localization.h"
40 /*--------------------------------------------------------------------------*/ 
41 /** This function use scicos_malloc on the whole structure and set up 0 for whole properties*/
42 void scoInitScopeMemory(void ** block_work, ScopeMemory ** pScopeMemory, int number_of_subwin, int * number_of_curves_by_subwin)
43 {
44         int i,j;
45         *block_work = (ScopeMemory*)scicos_malloc(sizeof(ScopeMemory));
46         if(*block_work == NULL)
47         {
48                 scoScopeError(*pScopeMemory,1);
49         }
50         *pScopeMemory = (ScopeMemory*) *block_work;
51
52         scoSetHandleScopeWindow((*pScopeMemory),0);
53
54         scoSetNumberOfSubwin((*pScopeMemory),number_of_subwin);
55
56         (*pScopeMemory)->number_of_curves_by_subwin = (scoLineOfInteger)scicos_malloc(number_of_subwin*sizeof(scoInteger));
57         if((*pScopeMemory)->number_of_curves_by_subwin == NULL)
58         {
59                 scoScopeError(*pScopeMemory,1);
60         }
61         for(i=0 ; i < number_of_subwin ; i++)
62         {
63                 scoSetNumberOfCurvesBySubwin((*pScopeMemory),i,number_of_curves_by_subwin[i]);
64         }
65
66         (*pScopeMemory)->new_draw = (scoLineOfInteger)scicos_malloc(number_of_subwin*sizeof(scoInteger));
67         if((*pScopeMemory)->new_draw == NULL)
68         {
69                 scoScopeError(*pScopeMemory,1);
70         }
71         for(i=0; i < number_of_subwin ; i++)
72         {
73                 scoSetNewDraw(*pScopeMemory,i,0);
74         }
75
76         (*pScopeMemory)->shortdraw_size = (scoLineOfInteger)scicos_malloc(number_of_subwin*sizeof(scoInteger));
77         if((*pScopeMemory)->shortdraw_size == NULL)
78         {
79                 scoScopeError(*pScopeMemory, 1);
80         }
81         for(i=0; i < number_of_subwin ; i++)
82         {
83                 scoSetShortDrawSize(*pScopeMemory,i,0);
84         }
85
86         (*pScopeMemory)->period = (scoLineOfDouble)scicos_malloc(number_of_subwin*sizeof(scoDouble));
87         if((*pScopeMemory)->period == NULL)
88         {
89                 scoScopeError(*pScopeMemory, 1);
90         }
91         for(i=0; i < number_of_subwin ; i++)
92         {
93                 scoSetPeriod(*pScopeMemory,i,0);
94         }
95
96         (*pScopeMemory)->period_counter = (scoLineOfInteger)scicos_malloc(number_of_subwin*sizeof(scoInteger));
97         if((*pScopeMemory)->period_counter == NULL)
98         {
99                 scoScopeError(*pScopeMemory, 1);
100         }
101         for(i=0; i < number_of_subwin ; i++)
102         {
103                 scoSetPeriodCounter(*pScopeMemory,i,0);
104         }
105
106         (*pScopeMemory)->longdraw_size = (scoLineOfInteger)scicos_malloc(number_of_subwin*sizeof(scoInteger));
107         if((*pScopeMemory)->longdraw_size == NULL)
108         {
109                 scoScopeError(*pScopeMemory,1);
110         }
111         for(i=0; i < number_of_subwin ; i++)
112         {
113                 scoSetLongDrawSize(*pScopeMemory,i,0);
114         }
115
116         (*pScopeMemory)->hAxes = (scoLineOfLongInteger)scicos_malloc(number_of_subwin*sizeof(scoLongInteger));
117         if((*pScopeMemory)->hAxes == NULL)
118         {
119                 scoScopeError(*pScopeMemory, 1);
120         }
121         for(i=0; i < number_of_subwin ; i++)
122         {
123                 scoSetHandleAxes(*pScopeMemory,i,0);
124         }
125
126
127         (*pScopeMemory)->hShortDraw = (scoMatrixOfLongInteger)scicos_malloc(number_of_subwin*sizeof(scoLineOfLongInteger));
128         if((*pScopeMemory)->hShortDraw == NULL)
129         {
130                 scoScopeError(*pScopeMemory, 1);
131         }
132
133         (*pScopeMemory)->hLongDraw = (scoMatrixOfLongInteger)scicos_malloc(number_of_subwin*sizeof(scoLineOfLongInteger));
134         if((*pScopeMemory)->hLongDraw == NULL)
135         {
136                 scoScopeError(*pScopeMemory, 1);
137         }
138
139         for(i = 0 ; i < number_of_subwin ; i++)
140         {
141                 (*pScopeMemory)->hShortDraw[i] = (scoLineOfLongInteger)scicos_malloc(number_of_curves_by_subwin[i]*sizeof(scoLongInteger));
142                 if((*pScopeMemory)->hShortDraw[i] == NULL)
143                 {
144                         scoScopeError(*pScopeMemory, 1);
145                 }
146                 for(j=0; j < number_of_curves_by_subwin[i] ; j++)
147                 {
148                         scoSetHandleShortDraw(*pScopeMemory,i,j,0);
149                 }
150
151                 (*pScopeMemory)->hLongDraw[i]  = (scoLineOfLongInteger)scicos_malloc(number_of_curves_by_subwin[i]*sizeof(scoLongInteger));
152                 if((*pScopeMemory)->hLongDraw[i] == NULL)
153                 {
154                         scoScopeError(*pScopeMemory, 1);
155                 }
156                 for(j=0; j < number_of_curves_by_subwin[i] ; j++)
157                 {
158                         scoSetHandleLongDraw(*pScopeMemory,i,j,0);
159                 }
160         }
161         scoSetScopeActivation(*pScopeMemory,0); //Not activated by default
162 }
163 /*--------------------------------------------------------------------------*/ 
164 //** 15/16 May 2008
165 /* Some explanations about this garbled code: 
166 - if you stop a Scicos simulation (intentionally or not) and 
167 - you delete one or more scope windows and
168 - you try to "restart" or "end" the simulation
169 - you force the "ending" section of each Scicos blocks, including scopes
170 - inside the "ending" section of each Scicos scope there is a call to this
171 very function "scoFreeScopeMemory" that free some data structure
172
173 BUT
174
175 - if you have ALREADY close manually the windows, some of them are aready 
176 "freed" 
177
178 THEN
179
180 - you need to check before try to free the resources 
181
182 For the moment we (Benoit, Simone, JB) have decided to leave some commented
183 debug code.  
184
185 */
186
187 void scoFreeScopeMemory(void ** block_work, ScopeMemory ** pScopeMemory)
188 {
189         int i;
190
191         if (*pScopeMemory != NULL) 
192         {
193                 //**printf("BANZAI \n");
194
195                 //** if ( (*pScopeMemory)->new_draw != NULL ) 
196                 //**  printf("[ 1 ] \n");
197                 scicos_free((*pScopeMemory)->new_draw);
198
199                 //** if ( (*pScopeMemory)->number_of_curves_by_subwin != NULL )
200                 //**  printf("[ 2 ] \n");
201                 scicos_free((*pScopeMemory)->number_of_curves_by_subwin);
202
203                 //** if ( (*pScopeMemory)->period_counter != NULL )
204                 //**  printf("[ 3 ] \n");
205                 scicos_free((*pScopeMemory)->period_counter);
206
207                 //** if ( (*pScopeMemory)->period != NULL)
208                 //**  printf("[ 4 ] \n");
209                 scicos_free((*pScopeMemory)->period);
210
211                 //** if ( (*pScopeMemory)->longdraw_size != NULL )
212                 //**  printf("[ 5 ] \n");
213                 scicos_free((*pScopeMemory)->longdraw_size);
214
215                 //** if ( (*pScopeMemory)->shortdraw_size != NULL )
216                 //**  printf("[ 6 ] \n");
217                 scicos_free((*pScopeMemory)->shortdraw_size);
218
219                 if ( (*pScopeMemory)->number_of_subwin != NULL )
220                 {
221                         for (i = 0; i < (*pScopeMemory)->number_of_subwin ; i++) 
222                         {
223                                 //** if ( (*pScopeMemory)->hShortDraw[i] != NULL )
224                                 scicos_free((*pScopeMemory)->hShortDraw[i]);
225                                 //** if ( (*pScopeMemory)->hLongDraw[i] != NULL)
226                                 scicos_free((*pScopeMemory)->hLongDraw[i]);
227                         }
228                 }
229
230                 //** if ( (*pScopeMemory)->hShortDraw != NULL)
231                 //**  printf("[ 7 ] \n");
232                 scicos_free((*pScopeMemory)->hShortDraw);
233
234                 //** if ( (*pScopeMemory)->hLongDraw != NULL)
235                 //**  printf("[ 8 ] \n");
236                 scicos_free((*pScopeMemory)->hLongDraw);
237
238                 //** if ( (*pScopeMemory)->hAxes != NULL)
239                 //**  printf("[ 9 ] \n");
240                 scicos_free((*pScopeMemory)->hAxes);
241
242                 //** if ( *block_work != NULL )
243                 //**  printf("[ 10 ] \n");
244                 scicos_free(*block_work);
245
246                 //**  printf("[ END ] \n");
247         }
248
249 }
250 /*--------------------------------------------------------------------------*/ 
251 void scoRetrieveScopeMemory(void ** block_work, ScopeMemory ** pScopeMemory)
252 {
253         *pScopeMemory = (ScopeMemory*)*block_work;
254 }
255
256 /** 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*/
257 void scoReallocLongDraw(scoGraphicalObject pLongDraw, int NbrPtsLong, int shortdraw_size, int plus)
258 {
259         switch(sciGetEntityType(pLongDraw))
260         {
261         case SCI_POLYLINE:
262                 pPOLYLINE_FEATURE(pLongDraw)->pvx = REALLOC((pPOLYLINE_FEATURE(pLongDraw)->pvx),(NbrPtsLong+shortdraw_size+plus)*sizeof(double));
263                 pPOLYLINE_FEATURE(pLongDraw)->pvy = REALLOC((pPOLYLINE_FEATURE(pLongDraw)->pvy),(NbrPtsLong+shortdraw_size+plus)*sizeof(double));
264                 //If we are in 2D mode in the window at init then pvz is not initialized
265                 if(pPOLYLINE_FEATURE(pLongDraw)->pvz != NULL)
266                 {
267                         pPOLYLINE_FEATURE(pLongDraw)->pvz = REALLOC((pPOLYLINE_FEATURE(pLongDraw)->pvz),(NbrPtsLong+shortdraw_size+plus)*sizeof(double));
268                 }
269                 break;
270         case SCI_SEGS:
271                 pSEGS_FEATURE(pLongDraw)->vx = REALLOC((pSEGS_FEATURE(pLongDraw)->vx),(NbrPtsLong+shortdraw_size+plus)*sizeof(double));
272                 pSEGS_FEATURE(pLongDraw)->vy = REALLOC((pSEGS_FEATURE(pLongDraw)->vy),(NbrPtsLong+shortdraw_size+plus)*sizeof(double));
273                 //If we are in 2D mode in the window at init then vz is not initialized
274                 if(pSEGS_FEATURE(pLongDraw)->vz != NULL)
275                 {
276                         pSEGS_FEATURE(pLongDraw)->vz = REALLOC((pSEGS_FEATURE(pLongDraw)->vz),(NbrPtsLong+shortdraw_size+plus)*sizeof(double));
277                 }
278                 break;
279         default:
280                 sciprint(_("SCOPE ERROR : Error in scoReallocLongDraw()"));
281                 break;
282         }
283 }
284 /*--------------------------------------------------------------------------*/