core: C++-ify membuffer

C-style memory management is a pain and nearly nobody seems to get
it right. Add a C++-version of membuffer that frees the buffer
when it gets out-of-scope. Originally, I was thinking about
conditionally adding a constructor/destructor pair when compiling
with C++. But then decided to create a derived class membufferpp,
because it would be extremely confusing to have behavioral change
when changing a source from from C to C++ or vice-versa.

Also add a comment about the dangers of returned pointer: They
become dangling on changes to the membuffer.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2021-07-20 07:24:07 +02:00 committed by Dirk Hohndel
parent 0c84f369c3
commit f142e9a9c6
5 changed files with 26 additions and 9 deletions

View file

@ -397,7 +397,6 @@ int vasprintf_loc(char **dst, const char *cformat, va_list ap)
return utf8.size();
}
// This function is defined here instead of membuffer.c, because it needs to access QString.
extern "C" void put_vformat_loc(struct membuffer *b, const char *fmt, va_list args)
{
QByteArray utf8 = vqasprintf_loc(fmt, args).toUtf8();
@ -408,4 +407,3 @@ extern "C" void put_vformat_loc(struct membuffer *b, const char *fmt, va_list ar
memcpy(b->buffer + b->len, data, utf8_size);
b->len += utf8_size;
}