[dstring] Implement reserve

In the end, I'm not sure when I'll use it, but at least the expected
semantics are there.
This commit is contained in:
Bill Currie 2024-09-14 16:20:14 +09:00
parent ebaa557d2a
commit 4329537b1b
2 changed files with 18 additions and 0 deletions

View file

@ -66,6 +66,10 @@ void dstring_delete (dstring_t *dstr);
large enough to hold size bytes (rounded up to the next 1kB boundary)
*/
void dstring_adjust (dstring_t *dstr);
/** Ensure the string buffer is large enough to hold the requested additional
bytes (total size will be rounded up to the next 1kB boundary).
*/
void dstring_reserve (dstring_t *dstr, size_t delta);
/** Open up a hole in the string buffer. The contents of the opened hole
are undefined.
\param dstr the dstring in which to open the hole.

View file

@ -103,6 +103,20 @@ dstring_adjust (dstring_t *dstr)
}
}
VISIBLE void
dstring_reserve (dstring_t *dstr, size_t delta)
{
size_t newsize = dstr->size + delta;
if (newsize > dstr->truesize) {
dstr->truesize = (newsize + 1023) & ~1023;
dstr->str = dstr->mem->realloc (dstr->mem->data, dstr->str,
dstr->truesize);
if (!dstr->str) {
Sys_Error ("dstring_reserve: Failed to reallocate memory.");
}
}
}
VISIBLE char *
dstring_open (dstring_t *dstr, size_t len)
{