aboutsummaryrefslogtreecommitdiff
path: root/Src/ns-eel2/nseel-ram.c
blob: 2062fed4b364c3b56582420f742ab43f395233a8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
  Expression Evaluator Library (NS-EEL) v2
  Copyright (C) 2004-2013 Cockos Incorporated
  Copyright (C) 1999-2003 Nullsoft, Inc.
  
  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.
*/

#include "ns-eel.h"
#include "ns-eel-int.h"
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


#ifdef _WIN32
#include <malloc.h>
#ifdef _MSC_VER
#define inline __inline
#endif

#endif

unsigned int NSEEL_RAM_limitmem=0;
unsigned int NSEEL_RAM_memused=0;
int NSEEL_RAM_memused_errors=0;



int NSEEL_VM_wantfreeRAM(NSEEL_VMCTX ctx)
{
	if (ctx)
  {
    compileContext *c=(compileContext*)ctx;
    if (c->ram_state.needfree) 
      return 1;
  }
  return 0;
}

void NSEEL_VM_freeRAMIfCodeRequested(NSEEL_VMCTX ctx) // check to see if our free flag was set
{
	if (ctx)
	{
  	compileContext *c=(compileContext*)ctx;
  	if (c->ram_state.needfree) 
		{
      NSEEL_HOSTSTUB_EnterMutex();
      {
			  INT_PTR startpos=((INT_PTR)c->ram_state.needfree)-1;
	 		  EEL_F **blocks = c->ram_state.blocks;
			  INT_PTR pos=0;
			  int x;
  		  for (x = 0; x < NSEEL_RAM_BLOCKS; x ++)
  		  {
				  if (pos >= startpos)
				  {
					  if (blocks[x])
					  {
						  if (NSEEL_RAM_memused >= sizeof(EEL_F) * NSEEL_RAM_ITEMSPERBLOCK) 
							  NSEEL_RAM_memused -= sizeof(EEL_F) * NSEEL_RAM_ITEMSPERBLOCK;
						  else NSEEL_RAM_memused_errors++;
       	 	    free(blocks[x]);
       	 	    blocks[x]=0;
					  }
				  }
				  pos+=NSEEL_RAM_ITEMSPERBLOCK;
 			  }
			  c->ram_state.needfree=0;
      }
      NSEEL_HOSTSTUB_LeaveMutex();
		}

	}
}

EEL_F nseel_ramalloc_onfail;
EEL_F * volatile  nseel_gmembuf_default;


void *(*nseel_gmem_calloc)(size_t a, size_t b);

EEL_F * NSEEL_CGEN_CALL __NSEEL_RAMAllocGMEM(EEL_F ***blocks, unsigned int w)
{
  if (blocks) 
  {
    EEL_F **pblocks=*blocks;

    if (w < NSEEL_RAM_BLOCKS*NSEEL_RAM_ITEMSPERBLOCK)
    {
      const unsigned int whichblock = w/NSEEL_RAM_ITEMSPERBLOCK;
      EEL_F *p=NULL;
      if (!pblocks || !(p=pblocks[whichblock]))
      {
        NSEEL_HOSTSTUB_EnterMutex();
        if (!nseel_gmem_calloc) nseel_gmem_calloc=calloc;

        if (!(pblocks=*blocks)) pblocks = *blocks = (EEL_F **)nseel_gmem_calloc(sizeof(EEL_F *),NSEEL_RAM_BLOCKS);
        else p = pblocks[whichblock];

        if (!p && pblocks)
        {
          p=pblocks[whichblock]=(EEL_F *)nseel_gmem_calloc(sizeof(EEL_F),NSEEL_RAM_ITEMSPERBLOCK);
        }
        NSEEL_HOSTSTUB_LeaveMutex();
      }
      if (p) return p + (w&(NSEEL_RAM_ITEMSPERBLOCK-1));
    }
    return &nseel_ramalloc_onfail;
  }

  if (!nseel_gmembuf_default)
  {
    NSEEL_HOSTSTUB_EnterMutex(); 
    if (!nseel_gmembuf_default) nseel_gmembuf_default=(EEL_F*)calloc(sizeof(EEL_F),NSEEL_SHARED_GRAM_SIZE);
    NSEEL_HOSTSTUB_LeaveMutex();
    if (!nseel_gmembuf_default) return &nseel_ramalloc_onfail;
  }

  return nseel_gmembuf_default+(((unsigned int)w)&((NSEEL_SHARED_GRAM_SIZE)-1));
}


