From: havoc Date: Fri, 3 Jun 2005 14:11:07 +0000 (+0000) Subject: rewrote PRVM_AllocString and PRVM_FreeString to deal with string offsets directly... X-Git-Tag: xonotic-v0.1.0preview~4794 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=d191d2aae518b7fe21132a6c159554a350966978;p=xonotic%2Fdarkplaces.git rewrote PRVM_AllocString and PRVM_FreeString to deal with string offsets directly (rather than pointers) to avoid unnecessary GetString/SetString calls git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@5388 d7cf8633-e32d-0410-b094-e92efae38249 --- diff --git a/progsvm.h b/progsvm.h index b8ebe800..e389f0f0 100644 --- a/progsvm.h +++ b/progsvm.h @@ -152,7 +152,7 @@ typedef union prvm_eval_s int edict; } prvm_eval_t; -typedef struct prvm_required_field_s +typedef struct prvm_required_field_s { int type; const char *name; @@ -200,7 +200,7 @@ typedef struct prvm_edict_s // However, the first one should be preferred. } priv; // QuakeC fields (stored in dynamically resized array) - union + union { void *vp; entvars_t *server; @@ -479,8 +479,8 @@ void PRVM_ED_PrintNum (int ent); const char *PRVM_GetString(int num); int PRVM_SetQCString(const char *s); int PRVM_SetEngineString(const char *s); -char *PRVM_AllocString(int bufferlength); -void PRVM_FreeString(char *s); +int PRVM_AllocString(int bufferlength, char **pointer); +void PRVM_FreeString(int num); //============================================================================ diff --git a/prvm_cmds.c b/prvm_cmds.c index 7d8354a9..c68c3ab8 100644 --- a/prvm_cmds.c +++ b/prvm_cmds.c @@ -1686,9 +1686,8 @@ void VM_strzone(void) VM_SAFEPARMCOUNT(1,VM_strzone); in = PRVM_G_STRING(OFS_PARM0); - out = PRVM_AllocString(strlen(in) + 1); + PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(strlen(in) + 1, &out); strcpy(out, in); - PRVM_G_INT(OFS_RETURN) = PRVM_SetQCString(out); } /* @@ -1702,7 +1701,7 @@ strunzone(string s) void VM_strunzone(void) { VM_SAFEPARMCOUNT(1,VM_strunzone); - PRVM_FreeString((char *)PRVM_G_STRING(OFS_PARM0)); + PRVM_FreeString(PRVM_G_INT(OFS_PARM0)); } /* @@ -2651,7 +2650,7 @@ void VM_altstr_count( void ) for( count = 0, pos = altstr ; *pos ; pos++ ) { if( *pos == '\\' ) { if( !*++pos ) { - break; + break; } } else if( *pos == '\'' ) { count++; diff --git a/prvm_edict.c b/prvm_edict.c index 33b37cc4..af5c4d45 100644 --- a/prvm_edict.c +++ b/prvm_edict.c @@ -884,8 +884,7 @@ qboolean PRVM_ED_ParseEpair(prvm_edict_t *ent, ddef_t *key, const char *s) { case ev_string: l = strlen(s) + 1; - new_p = PRVM_AllocString(l); - val->string = PRVM_SetQCString(new_p); + val->string = PRVM_AllocString(l, &new_p); for (i = 0;i < l;i++) { if (s[i] == '\\' && i < l-1) @@ -1859,7 +1858,7 @@ int PRVM_SetEngineString(const char *s) return -1 - i; } -char *PRVM_AllocString(int bufferlength) +int PRVM_AllocString(int bufferlength, char **pointer) { int i; if (!bufferlength) @@ -1879,22 +1878,27 @@ char *PRVM_AllocString(int bufferlength) } prog->numknownstrings++; } - return (char *)(prog->knownstrings[i] = PRVM_Alloc(bufferlength)); + (char *)(prog->knownstrings[i]) = PRVM_Alloc(bufferlength); + if (pointer) + *pointer = (char *)(prog->knownstrings[i]); + return -1 - i; } -void PRVM_FreeString(char *s) +void PRVM_FreeString(int num) { - int i; - if (!s) + if (num == 0) Host_Error("PRVM_FreeString: attempt to free a NULL string\n"); - if (s >= prog->strings && s <= prog->strings + prog->stringssize) + else if (num >= 0 && num < prog->stringssize) Host_Error("PRVM_FreeString: attempt to free a constant string\n"); - for (i = 0;i < prog->numknownstrings;i++) - if (prog->knownstrings[i] == s) - break; - if (i == prog->numknownstrings) - Host_Error("PRVM_FreeString: attempt to free a non-existent or already freed string\n"); - PRVM_Free((char *)prog->knownstrings[i]); - prog->knownstrings[i] = NULL; + else if (num < 0 && num >= -prog->numknownstrings) + { + num = -1 - num; + if (!prog->knownstrings[num]) + Host_Error("PRVM_FreeString: attempt to free a non-existent or already freed string\n"); + PRVM_Free((char *)prog->knownstrings[num]); + prog->knownstrings[num] = NULL; + } + else + Host_Error("PRVM_FreeString: invalid string offset %i\n", num); }