Skip to content

Commit

Permalink
Define MeterModeId to unsigned int and use it throughout
Browse files Browse the repository at this point in the history
All uses of meter drawing "mode" numbers now have the type
`MeterModeId`, including the uses in structures and arrays.
`MeterModeId` is defined as `unsigned int`, as (currently) it doesn't
save any code size by defining it to any smaller type.

Signed-off-by: Kang-Che Sung <[email protected]>
  • Loading branch information
Explorer09 committed Apr 18, 2024
1 parent 9ae790e commit bea676b
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 21 deletions.
10 changes: 5 additions & 5 deletions CPUMeter.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ static void CPUMeterCommonInit(Meter* this) {
}
}

static void CPUMeterCommonUpdateMode(Meter* this, int mode, int ncol) {
static void CPUMeterCommonUpdateMode(Meter* this, MeterModeId mode, int ncol) {
CPUMeterData* data = this->meterData;
Meter** meters = data->meters;
this->mode = mode;
Expand All @@ -278,19 +278,19 @@ static void AllCPUsMeter_done(Meter* this) {
free(data);
}

static void SingleColCPUsMeter_updateMode(Meter* this, int mode) {
static void SingleColCPUsMeter_updateMode(Meter* this, MeterModeId mode) {
CPUMeterCommonUpdateMode(this, mode, 1);
}

static void DualColCPUsMeter_updateMode(Meter* this, int mode) {
static void DualColCPUsMeter_updateMode(Meter* this, MeterModeId mode) {
CPUMeterCommonUpdateMode(this, mode, 2);
}

static void QuadColCPUsMeter_updateMode(Meter* this, int mode) {
static void QuadColCPUsMeter_updateMode(Meter* this, MeterModeId mode) {
CPUMeterCommonUpdateMode(this, mode, 4);
}

static void OctoColCPUsMeter_updateMode(Meter* this, int mode) {
static void OctoColCPUsMeter_updateMode(Meter* this, MeterModeId mode) {
CPUMeterCommonUpdateMode(this, mode, 8);
}

Expand Down
2 changes: 1 addition & 1 deletion Header.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ void Header_writeBackToSettings(const Header* this) {
int len = Vector_size(vec);

colSettings->names = len ? xCalloc(len + 1, sizeof(char*)) : NULL;
colSettings->modes = len ? xCalloc(len, sizeof(int)) : NULL;
colSettings->modes = len ? xCalloc(len, sizeof(MeterModeId)) : NULL;
colSettings->len = len;

for (int i = 0; i < len; i++) {
Expand Down
2 changes: 1 addition & 1 deletion MemorySwapMeter.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ static void MemorySwapMeter_init(Meter* this) {
}
}

static void MemorySwapMeter_updateMode(Meter* this, int mode) {
static void MemorySwapMeter_updateMode(Meter* this, MeterModeId mode) {
MemorySwapMeterData* data = this->meterData;

this->mode = mode;
Expand Down
2 changes: 1 addition & 1 deletion Meter.c
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ void Meter_setCaption(Meter* this, const char* caption) {
free_and_xStrdup(&this->caption, caption);
}

void Meter_setMode(Meter* this, int modeIndex) {
void Meter_setMode(Meter* this, MeterModeId modeIndex) {
if (modeIndex > 0 && modeIndex == this->mode) {
return;
}
Expand Down
8 changes: 4 additions & 4 deletions Meter.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ typedef struct Meter_ Meter;

typedef void(*Meter_Init)(Meter*);
typedef void(*Meter_Done)(Meter*);
typedef void(*Meter_UpdateMode)(Meter*, int);
typedef void(*Meter_UpdateMode)(Meter*, MeterModeId);
typedef void(*Meter_UpdateValues)(Meter*);
typedef void(*Meter_Draw)(Meter*, int, int, int);
typedef const char* (*Meter_GetCaption)(const Meter*);
Expand All @@ -64,7 +64,7 @@ typedef struct MeterClass_ {
const Meter_Draw draw;
const Meter_GetCaption getCaption;
const Meter_GetUiName getUiName;
const int defaultMode;
const MeterModeId defaultMode;
const double total;
const int* const attributes;
const char* const name; /* internal name of the meter, must not contain any space */
Expand Down Expand Up @@ -105,7 +105,7 @@ struct Meter_ {
const Machine* host;

char* caption;
int mode;
MeterModeId mode;
unsigned int param;
GraphData drawData;
int h;
Expand Down Expand Up @@ -137,7 +137,7 @@ void Meter_delete(Object* cast);

void Meter_setCaption(Meter* this, const char* caption);

void Meter_setMode(Meter* this, int modeIndex);
void Meter_setMode(Meter* this, MeterModeId modeIndex);

ListItem* Meter_toListItem(const Meter* this, bool moving);

Expand Down
6 changes: 4 additions & 2 deletions MeterMode.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ in the source distribution for its full text.
*/


typedef enum {
enum MeterModeId_ {
/* Meter mode 0 is reserved */
BAR_METERMODE = 1,
TEXT_METERMODE,
GRAPH_METERMODE,
LED_METERMODE,
LAST_METERMODE
} MeterModeId;
};

typedef unsigned int MeterModeId;

#endif
2 changes: 1 addition & 1 deletion MetersPanel.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ static HandlerResult MetersPanel_eventHandler(Panel* super, int ch) {
if (!Vector_size(this->meters))
break;
Meter* meter = (Meter*) Vector_get(this->meters, selected);
int mode = meter->mode + 1;
MeterModeId mode = meter->mode + 1;
if (mode == LAST_METERMODE)
mode = 1;
Meter_setMode(meter, mode);
Expand Down
10 changes: 5 additions & 5 deletions Settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ static void Settings_readMeterModes(Settings* this, const char* line, unsigned i
}
column = MINIMUM(column, HeaderLayout_getColumns(this->hLayout) - 1);
this->hColumns[column].len = len;
int* modes = len ? xCalloc(len, sizeof(int)) : NULL;
MeterModeId* modes = len ? xCalloc(len, sizeof(MeterModeId)) : NULL;
for (int i = 0; i < len; i++) {
modes[i] = atoi(ids[i]);
modes[i] = (MeterModeId) atoi(ids[i]);
}
String_freeArray(ids);
this->hColumns[column].modes = modes;
Expand All @@ -123,7 +123,7 @@ static bool Settings_validateMeters(Settings* this) {

for (size_t column = 0; column < colCount; column++) {
char** names = this->hColumns[column].names;
const int* modes = this->hColumns[column].modes;
const MeterModeId* modes = this->hColumns[column].modes;
const size_t len = this->hColumns[column].len;

if (!len)
Expand Down Expand Up @@ -164,7 +164,7 @@ static void Settings_defaultMeters(Settings* this, unsigned int initialCpuCount)
this->hColumns = xCalloc(HeaderLayout_getColumns(this->hLayout), sizeof(MeterColumnSetting));
for (size_t i = 0; i < 2; i++) {
this->hColumns[i].names = xCalloc(sizes[i] + 1, sizeof(char*));
this->hColumns[i].modes = xCalloc(sizes[i], sizeof(int));
this->hColumns[i].modes = xCalloc(sizes[i], sizeof(MeterModeId));
this->hColumns[i].len = sizes[i];
}

Expand Down Expand Up @@ -646,7 +646,7 @@ static void writeMeterModes(const Settings* this, OutputFunc of,
if (this->hColumns[column].len) {
const char* sep = "";
for (size_t i = 0; i < this->hColumns[column].len; i++) {
of(fp, "%s%d", sep, this->hColumns[column].modes[i]);
of(fp, "%s%u", sep, this->hColumns[column].modes[i]);
sep = " ";
}
} else {
Expand Down
3 changes: 2 additions & 1 deletion Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ in the source distribution for its full text.

#include "Hashtable.h"
#include "HeaderLayout.h"
#include "MeterMode.h"
#include "Row.h"
#include "RowField.h"

Expand All @@ -34,7 +35,7 @@ typedef struct {
typedef struct {
size_t len;
char** names;
int* modes;
MeterModeId* modes;
} MeterColumnSetting;

typedef struct ScreenSettings_ {
Expand Down

0 comments on commit bea676b

Please sign in to comment.