123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463 |
- /*
- 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;
- }
|