mirror of https://github.com/xzeldon/htop.git
Mark Object classes and Object class fields const
This commit is contained in:
parent
164051354f
commit
08d85e6143
|
@ -146,7 +146,7 @@ static void CPUMeterCommonInit(Meter *this, int ncol) {
|
|||
AllCPUsMeter_getRange(this, &start, &count);
|
||||
for (int i = 0; i < count; 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]);
|
||||
}
|
||||
if (this->mode == 0)
|
||||
|
|
|
@ -14,14 +14,14 @@ typedef void(*InfoScreen_OnErr)(InfoScreen*);
|
|||
typedef bool(*InfoScreen_OnKey)(InfoScreen*, int);
|
||||
|
||||
typedef struct InfoScreenClass_ {
|
||||
ObjectClass super;
|
||||
const ObjectClass super;
|
||||
const InfoScreen_Scan scan;
|
||||
const InfoScreen_Draw draw;
|
||||
const InfoScreen_OnErr onErr;
|
||||
const InfoScreen_OnKey onKey;
|
||||
} 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_draw(this_) As_InfoScreen(this_)->draw((InfoScreen*)(this_))
|
||||
#define InfoScreen_onErr(this_) As_InfoScreen(this_)->onErr((InfoScreen*)(this_))
|
||||
|
|
8
Meter.c
8
Meter.c
|
@ -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));
|
||||
Object_setClass(this, type);
|
||||
this->h = 1;
|
||||
this->param = param;
|
||||
this->pl = pl;
|
||||
type->curItems = type->maxItems;
|
||||
this->curItems = type->maxItems;
|
||||
this->values = xCalloc(type->maxItems, sizeof(double));
|
||||
this->total = type->total;
|
||||
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...
|
||||
int offset = 0;
|
||||
int items = Meter_getItems(this);
|
||||
int items = this->curItems;
|
||||
for (int i = 0; i < items; i++) {
|
||||
double value = this->values[i];
|
||||
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);
|
||||
|
||||
double value = 0.0;
|
||||
int items = Meter_getItems(this);
|
||||
int items = this->curItems;
|
||||
for (int i = 0; i < items; i++)
|
||||
value += this->values[i];
|
||||
value /= this->total;
|
||||
|
|
20
Meter.h
20
Meter.h
|
@ -22,7 +22,7 @@ typedef void(*Meter_UpdateValues)(Meter*, char*, int);
|
|||
typedef void(*Meter_Draw)(Meter*, int, int, int);
|
||||
|
||||
typedef struct MeterClass_ {
|
||||
ObjectClass super;
|
||||
const ObjectClass super;
|
||||
const Meter_Init init;
|
||||
const Meter_Done done;
|
||||
const Meter_UpdateMode updateMode;
|
||||
|
@ -30,16 +30,15 @@ typedef struct MeterClass_ {
|
|||
const Meter_UpdateValues updateValues;
|
||||
const int defaultMode;
|
||||
const double total;
|
||||
const int* attributes;
|
||||
const char* name;
|
||||
const char* uiName;
|
||||
const char* caption;
|
||||
const char* description;
|
||||
const int* const attributes;
|
||||
const char* const name;
|
||||
const char* const uiName;
|
||||
const char* const caption;
|
||||
const char* const description;
|
||||
const char maxItems;
|
||||
char curItems;
|
||||
} 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_init(this_) As_Meter(this_)->init((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_) \
|
||||
As_Meter(this_)->updateValues((Meter*)(this_), buf_, sz_)
|
||||
#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_name(this_) As_Meter(this_)->name
|
||||
#define Meter_uiName(this_) As_Meter(this_)->uiName
|
||||
|
@ -66,6 +63,7 @@ struct Meter_ {
|
|||
void* drawData;
|
||||
int h;
|
||||
struct ProcessList_* pl;
|
||||
char curItems;
|
||||
double* values;
|
||||
double total;
|
||||
};
|
||||
|
@ -92,7 +90,7 @@ typedef struct GraphData_ {
|
|||
|
||||
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);
|
||||
|
||||
|
|
2
Object.c
2
Object.c
|
@ -8,7 +8,7 @@ in the source distribution for its full text.
|
|||
|
||||
#include "Object.h"
|
||||
|
||||
ObjectClass Object_class = {
|
||||
const ObjectClass Object_class = {
|
||||
.extends = NULL
|
||||
};
|
||||
|
||||
|
|
10
Object.h
10
Object.h
|
@ -19,26 +19,26 @@ typedef long(*Object_Compare)(const void*, const void*);
|
|||
typedef void(*Object_Delete)(Object*);
|
||||
|
||||
#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_displayFn(obj_) Object_getClass(obj_)->display
|
||||
#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 Class(class_) ((ObjectClass*)(&(class_ ## _class)))
|
||||
#define Class(class_) ((const ObjectClass*)(&(class_ ## _class)))
|
||||
|
||||
#define AllocThis(class_) (class_*) xMalloc(sizeof(class_)); Object_setClass(this, Class(class_));
|
||||
|
||||
typedef struct ObjectClass_ {
|
||||
const void* extends;
|
||||
const void* const extends;
|
||||
const Object_Display display;
|
||||
const Object_Delete delete;
|
||||
const Object_Compare compare;
|
||||
} ObjectClass;
|
||||
|
||||
struct Object_ {
|
||||
ObjectClass* klass;
|
||||
const ObjectClass* klass;
|
||||
};
|
||||
|
||||
typedef union {
|
||||
|
@ -46,7 +46,7 @@ typedef union {
|
|||
void* v;
|
||||
} Arg;
|
||||
|
||||
extern ObjectClass Object_class;
|
||||
extern const ObjectClass Object_class;
|
||||
|
||||
#ifndef NDEBUG
|
||||
|
||||
|
|
4
Panel.c
4
Panel.c
|
@ -27,7 +27,7 @@ PanelClass Panel_class = {
|
|||
.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;
|
||||
this = xMalloc(sizeof(Panel));
|
||||
Object_setClass(this, Class(Panel));
|
||||
|
@ -41,7 +41,7 @@ void Panel_delete(Object* cast) {
|
|||
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->y = y;
|
||||
this->w = w;
|
||||
|
|
6
Panel.h
6
Panel.h
|
@ -35,7 +35,7 @@ typedef struct PanelClass_ {
|
|||
const Panel_EventHandler eventHandler;
|
||||
} 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_eventHandler(this_, ev_) As_Panel(this_)->eventHandler((Panel*)(this_), ev_)
|
||||
|
||||
|
@ -62,11 +62,11 @@ struct Panel_ {
|
|||
|
||||
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_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);
|
||||
|
||||
|
|
|
@ -129,7 +129,7 @@ typedef struct ProcessClass_ {
|
|||
const Process_WriteField writeField;
|
||||
} 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)
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@ in the source distribution for its full text.
|
|||
#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->processTable = Hashtable_new(140, false);
|
||||
this->usersTable = usersTable;
|
||||
|
|
|
@ -69,7 +69,7 @@ void ProcessList_delete(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);
|
||||
|
||||
|
|
2
Vector.c
2
Vector.c
|
@ -13,7 +13,7 @@ in the source distribution for its full text.
|
|||
#include <stdbool.h>
|
||||
|
||||
|
||||
Vector* Vector_new(ObjectClass* type, bool owner, int size) {
|
||||
Vector* Vector_new(const ObjectClass* type, bool owner, int size) {
|
||||
Vector* this;
|
||||
|
||||
if (size == DEFAULT_SIZE)
|
||||
|
|
4
Vector.h
4
Vector.h
|
@ -17,14 +17,14 @@ in the source distribution for its full text.
|
|||
|
||||
typedef struct Vector_ {
|
||||
Object **array;
|
||||
ObjectClass* type;
|
||||
const ObjectClass* type;
|
||||
int arraySize;
|
||||
int growthRate;
|
||||
int items;
|
||||
bool owner;
|
||||
} 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);
|
||||
|
||||
|
|
|
@ -216,7 +216,7 @@ double Platform_setCPUValues(Meter* mtr, int cpu) {
|
|||
mtr->values[CPU_METER_KERNEL]
|
||||
= ((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 */
|
||||
total = mtr->values[CPU_METER_NICE] + mtr->values[CPU_METER_NORMAL] + mtr->values[CPU_METER_KERNEL];
|
||||
|
|
|
@ -167,11 +167,11 @@ double Platform_setCPUValues(Meter* this, int cpu) {
|
|||
if (this->pl->settings->detailedCPUTime) {
|
||||
v[CPU_METER_KERNEL] = cpuData->systemPercent;
|
||||
v[CPU_METER_IRQ] = cpuData->irqPercent;
|
||||
Meter_setItems(this, 4);
|
||||
this->curItems = 4;
|
||||
percent = v[0]+v[1]+v[2]+v[3];
|
||||
} else {
|
||||
v[2] = cpuData->systemAllPercent;
|
||||
Meter_setItems(this, 3);
|
||||
this->curItems = 3;
|
||||
percent = v[0]+v[1]+v[2];
|
||||
}
|
||||
|
||||
|
|
|
@ -170,11 +170,11 @@ double Platform_setCPUValues(Meter* this, int cpu) {
|
|||
if (this->pl->settings->detailedCPUTime) {
|
||||
v[CPU_METER_KERNEL] = cpuData->systemPercent;
|
||||
v[CPU_METER_IRQ] = cpuData->irqPercent;
|
||||
Meter_setItems(this, 4);
|
||||
this->curItems = 4;
|
||||
percent = v[0]+v[1]+v[2]+v[3];
|
||||
} else {
|
||||
v[2] = cpuData->systemAllPercent;
|
||||
Meter_setItems(this, 3);
|
||||
this->curItems = 3;
|
||||
percent = v[0]+v[1]+v[2];
|
||||
}
|
||||
|
||||
|
|
|
@ -191,7 +191,7 @@ double Platform_setCPUValues(Meter* this, int cpu) {
|
|||
v[CPU_METER_STEAL] = cpuData->stealPeriod / total * 100.0;
|
||||
v[CPU_METER_GUEST] = cpuData->guestPeriod / total * 100.0;
|
||||
v[CPU_METER_IOWAIT] = cpuData->ioWaitPeriod / total * 100.0;
|
||||
Meter_setItems(this, 8);
|
||||
this->curItems = 8;
|
||||
if (this->pl->settings->accountGuestInCPUMeter) {
|
||||
percent = v[0]+v[1]+v[2]+v[3]+v[4]+v[5]+v[6];
|
||||
} else {
|
||||
|
@ -200,7 +200,7 @@ double Platform_setCPUValues(Meter* this, int cpu) {
|
|||
} else {
|
||||
v[2] = cpuData->systemAllPeriod / 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 = CLAMP(percent, 0.0, 100.0);
|
||||
|
|
|
@ -173,13 +173,13 @@ double Platform_setCPUValues(Meter* this, int cpu) {
|
|||
v[CPU_METER_GUEST] = 0.0;
|
||||
v[CPU_METER_IOWAIT] = 0.0;
|
||||
v[CPU_METER_FREQUENCY] = NAN;
|
||||
Meter_setItems(this, 8);
|
||||
this->curItems = 8;
|
||||
totalPercent = v[0]+v[1]+v[2]+v[3];
|
||||
} else {
|
||||
v[2] = cpuData->sysAllPeriod / total * 100.0;
|
||||
v[3] = 0.0; // No steal nor guest on OpenBSD
|
||||
totalPercent = v[0]+v[1]+v[2];
|
||||
Meter_setItems(this, 4);
|
||||
this->curItems = 4;
|
||||
}
|
||||
|
||||
totalPercent = CLAMP(totalPercent, 0.0, 100.0);
|
||||
|
|
|
@ -185,11 +185,11 @@ double Platform_setCPUValues(Meter* this, int cpu) {
|
|||
if (this->pl->settings->detailedCPUTime) {
|
||||
v[CPU_METER_KERNEL] = cpuData->systemPercent;
|
||||
v[CPU_METER_IRQ] = cpuData->irqPercent;
|
||||
Meter_setItems(this, 4);
|
||||
this->curItems = 4;
|
||||
percent = v[0]+v[1]+v[2]+v[3];
|
||||
} else {
|
||||
v[2] = cpuData->systemAllPercent;
|
||||
Meter_setItems(this, 3);
|
||||
this->curItems = 3;
|
||||
percent = v[0]+v[1]+v[2];
|
||||
}
|
||||
|
||||
|
|
|
@ -38,7 +38,7 @@ void ZfsArcMeter_readStats(Meter* this, ZfsArcStats* stats) {
|
|||
// "Hide" the last value so it can
|
||||
// only be accessed by index and is not
|
||||
// displayed by the Bar or Graph style
|
||||
Meter_setItems(this, 5);
|
||||
this->curItems = 5;
|
||||
this->values[5] = stats->size;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue