5 typedef struct prmemb_s {
9 void *PRHunkAlloc(progfuncs_t *progfuncs, int ammount)
12 ammount = sizeof(prmemb_t)+((ammount + 3)&~3);
13 mem = memalloc(ammount);
14 memset(mem, 0, ammount);
19 mem->level = ((prmemb_t *)memb)->level+1;
22 return ((char *)mem)+sizeof(prmemb_t);
25 int PRHunkMark(progfuncs_t *progfuncs)
27 return ((prmemb_t *)memb)->level;
29 void PRHunkFree(progfuncs_t *progfuncs, int mark)
34 if (memb->level <= mark)
44 //for 64bit systems. :)
45 //addressable memory is memory available to the vm itself for writing.
46 //once allocated, it cannot be freed for the lifetime of the VM.
47 void *PRAddressableAlloc(progfuncs_t *progfuncs, int ammount)
49 ammount = (ammount + 4)&~3; //round up to 4
50 if (addressableused + ammount > addressablesize)
51 Sys_Error("Not enough addressable memory for progs VM");
53 addressableused += ammount;
56 if (!VirtualAlloc (addressablehunk, addressableused, MEM_COMMIT, PAGE_READWRITE))
57 Sys_Error("VirtualAlloc failed. Blame windows.");
60 return &addressablehunk[addressableused-ammount];
63 void PRAddressableFlush(progfuncs_t *progfuncs, int totalammount)
66 if (totalammount < 0) //flush
68 totalammount = addressablesize;
74 VirtualFree(addressablehunk, 0, MEM_RELEASE); //doesn't this look complicated? :p
75 addressablehunk = VirtualAlloc (NULL, totalammount, MEM_RESERVE, PAGE_NOACCESS);
77 free(addressablehunk);
78 addressablehunk = malloc(totalammount); //linux will allocate-on-use anyway, which is handy.
79 // memset(addressablehunk, 0xff, totalammount);
82 Sys_Error("Out of memory\n");
83 addressablesize = totalammount;
86 int PR_InitEnts(progfuncs_t *progfuncs, int max_ents)
92 max_fields_size = fields_size;
94 prinst->edicttable = PRHunkAlloc(progfuncs, maxedicts*sizeof(struct edicts_s *));
95 sv_edicts = PRHunkAlloc(progfuncs, externs->edictsize);
96 prinst->edicttable[0] = sv_edicts;
97 ((edictrun_t*)prinst->edicttable[0])->fields = PRAddressableAlloc(progfuncs, max_fields_size);
98 QC_ClearEdict(progfuncs, sv_edicts);
101 if (externs->entspawn)
102 externs->entspawn((struct edict_s *)sv_edicts, false);
104 return max_fields_size;
106 edictrun_t tempedict; //used as a safty buffer
107 float tempedictfields[2048];
109 void PR_Configure (progfuncs_t *progfuncs, int addressable_size, int max_progs) //can be used to wipe all memory
116 prinst->usejit = true;
121 progfuncs->stringtable = 0;
122 QC_StartShares(progfuncs);
123 QC_InitShares(progfuncs);
125 for ( i=1 ; i<maxedicts; i++)
127 e = (edictrun_t *)(prinst->edicttable[i]);
128 prinst->edicttable[i] = NULL;
134 PRHunkFree(progfuncs, 0); //clear mem - our hunk may not be a real hunk.
135 if (addressable_size<0)
136 addressable_size = 8*1024*1024;
137 PRAddressableFlush(progfuncs, addressable_size);
139 pr_progstate = PRHunkAlloc(progfuncs, sizeof(progstate_t) * max_progs);
141 /* for(a = 0; a < max_progs; a++)
143 pr_progstate[a].progs = NULL;
147 maxprogs = max_progs;
150 prinst->reorganisefields = false;
153 prinst->edicttable = &sv_edicts;
154 sv_num_edicts = 1; //set up a safty buffer so things won't go horribly wrong too often
155 sv_edicts=(struct edict_s *)&tempedict;
156 tempedict.readonly = true;
157 tempedict.fields = tempedictfields;
158 tempedict.isfree = false;
163 struct globalvars_s *PR_globals (progfuncs_t *progfuncs, progsnum_t pnum)
167 if (!current_progstate)
168 return NULL; //err.. you've not loaded one yet.
169 return (struct globalvars_s *)current_progstate->globals;
171 return (struct globalvars_s *)pr_progstate[pnum].globals;
174 struct entvars_s *PR_entvars (progfuncs_t *progfuncs, struct edict_s *ed)
176 if (((edictrun_t *)ed)->isfree)
179 return (struct entvars_s *)edvars(ed);
182 int PR_GetFuncArgCount(progfuncs_t *progfuncs, func_t func)
188 pnum = (func & 0xff000000)>>24;
189 fnum = (func & 0x00ffffff);
191 if (pnum >= (unsigned)maxprogs || !pr_progstate[pnum].functions)
193 else if (fnum >= pr_progstate[pnum].progs->numfunctions)
197 f = pr_progstate[pnum].functions + fnum;
202 func_t PR_FindFunc(progfuncs_t *progfuncs, char *funcname, progsnum_t pnum)
207 for (pnum = 0; (unsigned)pnum < maxprogs; pnum++)
209 if (!pr_progstate[pnum].progs)
211 f = ED_FindFunction(progfuncs, funcname, &pnum, pnum);
216 else if (pnum == PR_ANYBACK) //run backwards
218 for (pnum = maxprogs-1; pnum >= 0; pnum--)
220 if (!pr_progstate[pnum].progs)
222 f = ED_FindFunction(progfuncs, funcname, &pnum, pnum);
228 f = ED_FindFunction(progfuncs, funcname, &pnum, pnum);
235 switch(pr_progstate[pnum].intsize)
239 var16 = ED_FindTypeGlobalFromProgs16(progfuncs, funcname, pnum, ev_function); //we must make sure we actually have a function def - 'light' is defined as a field before it is defined as a function.
241 return (f - pr_progstate[pnum].functions) | (pnum << 24);
242 return *(int *)&pr_progstate[pnum].globals[var16->ofs];
244 var32 = ED_FindTypeGlobalFromProgs32(progfuncs, funcname, pnum, ev_function); //we must make sure we actually have a function def - 'light' is defined as a field before it is defined as a function.
246 return (f - pr_progstate[pnum].functions) | (pnum << 24);
247 return *(int *)&pr_progstate[pnum].globals[var32->ofs];
249 Sys_Error("Error with def size (PR_FindFunc)");
254 eval_t *PR_FindGlobal(progfuncs_t *progfuncs, char *globname, progsnum_t pnum)
259 if (pnum == PR_CURRENT)
260 pnum = pr_typecurrent;
264 for (i = 0; i < maxprogs; i++)
266 if (!pr_progstate[i].progs)
268 ev = PR_FindGlobal(progfuncs, globname, i);
274 if (pnum < 0 || (unsigned)pnum >= maxprogs || !pr_progstate[pnum].progs)
276 switch(pr_progstate[pnum].intsize)
280 if (!(var16 = ED_FindGlobalFromProgs16(progfuncs, globname, pnum)))
283 return (eval_t *)&pr_progstate[pnum].globals[var16->ofs];
285 if (!(var32 = ED_FindGlobalFromProgs32(progfuncs, globname, pnum)))
288 return (eval_t *)&pr_progstate[pnum].globals[var32->ofs];
290 Sys_Error("Error with def size (PR_FindGlobal)");
294 void SetGlobalEdict(progfuncs_t *progfuncs, struct edict_s *ed, int ofs)
296 ((int*)pr_globals)[ofs] = EDICT_TO_PROG(progfuncs, ed);
299 char *PR_VarString (progfuncs_t *progfuncs, int first)
302 static char out[1024];
306 for (i=first ; i<pr_argc ; i++)
308 if (G_STRING(OFS_PARM0+i*3))
310 s=G_STRING((OFS_PARM0+i*3)) + progfuncs->stringtable;
314 if (strlen(out)+1 >= sizeof(out))
315 Sys_Error("VarString (builtin call ending with strings) exceeded maximum string length of %i chars", sizeof(out));
322 int PR_QueryField (progfuncs_t *progfuncs, unsigned int fieldoffset, etype_t *type, char **name, evalc_t *fieldcache)
325 var = ED_FieldAtOfs(progfuncs, fieldoffset);
330 *type = var->type & ~(DEF_SAVEGLOBAL|DEF_SHARED);
335 fieldcache->ofs32 = var;
336 fieldcache->varname = var->name;
342 eval_t *GetEdictFieldValue(progfuncs_t *progfuncs, struct edict_s *ed, char *name, evalc_t *cache)
347 var = ED_FindField(progfuncs, name);
350 return (eval_t *) &(((int*)(((edictrun_t*)ed)->fields))[var->ofs]);
354 cache->varname = name;
355 var = ED_FindField(progfuncs, name);
362 cache->varname = var->name;
364 return (void*)~0; //something not null
365 return (eval_t *) &(((int*)(((edictrun_t*)ed)->fields))[var->ofs]);
367 if (cache->ofs32 == NULL)
369 return (eval_t *) &(((int*)(((edictrun_t*)ed)->fields))[cache->ofs32->ofs]);
372 struct edict_s *ProgsToEdict (progfuncs_t *progfuncs, int progs)
374 if ((unsigned)progs >= (unsigned)maxedicts)
376 printf("Bad entity index %i\n", progs);
379 return (struct edict_s *)PROG_TO_EDICT(progfuncs, progs);
381 int EdictToProgs (progfuncs_t *progfuncs, struct edict_s *ed)
383 return EDICT_TO_PROG(progfuncs, ed);
386 string_t PR_StringToProgs (progfuncs_t *progfuncs, char *str)
394 // if (str-progfuncs->stringtable < progfuncs->stringtablesize)
395 // return str - progfuncs->stringtable;
397 for (i = prinst->numallocedstrings-1; i >= 0; i--)
399 if (prinst->allocedstrings[i] == str)
400 return (string_t)((unsigned int)i | 0x80000000);
401 if (!prinst->allocedstrings[i])
408 prinst->allocedstrings[i] = str;
409 return (string_t)((unsigned int)i | 0x80000000);
412 prinst->maxallocedstrings += 1024;
413 ntable = memalloc(sizeof(char*) * prinst->maxallocedstrings);
414 memcpy(ntable, prinst->allocedstrings, sizeof(char*) * prinst->numallocedstrings);
415 memset(ntable + prinst->numallocedstrings, 0, sizeof(char*) * (prinst->maxallocedstrings - prinst->numallocedstrings));
416 prinst->numallocedstrings = prinst->maxallocedstrings;
417 if (prinst->allocedstrings)
418 memfree(prinst->allocedstrings);
419 prinst->allocedstrings = ntable;
421 for (i = prinst->numallocedstrings-1; i >= 0; i--)
423 if (!prinst->allocedstrings[i])
425 prinst->allocedstrings[i] = str;
426 return (string_t)((unsigned int)i | 0x80000000);
433 char *PR_RemoveProgsString (progfuncs_t *progfuncs, string_t str)
437 //input string is expected to be an allocated string
438 //if its a temp, or a constant, just return NULL.
439 if ((unsigned int)str & 0xc0000000)
441 if ((unsigned int)str & 0x80000000)
443 int i = str & ~0x80000000;
444 if (i >= prinst->numallocedstrings)
449 if (prinst->allocedstrings[i])
451 ret = prinst->allocedstrings[i];
452 prinst->allocedstrings[i] = NULL; //remove it
458 return NULL; //urm, was freed...
466 char *PR_StringToNative (progfuncs_t *progfuncs, string_t str)
468 if ((unsigned int)str & 0xc0000000)
470 if ((unsigned int)str & 0x80000000)
472 int i = str & ~0x80000000;
473 if (i >= prinst->numallocedstrings)
478 if (prinst->allocedstrings[i])
479 return prinst->allocedstrings[i];
483 return ""; //urm, was freed...
486 if ((unsigned int)str & 0x40000000)
488 int i = str & ~0x40000000;
489 if (i >= prinst->numtempstrings)
494 return prinst->tempstrings[i];
498 if (str >= progfuncs->stringtablesize)
503 return progfuncs->stringtable + str;
507 string_t PR_AllocTempString (progfuncs_t *progfuncs, char *str)
516 if (prinst->numtempstrings == prinst->maxtempstrings)
518 newmax = prinst->maxtempstrings += 1024;
519 prinst->maxtempstrings += 1024;
520 ntable = memalloc(sizeof(char*) * newmax);
521 memcpy(ntable, prinst->tempstrings, sizeof(char*) * prinst->numtempstrings);
522 prinst->maxtempstrings = newmax;
523 if (prinst->tempstrings)
524 memfree(prinst->tempstrings);
525 prinst->tempstrings = ntable;
528 i = prinst->numtempstrings;
532 prinst->numtempstrings++;
534 prinst->tempstrings[i] = memalloc(strlen(str)+1);
535 strcpy(prinst->tempstrings[i], str);
537 return (string_t)((unsigned int)i | 0x40000000);
540 void PR_FreeTemps (progfuncs_t *progfuncs, int depth)
543 if (depth > prinst->numtempstrings)
545 Sys_Error("QC Temp stack inverted\n");
548 for (i = depth; i < prinst->numtempstrings; i++)
550 memfree(prinst->tempstrings[i]);
553 prinst->numtempstrings = depth;
557 struct qcthread_s *PR_ForkStack (progfuncs_t *progfuncs);
558 void PR_ResumeThread (progfuncs_t *progfuncs, struct qcthread_s *thread);
559 void PR_AbortStack (progfuncs_t *progfuncs);
562 void RegisterBuiltin(progfuncs_t *progfncs, char *name, builtin_t func);
564 progfuncs_t deffuncs = {
597 NULL,//filefromnewprogs,
654 //defs incase following structure is not passed.
655 struct edict_s *safesv_edicts;
656 int safesv_num_edicts;
659 progexterns_t defexterns = {
662 NULL, //char *(*ReadFile) (char *fname, void *buffer, int len);
663 NULL, //int (*FileSize) (char *fname); //-1 if file does not exist
664 NULL, //bool (*WriteFile) (char *name, void *data, int len);
665 printf, //void (*printf) (char *, ...);
666 (void*)exit, //void (*Sys_Error) (char *, ...);
667 NULL, //void (*Abort) (char *, ...);
668 sizeof(edictrun_t), //int edictsize; //size of edict_t
670 NULL, //void (*entspawn) (struct edict_s *ent); //ent has been spawned, but may not have all the extra variables (that may need to be set) set
671 NULL, //bool (*entcanfree) (struct edict_s *ent); //return true to stop ent from being freed
672 NULL, //void (*stateop) (float var, func_t func);
677 //used when loading a game
678 NULL, //builtin_t *(*builtinsfor) (int num); //must return a pointer to the builtins that were used before the state was saved.
679 NULL, //void (*loadcompleate) (int edictsize); //notification to reset any pointers.
681 (void*)malloc, //void *(*memalloc) (int size); //small string allocation malloced and freed randomly by the executor. (use memalloc if you want)
682 free, //void (*memfree) (void * mem);
685 NULL, //builtin_t *globalbuiltins; //these are available to all progs
686 0, //int numglobalbuiltins;
690 &safetime, //double *gametime;
692 &safesv_edicts, //struct edict_s **sv_edicts;
693 &safesv_num_edicts, //int *sv_num_edicts;
695 NULL, //int (*useeditor) (char *filename, int line, int nump, char **parms);
698 //progfuncs_t *progfuncs = NULL;
701 #undef extensionbuiltin
707 #ifdef QCLIBDLL_EXPORTS
708 __declspec(dllexport)
710 void CloseProgs(progfuncs_t *inst)
712 // extensionbuiltin_t *eb;
713 void (VARGS *f) (void *);
718 f = inst->parms->memfree;
720 for ( i=1 ; i<inst->maxedicts; i++)
722 e = (edictrun_t *)(inst->prinst->edicttable[i]);
723 inst->prinst->edicttable[i] = NULL;
734 VirtualFree(inst->addressablehunk, 0, MEM_RELEASE); //doesn't this look complicated? :p
736 free(inst->addressablehunk);
740 while(inst->prinst->extensionbuiltin)
742 eb = inst->prinst->extensionbuiltin->prev;
743 f(inst->prinst->extensionbuiltin);
744 inst->prinst->extensionbuiltin = eb;
747 if (inst->prinst->field)
748 f(inst->prinst->field);
749 if (inst->prinst->shares)
750 f(inst->prinst->shares); //free memory
755 void RegisterBuiltin(progfuncs_t *progfuncs, char *name, builtin_t func)
758 extensionbuiltin_t *eb;
759 eb = memalloc(sizeof(extensionbuiltin_t));
760 eb->prev = progfuncs->prinst->extensionbuiltin;
761 progfuncs->prinst->extensionbuiltin = eb;
768 #define QCLIBINT //don't use dllspecifications
771 #if defined(QCLIBDLL_EXPORTS)
772 __declspec(dllexport)
774 progfuncs_t * InitProgs(progexterns_t *ext)
783 if (ext->progsversion > PROGSTRUCT_VERSION)
786 for (i=0;i<sizeof(progexterns_t); i+=4) //make sure there are no items left out.
787 if (!*(int *)((char *)ext+i))
788 *(int *)((char *)ext+i) = *(int *)((char *)&defexterns+i);
792 funcs = ext->memalloc(sizeof(progfuncs_t));
793 memcpy(funcs, &deffuncs, sizeof(progfuncs_t));
795 funcs->prinst = ext->memalloc(sizeof(prinst_t));
796 memset(funcs->prinst,0, sizeof(prinst_t));
798 funcs->pr_trace = &funcs->prinst->pr_trace;
799 funcs->progstate = &funcs->pr_progstate;
800 funcs->callargc = &funcs->pr_argc;
825 void main (int argc, char **argv)
830 funcs = InitProgs(&ext);
831 if (funcs->PR_StartCompile(argc, argv))
832 while(funcs->PR_ContinueCompile());