Skip to content

Commit

Permalink
Minor changes for switch to cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
bensong04 committed Feb 3, 2024
1 parent 23e2e92 commit 238e583
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 30 deletions.
38 changes: 12 additions & 26 deletions src/leaffunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,12 @@

#define UBA_DEFAULT_SIZE 10

leaffn_t* new_leaffn(leaffn_t* caller, char* fn_ident) {
leaffn_t* new_leaf = malloc(sizeof(leaffn_t));
leaffn_t* new_leaffn(leaffn_t* caller, std::string fn_ident) {
leaffn_t* new_leaf = (leaffn_t*) malloc(sizeof(leaffn_t));
if (new_leaf == NULL) return NULL; // allocating memory for the header failed
new_leaf->total_energy_usage = 0;
new_leaf->caller = caller;
new_leaf->fn_ident = (char*)malloc(strlen(fn_ident) + 1); // allocate enough memory for the
// null terminating byte
if (new_leaf->fn_ident == NULL) { // allocating memory for the identifier failed
free(new_leaf); // free the header
return NULL;
}
strncpy(new_leaf->fn_ident, fn_ident, strlen(fn_ident));
new_leaf->fn_ident[strlen(fn_ident)] = '\0'; // manually setting the terminating byte
// new_leaf->callees = uba_new(UBA_DEFAULT_SIZE, sizeof(leaffn_t));
// if (new_leaf->callees == NULL) {
// free(new_leaf->fn_ident);
// free(new_leaf);
// return NULL;
// }
new_leaf->fn_ident = fn_ident;
return new_leaf;
}

Expand All @@ -49,28 +36,27 @@ leaffn_t* get_caller(leaffn_t* fn_node) {
return fn_node->caller;
}

std::vector<leaffn_t*> get_callees(leaffn_t* fn_node) {
std::unordered_map<std::string, leaffn_t*> get_callees(leaffn_t* fn_node) {
return fn_node->callees;
}

char* get_fn_ident(leaffn_t* fn_node) {
std::string get_fn_ident(leaffn_t* fn_node) {
return fn_node->fn_ident;
}

leaffn_t* find_callee(leaffn_t* fn_node, char* fn_ident) {
std::vector<leaffn_t*> callees = fn_node->callees;
size_t num_callees = callees.size();
for (size_t callee_idx = 0; callee_idx < num_callees; callee_idx++) {
// TODO
leaffn_t* find_callee(leaffn_t* fn_node, std::string fn_ident) {
std::unordered_map<std::string, leaffn_t*> callees = fn_node->callees;
leaffn_t* found_callee = callees[fn_ident];
if (found_callee == NULL) { // hopefully true
callees[fn_ident] = new_leaffn(fn_node, fn_ident);
}

}

void add_callee(leaffn_t* fn_node, leaffn_t* new_callee) {
fn_node->callees.push_back(new_callee);
fn_node->callees.insert({new_callee->fn_ident, new_callee});
}

void free_leaffn(leaffn_t* fn_node) {
uba_free(fn_node->callees);
free(fn_node->fn_ident);
free(fn_node);
}
9 changes: 5 additions & 4 deletions src/leaffunction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
#pragma once

#include <vector>
#include <map>
#include <stdint.h>

typedef struct leaffunction_header {
uint64_t total_energy_usage;
struct leaffunction_header* caller;
std::vector<struct leaffunction_header*> callees; // pointer abuse: cast leaffn_t* to char* and back
char* fn_ident;
std::unordered_map<std::string, struct leaffunction_header*> callees;
std::string fn_ident;
} leaffn_t;

leaffn_t* new_leaffn(leaffn_t* caller, char* fn_ident);
Expand All @@ -27,12 +28,12 @@ uint64_t increment_total_energy_usage(leaffn_t* fn_node, uint32_t energy_uj);

leaffn_t* get_caller(leaffn_t* fn_node);

uba_t* get_callees(leaffn_t* fn_node);
std::unordered_map<std::string, struct leaffunction_header*> get_callees(leaffn_t* fn_node);

leaffn_t* find_callee(leaffn_t* fn_node, char* fn_ident);

void add_callee(leaffn_t* fn_node, leaffn_t* new_callee);

char* get_fn_ident(leaffn_t* fn_node);
std::string get_fn_ident(leaffn_t* fn_node);

void free_leaffn(leaffn_t* fn_node);

0 comments on commit 238e583

Please sign in to comment.