EEL_F * NSEEL_CGEN_CALL  __NSEEL_RAMAlloc(EEL_F **pblocks, unsigned int w)
{
//  fprintf(stderr,"got request at %d, %d\n",w/NSEEL_RAM_ITEMSPERBLOCK, w&(NSEEL_RAM_ITEMSPERBLOCK-1));
  if (w < NSEEL_RAM_BLOCKS*NSEEL_RAM_ITEMSPERBLOCK)
  {
    unsigned int whichblock = w/NSEEL_RAM_ITEMSPERBLOCK;
    EEL_F *p=pblocks[whichblock];
    if (!p && whichblock < ((unsigned int *)pblocks)[-3]) // pblocks -1/-2 are closefact, -3 is maxblocks
    {
      NSEEL_HOSTSTUB_EnterMutex();

      if (!(p=pblocks[whichblock]))
      {

      	const int msize=sizeof(EEL_F) * NSEEL_RAM_ITEMSPERBLOCK;
      	if (!NSEEL_RAM_limitmem || NSEEL_RAM_memused+msize < NSEEL_RAM_limitmem) 
      	{
	      	p=pblocks[whichblock]=(EEL_F *)calloc(sizeof(EEL_F),NSEEL_RAM_ITEMSPERBLOCK);
      		if (p) NSEEL_RAM_memused+=msize;
      	}
      }
      NSEEL_HOSTSTUB_LeaveMutex();
    }	  
    if (p) return p + (w&(NSEEL_RAM_ITEMSPERBLOCK-1));
  }
//  fprintf(stderr,"ret 0\n");
  return &nseel_ramalloc_onfail;
}


EEL_F * NSEEL_CGEN_CALL __NSEEL_RAM_MemFree(void *blocks, EEL_F *which)
{
  // blocks points to ram_state.blocks, so back it up past closefact and maxblocks to needfree
  int *flag = (int *)((char *)blocks - sizeof(double) - 2*sizeof(int));
	int d=(int)(*which);
	if (d < 0) d=0;
	if (d < flag[1]*NSEEL_RAM_ITEMSPERBLOCK) flag[0]=1+d;
	return which;
}

EEL_F * NSEEL_CGEN_CALL __NSEEL_RAM_MemTop(void *blocks, EEL_F *which)
{
  // blocks points to ram_state.blocks, so back it up past closefact to maxblocks
  const int *flag = (int *)((char *)blocks - sizeof(double) - sizeof(int));
  *which = flag[0]*NSEEL_RAM_ITEMSPERBLOCK;
  return which;
}






