summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZev Weiss <zev@bewilderbeest.net>2022-05-21 20:14:02 -0700
committerZev Weiss <zev@bewilderbeest.net>2023-10-08 16:37:20 -0700
commita33cbae28826de4f20930d225e31f3be0db0502b (patch)
treeb068db06d67e953177938423b6b89c2a5ea51ce2
parent2978af01a79d1627d7fa90fda0efec9cf774d901 (diff)
Meter: allocate GraphData buffer dynamically
On wide screens the previous value of 256 could end up insufficient to cover the screen space allocated to the graph, leaving it awkwardly truncated mid-column. We now allocate the buffer dynamically instead, growing it to accommodate whatever width the graph occupies. Signed-off-by: Zev Weiss <zev@bewilderbeest.net>
-rw-r--r--Meter.c22
-rw-r--r--Meter.h6
2 files changed, 17 insertions, 11 deletions
diff --git a/Meter.c b/Meter.c
index 5e7d3b70..b6fe6402 100644
--- a/Meter.c
+++ b/Meter.c
@@ -86,7 +86,7 @@ void Meter_delete(Object* cast) {
if (Meter_doneFn(this)) {
Meter_done(this);
}
- free(this->drawData);
+ free(this->drawData.values);
free(this->caption);
free(this->values);
free(this);
@@ -121,8 +121,9 @@ void Meter_setMode(Meter* this, int modeIndex) {
}
} else {
assert(modeIndex >= 1);
- free(this->drawData);
- this->drawData = NULL;
+ free(this->drawData.values);
+ this->drawData.values = NULL;
+ this->drawData.nValues = 0;
const MeterMode* mode = Meter_modes[modeIndex];
this->draw = mode->draw;
@@ -289,12 +290,17 @@ static const char* const GraphMeterMode_dotsAscii[] = {
static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
const Machine* host = this->host;
-
- if (!this->drawData) {
- this->drawData = xCalloc(1, sizeof(GraphData));
+ GraphData* data = &this->drawData;
+
+ assert(w > 0);
+ if ((size_t)w * 2 > data->nValues) {
+ size_t oldNValues = data->nValues;
+ data->nValues = MAXIMUM(oldNValues + (oldNValues / 2), (unsigned int)w * 2);
+ data->values = xReallocArray(data->values, data->nValues, sizeof(*data->values));
+ memmove(data->values + (data->nValues - oldNValues), data->values, oldNValues * sizeof(*data->values));
+ memset(data->values, 0, (data->nValues - oldNValues) * sizeof(*data->values));
}
- GraphData* data = this->drawData;
- const int nValues = METER_GRAPHDATA_SIZE;
+ const int nValues = data->nValues;
const char* const* GraphMeterMode_dots;
int GraphMeterMode_pixPerRow;
diff --git a/Meter.h b/Meter.h
index 89f0570a..c0a44338 100644
--- a/Meter.h
+++ b/Meter.h
@@ -20,7 +20,6 @@ in the source distribution for its full text.
#define METER_TXTBUFFER_LEN 256
-#define METER_GRAPHDATA_SIZE 256
#define METER_BUFFER_CHECK(buffer, size, written) \
do { \
@@ -97,7 +96,8 @@ typedef struct MeterClass_ {
typedef struct GraphData_ {
struct timeval time;
- double values[METER_GRAPHDATA_SIZE];
+ size_t nValues;
+ double* values;
} GraphData;
struct Meter_ {
@@ -108,7 +108,7 @@ struct Meter_ {
char* caption;
int mode;
unsigned int param;
- GraphData* drawData;
+ GraphData drawData;
int h;
int columnWidthCount; /**< only used internally by the Header */
uint8_t curItems;

© 2014-2024 Faster IT GmbH | imprint | privacy policy