// creates and initializes an empty closure LClosure *l_closure_new(LNode *node) { l_debug(L_DEBUG_STACK) printf("+++ creating new closure\n"); LClosure *closure = GC_MALLOC(sizeof(LClosure)); closure->vars = create_hashmap(); closure->locals = create_hashmap(); closure->parent = NULL; closure->cloneable = true; closure->node = node; return closure; }
// creates a new closure from the given parent closure LClosure *l_closure_clone(LClosure *parent, LNode *node) { l_debug(L_DEBUG_STACK) printf("+++ cloning closure\n"); if(!parent->cloneable) return parent; LClosure *closure = GC_MALLOC(sizeof(LClosure)); closure->vars = create_hashmap(); closure->locals = create_hashmap(); closure->parent = parent; closure->cloneable = true; closure->node = node; // copy vars from function closure l_clone_vars(parent->vars, closure->vars); l_clone_vars(parent->locals, closure->locals); return closure; }
void set_value(hashmap* container, const char* key, const char* value) { hashmap* current = container; hashmap* previous = NULL; while(current && strcmp(current->key, key) != 0) { previous = current; current = current->next; } if(current) { free(current->value); current->value = malloc(strlen(value)+1); assert(current->value != NULL); strcpy(current->value, value); } else { previous->next = create_hashmap(key, value); } }
LValue *l_func_require(LValue *args, LClosure *closure) { if(l_loaded_libs == NULL) l_loaded_libs = create_hashmap(); int i; char *p; LValue *path; closure = l_closure_root(closure); for(i=0; i<args->core.list->length; i++) { path = l_list_get(args, i); l_assert_is(path, L_STR_TYPE, "Path for require must be a string.", closure); p = path->core.str->str; // TODO search in cwd, then in lib/extra if(!hashmap_get(l_loaded_libs, p)) { l_eval_path(p, closure); hashmap_put(l_loaded_libs, p, p, sizeof(p)); } } return args; }
int main() { unsigned int key_space = 1024; HashMap * hm = create_hashmap(key_space); char * string_1 = "TI2725-C"; char * string_2 = "Embedded"; char * string_3 = "Software"; const char * key_1 = "ab"; const char * key_2 = "cd"; const char * key_3 = "ad"; const char * key_4 = "xy"; // Insert ("ab" -> "TI2725-C"). insert_data(hm, key_1, string_1, resolve_collision); assert(memcmp(get_data(hm, key_1), string_1, mystrlen(string_1)) == 0); // Insert ("cd" -> "Embedded"). insert_data(hm, key_2, string_2, resolve_collision); assert(memcmp(get_data(hm, key_2), string_2, mystrlen(string_2)) == 0); // Insert ("ad" -> "Software"). insert_data(hm, key_3, string_3, resolve_collision); assert(memcmp(get_data(hm, key_3), string_3, mystrlen(string_3)) == 0); // Insert ("ab" -> "Embedded"). insert_data(hm, key_1, string_2, resolve_collision); assert(memcmp(get_data(hm, key_1), string_2, mystrlen(string_2)) == 0); // Get data for a not inserted key. assert(get_data(hm, key_4) == NULL); // Iterate the hash map iterate(hm, print_element); #ifdef NEW_HASH set_hash_function(hm, your_own_hash); printf("\nHERE WE GO AGAIN!\n\n"); // Iterate the hash map iterate(hm, print_element); #endif // Delete key "cd". remove_data(hm, key_2, NULL); assert(get_data(hm, key_2) == NULL); // Delete key "ab". remove_data(hm, key_1, NULL); assert(get_data(hm, key_1) == NULL); // Delete key "ad". remove_data(hm, key_3, NULL); assert(get_data(hm, key_3) == NULL); // Delete the hash map. delete_hashmap(hm, NULL); #ifdef COUNTING_WORDS // Create a temporary file FILE *stream = tmpfile(); // Write to the stream fprintf(stream, "foo bar_, baz!\n"); fprintf(stream, "foo\t\"bar\".\n"); fprintf(stream, "foo?\n"); // Set the position to the start of the stream fseek(stream, 0, SEEK_SET); // Count the words count_words(stream); // Close the file fclose(stream); #endif return 0; }
int main(int argc, char *argv[]) { pthread_t t_cleanup; // thread for cleaning up dead sessions. pthread_t t_healthagent; // thread for health agent. pthread_t t_cli; // thread for cli. pthread_t t_counters; // thread for performance counters. pthread_t t_memorymanager; // thread for the memory manager. struct ifaddrs *ifaddr;//, *ifa; __u32 tempIP; // int s; int i; char message[LOGSZ]; char strIP[20]; // char host[NI_MAXHOST]; char path[100] = "/etc/opennop/opennop.conf"; int tmp; __u32 packet_size = PACKET_SIZE, packet_number = PACKET_NUMBER, thr_num = DEF_THR_NUM; __u32 fpPerPkt = DEF_FP_PER_PKT, fpsFactor = DEF_FPS_FACTOR; #if defined(DEBUG) int daemonize = false; #else int daemonize = true; #endif /* Setup signal handling */ signal(SIGHUP, signal_handler); signal(SIGTERM, signal_handler); signal(SIGINT, signal_handler); signal(SIGQUIT, signal_handler); signal(SIGPIPE,SIG_IGN); int c; while ((c = getopt(argc, argv, "nh|help")) != -1) { switch (c) { case 'h': PrintUsage(argc, argv); exit(0); break; case 'n': daemonize = 0; isdaemon = false; break; default: PrintUsage(argc, argv); break; } } sprintf(message, "Initialization: %s daemon starting up.\n", DAEMON_NAME); logger(LOG_INFO, message); /* * Configuration: read the configuration file */ sprintf(message, "Configuring: reading file %s.\n", path); logger(LOG_INFO, message); tmp = configure(path, &localID, &packet_number, &packet_size, &thr_num, &fpPerPkt, &fpsFactor); if(tmp == 1){ sprintf(message, "Configuring error. EXIT\n"); logger(LOG_INFO, message); exit(EXIT_FAILURE); } sprintf(message, "Initialization: Version %s.\n", VERSION); logger(LOG_INFO, message); /* * Get the numerically highest local IP address. * This will be used as the acceleratorID. */ if (getifaddrs(&ifaddr) == -1) { sprintf(message, "Initialization: Error opening interfaces.\n"); logger(LOG_INFO, message); exit(EXIT_FAILURE); } // for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { // loop through all interfaces. // // if (ifa->ifa_addr != NULL) { // // if (ifa->ifa_addr->sa_family == AF_INET) { // get all IPv4 addresses. // s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), // host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); // // if (s != 0) { // exit(EXIT_FAILURE); // } // // inet_pton(AF_INET, (char *) &host, &tempIP); // convert string to decimal. // // /* // * Lets fine the largest local IP, and use that as accelleratorID // * Lets also exclude 127.0.0.1 as a valid ID. // */ // if ((tempIP > localID) && (tempIP != LOOPBACKIP)) { // localID = tempIP; // } // } // end get all IPv4 addresses. // } // end ifa->ifa_addr NULL test. // } // end loop through all interfaces. if (localID == 0) { // fail if no usable IP found. inet_ntop(AF_INET, &tempIP, strIP, INET_ADDRSTRLEN); sprintf(message, "Initialization: No usable IP Address. %s\n", strIP); logger(LOG_INFO, message); exit(EXIT_FAILURE); } #if defined(DEBUG) setlogmask(LOG_UPTO(LOG_DEBUG)); openlog(DAEMON_NAME, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER); #else setlogmask(LOG_UPTO(LOG_INFO)); openlog(DAEMON_NAME, LOG_CONS, LOG_USER); #endif /* Our process ID and Session ID */ pid_t pid, sid; if (daemonize) { sprintf(message, "Initialization: Daemonizing the %s process.\n", DAEMON_NAME); logger(LOG_INFO, message); /* Fork off the parent process */ pid = fork(); if (pid < 0) { exit(EXIT_FAILURE); } /* If we got a good PID, then we can exit the parent process. */ if (pid > 0) { exit(EXIT_SUCCESS); } /* Change the file mode mask */ umask(0); /* Create a new SID for the child process */ sid = setsid(); if (sid < 0) { /* Log the failure */ exit(EXIT_FAILURE); } /* Change the current working directory */ if ((chdir("/")) < 0) { /* Log the failure */ exit(EXIT_FAILURE); } /* Close out the standard file descriptors */ close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); } /* * Starting up the daemon. */ initialize_sessiontable(); if (get_workers() == 0) { set_workers(thr_num); //set_workers(sysconf(_SC_NPROCESSORS_ONLN) * 2); } #ifdef ROLLING init_common(packet_number,packet_size, fpPerPkt, fpsFactor, shareddict); init_debugd() ; #endif for (i = 0; i < get_workers(); i++) { create_worker(i); } #ifdef BASIC create_hashmap(&ht); // Create hash table #endif // Only one type of optimization can be active // assert(deduplication != compression); /* * Create the fetcher thread that retrieves * IP packets from the Netfilter Queue. */ create_fetcher(); pthread_create(&t_cleanup, NULL, cleanup_function, (void *) NULL); pthread_create(&t_healthagent, NULL, healthagent_function, (void *) NULL); pthread_create(&t_cli, NULL, cli_manager_init, (void *) NULL); pthread_create(&t_counters, NULL, counters_function, (void *) NULL); pthread_create(&t_memorymanager, NULL, memorymanager_function, (void *) NULL); sprintf(message, "[OpenNOP]: Started all threads.\n"); logger(LOG_INFO, message); /* * All the threads have been created now commands should be registered. */ register_command("show traces mask", cli_show_traces_mask, false, false); register_command("traces mask orr", cli_traces_mask_orr, true, false); register_command("traces mask and", cli_traces_mask_and, true, false); register_command("traces mask nand", cli_traces_mask_nand, true, false); register_command("reset stats in_dedup", cli_reset_stats_in_dedup, false, false); register_command("reset stats in_dedup_thread", cli_reset_stats_in_dedup, false, false); register_command("show stats in_dedup", cli_show_stats_in_dedup, false, false); register_command("show stats in_dedup_thread", cli_show_stats_in_dedup_thread, false, false); register_command("reset stats out_dedup", cli_reset_stats_out_dedup, false, false); register_command("reset stats out_dedup_thread", cli_reset_stats_out_dedup, false, false); register_command("show stats out_dedup", cli_show_stats_out_dedup, false, false); register_command("show stats out_dedup_thread", cli_show_stats_out_dedup_thread, false, false); register_command("traces enable", cli_traces_enable, true, false); register_command("traces disable", cli_traces_disable, true, false); register_command("show version", cli_show_version, false, false); register_command("show compression", cli_show_compression, false, false); register_command("show workers", cli_show_workers, false, false); register_command("show fetcher", cli_show_fetcher, false, false); register_command("show sessions", cli_show_sessionss, false, false); register_command("show deduplication", cli_show_deduplication, false, false); register_command("compression enable", cli_compression_enable, false, false); register_command("compression disable", cli_compression_disable, false, false); register_command("deduplication enable", cli_deduplication_enable, false, false); register_command("deduplication disable", cli_deduplication_disable, false, false); /* * Rejoin all threads before we exit! */ rejoin_fetcher(); sprintf(message, "[OpenNOP] Fetcher joined\n"); logger(LOG_INFO, message); pthread_join(t_cleanup, NULL); sprintf(message, "joined cleanup\n"); logger(LOG_INFO, message); pthread_join(t_healthagent, NULL); sprintf(message, "joined health\n"); logger(LOG_INFO, message); pthread_join(t_cli, NULL); sprintf(message, "joined cli\n"); logger(LOG_INFO, message); pthread_join(t_counters, NULL); sprintf(message, "joined counters\n"); logger(LOG_INFO, message); pthread_join(t_memorymanager, NULL); for (i = 0; i < get_workers(); i++) { rejoin_worker(i); } #ifdef BASIC remove_hashmap(ht); #endif clear_sessiontable(); sprintf(message, "Exiting: %s daemon exiting", DAEMON_NAME); logger(LOG_INFO, message); exit(EXIT_SUCCESS); }