Commit ffcf48fb authored by Hisham's avatar Hisham
Browse files

Merge branch 'xalloc'

parents 301c346c b54d2dde
......@@ -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* this;
this = malloc(sizeof(ScreenManager));
this = xMalloc(sizeof(ScreenManager));
this->x1 = x1;
this->y1 = y1;
this->x2 = x2;
......
......@@ -96,7 +96,7 @@ static void Settings_readMeterModes(Settings* this, char* line, int column) {
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++) {
modes[i] = atoi(ids[i]);
}
......@@ -110,27 +110,27 @@ static void Settings_defaultMeters(Settings* this) {
sizes[1]++;
}
for (int i = 0; i < 2; i++) {
this->columns[i].names = calloc(sizes[i] + 1, sizeof(char*));
this->columns[i].modes = calloc(sizes[i], sizeof(int));
this->columns[i].names = xCalloc(sizes[i] + 1, sizeof(char*));
this->columns[i].modes = xCalloc(sizes[i], sizeof(int));
this->columns[i].len = sizes[i];
}
int r = 0;
if (this->cpuCount > 8) {
this->columns[0].names[0] = strdup("LeftCPUs2");
this->columns[1].names[r++] = strdup("RightCPUs2");
this->columns[0].names[0] = xStrdup("LeftCPUs2");
this->columns[1].names[r++] = xStrdup("RightCPUs2");
} else if (this->cpuCount > 4) {
this->columns[0].names[0] = strdup("LeftCPUs");
this->columns[1].names[r++] = strdup("RightCPUs");
this->columns[0].names[0] = xStrdup("LeftCPUs");
this->columns[1].names[r++] = xStrdup("RightCPUs");
} 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[2] = strdup("Swap");
this->columns[0].names[1] = xStrdup("Memory");
this->columns[0].names[2] = xStrdup("Swap");
this->columns[1].names[r++] = strdup("Tasks");
this->columns[1].names[r++] = strdup("LoadAverage");
this->columns[1].names[r++] = strdup("Uptime");
this->columns[1].names[r++] = xStrdup("Tasks");
this->columns[1].names[r++] = xStrdup("LoadAverage");
this->columns[1].names[r++] = xStrdup("Uptime");
}
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* this = calloc(1, sizeof(Settings));
Settings* this = xCalloc(1, sizeof(Settings));
this->sortKey = PERCENT_CPU;
this->direction = 1;
......@@ -325,7 +325,7 @@ Settings* Settings_new(int cpuCount) {
this->showProgramPath = 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,
// (and ProcessFields into proper objects).
this->flags = 0;
......@@ -338,7 +338,7 @@ Settings* Settings_new(int cpuCount) {
char* legacyDotfile = NULL;
char* rcfile = getenv("HTOPRC");
if (rcfile) {
this->filename = strdup(rcfile);
this->filename = xStrdup(rcfile);
} else {
const char* home = getenv("HOME");
if (!home) home = "";
......@@ -347,7 +347,7 @@ Settings* Settings_new(int cpuCount) {
char* htopDir = NULL;
if (xdgConfigHome) {
this->filename = String_cat(xdgConfigHome, "/htop/htoprc");
configDir = strdup(xdgConfigHome);
configDir = xStrdup(xdgConfigHome);
htopDir = String_cat(xdgConfigHome, "/htop");
} else {
this->filename = String_cat(home, "/.config/htop/htoprc");
......
......@@ -6,6 +6,7 @@ in the source distribution for its full text.
*/
#include "StringUtils.h"
#include "XAlloc.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) {
int l1 = strlen(s1);
int l2 = strlen(s2);
char* out = malloc(l1 + l2 + 1);
char* out = xMalloc(l1 + l2 + 1);
strncpy(out, s1, l1);
strncpy(out+l1, s2, l2+1);
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')) {
len--;
}
char* out = malloc(len+1);
char* out = xMalloc(len+1);
strncpy(out, in, len);
out[len] = '\0';
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) {
*n = 0;
const int rate = 10;
char** out = calloc(rate, sizeof(char*));
char** out = xCalloc(rate, sizeof(char*));
int ctr = 0;
int blocks = rate;
char* where;
while ((where = strchr(s, sep)) != NULL) {
int size = where - s;
char* token = malloc(size + 1);
char* token = xMalloc(size + 1);
strncpy(token, s, size);
token[size] = '\0';
out[ctr] = token;
ctr++;
if (ctr == blocks) {
blocks += rate;
char** newOut = (char**) realloc(out, sizeof(char*) * blocks);
char** newOut = (char**) xRealloc(out, sizeof(char*) * blocks);
if (newOut) {
out = newOut;
} else {
......@@ -80,12 +81,12 @@ char** String_split(const char* s, char sep, int* n) {
}
if (s[0] != '\0') {
int size = strlen(s);
char* token = malloc(size + 1);
char* token = xMalloc(size + 1);
strncpy(token, s, size + 1);
out[ctr] = token;
ctr++;
}
char** newOut = realloc(out, sizeof(char*) * (ctr + 1));
char** newOut = xRealloc(out, sizeof(char*) * (ctr + 1));
if (newOut) {
out = newOut;
}
......@@ -125,5 +126,5 @@ char* String_getToken(const char* line, const unsigned short int numMatch) {
}
match[foundCount] = '\0';
return((char*)strdup(match));
return((char*)xStrdup(match));
}
......@@ -59,7 +59,7 @@ InfoScreenClass TraceScreen_class = {
};
TraceScreen* TraceScreen_new(Process* process) {
TraceScreen* this = malloc(sizeof(TraceScreen));
TraceScreen* this = xMalloc(sizeof(TraceScreen));
Object_setClass(this, Class(TraceScreen));
this->tracing = true;
this->contLine = false;
......
......@@ -6,6 +6,7 @@ in the source distribution for its full text.
*/
#include "UsersTable.h"
#include "XAlloc.h"
#include "config.h"
......@@ -27,7 +28,7 @@ typedef struct UsersTable_ {
UsersTable* UsersTable_new() {
UsersTable* this;
this = malloc(sizeof(UsersTable));
this = xMalloc(sizeof(UsersTable));
this->users = Hashtable_new(20, true);
return this;
}
......@@ -42,7 +43,7 @@ char* UsersTable_getRef(UsersTable* this, unsigned int uid) {
if (name == NULL) {
struct passwd* userData = getpwuid(uid);
if (userData != NULL) {
name = strdup(userData->pw_name);
name = xStrdup(userData->pw_name);
Hashtable_put(this->users, uid, name);
}
}
......
......@@ -37,9 +37,9 @@ Vector* Vector_new(ObjectClass* type, bool owner, int size) {
if (size == DEFAULT_SIZE)
size = 10;
this = malloc(sizeof(Vector));
this = xMalloc(sizeof(Vector));
this->growthRate = size;
this->array = (Object**) calloc(size, sizeof(Object*));
this->array = (Object**) xCalloc(size, sizeof(Object*));
this->arraySize = size;
this->items = 0;
this->type = type;
......@@ -179,7 +179,7 @@ static void Vector_checkArraySize(Vector* this) {
//int i;
//i = this->arraySize;
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++)
// this->array[i] = NULL;
}
......
#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;
}
/* 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
......@@ -39,7 +39,7 @@ ProcessClass DarwinProcess_class = {
};
DarwinProcess* DarwinProcess_new(Settings* settings) {
DarwinProcess* this = calloc(1, sizeof(DarwinProcess));
DarwinProcess* this = xCalloc(1, sizeof(DarwinProcess));
Object_setClass(this, Class(DarwinProcess));
Process_init(&this->super, settings);
......@@ -85,7 +85,7 @@ char *DarwinProcess_getCmdLine(struct kinfo_proc* k, int show_args ) {
}
/* Allocate space for the arguments. */
procargs = ( char * ) malloc( argmax );
procargs = ( char * ) xMalloc( argmax );
if ( procargs == NULL ) {
goto ERROR_A;
}
......@@ -237,7 +237,7 @@ char *DarwinProcess_getCmdLine(struct kinfo_proc* k, int show_args ) {
}
/* Make a copy of the string. */
retval = strdup(sp);
retval = xStrdup(sp);
/* Clean up. */
free( procargs );
......@@ -247,7 +247,7 @@ char *DarwinProcess_getCmdLine(struct kinfo_proc* k, int show_args ) {
ERROR_B:
free( procargs );
ERROR_A:
retval = strdup(k->kp_proc.p_comm);
retval = xStrdup(k->kp_proc.p_comm);
return retval;
}
......
......@@ -87,7 +87,7 @@ struct kinfo_proc *ProcessList_getKInfoProcs(size_t *count) {
if (sysctl(mib, 4, NULL, count, NULL, 0) < 0)
CRT_fatalError("Unable to get size of kproc_infos");
processes = malloc(*count);
processes = xMalloc(*count);
if (processes == NULL)
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) {
DarwinProcessList* this = calloc(1, sizeof(DarwinProcessList));
DarwinProcessList* this = xCalloc(1, sizeof(DarwinProcessList));
ProcessList_init(&this->super, Class(Process), usersTable, pidWhiteList, userId);
......
......@@ -251,7 +251,7 @@ char* Platform_getProcessEnv(pid_t pid) {
mib[0] = CTL_KERN;
mib[1] = KERN_ARGMAX;
if (sysctl(mib, 2, &argmax, &bufsz, 0, 0) == 0) {
char* buf = malloc(argmax);
char* buf = xMalloc(argmax);
if (buf) {
mib[0] = CTL_KERN;
mib[1] = KERN_PROCARGS2;
......@@ -279,7 +279,7 @@ char* Platform_getProcessEnv(pid_t pid) {
++p;
size_t size = endp - p;
env = malloc(size+2);
env = xMalloc(size+2);
if (env) {
memcpy(env, p, size);
env[size] = 0;
......
......@@ -97,7 +97,7 @@ ProcessPidColumn Process_pidColumns[] = {
};
FreeBSDProcess* FreeBSDProcess_new(Settings* settings) {
FreeBSDProcess* this = calloc(1, sizeof(FreeBSDProcess));
FreeBSDProcess* this = xCalloc(1, sizeof(FreeBSDProcess));
Object_setClass(this, Class(FreeBSDProcess));
Process_init(&this->super, settings);
return this;
......
......@@ -86,7 +86,7 @@ static int kernelFScale;
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_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;
len = 2; sysctlnametomib("kern.cp_time", MIB_kern_cp_time, &len);
fpl->cp_time_o = calloc(cpus, sizeof_cp_time_array);
fpl->cp_time_n = calloc(cpus, sizeof_cp_time_array);
fpl->cp_time_o = xCalloc(cpus, sizeof_cp_time_array);
fpl->cp_time_n = xCalloc(cpus, sizeof_cp_time_array);
len = sizeof_cp_time_array;
// 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
if (cpus > 1) {
len = 2; sysctlnametomib("kern.cp_times", MIB_kern_cp_times, &len);
fpl->cp_times_o = calloc(cpus, sizeof_cp_time_array);
fpl->cp_times_n = calloc(cpus, sizeof_cp_time_array);
fpl->cp_times_o = xCalloc(cpus, sizeof_cp_time_array);
fpl->cp_times_n = xCalloc(cpus, sizeof_cp_time_array);
len = cpus * sizeof_cp_time_array;
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);
if (cpus == 1 ) {
fpl->cpus = realloc(fpl->cpus, sizeof(CPUData));
fpl->cpus = xRealloc(fpl->cpus, sizeof(CPUData));
} else {
// 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** argv = kvm_getargv(kd, kproc, 0);
if (!argv) {
return strdup(kproc->ki_comm);
return xStrdup(kproc->ki_comm);
}
int len = 0;
for (int i = 0; argv[i]; i++) {
len += strlen(argv[i]) + 1;
}
char* comm = malloc(len);
char* comm = xMalloc(len);
char* at = comm;
*basenameEnd = 0;
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));
return NULL;
} else if (jid == kproc->ki_jid) {
jname = strdup(jnamebuf);
jname = xStrdup(jnamebuf);
if (jname == NULL)
strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
return jname;
......@@ -408,7 +408,7 @@ char* FreeBSDProcessList_readJailName(struct kinfo_proc* kproc) {
} else {
jnamebuf[0]='-';
jnamebuf[1]='\0';
jname = strdup(jnamebuf);
jname = xStrdup(jnamebuf);
}
return jname;
}
......
......@@ -128,7 +128,7 @@ static CommandLineSettings parseArguments(int argc, char** argv) {
flags.useColors = false;
break;
case 'p': {
char* argCopy = strdup(optarg);
char* argCopy = xStrdup(optarg);
char* saveptr;
char* pid = strtok_r(argCopy, ",", &saveptr);
......
......@@ -50,7 +50,7 @@ static unsigned long int parseBatInfo(const char *fileName, const unsigned short
char* entryName = dirEntry->d_name;
if (strncmp(entryName, "BAT", 3))
continue;
batteries[nBatteries] = strdup(entryName);
batteries[nBatteries] = xStrdup(entryName);
nBatteries++;
}
closedir(batteryDir);
......
......@@ -242,7 +242,7 @@ ProcessClass LinuxProcess_class = {
};
LinuxProcess* LinuxProcess_new(Settings* settings) {
LinuxProcess* this = calloc(1, sizeof(LinuxProcess));
LinuxProcess* this = xCalloc(1, sizeof(LinuxProcess));
Object_setClass(this, Class(LinuxProcess));
Process_init(&this->super, settings);
return this;
......
......@@ -89,7 +89,7 @@ typedef struct LinuxProcessList_ {
#endif
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_init(pl, Class(LinuxProcess), usersTable, pidWhiteList, userId);
......@@ -108,7 +108,7 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui
fclose(file);
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++) {
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);
FILE* file = fopen(filename, "r");
if (!file) {
process->cgroup = strdup("");
process->cgroup = xStrdup("");
return;
}
char output[PROC_LINE_LENGTH + 1];
......@@ -389,7 +389,7 @@ static void LinuxProcessList_readCGroupFile(LinuxProcess* process, const char* d
}
fclose(file);
free(process->cgroup);
process->cgroup = strdup(output);
process->cgroup = xStrdup(output);
}
#endif
......
......@@ -220,16 +220,16 @@ char* Platform_getProcessEnv(pid_t pid) {
char *env = NULL;
if (fd) {
size_t capacity = 4096, size = 0, bytes;
env = malloc(capacity);
env = xMalloc(capacity);
while (env && (bytes = fread(env+size, 1, capacity-size, fd)) > 0) {
size += bytes;
capacity *= 2;
env = realloc(env, capacity);
env = xRealloc(env, capacity);
}
fclose(fd);
if (size < 2 || env[size-1] || env[size-2]) {
if (size + 2 < capacity) {
env = realloc(env, capacity+2);
env = xRealloc(env, capacity+2);
}
env[size] = 0;
env[size+1] = 0;
......
......@@ -186,7 +186,7 @@ ProcessPidColumn Process_pidColumns[] = {
};
OpenBSDProcess* OpenBSDProcess_new(Settings* settings) {
OpenBSDProcess* this = calloc(sizeof(OpenBSDProcess), 1);
OpenBSDProcess* this = xCalloc(sizeof(OpenBSDProcess), 1);
Object_setClass(this, Class(OpenBSDProcess));
Process_init(&this->super, settings);
return this;
......
......@@ -53,7 +53,7 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui
int mib[] = { CTL_HW, HW_NCPU };
int fmib[] = { CTL_KERN, KERN_FSCALE };
int i, e;
OpenBSDProcessList* opl = calloc(1, sizeof(OpenBSDProcessList));
OpenBSDProcessList* opl = xCalloc(1, sizeof(OpenBSDProcessList));
ProcessList* pl = (ProcessList*) opl;
size_t size = sizeof(pl->cpuCount);
......@@ -62,7 +62,7 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui
if (e == -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);
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);
if (arg == NULL) {
if ((s = strdup(kproc->p_comm)) == NULL) {
if ((s = xStrdup(kproc->p_comm)) == NULL) {
err(1, NULL);
}
return s;
......@@ -152,8 +152,8 @@ char *OpenBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, in
for (i = 0; arg[i] != NULL; i++) {
len += strlen(arg[i]) + 1;
}
if ((buf = s = malloc(len)) == NULL) {
if ((s = strdup(kproc->p_comm)) == NULL) {
if ((buf = s = xMalloc(len)) == NULL) {
if ((s = xStrdup(kproc->p_comm)) == NULL) {
err(1, NULL);
}
return s;
......
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