1
0

megabuf.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include <windows.h>
  2. #include "../ns-eel/ns-eel.h"
  3. #include "../ns-eel/ns-eel-int.h"
  4. #include "megabuf.h"
  5. void megabuf_ppproc(void *data, int data_size, void **userfunc_data)
  6. {
  7. if (data_size > 5 && *(int*)((char *)data+1) == 0xFFFFFFFF)
  8. {
  9. *(int*)((char *)data+1) = (int) (userfunc_data+0);
  10. }
  11. }
  12. void megabuf_cleanup(NSEEL_VMCTX ctx)
  13. {
  14. if (ctx)
  15. {
  16. compileContext *c=(compileContext*)ctx;
  17. if (c->userfunc_data[0])
  18. {
  19. double **blocks = (double **)c->userfunc_data[0];
  20. int x;
  21. for (x = 0; x < MEGABUF_BLOCKS; x ++)
  22. {
  23. if (blocks[x]) VirtualFree(blocks[x], 0/*sizeof(double)*MEGABUF_ITEMSPERBLOCK*/, MEM_RELEASE);
  24. //GlobalFree(blocks[x]);
  25. blocks[x]=0;
  26. }
  27. VirtualFree(blocks, 0/*sizeof(double *)*MEGABUF_BLOCKS*/, MEM_RELEASE);
  28. //GlobalFree((HGLOBAL)blocks);
  29. c->userfunc_data[0]=0;
  30. }
  31. }
  32. }
  33. static double * NSEEL_CGEN_CALL megabuf_(double ***blocks, double *which)
  34. {
  35. static double error;
  36. int w=(int)(*which + 0.0001);
  37. int whichblock = w/MEGABUF_ITEMSPERBLOCK;
  38. if (!*blocks)
  39. {
  40. *blocks = (double **)VirtualAlloc(NULL, sizeof(double *)*MEGABUF_BLOCKS, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  41. //GlobalAlloc(GPTR,sizeof(double *)*MEGABUF_BLOCKS);
  42. }
  43. if (!*blocks) return &error;
  44. if (w >= 0 && whichblock >= 0 && whichblock < MEGABUF_BLOCKS)
  45. {
  46. int whichentry = w%MEGABUF_ITEMSPERBLOCK;
  47. if (!(*blocks)[whichblock])
  48. {
  49. (*blocks)[whichblock]=(double *)VirtualAlloc(NULL, sizeof(double)*MEGABUF_ITEMSPERBLOCK, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  50. //GlobalAlloc(GPTR,sizeof(double)*MEGABUF_ITEMSPERBLOCK);
  51. }
  52. if ((*blocks)[whichblock])
  53. return &(*blocks)[whichblock][whichentry];
  54. }
  55. return &error;
  56. }
  57. static double * (NSEEL_CGEN_CALL *__megabuf)(double ***,double *) = &megabuf_;
  58. __declspec ( naked ) void _asm_megabuf(void)
  59. {
  60. double ***my_ctx;
  61. double *parm_a, *__nextBlock;
  62. __asm { mov edx, 0xFFFFFFFF }
  63. __asm { mov ebp, esp }
  64. __asm { sub esp, __LOCAL_SIZE }
  65. __asm { mov dword ptr my_ctx, edx }
  66. __asm { mov dword ptr parm_a, eax }
  67. __nextBlock = __megabuf(my_ctx,parm_a);
  68. __asm { mov eax, __nextBlock } // this is custom, returning pointer
  69. __asm { mov esp, ebp }
  70. }
  71. __declspec ( naked ) void _asm_megabuf_end(void) {}