Skip to content

Commit

Permalink
Clean up some needless malloc casts, convert some mallocs to callocs,…
Browse files Browse the repository at this point in the history
… and fix some style
  • Loading branch information
Michael McConville committed Sep 17, 2015
1 parent 1d805b3 commit 445222e
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 28 deletions.
4 changes: 2 additions & 2 deletions FunctionBar.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
4 changes: 2 additions & 2 deletions Hashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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*));
Expand Down
2 changes: 1 addition & 1 deletion OpenFilesScreen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions StringUtils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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++;
Expand Down
2 changes: 1 addition & 1 deletion TraceScreen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
26 changes: 9 additions & 17 deletions darwin/DarwinProcessList.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion freebsd/FreeBSDProcessList.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down

0 comments on commit 445222e

Please sign in to comment.