EEL_F * NSEEL_CGEN_CALL __NSEEL_RAM_MemCpy(EEL_F **blocks,EEL_F *dest, EEL_F *src, EEL_F *lenptr)
{
  const int mem_size=NSEEL_RAM_BLOCKS*NSEEL_RAM_ITEMSPERBLOCK;
  int dest_offs = (int)(*dest + 0.0001);
  int src_offs = (int)(*src + 0.0001);
  int len = (int)(*lenptr + 0.0001);
  int want_mmove=0;

  // trim to front
  if (src_offs<0)
  {
    len += src_offs;
    dest_offs -= src_offs;
    src_offs=0;
  }
  if (dest_offs<0)
  {
    len += dest_offs;
    src_offs -= dest_offs;
    dest_offs=0;
  }
  if (src_offs + len > mem_size) len = mem_size-src_offs;
  if (dest_offs + len > mem_size) len = mem_size-dest_offs;

  if (src_offs == dest_offs || len < 1) return dest;

  if (src_offs < dest_offs && src_offs+len > dest_offs)
  {
    // if src_offs < dest_offs and overlapping, must copy right to left
    if ((dest_offs - src_offs) < NSEEL_RAM_ITEMSPERBLOCK) want_mmove = 1;
    src_offs += len;
    dest_offs += len;
    while (len > 0)
    {
      const int maxdlen=((dest_offs-1)&(NSEEL_RAM_ITEMSPERBLOCK-1)) + 1;
      const int maxslen=((src_offs-1)&(NSEEL_RAM_ITEMSPERBLOCK-1)) + 1;
      int copy_len = len;
      EEL_F *srcptr,*destptr;

      if (copy_len > maxdlen) copy_len=maxdlen;
      if (copy_len > maxslen) copy_len=maxslen;

      srcptr = __NSEEL_RAMAlloc(blocks,src_offs - copy_len);
      destptr = __NSEEL_RAMAlloc(blocks,dest_offs - copy_len);
      if (srcptr==&nseel_ramalloc_onfail || destptr==&nseel_ramalloc_onfail) break;

      if (want_mmove) memmove(destptr,srcptr,sizeof(EEL_F)*copy_len);
      else memcpy(destptr,srcptr,sizeof(EEL_F)*copy_len);
      src_offs-=copy_len;
      dest_offs-=copy_len;
      len-=copy_len;
    }
    return dest;
  }

  if (dest_offs < src_offs && dest_offs+len > src_offs)
  {
    // if dest_offs < src_offs and overlapping, and less than NSEEL_RAM_ITEMSPERBLOCK apart, use memmove()
    if ((src_offs-dest_offs) < NSEEL_RAM_ITEMSPERBLOCK) want_mmove = 1;
  }

  while (len > 0)
  {
    const int maxdlen=NSEEL_RAM_ITEMSPERBLOCK - (dest_offs&(NSEEL_RAM_ITEMSPERBLOCK-1));
    const int maxslen=NSEEL_RAM_ITEMSPERBLOCK -  (src_offs&(NSEEL_RAM_ITEMSPERBLOCK-1));
    int copy_len = len;
    EEL_F *srcptr,*destptr;

    if (copy_len > maxdlen) copy_len=maxdlen;
    if (copy_len > maxslen) copy_len=maxslen;

    srcptr = __NSEEL_RAMAlloc(blocks,src_offs);
    destptr = __NSEEL_RAMAlloc(blocks,dest_offs);
    if (srcptr==&nseel_ramalloc_onfail || destptr==&nseel_ramalloc_onfail) break;

    if (want_mmove) memmove(destptr,srcptr,sizeof(EEL_F)*copy_len);
    else memcpy(destptr,srcptr,sizeof(EEL_F)*copy_len);
    src_offs+=copy_len;
    dest_offs+=copy_len;
    len-=copy_len;
  }
  return dest;
}

EEL_F * NSEEL_CGEN_CALL __NSEEL_RAM_MemSet(EEL_F **blocks,EEL_F *dest, EEL_F *v, EEL_F *lenptr)
{  
  int offs = (int)(*dest + 0.0001);
  int len = (int)(*lenptr + 0.0001);
  EEL_F t;
  if (offs<0) 
  {
    len += offs;
    offs=0;
  }
  if (offs >= NSEEL_RAM_BLOCKS*NSEEL_RAM_ITEMSPERBLOCK) return dest;

  if (offs+len > NSEEL_RAM_BLOCKS*NSEEL_RAM_ITEMSPERBLOCK) len = NSEEL_RAM_BLOCKS*NSEEL_RAM_ITEMSPERBLOCK - offs;

  if (len < 1) return dest;
  

  t=*v; // set value

//  int lastBlock=-1;
  while (len > 0)
  {
    int lcnt;
    EEL_F *ptr=__NSEEL_RAMAlloc(blocks,offs);
    if (ptr==&nseel_ramalloc_onfail) break;

    lcnt=NSEEL_RAM_ITEMSPERBLOCK-(offs&(NSEEL_RAM_ITEMSPERBLOCK-1));
    if (lcnt > len) lcnt=len;

    len -= lcnt;
    offs += lcnt;

    while (lcnt--)
    {
      *ptr++=t;
    }       
  }
  return dest;
}


static inline int __getset_values(EEL_F **blocks, int isset, int len, EEL_F **parms)
{
  int offs, lout=0;
  unsigned int pageidx, sub_offs;
  if (--len < 1) return 0;
  offs = (int)(parms++[0][0] + 0.0001);

  if (offs<=0) 
  {
    len += offs;
    parms -= offs;
    offs=0;
    pageidx=sub_offs=0;
    if (len<1) return 0;
  }
  else
  {
    sub_offs = ((unsigned int)offs) & (NSEEL_RAM_ITEMSPERBLOCK-1);
    pageidx = ((unsigned int)offs)>>NSEEL_RAM_ITEMSPERBLOCK_LOG2;
    if (pageidx>=NSEEL_RAM_BLOCKS) return 0;
  }

  for (;;)
  {
    int lcnt=NSEEL_RAM_ITEMSPERBLOCK-sub_offs;
    EEL_F *ptr=blocks[pageidx];
    if (!ptr)
    {
      ptr = __NSEEL_RAMAlloc(blocks,offs + lout);
      if (ptr==&nseel_ramalloc_onfail) return lout;
    }
    else
    {
      ptr += sub_offs;
    }

    if (lcnt >= len) 
    { 
      // this page satisfies the request (normal behavior)
      lout += len;
      if (isset) while (len--) *ptr++=parms++[0][0];
      else while (len--) parms++[0][0] = *ptr++;
      return lout;
    }

    // crossing a page boundary
    len -= lcnt;
    lout += lcnt;
    if (isset) while (lcnt--) *ptr++=parms++[0][0];
    else while (lcnt--) parms++[0][0] = *ptr++;

    if (len <= 0 || ++pageidx >= NSEEL_RAM_BLOCKS) return lout;
    sub_offs=0;
  }
}

