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

This commit is contained in:
Michael McConville 2015-09-16 23:42:36 -04:00
parent 1d805b36b4
commit 445222e48c
8 changed files with 20 additions and 28 deletions

View File

@ -52,8 +52,8 @@ FunctionBar* FunctionBar_new(const char** functions, const char** keys, int* eve
} }
if (keys && events) { if (keys && events) {
this->staticData = false; this->staticData = false;
this->keys = malloc(sizeof(char*) * 15); this->keys = calloc(15, sizeof(char*));
this->events = malloc(sizeof(int) * 15); this->events = calloc(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] = strdup(keys[i]);

View File

@ -63,7 +63,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 = (HashtableItem*) malloc(sizeof(HashtableItem)); this = malloc(sizeof(HashtableItem));
this->key = key; this->key = key;
this->value = value; this->value = value;
this->next = NULL; this->next = NULL;
@ -73,7 +73,7 @@ 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 = (Hashtable*) malloc(sizeof(Hashtable)); this = malloc(sizeof(Hashtable));
this->items = 0; this->items = 0;
this->size = size; this->size = size;
this->buckets = (HashtableItem**) calloc(size, sizeof(HashtableItem*)); this->buckets = (HashtableItem**) calloc(size, sizeof(HashtableItem*));

View File

@ -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}; static int OpenFilesScreenEvents[] = {KEY_F(3), KEY_F(4), KEY_F(5), 27};
OpenFilesScreen* OpenFilesScreen_new(Process* process) { OpenFilesScreen* OpenFilesScreen_new(Process* process) {
OpenFilesScreen* this = (OpenFilesScreen*) malloc(sizeof(OpenFilesScreen)); OpenFilesScreen* this = malloc(sizeof(OpenFilesScreen));
this->process = process; this->process = process;
FunctionBar* bar = FunctionBar_new(OpenFilesScreenFunctions, OpenFilesScreenKeys, OpenFilesScreenEvents); FunctionBar* bar = FunctionBar_new(OpenFilesScreenFunctions, OpenFilesScreenKeys, OpenFilesScreenEvents);
this->display = Panel_new(0, 1, COLS, LINES-3, false, Class(ListItem), bar); this->display = Panel_new(0, 1, COLS, LINES-3, false, Class(ListItem), bar);

View File

@ -55,13 +55,13 @@ 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 = (char**) malloc(sizeof(char*) * rate); char** out = calloc(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 = (char*) malloc(size + 1); char* token = malloc(size + 1);
strncpy(token, s, size); strncpy(token, s, size);
token[size] = '\0'; token[size] = '\0';
out[ctr] = token; out[ctr] = token;
@ -80,7 +80,7 @@ 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 = (char*) malloc(size + 1); char* token = malloc(size + 1);
strncpy(token, s, size + 1); strncpy(token, s, size + 1);
out[ctr] = token; out[ctr] = token;
ctr++; ctr++;

View File

@ -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}; static int TraceScreenEvents[] = {KEY_F(3), KEY_F(4), KEY_F(8), KEY_F(9), 27};
TraceScreen* TraceScreen_new(Process* process) { TraceScreen* TraceScreen_new(Process* process) {
TraceScreen* this = (TraceScreen*) malloc(sizeof(TraceScreen)); TraceScreen* this = malloc(sizeof(TraceScreen));
this->process = process; this->process = process;
FunctionBar* fuBar = FunctionBar_new(TraceScreenFunctions, TraceScreenKeys, TraceScreenEvents); FunctionBar* fuBar = FunctionBar_new(TraceScreenFunctions, TraceScreenKeys, TraceScreenEvents);
this->display = Panel_new(0, 1, COLS, LINES-2, false, Class(ListItem), fuBar); this->display = Panel_new(0, 1, COLS, LINES-2, false, Class(ListItem), fuBar);

View File

@ -37,7 +37,7 @@ Vector* Vector_new(ObjectClass* type, bool owner, int size) {
if (size == DEFAULT_SIZE) if (size == DEFAULT_SIZE)
size = 10; size = 10;
this = (Vector*) malloc(sizeof(Vector)); this = malloc(sizeof(Vector));
this->growthRate = size; this->growthRate = size;
this->array = (Object**) calloc(size, sizeof(Object*)); this->array = (Object**) calloc(size, sizeof(Object*));
this->arraySize = size; this->arraySize = size;

View File

@ -73,10 +73,8 @@ unsigned ProcessList_allocateCPULoadInfo(processor_cpu_load_info_t *p) {
void ProcessList_getVMStats(vm_statistics64_t p) { void ProcessList_getVMStats(vm_statistics64_t p) {
mach_msg_type_number_t info_size = HOST_VM_INFO64_COUNT; 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)) { if (host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info_t)p, &info_size) != 0)
fprintf(stderr, "Unable to retrieve VM statistics\n"); err(9, "Unable to retrieve VM statistics\n");
exit(9);
}
} }
struct kinfo_proc *ProcessList_getKInfoProcs(size_t *count) { struct kinfo_proc *ProcessList_getKInfoProcs(size_t *count) {
@ -88,21 +86,15 @@ struct kinfo_proc *ProcessList_getKInfoProcs(size_t *count) {
* process entry or two. * process entry or two.
*/ */
*count = 0; *count = 0;
if(0 > sysctl(mib, 4, NULL, count, NULL, 0)) { if (sysctl(mib, 4, NULL, count, NULL, 0) < 0)
fprintf(stderr, "Unable to get size of kproc_infos"); err(5, "Unable to get size of kproc_infos");
exit(5);
}
processes = (struct kinfo_proc *)malloc(*count); processes = malloc(*count);
if(NULL == processes) { if (processes == NULL)
fprintf(stderr, "Out of memory for kproc_infos\n"); err(6, "Out of memory for kproc_infos");
exit(6);
}
if(0 > sysctl(mib, 4, processes, count, NULL, 0)) { if (sysctl(mib, 4, processes, count, NULL, 0) < 0)
fprintf(stderr, "Unable to get kinfo_procs\n"); err(7, "Unable to get kinfo_procs");
exit(7);
}
*count = *count / sizeof(struct kinfo_proc); *count = *count / sizeof(struct kinfo_proc);

View File

@ -115,7 +115,7 @@ char* FreeBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, in
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 * sizeof(char)); char* comm = malloc(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++) {