1
0

nseel-ram.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. Expression Evaluator Library (NS-EEL) v2
  3. Copyright (C) 2004-2008 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. #endif
  28. unsigned int NSEEL_RAM_limitmem=0;
  29. unsigned int NSEEL_RAM_memused=0;
  30. int NSEEL_RAM_memused_errors=0;
  31. int NSEEL_VM_wantfreeRAM(NSEEL_VMCTX ctx)
  32. {
  33. if (ctx)
  34. {
  35. compileContext *c=(compileContext*)ctx;
  36. if (c->ram_needfree)
  37. return 1;
  38. }
  39. return 0;
  40. }
  41. void NSEEL_VM_freeRAMIfCodeRequested(NSEEL_VMCTX ctx) // check to see if our free flag was set
  42. {
  43. if (ctx)
  44. {
  45. compileContext *c=(compileContext*)ctx;
  46. if (c->ram_needfree)
  47. {
  48. NSEEL_HOSTSTUB_EnterMutex();
  49. if (c->ram_blocks)
  50. {
  51. INT_PTR startpos=((INT_PTR)c->ram_needfree)-1;
  52. EEL_F **blocks = (EEL_F **)c->ram_blocks;
  53. INT_PTR pos=0;
  54. int x;
  55. for (x = 0; x < NSEEL_RAM_BLOCKS; x ++)
  56. {
  57. if (pos >= startpos)
  58. {
  59. if (blocks[x])
  60. {
  61. if (NSEEL_RAM_memused >= sizeof(EEL_F) * NSEEL_RAM_ITEMSPERBLOCK)
  62. NSEEL_RAM_memused -= sizeof(EEL_F) * NSEEL_RAM_ITEMSPERBLOCK;
  63. else NSEEL_RAM_memused_errors++;
  64. }
  65. free(blocks[x]);
  66. blocks[x]=0;
  67. }
  68. pos+=NSEEL_RAM_ITEMSPERBLOCK;
  69. }
  70. if (!startpos)
  71. {
  72. free(blocks);
  73. c->ram_blocks=0;
  74. }
  75. }
  76. c->ram_needfree=0;
  77. NSEEL_HOSTSTUB_LeaveMutex();
  78. }
  79. }
  80. }
  81. EEL_F * NSEEL_CGEN_CALL __NSEEL_RAMAllocGMEM(EEL_F ***blocks, int w)
  82. {
  83. static EEL_F * volatile gmembuf;
  84. if (blocks) return __NSEEL_RAMAlloc(blocks,w);
  85. if (!gmembuf)
  86. {
  87. NSEEL_HOSTSTUB_EnterMutex();
  88. if (!gmembuf) gmembuf=(EEL_F*)calloc(sizeof(EEL_F),NSEEL_SHARED_GRAM_SIZE);
  89. NSEEL_HOSTSTUB_LeaveMutex();
  90. if (!gmembuf) return 0;
  91. }
  92. return gmembuf+(((unsigned int)w)&((NSEEL_SHARED_GRAM_SIZE)-1));
  93. }
  94. EEL_F * NSEEL_CGEN_CALL __NSEEL_RAMAlloc(EEL_F ***blocks, int w)
  95. {
  96. int whichblock;
  97. EEL_F **pblocks=*blocks;
  98. int is_locked=0;
  99. if (!pblocks)
  100. {
  101. if (!is_locked) { is_locked=1; NSEEL_HOSTSTUB_EnterMutex(); }
  102. if (!(pblocks=*blocks))
  103. {
  104. pblocks = *blocks = (EEL_F **)calloc(sizeof(EEL_F *),NSEEL_RAM_BLOCKS);
  105. if (!pblocks) {
  106. if (is_locked) NSEEL_HOSTSTUB_LeaveMutex();
  107. return 0;
  108. }
  109. }
  110. }
  111. // fprintf(stderr,"got request at %d, %d\n",w/NSEEL_RAM_ITEMSPERBLOCK, w&(NSEEL_RAM_ITEMSPERBLOCK-1));
  112. if (w >= 0 && (whichblock = w/NSEEL_RAM_ITEMSPERBLOCK) < NSEEL_RAM_BLOCKS)
  113. {
  114. EEL_F *p=pblocks[whichblock];
  115. if (!p)
  116. {
  117. if (!is_locked) { is_locked=1; NSEEL_HOSTSTUB_EnterMutex(); }
  118. if (!(p=pblocks[whichblock]))
  119. {
  120. const int msize=sizeof(EEL_F) * NSEEL_RAM_ITEMSPERBLOCK;
  121. if (!NSEEL_RAM_limitmem || NSEEL_RAM_memused+msize < NSEEL_RAM_limitmem)
  122. {
  123. p=pblocks[whichblock]=(EEL_F *)calloc(sizeof(EEL_F),NSEEL_RAM_ITEMSPERBLOCK);
  124. if (p) NSEEL_RAM_memused+=msize;
  125. }
  126. if (!p) w=0;
  127. }
  128. }
  129. if (is_locked) NSEEL_HOSTSTUB_LeaveMutex();
  130. return p + (w&(NSEEL_RAM_ITEMSPERBLOCK-1));
  131. }
  132. if (is_locked) NSEEL_HOSTSTUB_LeaveMutex();
  133. // fprintf(stderr,"ret 0\n");
  134. return 0;
  135. }
  136. EEL_F * NSEEL_CGEN_CALL __NSEEL_RAM_MemFree(EEL_F ***blocks, EEL_F *which)
  137. {
  138. int d=EEL_F2int(*which);
  139. if (d < 0) d=0;
  140. if (d < NSEEL_RAM_BLOCKS*NSEEL_RAM_ITEMSPERBLOCK) ((INT_PTR *)blocks)[1]=1+d;
  141. return which;
  142. }
  143. EEL_F * NSEEL_CGEN_CALL __NSEEL_RAM_MemCpy(EEL_F ***blocks,EEL_F *dest, EEL_F *src, EEL_F *lenptr)
  144. {
  145. int dest_offs = EEL_F2int(*dest + 0.0001);
  146. int src_offs = EEL_F2int(*src + 0.0001);
  147. int len = EEL_F2int(*lenptr + 0.0001);
  148. // trim to front
  149. if (src_offs<0)
  150. {
  151. len += src_offs;
  152. dest_offs -= src_offs;
  153. src_offs=0;
  154. }
  155. if (dest_offs<0)
  156. {
  157. len += dest_offs;
  158. src_offs -= dest_offs;
  159. dest_offs=0;
  160. }
  161. while (len > 0)
  162. {
  163. EEL_F *srcptr,*destptr;
  164. int copy_len = len;
  165. int maxdlen=NSEEL_RAM_ITEMSPERBLOCK - (dest_offs&(NSEEL_RAM_ITEMSPERBLOCK-1));
  166. int maxslen=NSEEL_RAM_ITEMSPERBLOCK - (src_offs&(NSEEL_RAM_ITEMSPERBLOCK-1));
  167. if (dest_offs >= NSEEL_RAM_BLOCKS*NSEEL_RAM_ITEMSPERBLOCK ||
  168. src_offs >= NSEEL_RAM_BLOCKS*NSEEL_RAM_ITEMSPERBLOCK) break;
  169. if (copy_len > maxdlen) copy_len=maxdlen;
  170. if (copy_len > maxslen) copy_len=maxslen;
  171. if (copy_len<1) break;
  172. srcptr = __NSEEL_RAMAlloc(blocks,src_offs);
  173. destptr = __NSEEL_RAMAlloc(blocks,dest_offs);
  174. if (!srcptr || !destptr) break;
  175. memmove(destptr,srcptr,sizeof(EEL_F)*copy_len);
  176. src_offs+=copy_len;
  177. dest_offs+=copy_len;
  178. len-=copy_len;
  179. }
  180. return dest;
  181. }
  182. EEL_F * NSEEL_CGEN_CALL __NSEEL_RAM_MemSet(EEL_F ***blocks,EEL_F *dest, EEL_F *v, EEL_F *lenptr)
  183. {
  184. int offs = EEL_F2int(*dest + 0.0001);
  185. int len = EEL_F2int(*lenptr + 0.0001);
  186. EEL_F t;
  187. if (offs<0)
  188. {
  189. len += offs;
  190. offs=0;
  191. }
  192. if (offs >= NSEEL_RAM_BLOCKS*NSEEL_RAM_ITEMSPERBLOCK) return dest;
  193. if (offs+len > NSEEL_RAM_BLOCKS*NSEEL_RAM_ITEMSPERBLOCK) len = NSEEL_RAM_BLOCKS*NSEEL_RAM_ITEMSPERBLOCK - offs;
  194. if (len < 1) return dest;
  195. t=*v; // set value
  196. // int lastBlock=-1;
  197. while (len > 0)
  198. {
  199. int lcnt;
  200. EEL_F *ptr=__NSEEL_RAMAlloc(blocks,offs);
  201. if (!ptr) break;
  202. lcnt=NSEEL_RAM_ITEMSPERBLOCK-(offs&(NSEEL_RAM_ITEMSPERBLOCK-1));
  203. if (lcnt > len) lcnt=len;
  204. len -= lcnt;
  205. offs += lcnt;
  206. while (lcnt--)
  207. {
  208. *ptr++=t;
  209. }
  210. }
  211. return dest;
  212. }
  213. void NSEEL_VM_SetGRAM(NSEEL_VMCTX ctx, void **gram)
  214. {
  215. if (ctx)
  216. {
  217. compileContext *c=(compileContext*)ctx;
  218. c->gram_blocks = gram;
  219. }
  220. }
  221. void NSEEL_VM_freeRAM(NSEEL_VMCTX ctx)
  222. {
  223. if (ctx)
  224. {
  225. int x;
  226. compileContext *c=(compileContext*)ctx;
  227. if (c->ram_blocks)
  228. {
  229. EEL_F **blocks = (EEL_F **)c->ram_blocks;
  230. for (x = 0; x < NSEEL_RAM_BLOCKS; x ++)
  231. {
  232. if (blocks[x])
  233. {
  234. if (NSEEL_RAM_memused >= sizeof(EEL_F) * NSEEL_RAM_ITEMSPERBLOCK)
  235. NSEEL_RAM_memused -= sizeof(EEL_F) * NSEEL_RAM_ITEMSPERBLOCK;
  236. else NSEEL_RAM_memused_errors++;
  237. }
  238. free(blocks[x]);
  239. blocks[x]=0;
  240. }
  241. free(blocks);
  242. c->ram_blocks=0;
  243. }
  244. c->ram_needfree=0; // no need to free anymore
  245. }
  246. }
  247. void NSEEL_VM_FreeGRAM(void **ufd)
  248. {
  249. if (ufd[0])
  250. {
  251. EEL_F **blocks = (EEL_F **)ufd[0];
  252. int x;
  253. for (x = 0; x < NSEEL_RAM_BLOCKS; x ++)
  254. {
  255. if (blocks[x])
  256. {
  257. if (NSEEL_RAM_memused >= sizeof(EEL_F) * NSEEL_RAM_ITEMSPERBLOCK)
  258. NSEEL_RAM_memused -= sizeof(EEL_F) * NSEEL_RAM_ITEMSPERBLOCK;
  259. else NSEEL_RAM_memused_errors++;
  260. }
  261. free(blocks[x]);
  262. blocks[x]=0;
  263. }
  264. free(blocks);
  265. ufd[0]=0;
  266. }
  267. }