EEL_F NSEEL_CGEN_CALL __NSEEL_RAM_Mem_SetValues(EEL_F **blocks, INT_PTR np, EEL_F **parms)
{
  return __getset_values(blocks,1,(int)np,parms);
}

EEL_F NSEEL_CGEN_CALL __NSEEL_RAM_Mem_GetValues(EEL_F **blocks, INT_PTR np, EEL_F **parms)
{
  return __getset_values(blocks,0,(int)np,parms);
}

void NSEEL_VM_SetGRAM(NSEEL_VMCTX ctx, void **gram)
{
  if (ctx)
  {
    compileContext *c=(compileContext*)ctx;
    c->gram_blocks = gram;
  }
}


void NSEEL_VM_freeRAM(NSEEL_VMCTX ctx)
{
  if (ctx)
  {
    int x;
    compileContext *c=(compileContext*)ctx;
    EEL_F **blocks = c->ram_state.blocks;
    for (x = 0; x < NSEEL_RAM_BLOCKS; x ++)
    {
	    if (blocks[x])
	    {
		    if (NSEEL_RAM_memused >= sizeof(EEL_F) * NSEEL_RAM_ITEMSPERBLOCK) 
			    NSEEL_RAM_memused -= sizeof(EEL_F) * NSEEL_RAM_ITEMSPERBLOCK;
		    else NSEEL_RAM_memused_errors++;
        free(blocks[x]);
        blocks[x]=0;
	    }
    }
    c->ram_state.needfree=0; // no need to free anymore
  }
}

void NSEEL_VM_FreeGRAM(void **ufd)
{
  if (ufd[0])
  {
    EEL_F **blocks = (EEL_F **)ufd[0];
    int x;
    for (x = 0; x < NSEEL_RAM_BLOCKS; x ++)
    {
	    if (blocks[x])
	    {
		    if (NSEEL_RAM_memused >= sizeof(EEL_F) * NSEEL_RAM_ITEMSPERBLOCK) 
			    NSEEL_RAM_memused -= sizeof(EEL_F) * NSEEL_RAM_ITEMSPERBLOCK;
		    else NSEEL_RAM_memused_errors++;
	    }
      free(blocks[x]);
      blocks[x]=0;
    }
    free(blocks);
    ufd[0]=0;
  }
}

EEL_F *NSEEL_VM_getramptr(NSEEL_VMCTX ctx, unsigned int offs, int *validCount)
{
  EEL_F *d=__NSEEL_RAMAlloc(ctx ? ((compileContext*)ctx)->ram_state.blocks : 0,offs);
  if (!d || d == &nseel_ramalloc_onfail) return NULL;
  if (validCount) *validCount = NSEEL_RAM_ITEMSPERBLOCK - (offs%NSEEL_RAM_ITEMSPERBLOCK);

  return d;
}

EEL_F *NSEEL_VM_getramptr_noalloc(NSEEL_VMCTX ctx, unsigned int offs, int *validCount)
{
  EEL_F *d;
  compileContext *cc = (compileContext *)ctx;

  if (!cc ||
      offs >= NSEEL_RAM_ITEMSPERBLOCK*NSEEL_RAM_BLOCKS ||
      NULL == (d = cc->ram_state.blocks[offs/NSEEL_RAM_ITEMSPERBLOCK])
      ) 
  {
    if (validCount) *validCount = 0;
    return NULL;
  }

  offs %= NSEEL_RAM_ITEMSPERBLOCK;
  if (validCount) *validCount = NSEEL_RAM_ITEMSPERBLOCK - offs;
  return d + offs;
}