Commit 445222e4 authored by Michael McConville's avatar Michael McConville
Browse files

Clean up some needless malloc casts, convert some mallocs to callocs, and fix some style

parent 1d805b36
......@@ -52,8 +52,8 @@ FunctionBar* FunctionBar_new(const char** functions, const char** keys, int* eve
}
if (keys && events) {
this->staticData = false;
this->keys = malloc(sizeof(char*) * 15);
this->events = malloc(sizeof(int) * 15);
this->keys = calloc(15, sizeof(char*));
this->events = calloc(15, sizeof(int));
int i = 0;
while (i < 15 && functions[i]) {
this->keys[i] = strdup(keys[i]);
......
......@@ -63,7 +63,7 @@ int Hashtable_count(Hashtable* this) {
static HashtableItem* HashtableItem_new(unsigned int key, void* value) {
HashtableItem* this;
this = (HashtableItem*) malloc(sizeof(HashtableItem));
this = malloc(sizeof(HashtableItem));
this->key = key;
this->value = value;
this->next = NULL;
......@@ -73,7 +73,7 @@ static HashtableItem* HashtableItem_new(unsigned int key, void* value) {
Hashtable* Hashtable_new(int size, bool owner) {
Hashtable* this;
this = (Hashtable*) malloc(sizeof(Hashtable));
this = malloc(sizeof(Hashtable));
this->items = 0;
this->size = size;
this->buckets = (HashtableItem**) calloc(size, sizeof(HashtableItem*));
......
......@@ -58,7 +58,7 @@ static const char* OpenFilesScreenKeys[] = {"F3", "F4", "F5", "Esc"};
static int OpenFilesScreenEvents[] = {KEY_F(3), KEY_F(4), KEY_F(5), 27};
OpenFilesScreen* OpenFilesScreen_new(Process* process) {
OpenFilesScreen* this = (OpenFilesScreen*) malloc(sizeof(OpenFilesScreen));
OpenFilesScreen* this = malloc(sizeof(OpenFilesScreen));
this->process = process;
FunctionBar* bar = FunctionBar_new(OpenFilesScreenFunctions, OpenFilesScreenKeys, OpenFilesScreenEvents);
this->display = Panel_new(0, 1, COLS, LINES-3, false, Class(ListItem), bar);
......
......@@ -55,13 +55,13 @@ 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 = (char**) malloc(sizeof(char*) * rate);
char** out = calloc(rate, sizeof(char**));
int ctr = 0;
int blocks = rate;
char* where;
while ((where = strchr(s, sep)) != NULL) {
int size = where - s;
char* token = (char*) malloc(size + 1);
char* token = malloc(size + 1);
strncpy(token, s, size);
token[size] = '\0';
out[ctr] = token;
......@@ -80,7 +80,7 @@ char** String_split(const char* s, char sep, int* n) {
}
if (s[0] != '\0') {
int size = strlen(s);
char* token = (char*) malloc(size + 1);
char* token = malloc(size + 1);
strncpy(token, s, size + 1);
out[ctr] = token;
ctr++;
......
......@@ -44,7 +44,7 @@ static const char* TraceScreenKeys[] = {"F3", "F4", "F8", "F9", "Esc"};
static int TraceScreenEvents[] = {KEY_F(3), KEY_F(4), KEY_F(8), KEY_F(9), 27};
TraceScreen* TraceScreen_new(Process* process) {
TraceScreen* this = (TraceScreen*) malloc(sizeof(TraceScreen));
TraceScreen* this = malloc(sizeof(TraceScreen));
this->process = process;
FunctionBar* fuBar = FunctionBar_new(TraceScreenFunctions, TraceScreenKeys, TraceScreenEvents);
this->display = Panel_new(0, 1, COLS, LINES-2, false, Class(ListItem), fuBar);
......
......@@ -37,7 +37,7 @@ Vector* Vector_new(ObjectClass* type, bool owner, int size) {
if (size == DEFAULT_SIZE)
size = 10;
this = (Vector*) malloc(sizeof(Vector));
this = malloc(sizeof(Vector));
this->growthRate = size;
this->array = (Object**) calloc(size, sizeof(Object*));
this->arraySize = size;
......
......@@ -73,10 +73,8 @@ unsigned ProcessList_allocateCPULoadInfo(processor_cpu_load_info_t *p) {
void ProcessList_getVMStats(vm_statistics64_t p) {
mach_msg_type_number_t info_size = HOST_VM_INFO64_COUNT;
if(0 != host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info_t)p, &info_size)) {
fprintf(stderr, "Unable to retrieve VM statistics\n");
exit(9);
}
if (host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info_t)p, &info_size) != 0)
err(9, "Unable to retrieve VM statistics\n");
}
struct kinfo_proc *ProcessList_getKInfoProcs(size_t *count) {
......@@ -88,21 +86,15 @@ struct kinfo_proc *ProcessList_getKInfoProcs(size_t *count) {
* process entry or two.
*/
*count = 0;
if(0 > sysctl(mib, 4, NULL, count, NULL, 0)) {
fprintf(stderr, "Unable to get size of kproc_infos");
exit(5);
}
if (sysctl(mib, 4, NULL, count, NULL, 0) < 0)
err(5, "Unable to get size of kproc_infos");
processes = (struct kinfo_proc *)malloc(*count);
if(NULL == processes) {
fprintf(stderr, "Out of memory for kproc_infos\n");
exit(6);
}
processes = malloc(*count);
if (processes == NULL)
err(6, "Out of memory for kproc_infos");
if(0 > sysctl(mib, 4, processes, count, NULL, 0)) {
fprintf(stderr, "Unable to get kinfo_procs\n");
exit(7);
}
if (sysctl(mib, 4, processes, count, NULL, 0) < 0)
err(7, "Unable to get kinfo_procs");
*count = *count / sizeof(struct kinfo_proc);
......
......@@ -115,7 +115,7 @@ char* FreeBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, in
for (int i = 0; argv[i]; i++) {
len += strlen(argv[i]) + 1;
}
char* comm = malloc(len * sizeof(char));
char* comm = malloc(len);
char* at = comm;
*basenameEnd = 0;
for (int i = 0; argv[i]; i++) {
......
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