Commit ffcf48fb authored by Hisham's avatar Hisham
Browse files

Merge branch 'xalloc'

parents 301c346c b54d2dde
......@@ -29,9 +29,9 @@ typedef struct Affinity_ {
}*/
Affinity* Affinity_new(ProcessList* pl) {
Affinity* this = calloc(1, sizeof(Affinity));
Affinity* this = xCalloc(1, sizeof(Affinity));
this->size = 8;
this->cpus = calloc(this->size, sizeof(int));
this->cpus = xCalloc(this->size, sizeof(int));
this->pl = pl;
return this;
}
......@@ -44,7 +44,7 @@ void Affinity_delete(Affinity* this) {
void Affinity_add(Affinity* this, int id) {
if (this->used == this->size) {
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->used++;
......
......@@ -60,7 +60,7 @@ Panel* AffinityPanel_new(ProcessList* pl, Affinity* affinity) {
} else {
mode = false;
}
Panel_add(this, (Object*) CheckItem_newByVal(strdup(number), mode));
Panel_add(this, (Object*) CheckItem_newByVal(xStrdup(number), mode));
}
return this;
}
......
......@@ -140,7 +140,7 @@ static void AllCPUsMeter_getRange(Meter* this, int* start, int* count) {
static void AllCPUsMeter_init(Meter* this) {
int cpus = this->pl->cpuCount;
if (!this->drawData)
this->drawData = calloc(cpus, sizeof(Meter*));
this->drawData = xCalloc(cpus, sizeof(Meter*));
Meter** meters = (Meter**) this->drawData;
int start, count;
AllCPUsMeter_getRange(this, &start, &count);
......
......@@ -106,7 +106,7 @@ ColorsPanel* ColorsPanel_new(Settings* settings, ScreenManager* scr) {
Panel_setHeader(super, "Colors");
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);
return this;
......
......@@ -155,7 +155,7 @@ void ColumnsPanel_update(Panel* super) {
ColumnsPanel* this = (ColumnsPanel*) super;
int size = Panel_size(super);
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;
for (int i = 0; i < size; i++) {
int key = ((ListItem*) Panel_get(super, i))->key;
......
......@@ -83,19 +83,19 @@ DisplayOptionsPanel* DisplayOptionsPanel_new(Settings* settings, ScreenManager*
this->scr = scr;
Panel_setHeader(super, "Display options");
Panel_add(super, (Object*) CheckItem_newByRef(strdup("Tree view"), &(settings->treeView)));
Panel_add(super, (Object*) CheckItem_newByRef(strdup("Shadow other users' processes"), &(settings->shadowOtherUsers)));
Panel_add(super, (Object*) CheckItem_newByRef(strdup("Hide kernel threads"), &(settings->hideKernelThreads)));
Panel_add(super, (Object*) CheckItem_newByRef(strdup("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(strdup("Show custom thread names"), &(settings->showThreadNames)));
Panel_add(super, (Object*) CheckItem_newByRef(strdup("Show program path"), &(settings->showProgramPath)));
Panel_add(super, (Object*) CheckItem_newByRef(strdup("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(strdup("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(strdup("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(strdup("Add guest time in CPU meter percentage"), &(settings->accountGuestInCPUMeter)));
Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Tree view"), &(settings->treeView)));
Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Shadow other users' processes"), &(settings->shadowOtherUsers)));
Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Hide kernel threads"), &(settings->hideKernelThreads)));
Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Hide userland threads"), &(settings->hideUserlandThreads)));
Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Display threads in a different color"), &(settings->highlightThreads)));
Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Show custom thread names"), &(settings->showThreadNames)));
Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Show program path"), &(settings->showProgramPath)));
Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Highlight program \"basename\""), &(settings->highlightBaseName)));
Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Highlight large numbers in memory counters"), &(settings->highlightMegabytes)));
Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Leave a margin around header"), &(settings->headerMargin)));
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(xStrdup("Count CPUs from 0 instead of 1"), &(settings->countCPUsFromZero)));
Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Update process names on every refresh"), &(settings->updateProcessNames)));
Panel_add(super, (Object*) CheckItem_newByRef(xStrdup("Add guest time in CPU meter percentage"), &(settings->accountGuestInCPUMeter)));
return this;
}
......@@ -29,7 +29,7 @@ InfoScreenClass EnvScreen_class = {
};
EnvScreen* EnvScreen_new(Process* process) {
EnvScreen* this = malloc(sizeof(EnvScreen));
EnvScreen* this = xMalloc(sizeof(EnvScreen));
Object_setClass(this, Class(EnvScreen));
return (EnvScreen*) InfoScreen_init(&this->super, process, NULL, LINES-3, " ");
}
......
......@@ -8,6 +8,7 @@ in the source distribution for its full text.
#include "FunctionBar.h"
#include "CRT.h"
#include "RichString.h"
#include "XAlloc.h"
#include <assert.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* this = calloc(1, sizeof(FunctionBar));
this->functions = calloc(16, sizeof(char*));
FunctionBar* this = xCalloc(1, sizeof(FunctionBar));
this->functions = xCalloc(16, sizeof(char*));
if (!functions) {
functions = FunctionBar_FLabels;
}
for (int i = 0; i < 15 && functions[i]; i++) {
this->functions[i] = strdup(functions[i]);
this->functions[i] = xStrdup(functions[i]);
}
if (keys && events) {
this->staticData = false;
this->keys = calloc(15, sizeof(char*));
this->events = calloc(15, sizeof(int));
this->keys = xCalloc(15, sizeof(char*));
this->events = xCalloc(15, sizeof(int));
int i = 0;
while (i < 15 && functions[i]) {
this->keys[i] = strdup(keys[i]);
this->keys[i] = xStrdup(keys[i]);
this->events[i] = events[i];
i++;
}
......@@ -89,7 +90,7 @@ void FunctionBar_setLabel(FunctionBar* this, int event, const char* text) {
for (int i = 0; i < this->size; i++) {
if (this->events[i] == event) {
free(this->functions[i]);
this->functions[i] = strdup(text);
this->functions[i] = xStrdup(text);
break;
}
}
......
......@@ -6,6 +6,7 @@ in the source distribution for its full text.
*/
#include "Hashtable.h"
#include "XAlloc.h"
#include <stdlib.h>
#include <assert.h>
......@@ -63,7 +64,7 @@ int Hashtable_count(Hashtable* this) {
static HashtableItem* HashtableItem_new(unsigned int key, void* value) {
HashtableItem* this;
this = malloc(sizeof(HashtableItem));
this = xMalloc(sizeof(HashtableItem));
this->key = key;
this->value = value;
this->next = NULL;
......@@ -73,10 +74,10 @@ static HashtableItem* HashtableItem_new(unsigned int key, void* value) {
Hashtable* Hashtable_new(int size, bool owner) {
Hashtable* this;
this = malloc(sizeof(Hashtable));
this = xMalloc(sizeof(Hashtable));
this->items = 0;
this->size = size;
this->buckets = (HashtableItem**) calloc(size, sizeof(HashtableItem*));
this->buckets = (HashtableItem**) xCalloc(size, sizeof(HashtableItem*));
this->owner = owner;
assert(Hashtable_isConsistent(this));
return this;
......
......@@ -41,8 +41,8 @@ typedef struct Header_ {
#endif
Header* Header_new(struct ProcessList_* pl, Settings* settings, int nrColumns) {
Header* this = calloc(1, sizeof(Header));
this->columns = calloc(nrColumns, sizeof(Vector*));
Header* this = xCalloc(1, sizeof(Header));
this->columns = xCalloc(nrColumns, sizeof(Vector*));
this->settings = settings;
this->pl = pl;
this->nrColumns = nrColumns;
......@@ -83,13 +83,13 @@ void Header_writeBackToSettings(const Header* this) {
Vector* vec = this->columns[col];
int len = Vector_size(vec);
colSettings->names = calloc(len+1, sizeof(char*));
colSettings->modes = calloc(len, sizeof(int));
colSettings->names = xCalloc(len+1, sizeof(char*));
colSettings->modes = xCalloc(len, sizeof(int));
colSettings->len = len;
for (int i = 0; i < len; i++) {
Meter* meter = (Meter*) Vector_get(vec, i);
char* name = calloc(64, sizeof(char));
char* name = xCalloc(64, sizeof(char));
if (meter->param) {
snprintf(name, 63, "%s(%d)", As_Meter(meter)->name, meter->param);
} else {
......@@ -151,7 +151,7 @@ char* Header_readMeterName(Header* this, int i, int column) {
int nameLen = strlen(Meter_name(meter));
int len = nameLen + 100;
char* name = malloc(len);
char* name = xMalloc(len);
strncpy(name, Meter_name(meter), nameLen);
name[nameLen] = '\0';
if (meter->param)
......
......@@ -76,7 +76,7 @@ static inline void IncMode_done(IncMode* mode) {
}
IncSet* IncSet_new(FunctionBar* bar) {
IncSet* this = calloc(1, sizeof(IncSet));
IncSet* this = xCalloc(1, sizeof(IncSet));
IncMode_initSearch(&(this->modes[INC_SEARCH]));
IncMode_initFilter(&(this->modes[INC_FILTER]));
this->active = NULL;
......
......@@ -61,7 +61,7 @@ ObjectClass ListItem_class = {
ListItem* ListItem_new(const char* value, int key) {
ListItem* this = AllocThis(ListItem);
this->value = strdup(value);
this->value = xStrdup(value);
this->key = key;
this->moving = false;
return this;
......@@ -71,7 +71,7 @@ void ListItem_append(ListItem* this, const char* text) {
int oldLen = strlen(this->value);
int textLen = strlen(text);
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);
this->value[newLen] = '\0';
}
......
......@@ -190,7 +190,7 @@ PanelClass MainPanel_class = {
MainPanel* MainPanel_new() {
MainPanel* this = AllocThis(MainPanel);
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));
Action_setBindings(this->keys);
......
......@@ -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 \
TraceScreen.c UsersTable.c Vector.c AvailableColumnsPanel.c AffinityPanel.c \
HostnameMeter.c OpenFilesScreen.c Affinity.c IncSet.c Action.c EnvScreen.c \
InfoScreen.c
InfoScreen.c XAlloc.c
myhtopheaders = AvailableColumnsPanel.h AvailableMetersPanel.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 \
TasksMeter.h UptimeMeter.h TraceScreen.h UsersTable.h Vector.h Process.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
htop_CFLAGS += -rdynamic
......
......@@ -127,7 +127,7 @@ MeterClass Meter_class = {
};
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);
this->h = 1;
this->param = param;
......@@ -137,9 +137,9 @@ Meter* Meter_new(struct ProcessList_* pl, int param, MeterClass* type) {
maxItems = 1;
}
type->curItems = maxItems;
this->values = calloc(maxItems, sizeof(double));
this->values = xCalloc(maxItems, sizeof(double));
this->total = type->total;
this->caption = strdup(type->caption);
this->caption = xStrdup(type->caption);
if (Meter_initFn(this))
Meter_init(this);
Meter_setMode(this, type->defaultMode);
......@@ -193,7 +193,7 @@ void Meter_delete(Object* cast) {
void Meter_setCaption(Meter* this, const char* caption) {
free(this->caption);
this->caption = strdup(caption);
this->caption = xStrdup(caption);
}
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) {
if (!this->drawData) this->drawData = calloc(1, sizeof(GraphData));
if (!this->drawData) this->drawData = xCalloc(1, sizeof(GraphData));
GraphData* data = (GraphData*) this->drawData;
const int nValues = METER_BUFFER_LEN;
......
......@@ -9,6 +9,7 @@ in the source distribution for its full text.
/*{
#include "RichString.h"
#include "XAlloc.h"
typedef struct Object_ Object;
......@@ -26,7 +27,7 @@ typedef void(*Object_Delete)(Object*);
#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_ {
const void* extends;
......
......@@ -10,6 +10,7 @@ in the source distribution for its full text.
*/
#include "RichString.h"
#include "XAlloc.h"
typedef struct Object_ Object;
......@@ -27,7 +28,7 @@ typedef void(*Object_Delete)(Object*);
#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_ {
const void* extends;
......
......@@ -58,7 +58,7 @@ InfoScreenClass OpenFilesScreen_class = {
};
OpenFilesScreen* OpenFilesScreen_new(Process* process) {
OpenFilesScreen* this = malloc(sizeof(OpenFilesScreen));
OpenFilesScreen* this = xMalloc(sizeof(OpenFilesScreen));
Object_setClass(this, Class(OpenFilesScreen));
if (Process_isThread(process))
this->pid = process->tgid;
......@@ -79,7 +79,7 @@ static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) {
char command[1025];
snprintf(command, 1024, "lsof -P -p %d -F 2> /dev/null", pid);
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_Data* item = &(pdata->data);
if (!fd) {
......@@ -90,7 +90,7 @@ static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) {
int cmd = fgetc(fd);
if (cmd == EOF)
break;
char* entry = malloc(1024);
char* entry = xMalloc(1024);
if (!fgets(entry, 1024, fd)) {
free(entry);
break;
......@@ -98,7 +98,7 @@ static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) {
char* newline = strrchr(entry, '\n');
*newline = '\0';
if (cmd == 'f') {
OpenFiles_FileData* nextFile = calloc(1, sizeof(OpenFiles_FileData));
OpenFiles_FileData* nextFile = xCalloc(1, sizeof(OpenFiles_FileData));
if (fdata == NULL) {
pdata->files = nextFile;
} else {
......
......@@ -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* this;
this = malloc(sizeof(Panel));
this = xMalloc(sizeof(Panel));
Object_setClass(this, Class(Panel));
Panel_init(this, x, y, w, h, type, owner, fuBar);
return this;
......@@ -455,7 +455,7 @@ bool Panel_onKey(Panel* this, int key) {
HandlerResult Panel_selectByTyping(Panel* this, int ch) {
int size = Panel_size(this);
if (!this->eventHandlerState)
this->eventHandlerState = calloc(100, sizeof(char));
this->eventHandlerState = xCalloc(100, sizeof(char));
char* buffer = this->eventHandlerState;
if (ch < 255 && isalnum(ch)) {
......
......@@ -6,6 +6,7 @@ in the source distribution for its full text.
*/
#include "RichString.h"
#include "XAlloc.h"
#include <stdlib.h>
#include <string.h>
......@@ -67,7 +68,7 @@ typedef struct RichString_ {
static void RichString_extendLen(RichString* this, int len) {
if (this->chlen <= 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));
}
} else {
......@@ -76,7 +77,7 @@ static void RichString_extendLen(RichString* this, int len) {
free(this->chptr);
this->chptr = this->chstr;
} else {
this->chptr = realloc(this->chptr, charBytes(len + 1));
this->chptr = xRealloc(this->chptr, charBytes(len + 1));
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment