rewrote PRVM_AllocString and PRVM_FreeString to deal with string offsets directly...
authorhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Fri, 3 Jun 2005 14:11:07 +0000 (14:11 +0000)
committerhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Fri, 3 Jun 2005 14:11:07 +0000 (14:11 +0000)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@5388 d7cf8633-e32d-0410-b094-e92efae38249

progsvm.h
prvm_cmds.c
prvm_edict.c

index b8ebe80..e389f0f 100644 (file)
--- 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);
 
 //============================================================================
 
index 7d8354a..c68c3ab 100644 (file)
@@ -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++;
index 33b37cc..af5c4d4 100644 (file)
@@ -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);
 }