From 445222e48c380bbb5d209a82f9614187bc751b41 Mon Sep 17 00:00:00 2001 From: Michael McConville Date: Wed, 16 Sep 2015 23:42:36 -0400 Subject: [PATCH] Clean up some needless malloc casts, convert some mallocs to callocs, and fix some style --- FunctionBar.c | 4 ++-- Hashtable.c | 4 ++-- OpenFilesScreen.c | 2 +- StringUtils.c | 6 +++--- TraceScreen.c | 2 +- Vector.c | 2 +- darwin/DarwinProcessList.c | 26 +++++++++----------------- freebsd/FreeBSDProcessList.c | 2 +- 8 files changed, 20 insertions(+), 28 deletions(-) diff --git a/FunctionBar.c b/FunctionBar.c index 55e1f9a2..ab44839a 100644 --- a/FunctionBar.c +++ b/FunctionBar.c @@ -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]); diff --git a/Hashtable.c b/Hashtable.c index 0f61519e..9cb2f932 100644 --- a/Hashtable.c +++ b/Hashtable.c @@ -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*)); diff --git a/OpenFilesScreen.c b/OpenFilesScreen.c index 86eb3f8e..2a5e20af 100644 --- a/OpenFilesScreen.c +++ b/OpenFilesScreen.c @@ -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); diff --git a/StringUtils.c b/StringUtils.c index 834b4311..54a61584 100644 --- a/StringUtils.c +++ b/StringUtils.c @@ -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++; diff --git a/TraceScreen.c b/TraceScreen.c index 48107438..ecd0c0ab 100644 --- a/TraceScreen.c +++ b/TraceScreen.c @@ -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); diff --git a/Vector.c b/Vector.c index c1e2768a..8200564c 100644 --- a/Vector.c +++ b/Vector.c @@ -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; diff --git a/darwin/DarwinProcessList.c b/darwin/DarwinProcessList.c index 5d125891..9dbe4c89 100644 --- a/darwin/DarwinProcessList.c +++ b/darwin/DarwinProcessList.c @@ -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); diff --git a/freebsd/FreeBSDProcessList.c b/freebsd/FreeBSDProcessList.c index cc51a7cd..4abe7b8c 100644 --- a/freebsd/FreeBSDProcessList.c +++ b/freebsd/FreeBSDProcessList.c @@ -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++) {