[gamecode] Add function PR_AllocTempBlock()

PR_AllocTempBlock() works the same way as PR_SetTempString(), except
that it takes a size parameter and always allocates (never tries to
merge). This is, in a way, abusing the string system, but I needed a way
to allocate a block of progs memory that would be automatically freed
when the current frame ended. The biggest abuse is the need to cast away
the const of PR_GetString()'s return value.
This commit is contained in:
Bill Currie 2020-03-09 22:16:19 +09:00
parent e3fe93c586
commit 7d43bd5c66
2 changed files with 26 additions and 1 deletions

View file

@ -1274,6 +1274,18 @@ string_t PR_SetReturnString(progs_t *pr, const char *s);
*/
string_t PR_SetTempString(progs_t *pr, const char *s);
/** Make a temporary memory block that will be freed when the current progs
stack frame is exited. The contents may be anything and a new block is
returned every time, and the block is in VM addressible space. To access
the contents of the block (for reading, writing, etc), use PR_GetString()
and cast the pointer as necessary.
\param pr pointer to ::progs_t VM struct
\param size size of block in bytes
\return string index of the block
*/
string_t PR_AllocTempBlock (progs_t *pr, size_t size);
/** Make a temporary progs string that is the concatenation of two C strings.
\param pr pointer to ::progs_t VM struct
\param a C string

View file

@ -384,10 +384,16 @@ PR_GetMutableString (progs_t *pr, string_t num)
PR_RunError (pr, "Invalid string offset: %d", num);
}
static inline void *
pr_strmalloc (progs_t *pr, size_t size)
{
return PR_Zone_Malloc (pr, size);
}
static inline char *
pr_stralloc (progs_t *pr, size_t len)
{
return PR_Zone_Malloc (pr, len + 1);
return pr_strmalloc (pr, len + 1);
}
static inline void
@ -507,6 +513,13 @@ PR_SetTempString (progs_t *pr, const char *s)
return pr_settempstring (pr, res, pr_strdup (pr, s));
}
VISIBLE string_t
PR_AllocTempBlock (progs_t *pr, size_t size)
{
prstr_resources_t *res = pr->pr_string_resources;
return pr_settempstring (pr, res, pr_strmalloc (pr, size));
}
VISIBLE string_t
PR_SetDynamicString (progs_t *pr, const char *s)
{