1
0

nseel-ram.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. Expression Evaluator Library (NS-EEL) v2
  3. Copyright (C) 2004-2013 Cockos Incorporated
  4. Copyright (C) 1999-2003 Nullsoft, Inc.
  5. This software is provided 'as-is', without any express or implied
  6. warranty. In no event will the authors be held liable for any damages
  7. arising from the use of this software.
  8. Permission is granted to anyone to use this software for any purpose,
  9. including commercial applications, and to alter it and redistribute it
  10. freely, subject to the following restrictions:
  11. 1. The origin of this software must not be misrepresented; you must not
  12. claim that you wrote the original software. If you use this software
  13. in a product, an acknowledgment in the product documentation would be
  14. appreciated but is not required.
  15. 2. Altered source versions must be plainly marked as such, and must not be
  16. misrepresented as being the original software.
  17. 3. This notice may not be removed or altered from any source distribution.
  18. */
  19. #include "ns-eel.h"
  20. #include "ns-eel-int.h"
  21. #include <math.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #ifdef _WIN32
  26. #include <malloc.h>
  27. #ifdef _MSC_VER
  28. #define inline __inline
  29. #endif
  30. #endif
  31. unsigned int NSEEL_RAM_limitmem=0;
  32. unsigned int NSEEL_RAM_memused=0;
  33. int NSEEL_RAM_memused_errors=0;
  34. int NSEEL_VM_wantfreeRAM(NSEEL_VMCTX ctx)
  35. {
  36. if (ctx)
  37. {
  38. compileContext *c=(compileContext*)ctx;
  39. if (c->ram_state.needfree)
  40. return 1;
  41. }
  42. return 0;
  43. }
  44. void NSEEL_VM_freeRAMIfCodeRequested(NSEEL_VMCTX ctx) // check to see if our free flag was set
  45. {
  46. if (ctx)
  47. {
  48. compileContext *c=(compileContext*)ctx;
  49. if (c->ram_state.needfree)
  50. {
  51. NSEEL_HOSTSTUB_EnterMutex();
  52. {
  53. INT_PTR startpos=((INT_PTR)c->ram_state.needfree)-1;
  54. EEL_F **blocks = c->ram_state.blocks;
  55. INT_PTR pos=0;
  56. int x;
  57. for (x = 0; x < NSEEL_RAM_BLOCKS; x ++)
  58. {
  59. if (pos >= startpos)
  60. {
  61. if (blocks[x])
  62. {
  63. if (NSEEL_RAM_memused >= sizeof(EEL_F) * NSEEL_RAM_ITEMSPERBLOCK)
  64. NSEEL_RAM_memused -= sizeof(EEL_F) * NSEEL_RAM_ITEMSPERBLOCK;
  65. else NSEEL_RAM_memused_errors++;
  66. free(blocks[x]);
  67. blocks[x]=0;
  68. }
  69. }
  70. pos+=NSEEL_RAM_ITEMSPERBLOCK;
  71. }
  72. c->ram_state.needfree=0;
  73. }
  74. NSEEL_HOSTSTUB_LeaveMutex();
  75. }
  76. }
  77. }
  78. EEL_F nseel_ramalloc_onfail;
  79. EEL_F * volatile nseel_gmembuf_default;
  80. void *(*nseel_gmem_calloc)(size_t a, size_t b);
  81. EEL_F * NSEEL_CGEN_CALL __NSEEL_RAMAllocGMEM(EEL_F ***blocks, unsigned int w)
  82. {
  83. if (blocks)
  84. {
  85. EEL_F **pblocks=*blocks;
  86. if (w < NSEEL_RAM_BLOCKS*NSEEL_RAM_ITEMSPERBLOCK)
  87. {
  88. const unsigned int whichblock = w/NSEEL_RAM_ITEMSPERBLOCK;
  89. EEL_F *p=NULL;
  90. if (!pblocks || !(p=pblocks[whichblock]))
  91. {
  92. NSEEL_HOSTSTUB_EnterMutex();
  93. if (!nseel_gmem_calloc) nseel_gmem_calloc=calloc;
  94. if (!(pblocks=*blocks)) pblocks = *blocks = (EEL_F **)nseel_gmem_calloc(sizeof(EEL_F *),NSEEL_RAM_BLOCKS);
  95. else p = pblocks[whichblock];
  96. if (!p && pblocks)
  97. {
  98. p=pblocks[whichblock]=(EEL_F *)nseel_gmem_calloc(sizeof(EEL_F),NSEEL_RAM_ITEMSPERBLOCK);
  99. }
  100. NSEEL_HOSTSTUB_LeaveMutex();
  101. }
  102. if (p) return p + (w&(NSEEL_RAM_ITEMSPERBLOCK-1));
  103. }
  104. return &nseel_ramalloc_onfail;
  105. }
  106. if (!nseel_gmembuf_default)
  107. {
  108. NSEEL_HOSTSTUB_EnterMutex();
  109. if (!nseel_gmembuf_default) nseel_gmembuf_default=(EEL_F*)calloc(sizeof(EEL_F),NSEEL_SHARED_GRAM_SIZE);
  110. NSEEL_HOSTSTUB_LeaveMutex();
  111. if (!nseel_gmembuf_default) return &nseel_ramalloc_onfail;
  112. }
  113. return nseel_gmembuf_default+(((unsigned int)w)&((NSEEL_SHARED_GRAM_SIZE)-1));
  114. }
  115. EEL_F * NSEEL_CGEN_CALL __NSEEL_RAMAlloc(EEL_F **pblocks, unsigned int w)
  116. {
  117. // fprintf(stderr,"got request at %d, %d\n",w/NSEEL_RAM_ITEMSPERBLOCK, w&(NSEEL_RAM_ITEMSPERBLOCK-1));
  118. if (w < NSEEL_RAM_BLOCKS*NSEEL_RAM_ITEMSPERBLOCK)
  119. {
  120. unsigned int whichblock = w/NSEEL_RAM_ITEMSPERBLOCK;
  121. EEL_F *p=pblocks[whichblock];
  122. if (!p && whichblock < ((unsigned int *)pblocks)[-3]) // pblocks -1/-2 are closefact, -3 is maxblocks
  123. {
  124. NSEEL_HOSTSTUB_EnterMutex();
  125. if (!(p=pblocks[whichblock]))
  126. {
  127. const int msize=sizeof(EEL_F) * NSEEL_RAM_ITEMSPERBLOCK;
  128. if (!NSEEL_RAM_limitmem || NSEEL_RAM_memused+msize < NSEEL_RAM_limitmem)
  129. {
  130. p=pblocks[whichblock]=(EEL_F *)calloc(sizeof(EEL_F),NSEEL_RAM_ITEMSPERBLOCK);
  131. if (p) NSEEL_RAM_memused+=msize;
  132. }
  133. }
  134. NSEEL_HOSTSTUB_LeaveMutex();
  135. }
  136. if (p) return p + (w&(NSEEL_RAM_ITEMSPERBLOCK-1));
  137. }
  138. // fprintf(stderr,"ret 0\n");
  139. return &nseel_ramalloc_onfail;
  140. }
  141. EEL_F * NSEEL_CGEN_CALL __NSEEL_RAM_MemFree(void *blocks, EEL_F *which)
  142. {
  143. // blocks points to ram_state.blocks, so back it up past closefact and maxblocks to needfree
  144. int *flag = (int *)((char *)blocks - sizeof(double) - 2*sizeof(int));
  145. int d=(int)(*which);
  146. if (d < 0) d=0;
  147. if (d < flag[1]*NSEEL_RAM_ITEMSPERBLOCK) flag[0]=1+d;
  148. return which;
  149. }
  150. EEL_F * NSEEL_CGEN_CALL __NSEEL_RAM_MemTop(void *blocks, EEL_F *which)
  151. {
  152. // blocks points to ram_state.blocks, so back it up past closefact to maxblocks
  153. const int *flag = (int *)((char *)blocks - sizeof(double) - sizeof(int));
  154. *which = flag[0]*NSEEL_RAM_ITEMSPERBLOCK;
  155. return which;
  156. }
  157. EEL_F * NSEEL_CGEN_CALL __NSEEL_RAM_MemCpy(EEL_F **blocks,EEL_F *dest, EEL_F *src, EEL_F *lenptr)
  158. {
  159. const int mem_size=NSEEL_RAM_BLOCKS*NSEEL_RAM_ITEMSPERBLOCK;
  160. int dest_offs = (int)(*dest + 0.0001);
  161. int src_offs = (int)(*src + 0.0001);
  162. int len = (int)(*lenptr + 0.0001);
  163. int want_mmove=0;
  164. // trim to front
  165. if (src_offs<0)
  166. {
  167. len += src_offs;
  168. dest_offs -= src_offs;
  169. src_offs=0;
  170. }
  171. if (dest_offs<0)
  172. {
  173. len += dest_offs;
  174. src_offs -= dest_offs;
  175. dest_offs=0;
  176. }
  177. if (src_offs + len > mem_size) len = mem_size-src_offs;
  178. if (dest_offs + len > mem_size) len = mem_size-dest_offs;
  179. if (src_offs == dest_offs || len < 1) return dest;
  180. if (src_offs < dest_offs && src_offs+len > dest_offs)
  181. {
  182. // if src_offs < dest_offs and overlapping, must copy right to left
  183. if ((dest_offs - src_offs) < NSEEL_RAM_ITEMSPERBLOCK) want_mmove = 1;
  184. src_offs += len;
  185. dest_offs += len;
  186. while (len > 0)
  187. {
  188. const int maxdlen=((dest_offs-1)&(NSEEL_RAM_ITEMSPERBLOCK-1)) + 1;
  189. const int maxslen=((src_offs-1)&(NSEEL_RAM_ITEMSPERBLOCK-1)) + 1;
  190. int copy_len = len;
  191. EEL_F *srcptr,*destptr;
  192. if (copy_len > maxdlen) copy_len=maxdlen;
  193. if (copy_len > maxslen) copy_len=maxslen;
  194. srcptr = __NSEEL_RAMAlloc(blocks,src_offs - copy_len);
  195. destptr = __NSEEL_RAMAlloc(blocks,dest_offs - copy_len);
  196. if (srcptr==&nseel_ramalloc_onfail || destptr==&nseel_ramalloc_onfail) break;
  197. if (want_mmove) memmove(destptr,srcptr,sizeof(EEL_F)*copy_len);
  198. else memcpy(destptr,srcptr,sizeof(EEL_F)*copy_len);
  199. src_offs-=copy_len;
  200. dest_offs-=copy_len;
  201. len-=copy_len;
  202. }
  203. return dest;
  204. }
  205. if (dest_offs < src_offs && dest_offs+len > src_offs)
  206. {
  207. // if dest_offs < src_offs and overlapping, and less than NSEEL_RAM_ITEMSPERBLOCK apart, use memmove()
  208. if ((src_offs-dest_offs) < NSEEL_RAM_ITEMSPERBLOCK) want_mmove = 1;
  209. }
  210. while (len > 0)
  211. {
  212. const int maxdlen=NSEEL_RAM_ITEMSPERBLOCK - (dest_offs&(NSEEL_RAM_ITEMSPERBLOCK-1));
  213. const int maxslen=NSEEL_RAM_ITEMSPERBLOCK - (src_offs&(NSEEL_RAM_ITEMSPERBLOCK-1));
  214. int copy_len = len;
  215. EEL_F *srcptr,*destptr;
  216. if (copy_len > maxdlen) copy_len=maxdlen;
  217. if (copy_len > maxslen) copy_len=maxslen;
  218. srcptr = __NSEEL_RAMAlloc(blocks,src_offs);
  219. destptr = __NSEEL_RAMAlloc(blocks,dest_offs);
  220. if (srcptr==&nseel_ramalloc_onfail || destptr==&nseel_ramalloc_onfail) break;
  221. if (want_mmove) memmove(destptr,srcptr,sizeof(EEL_F)*copy_len);
  222. else memcpy(destptr,srcptr,sizeof(EEL_F)*copy_len);
  223. src_offs+=copy_len;
  224. dest_offs+=copy_len;
  225. len-=copy_len;
  226. }
  227. return dest;
  228. }
  229. EEL_F * NSEEL_CGEN_CALL __NSEEL_RAM_MemSet(EEL_F **blocks,EEL_F *dest, EEL_F *v, EEL_F *lenptr)
  230. {
  231. int offs = (int)(*dest + 0.0001);
  232. int len = (int)(*lenptr + 0.0001);
  233. EEL_F t;
  234. if (offs<0)
  235. {
  236. len += offs;
  237. offs=0;
  238. }
  239. if (offs >= NSEEL_RAM_BLOCKS*NSEEL_RAM_ITEMSPERBLOCK) return dest;
  240. if (offs+len > NSEEL_RAM_BLOCKS*NSEEL_RAM_ITEMSPERBLOCK) len = NSEEL_RAM_BLOCKS*NSEEL_RAM_ITEMSPERBLOCK - offs;
  241. if (len < 1) return dest;
  242. t=*v; // set value
  243. // int lastBlock=-1;
  244. while (len > 0)
  245. {
  246. int lcnt;
  247. EEL_F *ptr=__NSEEL_RAMAlloc(blocks,offs);
  248. if (ptr==&nseel_ramalloc_onfail) break;
  249. lcnt=NSEEL_RAM_ITEMSPERBLOCK-(offs&(NSEEL_RAM_ITEMSPERBLOCK-1));
  250. if (lcnt > len) lcnt=len;
  251. len -= lcnt;
  252. offs += lcnt;
  253. while (lcnt--)
  254. {
  255. *ptr++=t;
  256. }
  257. }
  258. return dest;
  259. }
  260. static inline int __getset_values(EEL_F **blocks, int isset, int len, EEL_F **parms)
  261. {
  262. int offs, lout=0;
  263. unsigned int pageidx, sub_offs;
  264. if (--len < 1) return 0;
  265. offs = (int)(parms++[0][0] + 0.0001);
  266. if (offs<=0)
  267. {
  268. len += offs;
  269. parms -= offs;
  270. offs=0;
  271. pageidx=sub_offs=0;
  272. if (len<1) return 0;
  273. }
  274. else
  275. {
  276. sub_offs = ((unsigned int)offs) & (NSEEL_RAM_ITEMSPERBLOCK-1);
  277. pageidx = ((unsigned int)offs)>>NSEEL_RAM_ITEMSPERBLOCK_LOG2;
  278. if (pageidx>=NSEEL_RAM_BLOCKS) return 0;
  279. }
  280. for (;;)
  281. {
  282. int lcnt=NSEEL_RAM_ITEMSPERBLOCK-sub_offs;
  283. EEL_F *ptr=blocks[pageidx];
  284. if (!ptr)
  285. {
  286. ptr = __NSEEL_RAMAlloc(blocks,offs + lout);
  287. if (ptr==&nseel_ramalloc_onfail) return lout;
  288. }
  289. else
  290. {
  291. ptr += sub_offs;
  292. }
  293. if (lcnt >= len)
  294. {
  295. // this page satisfies the request (normal behavior)
  296. lout += len;
  297. if (isset) while (len--) *ptr++=parms++[0][0];
  298. else while (len--) parms++[0][0] = *ptr++;
  299. return lout;
  300. }
  301. // crossing a page boundary
  302. len -= lcnt;
  303. lout += lcnt;
  304. if (isset) while (lcnt--) *ptr++=parms++[0][0];
  305. else while (lcnt--) parms++[0][0] = *ptr++;
  306. if (len <= 0 || ++pageidx >= NSEEL_RAM_BLOCKS) return lout;
  307. sub_offs=0;
  308. }
  309. }
  310. EEL_F NSEEL_CGEN_CALL __NSEEL_RAM_Mem_SetValues(EEL_F **blocks, INT_PTR np, EEL_F **parms)
  311. {
  312. return __getset_values(blocks,1,(int)np,parms);
  313. }
  314. EEL_F NSEEL_CGEN_CALL __NSEEL_RAM_Mem_GetValues(EEL_F **blocks, INT_PTR np, EEL_F **parms)
  315. {
  316. return __getset_values(blocks,0,(int)np,parms);
  317. }
  318. void NSEEL_VM_SetGRAM(NSEEL_VMCTX ctx, void **gram)
  319. {
  320. if (ctx)
  321. {
  322. compileContext *c=(compileContext*)ctx;
  323. c->gram_blocks = gram;
  324. }
  325. }
  326. void NSEEL_VM_freeRAM(NSEEL_VMCTX ctx)
  327. {
  328. if (ctx)
  329. {
  330. int x;
  331. compileContext *c=(compileContext*)ctx;
  332. EEL_F **blocks = c->ram_state.blocks;
  333. for (x = 0; x < NSEEL_RAM_BLOCKS; x ++)
  334. {
  335. if (blocks[x])
  336. {
  337. if (NSEEL_RAM_memused >= sizeof(EEL_F) * NSEEL_RAM_ITEMSPERBLOCK)
  338. NSEEL_RAM_memused -= sizeof(EEL_F) * NSEEL_RAM_ITEMSPERBLOCK;
  339. else NSEEL_RAM_memused_errors++;
  340. free(blocks[x]);
  341. blocks[x]=0;
  342. }
  343. }
  344. c->ram_state.needfree=0; // no need to free anymore
  345. }
  346. }
  347. void NSEEL_VM_FreeGRAM(void **ufd)
  348. {
  349. if (ufd[0])
  350. {
  351. EEL_F **blocks = (EEL_F **)ufd[0];
  352. int x;
  353. for (x = 0; x < NSEEL_RAM_BLOCKS; x ++)
  354. {
  355. if (blocks[x])
  356. {
  357. if (NSEEL_RAM_memused >= sizeof(EEL_F) * NSEEL_RAM_ITEMSPERBLOCK)
  358. NSEEL_RAM_memused -= sizeof(EEL_F) * NSEEL_RAM_ITEMSPERBLOCK;
  359. else NSEEL_RAM_memused_errors++;
  360. }
  361. free(blocks[x]);
  362. blocks[x]=0;
  363. }
  364. free(blocks);
  365. ufd[0]=0;
  366. }
  367. }
  368. EEL_F *NSEEL_VM_getramptr(NSEEL_VMCTX ctx, unsigned int offs, int *validCount)
  369. {
  370. EEL_F *d=__NSEEL_RAMAlloc(ctx ? ((compileContext*)ctx)->ram_state.blocks : 0,offs);
  371. if (!d || d == &nseel_ramalloc_onfail) return NULL;
  372. if (validCount) *validCount = NSEEL_RAM_ITEMSPERBLOCK - (offs%NSEEL_RAM_ITEMSPERBLOCK);
  373. return d;
  374. }
  375. EEL_F *NSEEL_VM_getramptr_noalloc(NSEEL_VMCTX ctx, unsigned int offs, int *validCount)
  376. {
  377. EEL_F *d;
  378. compileContext *cc = (compileContext *)ctx;
  379. if (!cc ||
  380. offs >= NSEEL_RAM_ITEMSPERBLOCK*NSEEL_RAM_BLOCKS ||
  381. NULL == (d = cc->ram_state.blocks[offs/NSEEL_RAM_ITEMSPERBLOCK])
  382. )
  383. {
  384. if (validCount) *validCount = 0;
  385. return NULL;
  386. }
  387. offs %= NSEEL_RAM_ITEMSPERBLOCK;
  388. if (validCount) *validCount = NSEEL_RAM_ITEMSPERBLOCK - offs;
  389. return d + offs;
  390. }