Merge branch 'xalloc'

This commit is contained in:
Hisham 2016-02-02 15:58:45 +01:00
commit ffcf48fb74
43 changed files with 212 additions and 130 deletions

View File

@ -29,9 +29,9 @@ typedef struct Affinity_ {
}*/ }*/
Affinity* Affinity_new(ProcessList* pl) { Affinity* Affinity_new(ProcessList* pl) {
Affinity* this = calloc(1, sizeof(Affinity)); Affinity* this = xCalloc(1, sizeof(Affinity));
this->size = 8; this->size = 8;
this->cpus = calloc(this->size, sizeof(int)); this->cpus = xCalloc(this->size, sizeof(int));
this->pl = pl; this->pl = pl;
return this; return this;
} }
@ -44,7 +44,7 @@ void Affinity_delete(Affinity* this) {
void Affinity_add(Affinity* this, int id) { void Affinity_add(Affinity* this, int id) {
if (this->used == this->size) { if (this->used == this->size) {
this->size *= 2; this->size *= 2;
this->cpus = realloc(this->cpus, sizeof(int) * this->size); this->cpus = xRealloc(this->cpus, sizeof(int) * this->size);
} }
this->cpus[this->used] = id; this->cpus[this->used] = id;
this->used++; this->used++;

View File

@ -60,7 +60,7 @@ Panel* AffinityPanel_new(ProcessList* pl, Affinity* affinity) {
} else { } else {
mode = false; mode = false;
} }
Panel_add(this, (Object*) CheckItem_newByVal(strdup(number), mode)); Panel_add(this, (Object*) CheckItem_newByVal(xStrdup(number), mode));
} }
return this; return this;
} }

View File

@ -140,7 +140,7 @@ static void AllCPUsMeter_getRange(Meter* this, int* start, int* count) {
static void AllCPUsMeter_init(Meter* this) { static void AllCPUsMeter_init(Meter* this) {
int cpus = this->pl->cpuCount; int cpus = this->pl->cpuCount;
if (!this->drawData) if (!this->drawData)
this->drawData = calloc(cpus, sizeof(Meter*)); this->drawData = xCalloc(cpus, sizeof(Meter*));
Meter** meters = (Meter**) this->drawData; Meter** meters = (Meter**) this->drawData;
int start, count; int start, count;
AllCPUsMeter_getRange(this, &start, &count); AllCPUsMeter_getRange(this, &start, &count);

View File

@ -106,7 +106,7 @@ ColorsPanel* ColorsPanel_new(Settings* settings, ScreenManager* scr) {
Panel_setHeader(super, "Colors"); Panel_setHeader(super, "Colors");
for (int i = 0; ColorSchemeNames[i] != NULL; i++) { for (int i = 0; ColorSchemeNames[i] != NULL; i++) {
Panel_add(super, (Object*) CheckItem_newByVal(strdup(ColorSchemeNames[i]), false)); Panel_add(super, (Object*) CheckItem_newByVal(xStrdup(ColorSchemeNames[i]), false));
} }
CheckItem_set((CheckItem*)Panel_get(super, settings->colorScheme), true); CheckItem_set((CheckItem*)Panel_get(super, settings->colorScheme), true);
return this; return this;

View File

@ -155,7 +155,7 @@ void ColumnsPanel_update(Panel* super) {
ColumnsPanel* this = (ColumnsPanel*) super; ColumnsPanel* this = (ColumnsPanel*) super;
int size = Panel_size(super); int size = Panel_size(super);
this->settings->changed = true; this->settings->changed = true;
this->settings->fields = realloc(this->settings->fields, sizeof(ProcessField) * (size+1)); this->settings->fields = xRealloc(this->settings->fields, sizeof(ProcessField) * (size+1));
this->settings->flags = 0; this->settings->flags = 0;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
int key = ((ListItem*) Panel_get(super, i))->key; int key = ((ListItem*) Panel_get(super, i))->key;

View File

@ -83,19 +83,19 @@ DisplayOptionsPanel* DisplayOptionsPanel_new(Settings* settings, ScreenManager*
this->scr = scr; this->scr = scr;
Panel_setHeader(super, "Display options"); Panel_setHeader(super, "Display options");
Panel_add(super, (Object*) CheckItem_newByRef(strdup("Tree view"), &(settings->treeView))); Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Tree view"), &(settings->treeView)));
Panel_add(super, (Object*) CheckItem_newByRef(strdup("Shadow other users' processes"), &(settings->shadowOtherUsers))); Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Shadow other users' processes"), &(settings->shadowOtherUsers)));
Panel_add(super, (Object*) CheckItem_newByRef(strdup("Hide kernel threads"), &(settings->hideKernelThreads))); Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Hide kernel threads"), &(settings->hideKernelThreads)));
Panel_add(super, (Object*) CheckItem_newByRef(strdup("Hide userland threads"), &(settings->hideUserlandThreads))); Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Hide userland threads"), &(settings->hideUserlandThreads)));
Panel_add(super, (Object*) CheckItem_newByRef(strdup("Display threads in a different color"), &(settings->highlightThreads))); Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Display threads in a different color"), &(settings->highlightThreads)));
Panel_add(super, (Object*) CheckItem_newByRef(strdup("Show custom thread names"), &(settings->showThreadNames))); Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Show custom thread names"), &(settings->showThreadNames)));
Panel_add(super, (Object*) CheckItem_newByRef(strdup("Show program path"), &(settings->showProgramPath))); Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Show program path"), &(settings->showProgramPath)));
Panel_add(super, (Object*) CheckItem_newByRef(strdup("Highlight program \"basename\""), &(settings->highlightBaseName))); Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Highlight program \"basename\""), &(settings->highlightBaseName)));
Panel_add(super, (Object*) CheckItem_newByRef(strdup("Highlight large numbers in memory counters"), &(settings->highlightMegabytes))); Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Highlight large numbers in memory counters"), &(settings->highlightMegabytes)));
Panel_add(super, (Object*) CheckItem_newByRef(strdup("Leave a margin around header"), &(settings->headerMargin))); Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Leave a margin around header"), &(settings->headerMargin)));
Panel_add(super, (Object*) CheckItem_newByRef(strdup("Detailed CPU time (System/IO-Wait/Hard-IRQ/Soft-IRQ/Steal/Guest)"), &(settings->detailedCPUTime))); Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Detailed CPU time (System/IO-Wait/Hard-IRQ/Soft-IRQ/Steal/Guest)"), &(settings->detailedCPUTime)));
Panel_add(super, (Object*) CheckItem_newByRef(strdup("Count CPUs from 0 instead of 1"), &(settings->countCPUsFromZero))); Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Count CPUs from 0 instead of 1"), &(settings->countCPUsFromZero)));
Panel_add(super, (Object*) CheckItem_newByRef(strdup("Update process names on every refresh"), &(settings->updateProcessNames))); Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Update process names on every refresh"), &(settings->updateProcessNames)));
Panel_add(super, (Object*) CheckItem_newByRef(strdup("Add guest time in CPU meter percentage"), &(settings->accountGuestInCPUMeter))); Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Add guest time in CPU meter percentage"), &(settings->accountGuestInCPUMeter)));
return this; return this;
} }

View File

@ -29,7 +29,7 @@ InfoScreenClass EnvScreen_class = {
}; };
EnvScreen* EnvScreen_new(Process* process) { EnvScreen* EnvScreen_new(Process* process) {
EnvScreen* this = malloc(sizeof(EnvScreen)); EnvScreen* this = xMalloc(sizeof(EnvScreen));
Object_setClass(this, Class(EnvScreen)); Object_setClass(this, Class(EnvScreen));
return (EnvScreen*) InfoScreen_init(&this->super, process, NULL, LINES-3, " "); return (EnvScreen*) InfoScreen_init(&this->super, process, NULL, LINES-3, " ");
} }

