Mark Object classes and Object class fields const

This commit is contained in:
Christian Göttsche 2020-10-04 17:55:08 +02:00 committed by cgzones
parent 164051354f
commit 08d85e6143
20 changed files with 45 additions and 47 deletions

View File

@ -146,7 +146,7 @@ static void CPUMeterCommonInit(Meter *this, int ncol) {
AllCPUsMeter_getRange(this, &start, &count); AllCPUsMeter_getRange(this, &start, &count);
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
if (!meters[i]) if (!meters[i])
meters[i] = Meter_new(this->pl, start+i+1, (MeterClass*) Class(CPUMeter)); meters[i] = Meter_new(this->pl, start+i+1, (const MeterClass*) Class(CPUMeter));
Meter_init(meters[i]); Meter_init(meters[i]);
} }
if (this->mode == 0) if (this->mode == 0)

View File

@ -14,14 +14,14 @@ typedef void(*InfoScreen_OnErr)(InfoScreen*);
typedef bool(*InfoScreen_OnKey)(InfoScreen*, int); typedef bool(*InfoScreen_OnKey)(InfoScreen*, int);
typedef struct InfoScreenClass_ { typedef struct InfoScreenClass_ {
ObjectClass super; const ObjectClass super;
const InfoScreen_Scan scan; const InfoScreen_Scan scan;
const InfoScreen_Draw draw; const InfoScreen_Draw draw;
const InfoScreen_OnErr onErr; const InfoScreen_OnErr onErr;
const InfoScreen_OnKey onKey; const InfoScreen_OnKey onKey;
} InfoScreenClass; } InfoScreenClass;
#define As_InfoScreen(this_) ((InfoScreenClass*)(((InfoScreen*)(this_))->super.klass)) #define As_InfoScreen(this_) ((const InfoScreenClass*)(((InfoScreen*)(this_))->super.klass))
#define InfoScreen_scan(this_) As_InfoScreen(this_)->scan((InfoScreen*)(this_)) #define InfoScreen_scan(this_) As_InfoScreen(this_)->scan((InfoScreen*)(this_))
#define InfoScreen_draw(this_) As_InfoScreen(this_)->draw((InfoScreen*)(this_)) #define InfoScreen_draw(this_) As_InfoScreen(this_)->draw((InfoScreen*)(this_))
#define InfoScreen_onErr(this_) As_InfoScreen(this_)->onErr((InfoScreen*)(this_)) #define InfoScreen_onErr(this_) As_InfoScreen(this_)->onErr((InfoScreen*)(this_))

View File

@ -27,13 +27,13 @@ MeterClass Meter_class = {
} }
}; };
Meter* Meter_new(struct ProcessList_* pl, int param, MeterClass* type) { Meter* Meter_new(struct ProcessList_* pl, int param, const MeterClass* type) {
Meter* this = xCalloc(1, sizeof(Meter)); Meter* this = xCalloc(1, sizeof(Meter));
Object_setClass(this, type); Object_setClass(this, type);
this->h = 1; this->h = 1;
this->param = param; this->param = param;
this->pl = pl; this->pl = pl;
type->curItems = type->maxItems; this->curItems = type->maxItems;
this->values = xCalloc(type->maxItems, sizeof(double)); this->values = xCalloc(type->maxItems, sizeof(double));
this->total = type->total; this->total = type->total;
this->caption = xStrdup(type->caption); this->caption = xStrdup(type->caption);
@ -191,7 +191,7 @@ static void BarMeterMode_draw(Meter* this, int x, int y, int w) {
// First draw in the bar[] buffer... // First draw in the bar[] buffer...
int offset = 0; int offset = 0;
int items = Meter_getItems(this); int items = this->curItems;
for (int i = 0; i < items; i++) { for (int i = 0; i < items; i++) {
double value = this->values[i]; double value = this->values[i];
value = CLAMP(value, 0.0, this->total); value = CLAMP(value, 0.0, this->total);
@ -292,7 +292,7 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
Meter_updateValues(this, buffer, nValues - 1); Meter_updateValues(this, buffer, nValues - 1);
double value = 0.0; double value = 0.0;
int items = Meter_getItems(this); int items = this->curItems;
for (int i = 0; i < items; i++) for (int i = 0; i < items; i++)
value += this->values[i]; value += this->values[i];
value /= this->total; value /= this->total;

20
Meter.h
View File

@ -22,7 +22,7 @@ typedef void(*Meter_UpdateValues)(Meter*, char*, int);
typedef void(*Meter_Draw)(Meter*, int, int, int); typedef void(*Meter_Draw)(Meter*, int, int, int);
typedef struct MeterClass_ { typedef struct MeterClass_ {
ObjectClass super; const ObjectClass super;
const Meter_Init init; const Meter_Init init;
const Meter_Done done; const Meter_Done done;
const Meter_UpdateMode updateMode; const Meter_UpdateMode updateMode;
@ -30,16 +30,15 @@ typedef struct MeterClass_ {
const Meter_UpdateValues updateValues; const Meter_UpdateValues updateValues;
const int defaultMode; const int defaultMode;
const double total; const double total;
const int* attributes; const int* const attributes;
const char* name; const char* const name;
const char* uiName; const char* const uiName;
const char* caption; const char* const caption;
const char* description; const char* const description;
const char maxItems; const char maxItems;
char curItems;
} MeterClass; } MeterClass;
#define As_Meter(this_) ((MeterClass*)((this_)->super.klass)) #define As_Meter(this_) ((const MeterClass*)((this_)->super.klass))
#define Meter_initFn(this_) As_Meter(this_)->init #define Meter_initFn(this_) As_Meter(this_)->init
#define Meter_init(this_) As_Meter(this_)->init((Meter*)(this_)) #define Meter_init(this_) As_Meter(this_)->init((Meter*)(this_))
#define Meter_done(this_) As_Meter(this_)->done((Meter*)(this_)) #define Meter_done(this_) As_Meter(this_)->done((Meter*)(this_))
@ -50,8 +49,6 @@ typedef struct MeterClass_ {
#define Meter_updateValues(this_, buf_, sz_) \ #define Meter_updateValues(this_, buf_, sz_) \
As_Meter(this_)->updateValues((Meter*)(this_), buf_, sz_) As_Meter(this_)->updateValues((Meter*)(this_), buf_, sz_)
#define Meter_defaultMode(this_) As_Meter(this_)->defaultMode #define Meter_defaultMode(this_) As_Meter(this_)->defaultMode
#define Meter_getItems(this_) As_Meter(this_)->curItems
#define Meter_setItems(this_, n_) As_Meter(this_)->curItems = (n_)
#define Meter_attributes(this_) As_Meter(this_)->attributes #define Meter_attributes(this_) As_Meter(this_)->attributes
#define Meter_name(this_) As_Meter(this_)->name #define Meter_name(this_) As_Meter(this_)->name
#define Meter_uiName(this_) As_Meter(this_)->uiName #define Meter_uiName(this_) As_Meter(this_)->uiName
@ -66,6 +63,7 @@ struct Meter_ {
void* drawData; void* drawData;
int h; int h;
struct ProcessList_* pl; struct ProcessList_* pl;
char curItems;
double* values; double* values;
double total; double total;
}; };
@ -92,7 +90,7 @@ typedef struct GraphData_ {
extern MeterClass Meter_class; extern MeterClass Meter_class;
Meter* Meter_new(struct ProcessList_* pl, int param, MeterClass* type); Meter* Meter_new(struct ProcessList_* pl, int param, const MeterClass* type);
int Meter_humanUnit(char* buffer, unsigned long int value, int size); int Meter_humanUnit(char* buffer, unsigned long int value, int size);

View File

@ -8,7 +8,7 @@ in the source distribution for its full text.
#include "Object.h" #include "Object.h"
ObjectClass Object_class = { const ObjectClass Object_class = {
.extends = NULL .extends = NULL
}; };

View File

@ -19,26 +19,26 @@ typedef long(*Object_Compare)(const void*, const void*);
typedef void(*Object_Delete)(Object*); typedef void(*Object_Delete)(Object*);
#define Object_getClass(obj_) ((Object*)(obj_))->klass #define Object_getClass(obj_) ((Object*)(obj_))->klass
#define Object_setClass(obj_, class_) Object_getClass(obj_) = (ObjectClass*) class_ #define Object_setClass(obj_, class_) Object_getClass(obj_) = (const ObjectClass*) class_
#define Object_delete(obj_) Object_getClass(obj_)->delete((Object*)(obj_)) #define Object_delete(obj_) Object_getClass(obj_)->delete((Object*)(obj_))
#define Object_displayFn(obj_) Object_getClass(obj_)->display #define Object_displayFn(obj_) Object_getClass(obj_)->display
#define Object_display(obj_, str_) Object_getClass(obj_)->display((Object*)(obj_), str_) #define Object_display(obj_, str_) Object_getClass(obj_)->display((Object*)(obj_), str_)
#define Object_compare(obj_, other_) Object_getClass(obj_)->compare((const void*)(obj_), other_) #define Object_compare(obj_, other_) Object_getClass(obj_)->compare((const void*)(obj_), other_)
#define Class(class_) ((ObjectClass*)(&(class_ ## _class))) #define Class(class_) ((const ObjectClass*)(&(class_ ## _class)))
#define AllocThis(class_) (class_*) xMalloc(sizeof(class_)); Object_setClass(this, Class(class_)); #define AllocThis(class_) (class_*) xMalloc(sizeof(class_)); Object_setClass(this, Class(class_));
typedef struct ObjectClass_ { typedef struct ObjectClass_ {
const void* extends; const void* const extends;
const Object_Display display; const Object_Display display;
const Object_Delete delete; const Object_Delete delete;
const Object_Compare compare; const Object_Compare compare;
} ObjectClass; } ObjectClass;
struct Object_ { struct Object_ {
ObjectClass* klass; const ObjectClass* klass;
}; };
typedef union { typedef union {
@ -46,7 +46,7 @@ typedef union {
void* v; void* v;
} Arg; } Arg;
extern ObjectClass Object_class; extern const ObjectClass Object_class;
#ifndef NDEBUG #ifndef NDEBUG

View File

@ -27,7 +27,7 @@ PanelClass Panel_class = {
.eventHandler = Panel_selectByTyping, .eventHandler = Panel_selectByTyping,
}; };
Panel* Panel_new(int x, int y, int w, int h, bool owner, ObjectClass* type, FunctionBar* fuBar) { Panel* Panel_new(int x, int y, int w, int h, bool owner, const ObjectClass* type, FunctionBar* fuBar) {
Panel* this; Panel* this;
this = xMalloc(sizeof(Panel)); this = xMalloc(sizeof(Panel));
Object_setClass(this, Class(Panel)); Object_setClass(this, Class(Panel));
@ -41,7 +41,7 @@ void Panel_delete(Object* cast) {
free(this); free(this);
} }
void Panel_init(Panel* this, int x, int y, int w, int h, ObjectClass* type, bool owner, FunctionBar* fuBar) { void Panel_init(Panel* this, int x, int y, int w, int h, const ObjectClass* type, bool owner, FunctionBar* fuBar) {
this->x = x; this->x = x;
this->y = y; this->y = y;
this->w = w; this->w = w;

View File

@ -35,7 +35,7 @@ typedef struct PanelClass_ {
const Panel_EventHandler eventHandler; const Panel_EventHandler eventHandler;
} PanelClass; } PanelClass;
#define As_Panel(this_) ((PanelClass*)((this_)->super.klass)) #define As_Panel(this_) ((const PanelClass*)((this_)->super.klass))
#define Panel_eventHandlerFn(this_) As_Panel(this_)->eventHandler #define Panel_eventHandlerFn(this_) As_Panel(this_)->eventHandler
#define Panel_eventHandler(this_, ev_) As_Panel(this_)->eventHandler((Panel*)(this_), ev_) #define Panel_eventHandler(this_, ev_) As_Panel(this_)->eventHandler((Panel*)(this_), ev_)
@ -62,11 +62,11 @@ struct Panel_ {
extern PanelClass Panel_class; extern PanelClass Panel_class;
Panel* Panel_new(int x, int y, int w, int h, bool owner, ObjectClass* type, FunctionBar* fuBar); Panel* Panel_new(int x, int y, int w, int h, bool owner, const ObjectClass* type, FunctionBar* fuBar);
void Panel_delete(Object* cast); void Panel_delete(Object* cast);
void Panel_init(Panel* this, int x, int y, int w, int h, ObjectClass* type, bool owner, FunctionBar* fuBar); void Panel_init(Panel* this, int x, int y, int w, int h, const ObjectClass* type, bool owner, FunctionBar* fuBar);
void Panel_done(Panel* this); void Panel_done(Panel* this);

View File

@ -129,7 +129,7 @@ typedef struct ProcessClass_ {
const Process_WriteField writeField; const Process_WriteField writeField;
} ProcessClass; } ProcessClass;
#define As_Process(this_) ((ProcessClass*)((this_)->super.klass)) #define As_Process(this_) ((const ProcessClass*)((this_)->super.klass))
#define Process_getParentPid(process_) (process_->tgid == process_->pid ? process_->ppid : process_->tgid) #define Process_getParentPid(process_) (process_->tgid == process_->pid ? process_->ppid : process_->tgid)

View File

@ -15,7 +15,7 @@ in the source distribution for its full text.
#include <string.h> #include <string.h>
ProcessList* ProcessList_init(ProcessList* this, ObjectClass* klass, UsersTable* usersTable, Hashtable* pidMatchList, uid_t userId) { ProcessList* ProcessList_init(ProcessList* this, const ObjectClass* klass, UsersTable* usersTable, Hashtable* pidMatchList, uid_t userId) {
this->processes = Vector_new(klass, true, DEFAULT_SIZE); this->processes = Vector_new(klass, true, DEFAULT_SIZE);
this->processTable = Hashtable_new(140, false); this->processTable = Hashtable_new(140, false);
this->usersTable = usersTable; this->usersTable = usersTable;

View File

@ -69,7 +69,7 @@ void ProcessList_delete(ProcessList* pl);
void ProcessList_goThroughEntries(ProcessList* pl); void ProcessList_goThroughEntries(ProcessList* pl);
ProcessList* ProcessList_init(ProcessList* this, ObjectClass* klass, UsersTable* usersTable, Hashtable* pidMatchList, uid_t userId); ProcessList* ProcessList_init(ProcessList* this, const ObjectClass* klass, UsersTable* usersTable, Hashtable* pidMatchList, uid_t userId);
void ProcessList_done(ProcessList* this); void ProcessList_done(ProcessList* this);

View File

@ -13,7 +13,7 @@ in the source distribution for its full text.
#include <stdbool.h> #include <stdbool.h>
Vector* Vector_new(ObjectClass* type, bool owner, int size) { Vector* Vector_new(const ObjectClass* type, bool owner, int size) {
Vector* this; Vector* this;
if (size == DEFAULT_SIZE) if (size == DEFAULT_SIZE)

View File

@ -17,14 +17,14 @@ in the source distribution for its full text.
typedef struct Vector_ { typedef struct Vector_ {
Object **array; Object **array;
ObjectClass* type; const ObjectClass* type;
int arraySize; int arraySize;
int growthRate; int growthRate;
int items; int items;
bool owner; bool owner;
} Vector; } Vector;
Vector* Vector_new(ObjectClass* type, bool owner, int size); Vector* Vector_new(const ObjectClass* type, bool owner, int size);
void Vector_delete(Vector* this); void Vector_delete(Vector* this);

View File

@ -216,7 +216,7 @@ double Platform_setCPUValues(Meter* mtr, int cpu) {
mtr->values[CPU_METER_KERNEL] mtr->values[CPU_METER_KERNEL]
= ((double)curr->cpu_ticks[CPU_STATE_SYSTEM] - (double)prev->cpu_ticks[CPU_STATE_SYSTEM])* 100.0 / total; = ((double)curr->cpu_ticks[CPU_STATE_SYSTEM] - (double)prev->cpu_ticks[CPU_STATE_SYSTEM])* 100.0 / total;
Meter_setItems(mtr, 3); mtr->curItems = 3;
/* Convert to percent and return */ /* Convert to percent and return */
total = mtr->values[CPU_METER_NICE] + mtr->values[CPU_METER_NORMAL] + mtr->values[CPU_METER_KERNEL]; total = mtr->values[CPU_METER_NICE] + mtr->values[CPU_METER_NORMAL] + mtr->values[CPU_METER_KERNEL];

View File

@ -167,11 +167,11 @@ double Platform_setCPUValues(Meter* this, int cpu) {
if (this->pl->settings->detailedCPUTime) { if (this->pl->settings->detailedCPUTime) {
v[CPU_METER_KERNEL] = cpuData->systemPercent; v[CPU_METER_KERNEL] = cpuData->systemPercent;
v[CPU_METER_IRQ] = cpuData->irqPercent; v[CPU_METER_IRQ] = cpuData->irqPercent;
Meter_setItems(this, 4); this->curItems = 4;
percent = v[0]+v[1]+v[2]+v[3]; percent = v[0]+v[1]+v[2]+v[3];
} else { } else {
v[2] = cpuData->systemAllPercent; v[2] = cpuData->systemAllPercent;
Meter_setItems(this, 3); this->curItems = 3;
percent = v[0]+v[1]+v[2]; percent = v[0]+v[1]+v[2];
} }

View File

@ -170,11 +170,11 @@ double Platform_setCPUValues(Meter* this, int cpu) {
if (this->pl->settings->detailedCPUTime) { if (this->pl->settings->detailedCPUTime) {
v[CPU_METER_KERNEL] = cpuData->systemPercent; v[CPU_METER_KERNEL] = cpuData->systemPercent;
v[CPU_METER_IRQ] = cpuData->irqPercent; v[CPU_METER_IRQ] = cpuData->irqPercent;
Meter_setItems(this, 4); this->curItems = 4;
percent = v[0]+v[1]+v[2]+v[3]; percent = v[0]+v[1]+v[2]+v[3];
} else { } else {
v[2] = cpuData->systemAllPercent; v[2] = cpuData->systemAllPercent;
Meter_setItems(this, 3); this->curItems = 3;
percent = v[0]+v[1]+v[2]; percent = v[0]+v[1]+v[2];
} }

View File

@ -191,7 +191,7 @@ double Platform_setCPUValues(Meter* this, int cpu) {
v[CPU_METER_STEAL] = cpuData->stealPeriod / total * 100.0; v[CPU_METER_STEAL] = cpuData->stealPeriod / total * 100.0;
v[CPU_METER_GUEST] = cpuData->guestPeriod / total * 100.0; v[CPU_METER_GUEST] = cpuData->guestPeriod / total * 100.0;
v[CPU_METER_IOWAIT] = cpuData->ioWaitPeriod / total * 100.0; v[CPU_METER_IOWAIT] = cpuData->ioWaitPeriod / total * 100.0;
Meter_setItems(this, 8); this->curItems = 8;
if (this->pl->settings->accountGuestInCPUMeter) { if (this->pl->settings->accountGuestInCPUMeter) {
percent = v[0]+v[1]+v[2]+v[3]+v[4]+v[5]+v[6]; percent = v[0]+v[1]+v[2]+v[3]+v[4]+v[5]+v[6];
} else { } else {
@ -200,7 +200,7 @@ double Platform_setCPUValues(Meter* this, int cpu) {
} else { } else {
v[2] = cpuData->systemAllPeriod / total * 100.0; v[2] = cpuData->systemAllPeriod / total * 100.0;
v[3] = (cpuData->stealPeriod + cpuData->guestPeriod) / total * 100.0; v[3] = (cpuData->stealPeriod + cpuData->guestPeriod) / total * 100.0;
Meter_setItems(this, 4); this->curItems = 4;
percent = v[0]+v[1]+v[2]+v[3]; percent = v[0]+v[1]+v[2]+v[3];
} }
percent = CLAMP(percent, 0.0, 100.0); percent = CLAMP(percent, 0.0, 100.0);

View File

@ -173,13 +173,13 @@ double Platform_setCPUValues(Meter* this, int cpu) {
v[CPU_METER_GUEST] = 0.0; v[CPU_METER_GUEST] = 0.0;
v[CPU_METER_IOWAIT] = 0.0; v[CPU_METER_IOWAIT] = 0.0;
v[CPU_METER_FREQUENCY] = NAN; v[CPU_METER_FREQUENCY] = NAN;
Meter_setItems(this, 8); this->curItems = 8;
totalPercent = v[0]+v[1]+v[2]+v[3]; totalPercent = v[0]+v[1]+v[2]+v[3];
} else { } else {
v[2] = cpuData->sysAllPeriod / total * 100.0; v[2] = cpuData->sysAllPeriod / total * 100.0;
v[3] = 0.0; // No steal nor guest on OpenBSD v[3] = 0.0; // No steal nor guest on OpenBSD
totalPercent = v[0]+v[1]+v[2]; totalPercent = v[0]+v[1]+v[2];
Meter_setItems(this, 4); this->curItems = 4;
} }
totalPercent = CLAMP(totalPercent, 0.0, 100.0); totalPercent = CLAMP(totalPercent, 0.0, 100.0);

View File

@ -185,11 +185,11 @@ double Platform_setCPUValues(Meter* this, int cpu) {
if (this->pl->settings->detailedCPUTime) { if (this->pl->settings->detailedCPUTime) {
v[CPU_METER_KERNEL] = cpuData->systemPercent; v[CPU_METER_KERNEL] = cpuData->systemPercent;
v[CPU_METER_IRQ] = cpuData->irqPercent; v[CPU_METER_IRQ] = cpuData->irqPercent;
Meter_setItems(this, 4); this->curItems = 4;
percent = v[0]+v[1]+v[2]+v[3]; percent = v[0]+v[1]+v[2]+v[3];
} else { } else {
v[2] = cpuData->systemAllPercent; v[2] = cpuData->systemAllPercent;
Meter_setItems(this, 3); this->curItems = 3;
percent = v[0]+v[1]+v[2]; percent = v[0]+v[1]+v[2];
} }

View File

@ -38,7 +38,7 @@ void ZfsArcMeter_readStats(Meter* this, ZfsArcStats* stats) {
// "Hide" the last value so it can // "Hide" the last value so it can
// only be accessed by index and is not // only be accessed by index and is not
// displayed by the Bar or Graph style // displayed by the Bar or Graph style
Meter_setItems(this, 5); this->curItems = 5;
this->values[5] = stats->size; this->values[5] = stats->size;
} }