gbranden pushed a commit to branch master
in repository groff.
commit 137e3f8b72fb207ae5bf54d7b967fc592574a5a9
Author: G. Branden Robinson <[email protected]>
AuthorDate: Tue Jun 9 06:46:06 2026 -0500
src/libs/libgroff/string.cpp: Refactor further.
* src/libs/libgroff/string.cpp: Continue refactoring to implement new
invariant: any existing `string` object is backed by "clean" storage.
(sfree): After deleting old storage, allocate "clean" replacement new
storage of `initial_string_buffer_size`, just as `salloc()` does when
given a zero second argument. Also delete unused second function
parameter.
(string::~string): Stop calling `sfree()` (since it reallocates the
backing storage) in favor of direct deletion, thus maintaining the
invariant--it occurs only when the entire object is being destroyed.
(string::operator=, string::move): Update to call `sfree()`with its
new, simpler signature.
---
ChangeLog | 16 ++++++++++++++++
src/libs/libgroff/string.cpp | 21 +++++++++++++++------
2 files changed, 31 insertions(+), 6 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 5238e512b..a7dcff6d7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+2026-06-09 G. Branden Robinson <[email protected]>
+
+ * src/libs/libgroff/string.cpp: Continue refactoring to
+ implement new invariant: any existing `string` object is backed
+ by "clean" storage.
+ (sfree): After deleting old storage, allocate "clean"
+ replacement new storage of `initial_string_buffer_size`, just as
+ `salloc()` does when given a zero second argument. Also delete
+ unused second function parameter.
+ (string::~string): Stop calling `sfree()` (since it reallocates
+ the backing storage) in favor of direct deletion, thus
+ maintaining the invariant--it occurs only when the entire object
+ is being destroyed.
+ (string::operator=, string::move): Update to call `sfree()`with
+ its new, simpler signature.
+
2026-06-11 G. Branden Robinson <[email protected]>
[libgroff]: Use a more appropriate return type for
diff --git a/src/libs/libgroff/string.cpp b/src/libs/libgroff/string.cpp
index d03d5e2a6..7f5e5a32c 100644
--- a/src/libs/libgroff/string.cpp
+++ b/src/libs/libgroff/string.cpp
@@ -63,9 +63,18 @@ static char *salloc(size_t len, size_t *sizep)
return p;
}
-static void sfree(char *ptr, size_t)
+static void sfree(char *ptr)
{
delete[] ptr;
+ size_t amount = initial_string_buffer_size;
+ try {
+ ptr = new char[amount];
+ }
+ catch (const std::bad_alloc &exc) {
+ fatal("cannot allocate %1 bytes for string reallocation after"
+ " freeing", amount);
+ }
+ memset(ptr, 0, amount);
}
static char *sfree_alloc(char *ptr, size_t oldsz, size_t len,
@@ -78,7 +87,7 @@ static char *sfree_alloc(char *ptr, size_t oldsz, size_t len,
delete[] ptr;
if (0 == len) {
*sizep = 0;
- return 0 /* nullptr */;
+ return 0 /* nullptr */; // XXX: GBR: breaks invariant
}
char *p = 0 /* nullptr */;
size_t amount = len * 2;
@@ -104,7 +113,7 @@ static char *srealloc(char *ptr, size_t oldsz, size_t
oldlen,
if (0 == newlen) {
delete[] ptr;
*sizep = 0;
- return 0 /* nullptr */;
+ return 0 /* nullptr */; // XXX: GBR: breaks invariant
}
else {
size_t amount = newlen * 2;
@@ -176,7 +185,7 @@ string::string(const string &s) : len(s.len)
string::~string()
{
- sfree(ptr, sz);
+ delete[] ptr;
}
string &string::operator=(const string &s)
@@ -191,7 +200,7 @@ string &string::operator=(const string &s)
string &string::operator=(const char *p)
{
if (0 /* nullptr */ == p) {
- sfree(ptr, len);
+ sfree(ptr);
len = 0;
ptr = 0 /* nullptr */;
sz = 0;
@@ -216,7 +225,7 @@ string &string::operator=(char c)
void string::move(string &s)
{
- sfree(ptr, sz);
+ sfree(ptr);
ptr = s.ptr;
len = s.len;
sz = s.sz;
_______________________________________________
groff-commit mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/groff-commit