View File

@ -8,6 +8,7 @@ in the source distribution for its full text.
#include "FunctionBar.h" #include "FunctionBar.h"
#include "CRT.h" #include "CRT.h"
#include "RichString.h" #include "RichString.h"
#include "XAlloc.h"
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
@ -42,21 +43,21 @@ FunctionBar* FunctionBar_newEnterEsc(const char* enter, const char* esc) {
} }
FunctionBar* FunctionBar_new(const char** functions, const char** keys, int* events) { FunctionBar* FunctionBar_new(const char** functions, const char** keys, int* events) {
FunctionBar* this = calloc(1, sizeof(FunctionBar)); FunctionBar* this = xCalloc(1, sizeof(FunctionBar));
this->functions = calloc(16, sizeof(char*)); this->functions = xCalloc(16, sizeof(char*));
if (!functions) { if (!functions) {
functions = FunctionBar_FLabels; functions = FunctionBar_FLabels;
} }
for (int i = 0; i < 15 && functions[i]; i++) { for (int i = 0; i < 15 && functions[i]; i++) {
this->functions[i] = strdup(functions[i]); this->functions[i] = xStrdup(functions[i]);
} }
if (keys && events) { if (keys && events) {
this->staticData = false; this->staticData = false;
this->keys = calloc(15, sizeof(char*)); this->keys = xCalloc(15, sizeof(char*));
this->events = calloc(15, sizeof(int)); this->events = xCalloc(15, sizeof(int));
int i = 0; int i = 0;
while (i < 15 && functions[i]) { while (i < 15 && functions[i]) {
this->keys[i] = strdup(keys[i]); this->keys[i] = xStrdup(keys[i]);
this->events[i] = events[i]; this->events[i] = events[i];
i++; i++;
} }
@ -89,7 +90,7 @@ void FunctionBar_setLabel(FunctionBar* this, int event, const char* text) {
for (int i = 0; i < this->size; i++) { for (int i = 0; i < this->size; i++) {
if (this->events[i] == event) { if (this->events[i] == event) {
free(this->functions[i]); free(this->functions[i]);
this->functions[i] = strdup(text); this->functions[i] = xStrdup(text);
break; break;
} }
} }

View File

@ -6,6 +6,7 @@ in the source distribution for its full text.
*/ */
#include "Hashtable.h" #include "Hashtable.h"
#include "XAlloc.h"
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
@ -63,7 +64,7 @@ int Hashtable_count(Hashtable* this) {
static HashtableItem* HashtableItem_new(unsigned int key, void* value) { static HashtableItem* HashtableItem_new(unsigned int key, void* value) {
HashtableItem* this; HashtableItem* this;
this = malloc(sizeof(HashtableItem)); this = xMalloc(sizeof(HashtableItem));
this->key = key; this->key = key;
this->value = value; this->value = value;
this->next = NULL; this->next = NULL;
@ -73,10 +74,10 @@ static HashtableItem* HashtableItem_new(unsigned int key, void* value) {
Hashtable* Hashtable_new(int size, bool owner) { Hashtable* Hashtable_new(int size, bool owner) {
Hashtable* this; Hashtable* this;
this = malloc(sizeof(Hashtable)); this = xMalloc(sizeof(Hashtable));
this->items = 0; this->items = 0;
this->size = size; this->size = size;
this->buckets = (HashtableItem**) calloc(size, sizeof(HashtableItem*)); this->buckets = (HashtableItem**) xCalloc(size, sizeof(HashtableItem*));
this->owner = owner; this->owner = owner;
assert(Hashtable_isConsistent(this)); assert(Hashtable_isConsistent(this));
return this; return this;

View File

@ -41,8 +41,8 @@ typedef struct Header_ {
#endif #endif
Header* Header_new(struct ProcessList_* pl, Settings* settings, int nrColumns) { Header* Header_new(struct ProcessList_* pl, Settings* settings, int nrColumns) {
Header* this = calloc(1, sizeof(Header)); Header* this = xCalloc(1, sizeof(Header));
this->columns = calloc(nrColumns, sizeof(Vector*)); this->columns = xCalloc(nrColumns, sizeof(Vector*));
this->settings = settings; this->settings = settings;
this->pl = pl; this->pl = pl;
this->nrColumns = nrColumns; this->nrColumns = nrColumns;
@ -83,13 +83,13 @@ void Header_writeBackToSettings(const Header* this) {
Vector* vec = this->columns[col]; Vector* vec = this->columns[col];
int len = Vector_size(vec); int len = Vector_size(vec);
colSettings->names = calloc(len+1, sizeof(char*)); colSettings->names = xCalloc(len+1, sizeof(char*));
colSettings->modes = calloc(len, sizeof(int)); colSettings->modes = xCalloc(len, sizeof(int));
colSettings->len = len; colSettings->len = len;
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
Meter* meter = (Meter*) Vector_get(vec, i); Meter* meter = (Meter*) Vector_get(vec, i);
char* name = calloc(64, sizeof(char)); char* name = xCalloc(64, sizeof(char));
if (meter->param) { if (meter->param) {
snprintf(name, 63, "%s(%d)", As_Meter(meter)->name, meter->param); snprintf(name, 63, "%s(%d)", As_Meter(meter)->name, meter->param);
} else { } else {
@ -151,7 +151,7 @@ char* Header_readMeterName(Header* this, int i, int column) {
int nameLen = strlen(Meter_name(meter)); int nameLen = strlen(Meter_name(meter));
int len = nameLen + 100; int len = nameLen + 100;
char* name = malloc(len); char* name = xMalloc(len);
strncpy(name, Meter_name(meter), nameLen); strncpy(name, Meter_name(meter), nameLen);
name[nameLen] = '\0'; name[nameLen] = '\0';
if (meter->param) if (meter->param)

View File

@ -76,7 +76,7 @@ static inline void IncMode_done(IncMode* mode) {
} }
IncSet* IncSet_new(FunctionBar* bar) { IncSet* IncSet_new(FunctionBar* bar) {
IncSet* this = calloc(1, sizeof(IncSet)); IncSet* this = xCalloc(1, sizeof(IncSet));
IncMode_initSearch(&(this->modes[INC_SEARCH])); IncMode_initSearch(&(this->modes[INC_SEARCH]));
IncMode_initFilter(&(this->modes[INC_FILTER])); IncMode_initFilter(&(this->modes[INC_FILTER]));
this->active = NULL; this->active = NULL;

View File

@ -61,7 +61,7 @@ ObjectClass ListItem_class = {
ListItem* ListItem_new(const char* value, int key) { ListItem* ListItem_new(const char* value, int key) {
ListItem* this = AllocThis(ListItem); ListItem* this = AllocThis(ListItem);
this->value = strdup(value); this->value = xStrdup(value);
this->key = key; this->key = key;
this->moving = false; this->moving = false;
return this; return this;
@ -71,7 +71,7 @@ void ListItem_append(ListItem* this, const char* text) {
int oldLen = strlen(this->value); int oldLen = strlen(this->value);
int textLen = strlen(text); int textLen = strlen(text);
int newLen = strlen(this->value) + textLen; int newLen = strlen(this->value) + textLen;
this->value = realloc(this->value, newLen + 1); this->value = xRealloc(this->value, newLen + 1);
memcpy(this->value + oldLen, text, textLen); memcpy(this->value + oldLen, text, textLen);
this->value[newLen] = '\0'; this->value[newLen] = '\0';
} }

View File

@ -190,7 +190,7 @@ PanelClass MainPanel_class = {
MainPanel* MainPanel_new() { MainPanel* MainPanel_new() {
MainPanel* this = AllocThis(MainPanel); MainPanel* this = AllocThis(MainPanel);
Panel_init((Panel*) this, 1, 1, 1, 1, Class(Process), false, FunctionBar_new(MainFunctions, NULL, NULL)); Panel_init((Panel*) this, 1, 1, 1, 1, Class(Process), false, FunctionBar_new(MainFunctions, NULL, NULL));
this->keys = calloc(KEY_MAX, sizeof(Htop_Action)); this->keys = xCalloc(KEY_MAX, sizeof(Htop_Action));
this->inc = IncSet_new(MainPanel_getFunctionBar(this)); this->inc = IncSet_new(MainPanel_getFunctionBar(this));
Action_setBindings(this->keys); Action_setBindings(this->keys);

View File

@ -24,7 +24,7 @@ BatteryMeter.c Process.c ProcessList.c RichString.c ScreenManager.c Settings.c \
SignalsPanel.c StringUtils.c SwapMeter.c TasksMeter.c UptimeMeter.c \ SignalsPanel.c StringUtils.c SwapMeter.c TasksMeter.c UptimeMeter.c \
TraceScreen.c UsersTable.c Vector.c AvailableColumnsPanel.c AffinityPanel.c \ TraceScreen.c UsersTable.c Vector.c AvailableColumnsPanel.c AffinityPanel.c \
HostnameMeter.c OpenFilesScreen.c Affinity.c IncSet.c Action.c EnvScreen.c \ HostnameMeter.c OpenFilesScreen.c Affinity.c IncSet.c Action.c EnvScreen.c \
InfoScreen.c InfoScreen.c XAlloc.c
myhtopheaders = AvailableColumnsPanel.h AvailableMetersPanel.h \ myhtopheaders = AvailableColumnsPanel.h AvailableMetersPanel.h \
CategoriesPanel.h CheckItem.h ClockMeter.h ColorsPanel.h ColumnsPanel.h \ CategoriesPanel.h CheckItem.h ClockMeter.h ColorsPanel.h ColumnsPanel.h \
@ -34,7 +34,7 @@ BatteryMeter.h Meter.h MetersPanel.h Object.h Panel.h ProcessList.h RichString.h
ScreenManager.h Settings.h SignalsPanel.h StringUtils.h SwapMeter.h \ ScreenManager.h Settings.h SignalsPanel.h StringUtils.h SwapMeter.h \
TasksMeter.h UptimeMeter.h TraceScreen.h UsersTable.h Vector.h Process.h \ TasksMeter.h UptimeMeter.h TraceScreen.h UsersTable.h Vector.h Process.h \
AffinityPanel.h HostnameMeter.h OpenFilesScreen.h Affinity.h IncSet.h Action.h \ AffinityPanel.h HostnameMeter.h OpenFilesScreen.h Affinity.h IncSet.h Action.h \
EnvScreen.h InfoScreen.h EnvScreen.h InfoScreen.h XAlloc.h
if HTOP_LINUX if HTOP_LINUX
htop_CFLAGS += -rdynamic htop_CFLAGS += -rdynamic

10
Meter.c
View File

@ -127,7 +127,7 @@ MeterClass Meter_class = {
}; };
Meter* Meter_new(struct ProcessList_* pl, int param, MeterClass* type) { Meter* Meter_new(struct ProcessList_* pl, int param, MeterClass* type) {
Meter* this = calloc(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;
@ -137,9 +137,9 @@ Meter* Meter_new(struct ProcessList_* pl, int param, MeterClass* type) {
maxItems = 1; maxItems = 1;
} }
type->curItems = maxItems; type->curItems = maxItems;
this->values = calloc(maxItems, sizeof(double)); this->values = xCalloc(maxItems, sizeof(double));
this->total = type->total; this->total = type->total;
this->caption = strdup(type->caption); this->caption = xStrdup(type->caption);
if (Meter_initFn(this)) if (Meter_initFn(this))
Meter_init(this); Meter_init(this);
Meter_setMode(this, type->defaultMode); Meter_setMode(this, type->defaultMode);
@ -193,7 +193,7 @@ void Meter_delete(Object* cast) {
void Meter_setCaption(Meter* this, const char* caption) { void Meter_setCaption(Meter* this, const char* caption) {
free(this->caption); free(this->caption);
this->caption = strdup(caption); this->caption = xStrdup(caption);
} }
static inline void Meter_displayBuffer(Meter* this, char* buffer, RichString* out) { static inline void Meter_displayBuffer(Meter* this, char* buffer, RichString* out) {
@ -366,7 +366,7 @@ static int GraphMeterMode_pixPerRow;
static void GraphMeterMode_draw(Meter* this, int x, int y, int w) { static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
if (!this->drawData) this->drawData = calloc(1, sizeof(GraphData)); if (!this->drawData) this->drawData = xCalloc(1, sizeof(GraphData));
GraphData* data = (GraphData*) this->drawData; GraphData* data = (GraphData*) this->drawData;
const int nValues = METER_BUFFER_LEN; const int nValues = METER_BUFFER_LEN;

View File

@ -9,6 +9,7 @@ in the source distribution for its full text.
/*{ /*{
#include "RichString.h" #include "RichString.h"
#include "XAlloc.h"
typedef struct Object_ Object; typedef struct Object_ Object;
@ -26,7 +27,7 @@ typedef void(*Object_Delete)(Object*);
#define Class(class_) ((ObjectClass*)(&(class_ ## _class))) #define Class(class_) ((ObjectClass*)(&(class_ ## _class)))
#define AllocThis(class_) (class_*) malloc(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* extends;

View File

@ -10,6 +10,7 @@ in the source distribution for its full text.
*/ */
#include "RichString.h" #include "RichString.h"
#include "XAlloc.h"
typedef struct Object_ Object; typedef struct Object_ Object;
@ -27,7 +28,7 @@ typedef void(*Object_Delete)(Object*);
#define Class(class_) ((ObjectClass*)(&(class_ ## _class))) #define Class(class_) ((ObjectClass*)(&(class_ ## _class)))
#define AllocThis(class_) (class_*) malloc(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* extends;

View File

@ -58,7 +58,7 @@ InfoScreenClass OpenFilesScreen_class = {
}; };
OpenFilesScreen* OpenFilesScreen_new(Process* process) { OpenFilesScreen* OpenFilesScreen_new(Process* process) {
OpenFilesScreen* this = malloc(sizeof(OpenFilesScreen)); OpenFilesScreen* this = xMalloc(sizeof(OpenFilesScreen));
Object_setClass(this, Class(OpenFilesScreen)); Object_setClass(this, Class(OpenFilesScreen));
if (Process_isThread(process)) if (Process_isThread(process))
this->pid = process->tgid; this->pid = process->tgid;
@ -79,7 +79,7 @@ static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) {
char command[1025]; char command[1025];
snprintf(command, 1024, "lsof -P -p %d -F 2> /dev/null", pid); snprintf(command, 1024, "lsof -P -p %d -F 2> /dev/null", pid);
FILE* fd = popen(command, "r"); FILE* fd = popen(command, "r");
OpenFiles_ProcessData* pdata = calloc(1, sizeof(OpenFiles_ProcessData)); OpenFiles_ProcessData* pdata = xCalloc(1, sizeof(OpenFiles_ProcessData));
OpenFiles_FileData* fdata = NULL; OpenFiles_FileData* fdata = NULL;
OpenFiles_Data* item = &(pdata->data); OpenFiles_Data* item = &(pdata->data);
if (!fd) { if (!fd) {
@ -90,7 +90,7 @@ static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) {
int cmd = fgetc(fd); int cmd = fgetc(fd);
if (cmd == EOF) if (cmd == EOF)
break; break;
char* entry = malloc(1024); char* entry = xMalloc(1024);
if (!fgets(entry, 1024, fd)) { if (!fgets(entry, 1024, fd)) {
free(entry); free(entry);
break; break;
@ -98,7 +98,7 @@ static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) {
char* newline = strrchr(entry, '\n'); char* newline = strrchr(entry, '\n');
*newline = '\0'; *newline = '\0';
if (cmd == 'f') { if (cmd == 'f') {
OpenFiles_FileData* nextFile = calloc(1, sizeof(OpenFiles_FileData)); OpenFiles_FileData* nextFile = xCalloc(1, sizeof(OpenFiles_FileData));
if (fdata == NULL) { if (fdata == NULL) {
pdata->files = nextFile; pdata->files = nextFile;
} else { } else {

View File

@ -97,7 +97,7 @@ 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, ObjectClass* type, FunctionBar* fuBar) {
Panel* this; Panel* this;
this = malloc(sizeof(Panel)); this = xMalloc(sizeof(Panel));
Object_setClass(this, Class(Panel)); Object_setClass(this, Class(Panel));
Panel_init(this, x, y, w, h, type, owner, fuBar); Panel_init(this, x, y, w, h, type, owner, fuBar);
return this; return this;
@ -455,7 +455,7 @@ bool Panel_onKey(Panel* this, int key) {
HandlerResult Panel_selectByTyping(Panel* this, int ch) { HandlerResult Panel_selectByTyping(Panel* this, int ch) {
int size = Panel_size(this); int size = Panel_size(this);
if (!this->eventHandlerState) if (!this->eventHandlerState)
this->eventHandlerState = calloc(100, sizeof(char)); this->eventHandlerState = xCalloc(100, sizeof(char));
char* buffer = this->eventHandlerState; char* buffer = this->eventHandlerState;
if (ch < 255 && isalnum(ch)) { if (ch < 255 && isalnum(ch)) {

View File

@ -6,6 +6,7 @@ in the source distribution for its full text.
*/ */
#include "RichString.h" #include "RichString.h"
#include "XAlloc.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -67,7 +68,7 @@ typedef struct RichString_ {
static void RichString_extendLen(RichString* this, int len) { static void RichString_extendLen(RichString* this, int len) {
if (this->chlen <= RICHSTRING_MAXLEN) { if (this->chlen <= RICHSTRING_MAXLEN) {
if (len > RICHSTRING_MAXLEN) { if (len > RICHSTRING_MAXLEN) {
this->chptr = malloc(charBytes(len + 1)); this->chptr = xMalloc(charBytes(len + 1));
memcpy(this->chptr, this->chstr, charBytes(this->chlen)); memcpy(this->chptr, this->chstr, charBytes(this->chlen));
} }
} else { } else {
@ -76,7 +77,7 @@ static void RichString_extendLen(RichString* this, int len) {
free(this->chptr); free(this->chptr);
this->chptr = this->chstr; this->chptr = this->chstr;
} else { } else {
this->chptr = realloc(this->chptr, charBytes(len + 1)); this->chptr = xRealloc(this->chptr, charBytes(len + 1));
} }
} }

View File

@ -46,7 +46,7 @@ typedef struct ScreenManager_ {
ScreenManager* ScreenManager_new(int x1, int y1, int x2, int y2, Orientation orientation, const Header* header, const Settings* settings, bool owner) { ScreenManager* ScreenManager_new(int x1, int y1, int x2, int y2, Orientation orientation, const Header* header, const Settings* settings, bool owner) {
ScreenManager* this; ScreenManager* this;
this = malloc(sizeof(ScreenManager)); this = xMalloc(sizeof(ScreenManager));
this->x1 = x1; this->x1 = x1;
this->y1 = y1; this->y1 = y1;
this->x2 = x2; this->x2 = x2;

View File

@ -96,7 +96,7 @@ static void Settings_readMeterModes(Settings* this, char* line, int column) {
len++; len++;
} }
this->columns[column].len = len; this->columns[column].len = len;
int* modes = calloc(len, sizeof(int)); int* modes = xCalloc(len, sizeof(int));
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
modes[i] = atoi(ids[i]); modes[i] = atoi(ids[i]);
} }
@ -110,27 +110,27 @@ static void Settings_defaultMeters(Settings* this) {
sizes[1]++; sizes[1]++;
} }
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
this->columns[i].names = calloc(sizes[i] + 1, sizeof(char*)); this->columns[i].names = xCalloc(sizes[i] + 1, sizeof(char*));
this->columns[i].modes = calloc(sizes[i], sizeof(int)); this->columns[i].modes = xCalloc(sizes[i], sizeof(int));
this->columns[i].len = sizes[i]; this->columns[i].len = sizes[i];
} }
int r = 0; int r = 0;
if (this->cpuCount > 8) { if (this->cpuCount > 8) {
this->columns[0].names[0] = strdup("LeftCPUs2"); this->columns[0].names[0] = xStrdup("LeftCPUs2");
this->columns[1].names[r++] = strdup("RightCPUs2"); this->columns[1].names[r++] = xStrdup("RightCPUs2");
} else if (this->cpuCount > 4) { } else if (this->cpuCount > 4) {
this->columns[0].names[0] = strdup("LeftCPUs"); this->columns[0].names[0] = xStrdup("LeftCPUs");
this->columns[1].names[r++] = strdup("RightCPUs"); this->columns[1].names[r++] = xStrdup("RightCPUs");
} else { } else {
this->columns[0].names[0] = strdup("AllCPUs"); this->columns[0].names[0] = xStrdup("AllCPUs");
} }
this->columns[0].names[1] = strdup("Memory"); this->columns[0].names[1] = xStrdup("Memory");
this->columns[0].names[2] = strdup("Swap"); this->columns[0].names[2] = xStrdup("Swap");
this->columns[1].names[r++] = strdup("Tasks"); this->columns[1].names[r++] = xStrdup("Tasks");
this->columns[1].names[r++] = strdup("LoadAverage"); this->columns[1].names[r++] = xStrdup("LoadAverage");
this->columns[1].names[r++] = strdup("Uptime"); this->columns[1].names[r++] = xStrdup("Uptime");
} }
static void readFields(ProcessField* fields, int* flags, const char* line) { static void readFields(ProcessField* fields, int* flags, const char* line) {
@ -306,7 +306,7 @@ bool Settings_write(Settings* this) {
Settings* Settings_new(int cpuCount) { Settings* Settings_new(int cpuCount) {
Settings* this = calloc(1, sizeof(Settings)); Settings* this = xCalloc(1, sizeof(Settings));
this->sortKey = PERCENT_CPU; this->sortKey = PERCENT_CPU;
this->direction = 1; this->direction = 1;
@ -325,7 +325,7 @@ Settings* Settings_new(int cpuCount) {
this->showProgramPath = true; this->showProgramPath = true;
this->highlightThreads = true; this->highlightThreads = true;
this->fields = calloc(Platform_numberOfFields+1, sizeof(ProcessField)); this->fields = xCalloc(Platform_numberOfFields+1, sizeof(ProcessField));
// TODO: turn 'fields' into a Vector, // TODO: turn 'fields' into a Vector,
// (and ProcessFields into proper objects). // (and ProcessFields into proper objects).
this->flags = 0; this->flags = 0;
@ -338,7 +338,7 @@ Settings* Settings_new(int cpuCount) {
char* legacyDotfile = NULL; char* legacyDotfile = NULL;
char* rcfile = getenv("HTOPRC"); char* rcfile = getenv("HTOPRC");
if (rcfile) { if (rcfile) {
this->filename = strdup(rcfile); this->filename = xStrdup(rcfile);
} else { } else {
const char* home = getenv("HOME"); const char* home = getenv("HOME");
if (!home) home = ""; if (!home) home = "";
@ -347,7 +347,7 @@ Settings* Settings_new(int cpuCount) {
char* htopDir = NULL; char* htopDir = NULL;
if (xdgConfigHome) { if (xdgConfigHome) {
this->filename = String_cat(xdgConfigHome, "/htop/htoprc"); this->filename = String_cat(xdgConfigHome, "/htop/htoprc");
configDir = strdup(xdgConfigHome); configDir = xStrdup(xdgConfigHome);
htopDir = String_cat(xdgConfigHome, "/htop"); htopDir = String_cat(xdgConfigHome, "/htop");
} else { } else {
this->filename = String_cat(home, "/.config/htop/htoprc"); this->filename = String_cat(home, "/.config/htop/htoprc");

View File

@ -6,6 +6,7 @@ in the source distribution for its full text.
*/ */
#include "StringUtils.h" #include "StringUtils.h"
#include "XAlloc.h"
#include "config.h" #include "config.h"
@ -22,7 +23,7 @@ in the source distribution for its full text.
char* String_cat(const char* s1, const char* s2) { char* String_cat(const char* s1, const char* s2) {
int l1 = strlen(s1); int l1 = strlen(s1);
int l2 = strlen(s2); int l2 = strlen(s2);
char* out = malloc(l1 + l2 + 1); char* out = xMalloc(l1 + l2 + 1);
strncpy(out, s1, l1); strncpy(out, s1, l1);
strncpy(out+l1, s2, l2+1); strncpy(out+l1, s2, l2+1);
return out; return out;
@ -36,7 +37,7 @@ char* String_trim(const char* in) {
while (len > 0 && (in[len-1] == ' ' || in[len-1] == '\t' || in[len-1] == '\n')) { while (len > 0 && (in[len-1] == ' ' || in[len-1] == '\t' || in[len-1] == '\n')) {
len--; len--;
} }
char* out = malloc(len+1); char* out = xMalloc(len+1);
strncpy(out, in, len); strncpy(out, in, len);
out[len] = '\0'; out[len] = '\0';
return out; return out;
@ -55,20 +56,20 @@ inline int String_eq(const char* s1, const char* s2) {
char** String_split(const char* s, char sep, int* n) { char** String_split(const char* s, char sep, int* n) {
*n = 0; *n = 0;
const int rate = 10; const int rate = 10;
char** out = calloc(rate, sizeof(char*)); char** out = xCalloc(rate, sizeof(char*));
int ctr = 0; int ctr = 0;
int blocks = rate; int blocks = rate;
char* where; char* where;
while ((where = strchr(s, sep)) != NULL) { while ((where = strchr(s, sep)) != NULL) {
int size = where - s; int size = where - s;
char* token = malloc(size + 1); char* token = xMalloc(size + 1);
strncpy(token, s, size); strncpy(token, s, size);
token[size] = '\0'; token[size] = '\0';
out[ctr] = token; out[ctr] = token;
ctr++; ctr++;
if (ctr == blocks) { if (ctr == blocks) {
blocks += rate; blocks += rate;
char** newOut = (char**) realloc(out, sizeof(char*) * blocks); char** newOut = (char**) xRealloc(out, sizeof(char*) * blocks);
if (newOut) { if (newOut) {
out = newOut; out = newOut;
} else { } else {
@ -80,12 +81,12 @@ char** String_split(const char* s, char sep, int* n) {
} }
if (s[0] != '\0') { if (s[0] != '\0') {
int size = strlen(s); int size = strlen(s);
char* token = malloc(size + 1); char* token = xMalloc(size + 1);
strncpy(token, s, size + 1); strncpy(token, s, size + 1);
out[ctr] = token; out[ctr] = token;
ctr++; ctr++;
} }
char** newOut = realloc(out, sizeof(char*) * (ctr + 1)); char** newOut = xRealloc(out, sizeof(char*) * (ctr + 1));
if (newOut) { if (newOut) {
out = newOut; out = newOut;
} }
@ -125,5 +126,5 @@ char* String_getToken(const char* line, const unsigned short int numMatch) {
} }
match[foundCount] = '\0'; match[foundCount] = '\0';
return((char*)strdup(match)); return((char*)xStrdup(match));
} }

View File

@ -59,7 +59,7 @@ InfoScreenClass TraceScreen_class = {
}; };
TraceScreen* TraceScreen_new(Process* process) { TraceScreen* TraceScreen_new(Process* process) {
TraceScreen* this = malloc(sizeof(TraceScreen)); TraceScreen* this = xMalloc(sizeof(TraceScreen));
Object_setClass(this, Class(TraceScreen)); Object_setClass(this, Class(TraceScreen));
this->tracing = true; this->tracing = true;
this->contLine = false; this->contLine = false;

View File

@ -6,6 +6,7 @@ in the source distribution for its full text.
*/ */
#include "UsersTable.h" #include "UsersTable.h"
#include "XAlloc.h"
#include "config.h" #include "config.h"
@ -27,7 +28,7 @@ typedef struct UsersTable_ {
UsersTable* UsersTable_new() { UsersTable* UsersTable_new() {
UsersTable* this; UsersTable* this;
this = malloc(sizeof(UsersTable)); this = xMalloc(sizeof(UsersTable));
this->users = Hashtable_new(20, true); this->users = Hashtable_new(20, true);
return this; return this;
} }
@ -42,7 +43,7 @@ char* UsersTable_getRef(UsersTable* this, unsigned int uid) {
if (name == NULL) { if (name == NULL) {
struct passwd* userData = getpwuid(uid); struct passwd* userData = getpwuid(uid);
if (userData != NULL) { if (userData != NULL) {
name = strdup(userData->pw_name); name = xStrdup(userData->pw_name);
Hashtable_put(this->users, uid, name); Hashtable_put(this->users, uid, name);
} }
} }

View File

@ -37,9 +37,9 @@ Vector* Vector_new(ObjectClass* type, bool owner, int size) {
if (size == DEFAULT_SIZE) if (size == DEFAULT_SIZE)
size = 10; size = 10;
this = malloc(sizeof(Vector)); this = xMalloc(sizeof(Vector));
this->growthRate = size; this->growthRate = size;
this->array = (Object**) calloc(size, sizeof(Object*)); this->array = (Object**) xCalloc(size, sizeof(Object*));
this->arraySize = size; this->arraySize = size;
this->items = 0; this->items = 0;
this->type = type; this->type = type;
@ -179,7 +179,7 @@ static void Vector_checkArraySize(Vector* this) {
//int i; //int i;
//i = this->arraySize; //i = this->arraySize;
this->arraySize = this->items + this->growthRate; this->arraySize = this->items + this->growthRate;
this->array = (Object**) realloc(this->array, sizeof(Object*) * this->arraySize); this->array = (Object**) xRealloc(this->array, sizeof(Object*) * this->arraySize);
//for (; i < this->arraySize; i++) //for (; i < this->arraySize; i++)
// this->array[i] = NULL; // this->array[i] = NULL;
} }

55
XAlloc.c Normal file
View File

@ -0,0 +1,55 @@
#include "XAlloc.h"
#include "RichString.h"
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <unistd.h>
#include <string.h>
/*{
#include <stdlib.h>
}*/
static char oomMessage[] = "Out of memory!\n";
void* xMalloc(size_t size) {
void* data = malloc(size);
if (!data && size > 0) {
curs_set(1);
endwin();
write(2, oomMessage, sizeof oomMessage - 1);
}
return data;
}
void* xCalloc(size_t nmemb, size_t size) {
void* data = calloc(nmemb, size);
if (!data && nmemb > 0 && size > 0) {
curs_set(1);
endwin();
write(2, oomMessage, sizeof oomMessage - 1);
}
return data;
}
void* xRealloc(void* ptr, size_t size) {
void* data = realloc(ptr, size);
if (!data && size > 0) {
curs_set(1);
endwin();
write(2, oomMessage, sizeof oomMessage - 1);
}
return data;
}
char* xStrdup(const char* str) {
char* data = strdup(str);
if (!data && str) {
curs_set(1);
endwin();
write(2, oomMessage, sizeof oomMessage - 1);
}
return data;
}

20
XAlloc.h Normal file
View File

@ -0,0 +1,20 @@
/* Do not edit this file. It was automatically generated. */
#ifndef HEADER_XAlloc
#define HEADER_XAlloc
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdlib.h>
void* xMalloc(size_t size);
void* xCalloc(size_t nmemb, size_t size);
void* xRealloc(void* ptr, size_t size);
char* xStrdup(const char* str);
#endif

View File

@ -39,7 +39,7 @@ ProcessClass DarwinProcess_class = {
}; };
DarwinProcess* DarwinProcess_new(Settings* settings) { DarwinProcess* DarwinProcess_new(Settings* settings) {
DarwinProcess* this = calloc(1, sizeof(DarwinProcess)); DarwinProcess* this = xCalloc(1, sizeof(DarwinProcess));
Object_setClass(this, Class(DarwinProcess)); Object_setClass(this, Class(DarwinProcess));
Process_init(&this->super, settings); Process_init(&this->super, settings);
@ -85,7 +85,7 @@ char *DarwinProcess_getCmdLine(struct kinfo_proc* k, int show_args ) {
} }
/* Allocate space for the arguments. */ /* Allocate space for the arguments. */
procargs = ( char * ) malloc( argmax ); procargs = ( char * ) xMalloc( argmax );
if ( procargs == NULL ) { if ( procargs == NULL ) {
goto ERROR_A; goto ERROR_A;
} }
@ -237,7 +237,7 @@ char *DarwinProcess_getCmdLine(struct kinfo_proc* k, int show_args ) {
} }
/* Make a copy of the string. */ /* Make a copy of the string. */
retval = strdup(sp); retval = xStrdup(sp);
/* Clean up. */ /* Clean up. */
free( procargs ); free( procargs );
@ -247,7 +247,7 @@ char *DarwinProcess_getCmdLine(struct kinfo_proc* k, int show_args ) {
ERROR_B: ERROR_B:
free( procargs ); free( procargs );
ERROR_A: ERROR_A:
retval = strdup(k->kp_proc.p_comm); retval = xStrdup(k->kp_proc.p_comm);
return retval; return retval;
} }

View File

@ -87,7 +87,7 @@ struct kinfo_proc *ProcessList_getKInfoProcs(size_t *count) {
if (sysctl(mib, 4, NULL, count, NULL, 0) < 0) if (sysctl(mib, 4, NULL, count, NULL, 0) < 0)
CRT_fatalError("Unable to get size of kproc_infos"); CRT_fatalError("Unable to get size of kproc_infos");
processes = malloc(*count); processes = xMalloc(*count);
if (processes == NULL) if (processes == NULL)
CRT_fatalError("Out of memory for kproc_infos"); CRT_fatalError("Out of memory for kproc_infos");
@ -101,7 +101,7 @@ struct kinfo_proc *ProcessList_getKInfoProcs(size_t *count) {
ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) { ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) {
DarwinProcessList* this = calloc(1, sizeof(DarwinProcessList)); DarwinProcessList* this = xCalloc(1, sizeof(DarwinProcessList));
ProcessList_init(&this->super, Class(Process), usersTable, pidWhiteList, userId); ProcessList_init(&this->super, Class(Process), usersTable, pidWhiteList, userId);

View File

@ -251,7 +251,7 @@ char* Platform_getProcessEnv(pid_t pid) {
mib[0] = CTL_KERN; mib[0] = CTL_KERN;
mib[1] = KERN_ARGMAX; mib[1] = KERN_ARGMAX;
if (sysctl(mib, 2, &argmax, &bufsz, 0, 0) == 0) { if (sysctl(mib, 2, &argmax, &bufsz, 0, 0) == 0) {
char* buf = malloc(argmax); char* buf = xMalloc(argmax);
if (buf) { if (buf) {
mib[0] = CTL_KERN; mib[0] = CTL_KERN;
mib[1] = KERN_PROCARGS2; mib[1] = KERN_PROCARGS2;
@ -279,7 +279,7 @@ char* Platform_getProcessEnv(pid_t pid) {
++p; ++p;
size_t size = endp - p; size_t size = endp - p;
env = malloc(size+2); env = xMalloc(size+2);
if (env) { if (env) {
memcpy(env, p, size); memcpy(env, p, size);
env[size] = 0; env[size] = 0;

View File

@ -97,7 +97,7 @@ ProcessPidColumn Process_pidColumns[] = {
}; };
FreeBSDProcess* FreeBSDProcess_new(Settings* settings) { FreeBSDProcess* FreeBSDProcess_new(Settings* settings) {
FreeBSDProcess* this = calloc(1, sizeof(FreeBSDProcess)); FreeBSDProcess* this = xCalloc(1, sizeof(FreeBSDProcess));
Object_setClass(this, Class(FreeBSDProcess)); Object_setClass(this, Class(FreeBSDProcess));
Process_init(&this->super, settings); Process_init(&this->super, settings);
return this; return this;

View File

@ -86,7 +86,7 @@ static int kernelFScale;
ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) { ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) {
FreeBSDProcessList* fpl = calloc(1, sizeof(FreeBSDProcessList)); FreeBSDProcessList* fpl = xCalloc(1, sizeof(FreeBSDProcessList));
ProcessList* pl = (ProcessList*) fpl; ProcessList* pl = (ProcessList*) fpl;
ProcessList_init(pl, Class(FreeBSDProcess), usersTable, pidWhiteList, userId); ProcessList_init(pl, Class(FreeBSDProcess), usersTable, pidWhiteList, userId);
@ -146,8 +146,8 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui
size_t sizeof_cp_time_array = sizeof(unsigned long) * CPUSTATES; size_t sizeof_cp_time_array = sizeof(unsigned long) * CPUSTATES;
len = 2; sysctlnametomib("kern.cp_time", MIB_kern_cp_time, &len); len = 2; sysctlnametomib("kern.cp_time", MIB_kern_cp_time, &len);
fpl->cp_time_o = calloc(cpus, sizeof_cp_time_array); fpl->cp_time_o = xCalloc(cpus, sizeof_cp_time_array);
fpl->cp_time_n = calloc(cpus, sizeof_cp_time_array); fpl->cp_time_n = xCalloc(cpus, sizeof_cp_time_array);
len = sizeof_cp_time_array; len = sizeof_cp_time_array;
// fetch intial single (or average) CPU clicks from kernel // fetch intial single (or average) CPU clicks from kernel
@ -156,8 +156,8 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui
// on smp box, fetch rest of intial CPU's clicks // on smp box, fetch rest of intial CPU's clicks
if (cpus > 1) { if (cpus > 1) {
len = 2; sysctlnametomib("kern.cp_times", MIB_kern_cp_times, &len); len = 2; sysctlnametomib("kern.cp_times", MIB_kern_cp_times, &len);
fpl->cp_times_o = calloc(cpus, sizeof_cp_time_array); fpl->cp_times_o = xCalloc(cpus, sizeof_cp_time_array);
fpl->cp_times_n = calloc(cpus, sizeof_cp_time_array); fpl->cp_times_n = xCalloc(cpus, sizeof_cp_time_array);
len = cpus * sizeof_cp_time_array; len = cpus * sizeof_cp_time_array;
sysctl(MIB_kern_cp_times, 2, fpl->cp_times_o, &len, NULL, 0); sysctl(MIB_kern_cp_times, 2, fpl->cp_times_o, &len, NULL, 0);
} }
@ -165,10 +165,10 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui
pl->cpuCount = MAX(cpus, 1); pl->cpuCount = MAX(cpus, 1);
if (cpus == 1 ) { if (cpus == 1 ) {
fpl->cpus = realloc(fpl->cpus, sizeof(CPUData)); fpl->cpus = xRealloc(fpl->cpus, sizeof(CPUData));
} else { } else {
// on smp we need CPUs + 1 to store averages too (as kernel kindly provides that as well) // on smp we need CPUs + 1 to store averages too (as kernel kindly provides that as well)
fpl->cpus = realloc(fpl->cpus, (pl->cpuCount + 1) * sizeof(CPUData)); fpl->cpus = xRealloc(fpl->cpus, (pl->cpuCount + 1) * sizeof(CPUData));
} }
@ -349,13 +349,13 @@ static inline void FreeBSDProcessList_scanMemoryInfo(ProcessList* pl) {
char* FreeBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, int* basenameEnd) { char* FreeBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, int* basenameEnd) {
char** argv = kvm_getargv(kd, kproc, 0); char** argv = kvm_getargv(kd, kproc, 0);
if (!argv) { if (!argv) {
return strdup(kproc->ki_comm); return xStrdup(kproc->ki_comm);
} }
int len = 0; int len = 0;
for (int i = 0; argv[i]; i++) { for (int i = 0; argv[i]; i++) {
len += strlen(argv[i]) + 1; len += strlen(argv[i]) + 1;
} }
char* comm = malloc(len); char* comm = xMalloc(len);
char* at = comm; char* at = comm;
*basenameEnd = 0; *basenameEnd = 0;
for (int i = 0; argv[i]; i++) { for (int i = 0; argv[i]; i++) {
@ -398,7 +398,7 @@ char* FreeBSDProcessList_readJailName(struct kinfo_proc* kproc) {
snprintf(jail_errmsg, JAIL_ERRMSGLEN, "jail_get: %s", strerror(errno)); snprintf(jail_errmsg, JAIL_ERRMSGLEN, "jail_get: %s", strerror(errno));
return NULL; return NULL;
} else if (jid == kproc->ki_jid) { } else if (jid == kproc->ki_jid) {
jname = strdup(jnamebuf); jname = xStrdup(jnamebuf);
if (jname == NULL) if (jname == NULL)
strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN); strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
return jname; return jname;
@ -408,7 +408,7 @@ char* FreeBSDProcessList_readJailName(struct kinfo_proc* kproc) {
} else { } else {
jnamebuf[0]='-'; jnamebuf[0]='-';
jnamebuf[1]='\0'; jnamebuf[1]='\0';
jname = strdup(jnamebuf); jname = xStrdup(jnamebuf);
} }
return jname; return jname;
} }

2
htop.c
View File

@ -128,7 +128,7 @@ static CommandLineSettings parseArguments(int argc, char** argv) {
flags.useColors = false; flags.useColors = false;
break; break;
case 'p': { case 'p': {
char* argCopy = strdup(optarg); char* argCopy = xStrdup(optarg);
char* saveptr; char* saveptr;
char* pid = strtok_r(argCopy, ",", &saveptr); char* pid = strtok_r(argCopy, ",", &saveptr);

View File

@ -50,7 +50,7 @@ static unsigned long int parseBatInfo(const char *fileName, const unsigned short
char* entryName = dirEntry->d_name; char* entryName = dirEntry->d_name;
if (strncmp(entryName, "BAT", 3)) if (strncmp(entryName, "BAT", 3))
continue; continue;
batteries[nBatteries] = strdup(entryName); batteries[nBatteries] = xStrdup(entryName);
nBatteries++; nBatteries++;
} }
closedir(batteryDir); closedir(batteryDir);

View File

@ -242,7 +242,7 @@ ProcessClass LinuxProcess_class = {
}; };
LinuxProcess* LinuxProcess_new(Settings* settings) { LinuxProcess* LinuxProcess_new(Settings* settings) {
LinuxProcess* this = calloc(1, sizeof(LinuxProcess)); LinuxProcess* this = xCalloc(1, sizeof(LinuxProcess));
Object_setClass(this, Class(LinuxProcess)); Object_setClass(this, Class(LinuxProcess));
Process_init(&this->super, settings); Process_init(&this->super, settings);
return this; return this;

View File

@ -89,7 +89,7 @@ typedef struct LinuxProcessList_ {
#endif #endif
ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) { ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) {
LinuxProcessList* this = calloc(1, sizeof(LinuxProcessList)); LinuxProcessList* this = xCalloc(1, sizeof(LinuxProcessList));
ProcessList* pl = &(this->super); ProcessList* pl = &(this->super);
ProcessList_init(pl, Class(LinuxProcess), usersTable, pidWhiteList, userId); ProcessList_init(pl, Class(LinuxProcess), usersTable, pidWhiteList, userId);
@ -108,7 +108,7 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui
fclose(file); fclose(file);
pl->cpuCount = MAX(cpus - 1, 1); pl->cpuCount = MAX(cpus - 1, 1);
this->cpus = calloc(cpus, sizeof(CPUData)); this->cpus = xCalloc(cpus, sizeof(CPUData));
for (int i = 0; i < cpus; i++) { for (int i = 0; i < cpus; i++) {
this->cpus[i].totalTime = 1; this->cpus[i].totalTime = 1;
@ -366,7 +366,7 @@ static void LinuxProcessList_readCGroupFile(LinuxProcess* process, const char* d
snprintf(filename, MAX_NAME, "%s/%s/cgroup", dirname, name); snprintf(filename, MAX_NAME, "%s/%s/cgroup", dirname, name);
FILE* file = fopen(filename, "r"); FILE* file = fopen(filename, "r");
if (!file) { if (!file) {
process->cgroup = strdup(""); process->cgroup = xStrdup("");
return; return;
} }
char output[PROC_LINE_LENGTH + 1]; char output[PROC_LINE_LENGTH + 1];
@ -389,7 +389,7 @@ static void LinuxProcessList_readCGroupFile(LinuxProcess* process, const char* d
} }
fclose(file); fclose(file);
free(process->cgroup); free(process->cgroup);
process->cgroup = strdup(output); process->cgroup = xStrdup(output);
} }
#endif #endif

View File

@ -220,16 +220,16 @@ char* Platform_getProcessEnv(pid_t pid) {
char *env = NULL; char *env = NULL;
if (fd) { if (fd) {
size_t capacity = 4096, size = 0, bytes; size_t capacity = 4096, size = 0, bytes;
env = malloc(capacity); env = xMalloc(capacity);
while (env && (bytes = fread(env+size, 1, capacity-size, fd)) > 0) { while (env && (bytes = fread(env+size, 1, capacity-size, fd)) > 0) {
size += bytes; size += bytes;
capacity *= 2; capacity *= 2;
env = realloc(env, capacity); env = xRealloc(env, capacity);
} }
fclose(fd); fclose(fd);
if (size < 2 || env[size-1] || env[size-2]) { if (size < 2 || env[size-1] || env[size-2]) {
if (size + 2 < capacity) { if (size + 2 < capacity) {
env = realloc(env, capacity+2); env = xRealloc(env, capacity+2);
} }
env[size] = 0; env[size] = 0;
env[size+1] = 0; env[size+1] = 0;

View File

@ -186,7 +186,7 @@ ProcessPidColumn Process_pidColumns[] = {
}; };
OpenBSDProcess* OpenBSDProcess_new(Settings* settings) { OpenBSDProcess* OpenBSDProcess_new(Settings* settings) {
OpenBSDProcess* this = calloc(sizeof(OpenBSDProcess), 1); OpenBSDProcess* this = xCalloc(sizeof(OpenBSDProcess), 1);
Object_setClass(this, Class(OpenBSDProcess)); Object_setClass(this, Class(OpenBSDProcess));
Process_init(&this->super, settings); Process_init(&this->super, settings);
return this; return this;

View File

@ -53,7 +53,7 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui
int mib[] = { CTL_HW, HW_NCPU }; int mib[] = { CTL_HW, HW_NCPU };
int fmib[] = { CTL_KERN, KERN_FSCALE }; int fmib[] = { CTL_KERN, KERN_FSCALE };
int i, e; int i, e;
OpenBSDProcessList* opl = calloc(1, sizeof(OpenBSDProcessList)); OpenBSDProcessList* opl = xCalloc(1, sizeof(OpenBSDProcessList));
ProcessList* pl = (ProcessList*) opl; ProcessList* pl = (ProcessList*) opl;
size_t size = sizeof(pl->cpuCount); size_t size = sizeof(pl->cpuCount);
@ -62,7 +62,7 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui
if (e == -1 || pl->cpuCount < 1) { if (e == -1 || pl->cpuCount < 1) {
pl->cpuCount = 1; pl->cpuCount = 1;
} }
opl->cpus = realloc(opl->cpus, pl->cpuCount * sizeof(CPUData)); opl->cpus = xRealloc(opl->cpus, pl->cpuCount * sizeof(CPUData));
size = sizeof(fscale); size = sizeof(fscale);
if (sysctl(fmib, 2, &fscale, &size, NULL, 0) < 0) if (sysctl(fmib, 2, &fscale, &size, NULL, 0) < 0)
@ -144,7 +144,7 @@ char *OpenBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, in
*/ */
arg = kvm_getargv(kd, kproc, 500); arg = kvm_getargv(kd, kproc, 500);
if (arg == NULL) { if (arg == NULL) {
if ((s = strdup(kproc->p_comm)) == NULL) { if ((s = xStrdup(kproc->p_comm)) == NULL) {
err(1, NULL); err(1, NULL);
} }
return s; return s;
@ -152,8 +152,8 @@ char *OpenBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, in
for (i = 0; arg[i] != NULL; i++) { for (i = 0; arg[i] != NULL; i++) {
len += strlen(arg[i]) + 1; len += strlen(arg[i]) + 1;
} }
if ((buf = s = malloc(len)) == NULL) { if ((buf = s = xMalloc(len)) == NULL) {
if ((s = strdup(kproc->p_comm)) == NULL) { if ((s = xStrdup(kproc->p_comm)) == NULL) {
err(1, NULL); err(1, NULL);
} }
return s; return s;

View File

@ -264,7 +264,7 @@ void Platform_setSwapValues(Meter* this) {
return; return;
} }
swdev = calloc(nswap, sizeof(*swdev)); swdev = xCalloc(nswap, sizeof(*swdev));
if (swdev == NULL) { if (swdev == NULL) {
return; return;
} }

View File

@ -17,7 +17,7 @@ in the source distribution for its full text.
}*/ }*/
Process* UnsupportedProcess_new(Settings* settings) { Process* UnsupportedProcess_new(Settings* settings) {
Process* this = calloc(1, sizeof(Process)); Process* this = xCalloc(1, sizeof(Process));
Object_setClass(this, Class(Process)); Object_setClass(this, Class(Process));
Process_init(this, settings); Process_init(this, settings);
return this; return this;

View File

@ -16,7 +16,7 @@ in the source distribution for its full text.
}*/ }*/
ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) { ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) {
ProcessList* this = calloc(1, sizeof(ProcessList)); ProcessList* this = xCalloc(1, sizeof(ProcessList));
ProcessList_init(this, Class(Process), usersTable, pidWhiteList, userId); ProcessList_init(this, Class(Process), usersTable, pidWhiteList, userId);
return this; return this;