From ead58cd0399e11e898b5a1e4a66f558191f7a6b4 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sun, 5 May 2024 06:47:28 +0200 Subject: [PATCH] core: remove membufferpp This the C++ version of membuffer. Since everything is C++, it can just be made the default. Signed-off-by: Berthold Stoeger --- backend-shared/exportfuncs.cpp | 4 +-- core/errorhelper.cpp | 4 +-- core/git-access.cpp | 2 +- core/membuffer.cpp | 9 +++--- core/membuffer.h | 20 +++--------- core/save-git.cpp | 32 +++++++++---------- core/save-html.cpp | 4 +-- core/save-profiledata.cpp | 2 +- core/save-xml.cpp | 8 ++--- core/uploadDiveLogsDE.cpp | 2 +- core/uploadDiveShare.cpp | 2 +- core/worldmap-save.cpp | 2 +- desktop-widgets/tab-widgets/TabDivePhotos.cpp | 2 +- 13 files changed, 40 insertions(+), 53 deletions(-) diff --git a/backend-shared/exportfuncs.cpp b/backend-shared/exportfuncs.cpp index b2ac25469..a24fba7a8 100644 --- a/backend-shared/exportfuncs.cpp +++ b/backend-shared/exportfuncs.cpp @@ -90,7 +90,7 @@ void export_TeX(const char *filename, bool selected_only, bool plain, ExportCall int i; bool need_pagebreak = false; - struct membufferpp buf; + membuffer buf; if (plain) { ssrf = ""; @@ -280,7 +280,7 @@ void export_depths(const char *filename, bool selected_only) int i; const char *unit = NULL; - struct membufferpp buf; + membuffer buf; for_each_dive (i, dive) { if (selected_only && !dive->selected) diff --git a/core/errorhelper.cpp b/core/errorhelper.cpp index 5d9518b62..66ec0e3c1 100644 --- a/core/errorhelper.cpp +++ b/core/errorhelper.cpp @@ -20,7 +20,7 @@ int verbose; void report_info(const char *fmt, ...) { - struct membufferpp buf; + membuffer buf; VA_BUF(&buf, fmt); strip_mb(&buf); @@ -31,7 +31,7 @@ static void (*error_cb)(char *) = NULL; int report_error(const char *fmt, ...) { - struct membufferpp buf; + membuffer buf; VA_BUF(&buf, fmt); strip_mb(&buf); diff --git a/core/git-access.cpp b/core/git-access.cpp index 7abc690c3..026a8a04b 100644 --- a/core/git-access.cpp +++ b/core/git-access.cpp @@ -346,7 +346,7 @@ static int try_to_git_merge(struct git_info *info, git_reference **local_p, git_ git_commit *local_commit, *remote_commit, *base_commit; git_index *merged_index; git_merge_options merge_options; - struct membufferpp msg; + membuffer msg; if (verbose) { char outlocal[41], outremote[41]; diff --git a/core/membuffer.cpp b/core/membuffer.cpp index 86872e6af..605b523e8 100644 --- a/core/membuffer.cpp +++ b/core/membuffer.cpp @@ -12,12 +12,11 @@ #include "units.h" #include "membuffer.h" -membufferpp::membufferpp() - : membuffer{0, 0, nullptr} +membuffer::membuffer() { } -membufferpp::~membufferpp() +membuffer::~membuffer() { free_buffer(this); } @@ -144,7 +143,7 @@ void put_vformat(struct membuffer *b, const char *fmt, va_list args) /* Silly helper using membuffer */ char *vformat_string(const char *fmt, va_list args) { - struct membuffer mb = { 0 }; + struct membuffer mb; put_vformat(&mb, fmt, args); return detach_cstring(&mb); } @@ -300,7 +299,7 @@ void put_quoted(struct membuffer *b, const char *text, int is_attribute, int is_ char *add_to_string_va(char *old, const char *fmt, va_list args) { char *res; - struct membufferpp o, n; + membuffer o, n; put_vformat(&n, fmt, args); put_format(&o, "%s\n%s", old ?: "", mb_cstring(&n)); res = strdup(mb_cstring(&o)); diff --git a/core/membuffer.h b/core/membuffer.h index 3c744bb08..7461739ef 100644 --- a/core/membuffer.h +++ b/core/membuffer.h @@ -4,10 +4,6 @@ * 'membuffer' functions will manage memory allocation avoiding performance * issues related to superfluous re-allocation. See 'make_room' function * - * Before using it membuffer struct should be properly initialized - * - * struct membuffer mb = { 0 }; - * * Internal membuffer buffer will not by default contain null terminator, * adding it should be done using 'mb_cstring' function * @@ -28,10 +24,6 @@ * ptr = detach_cstring(); * * where the caller now has a C string and is supposed to free it. - * - * Otherwise allocated memory should be freed - * - * free_buffer(&mb); */ #ifndef MEMBUFFER_H #define MEMBUFFER_H @@ -42,14 +34,10 @@ #include "units.h" struct membuffer { - unsigned int len, alloc; - char *buffer; -}; - -// In C++ code use this - it automatically frees the buffer, when going out of scope. -struct membufferpp : public membuffer { - membufferpp(); - ~membufferpp(); + unsigned int len = 0, alloc = 0; + char *buffer = nullptr; + membuffer(); + ~membuffer(); }; #ifdef __GNUC__ diff --git a/core/save-git.cpp b/core/save-git.cpp index 83f867c40..87138351a 100644 --- a/core/save-git.cpp +++ b/core/save-git.cpp @@ -512,7 +512,7 @@ struct dir { static int tree_insert(git_treebuilder *dir, const char *name, int mkunique, git_oid *id, git_filemode_t mode) { int ret; - membufferpp uniquename; + membuffer uniquename; if (mkunique && git_treebuilder_get(dir, name)) { char hex[8]; @@ -559,7 +559,7 @@ static struct dir *new_directory(git_repository *repo, struct dir *parent, struc static struct dir *mktree(git_repository *repo, struct dir *dir, const char *fmt, ...) { - membufferpp buf; + membuffer buf; VA_BUF(&buf, fmt); for (auto &subdir: dir->subdirs) { @@ -609,7 +609,7 @@ static int blob_insert(git_repository *repo, struct dir *tree, struct membuffer { int ret; git_oid blob_id; - struct membufferpp name; + membuffer name; ret = git_blob_create_frombuffer(&blob_id, repo, b->buffer, b->len); if (ret) @@ -623,7 +623,7 @@ static int blob_insert(git_repository *repo, struct dir *tree, struct membuffer static int save_one_divecomputer(git_repository *repo, struct dir *tree, struct dive *dive, struct divecomputer *dc, int idx) { int ret; - struct membufferpp buf; + membuffer buf; save_dc(&buf, dive, dc); ret = blob_insert(repo, tree, &buf, "Divecomputer%c%03u", idx ? '-' : 0, idx); @@ -635,7 +635,7 @@ static int save_one_divecomputer(git_repository *repo, struct dir *tree, struct static int save_one_picture(git_repository *repo, struct dir *dir, struct picture *pic) { int offset = pic->offset.seconds; - struct membufferpp buf; + membuffer buf; char sign = '+'; unsigned h; @@ -669,7 +669,7 @@ static int save_pictures(git_repository *repo, struct dir *dir, struct dive *div static int save_one_dive(git_repository *repo, struct dir *tree, struct dive *dive, struct tm *tm, bool cached_ok) { struct divecomputer *dc; - struct membufferpp buf, name; + membuffer buf, name; struct dir *subdir; int ret, nr; @@ -769,7 +769,7 @@ static int save_trip_description(git_repository *repo, struct dir *dir, dive_tri { int ret; git_oid blob_id; - struct membufferpp desc; + membuffer desc; put_format(&desc, "date %04u-%02u-%02u\n", tm->tm_year, tm->tm_mon + 1, tm->tm_mday); @@ -809,7 +809,7 @@ static int save_one_trip(git_repository *repo, struct dir *tree, dive_trip_t *tr int i; struct dive *dive; struct dir *subdir; - struct membufferpp name; + membuffer name; timestamp_t first, last; /* Create trip directory */ @@ -890,7 +890,7 @@ static void save_one_fingerprint(struct membuffer *b, int i) static void save_settings(git_repository *repo, struct dir *tree) { - struct membufferpp b; + membuffer b; put_format(&b, "version %d\n", DATAFORMAT_VERSION); for (int i = 0; i < nr_devices(divelog.devices); i++) @@ -918,15 +918,15 @@ static void save_settings(git_repository *repo, struct dir *tree) static void save_divesites(git_repository *repo, struct dir *tree) { struct dir *subdir; - struct membufferpp dirname; + membuffer dirname; put_format(&dirname, "01-Divesites"); subdir = new_directory(repo, tree, &dirname); purge_empty_dive_sites(divelog.sites); for (int i = 0; i < divelog.sites->nr; i++) { - struct membufferpp b; + membuffer b; struct dive_site *ds = get_dive_site(i, divelog.sites); - struct membufferpp site_file_name; + membuffer site_file_name; put_format(&site_file_name, "Site-%08x", ds->uuid); show_utf8(&b, "name ", ds->name.c_str(), "\n"); show_utf8(&b, "description ", ds->description.c_str(), "\n"); @@ -997,15 +997,15 @@ static void format_one_filter_preset(int preset_id, struct membuffer *b) static void save_filter_presets(git_repository *repo, struct dir *tree) { - struct membufferpp dirname; + membuffer dirname; struct dir *filter_dir; put_format(&dirname, "02-Filterpresets"); filter_dir = new_directory(repo, tree, &dirname); for (int i = 0; i < filter_presets_count(); i++) { - membufferpp preset_name; - membufferpp preset_buffer; + membuffer preset_name; + membuffer preset_buffer; put_format(&preset_name, "Preset-%03d", i); format_one_filter_preset(i, &preset_buffer); @@ -1221,7 +1221,7 @@ static int create_new_commit(struct git_info *info, git_oid *tree_id, bool creat /* Else we do want to create the new branch, but with the old commit */ commit = (git_commit *) parent; } else { - struct membufferpp commit_msg; + membuffer commit_msg; create_commit_message(&commit_msg, create_empty); if (git_commit_create_v(&commit_id, info->repo, NULL, author, author, NULL, mb_cstring(&commit_msg), tree, parent != NULL, parent)) { diff --git a/core/save-html.cpp b/core/save-html.cpp index bc59db059..b3625cdc9 100644 --- a/core/save-html.cpp +++ b/core/save-html.cpp @@ -482,7 +482,7 @@ void export_HTML(const char *file_name, const char *photos_dir, const bool selec { FILE *f; - struct membufferpp buf; + membuffer buf; export_list(&buf, photos_dir, selected_only, list_only); f = subsurface_fopen(file_name, "w+"); @@ -498,7 +498,7 @@ void export_translation(const char *file_name) { FILE *f; - struct membufferpp buf; + membuffer buf; //export translated words here put_format(&buf, "translate={"); diff --git a/core/save-profiledata.cpp b/core/save-profiledata.cpp index c5cc63035..06c22d190 100644 --- a/core/save-profiledata.cpp +++ b/core/save-profiledata.cpp @@ -239,7 +239,7 @@ void save_subtitles_buffer(struct membuffer *b, struct dive *dive, int offset, i int save_profiledata(const char *filename, bool select_only) { - struct membufferpp buf; + membuffer buf; FILE *f; int error = 0; diff --git a/core/save-xml.cpp b/core/save-xml.cpp index ca6133fcf..1affe29bd 100644 --- a/core/save-xml.cpp +++ b/core/save-xml.cpp @@ -558,7 +558,7 @@ void save_one_dive_to_mb(struct membuffer *b, struct dive *dive, bool anonymize) int save_dive(FILE *f, struct dive *dive, bool anonymize) { - struct membufferpp buf; + membuffer buf; save_one_dive_to_mb(&buf, dive, anonymize); flush_buffer(&buf, f); @@ -812,7 +812,7 @@ static void try_to_backup(const char *filename) int save_dives_logic(const char *filename, const bool select_only, bool anonymize) { - struct membufferpp buf; + membuffer buf; struct git_info info; FILE *f; int error = 0; @@ -854,7 +854,7 @@ int export_dives_xslt(const char *filename, const bool selected, const int units static int export_dives_xslt_doit(const char *filename, struct xml_params *params, bool selected, int units, const char *export_xslt, bool anonymize) { FILE *f; - struct membufferpp buf; + membuffer buf; xmlDoc *doc; xsltStylesheetPtr xslt = NULL; xmlDoc *transformed; @@ -933,7 +933,7 @@ static void save_dive_sites_buffer(struct membuffer *b, const struct dive_site * int save_dive_sites_logic(const char *filename, const struct dive_site *sites[], int nr_sites, bool anonymize) { - struct membufferpp buf; + membuffer buf; FILE *f; int error = 0; diff --git a/core/uploadDiveLogsDE.cpp b/core/uploadDiveLogsDE.cpp index 4cdbb3a2c..7990bd44c 100644 --- a/core/uploadDiveLogsDE.cpp +++ b/core/uploadDiveLogsDE.cpp @@ -95,7 +95,7 @@ bool uploadDiveLogsDE::prepareDives(const QString &tempfile, bool selected) char *membuf; xmlDoc *transformed; struct zip_source *s; - struct membufferpp mb; + membuffer mb; struct xml_params *params = alloc_xml_params(); /* diff --git a/core/uploadDiveShare.cpp b/core/uploadDiveShare.cpp index 329cce9a1..420e59cc5 100644 --- a/core/uploadDiveShare.cpp +++ b/core/uploadDiveShare.cpp @@ -27,7 +27,7 @@ uploadDiveShare::uploadDiveShare(): void uploadDiveShare::doUpload(bool selected, const QString &uid, bool noPublic) { //generate json - struct membufferpp buf; + membuffer buf; export_list(&buf, NULL, selected, false); QByteArray json_data(buf.buffer, buf.len); diff --git a/core/worldmap-save.cpp b/core/worldmap-save.cpp index 803c3cb32..bf22d8a5a 100644 --- a/core/worldmap-save.cpp +++ b/core/worldmap-save.cpp @@ -110,7 +110,7 @@ void export_worldmap_HTML(const char *file_name, const bool selected_only) { FILE *f; - struct membufferpp buf; + membuffer buf; export_doit(&buf, selected_only); f = subsurface_fopen(file_name, "w+"); diff --git a/desktop-widgets/tab-widgets/TabDivePhotos.cpp b/desktop-widgets/tab-widgets/TabDivePhotos.cpp index 38ec05e69..0f1468b3c 100644 --- a/desktop-widgets/tab-widgets/TabDivePhotos.cpp +++ b/desktop-widgets/tab-widgets/TabDivePhotos.cpp @@ -131,7 +131,7 @@ void TabDivePhotos::saveSubtitles() // Only videos have non-zero duration if (!duration) continue; - struct membufferpp b; + membuffer b; save_subtitles_buffer(&b, parent.currentDive, offset, duration); const char *data = mb_cstring(&b); subtitlefile.open(QIODevice::WriteOnly);