void jemallocTest(){ void *p; #ifdef WIN32 // If you use jemalloc through the static lib, // must be manual initialize jemalloc first. je_init(); #endif p = je_malloc(128); if (p) { printf("malloc(%u) result ptr = 0x%016" PRIXPTR "\n\n", 128, (unsigned long)p); je_free(p); } p = je_malloc(256); if (p) { printf("malloc(%u) result ptr = 0x%016" PRIXPTR "\n\n", 256, (unsigned long)p); je_free(p); } #ifdef WIN32 // Unload the jemalloc je_uninit(); #endif }
sflow_socket_t* sflow_socket_create(sflow_key_t* key, ss_frame_t* rx_buf) { int is_error = 0; sflow_key_dump("create socket for key", key); // XXX: should these be allocated from jemalloc or RTE alloc? sflow_socket_t* socket = je_calloc(1, sizeof(sflow_socket_t)); if (socket == NULL) { is_error = 1; goto error_out; } sflow_socket_init(key, socket); rte_rwlock_write_lock(&sflow_hash_lock); int32_t socket_id = rte_hash_add_key(sflow_hash, key); socket->id = (uint64_t) socket_id; if (socket_id >= 0) { sflow_sockets[socket->id] = socket; } else { is_error = 1; } rte_rwlock_write_unlock(&sflow_hash_lock); // XXX: figure out what should be in this //RTE_LOG(INFO, L3L4, "new sflow socket: sport: %hu dport: %hu id: %lu is_error: %d\n", // rte_bswap16(key->sport), rte_bswap16(key->dport), socket->id, is_error); error_out: if (unlikely(is_error)) { if (socket) { je_free(socket); socket = NULL; } RTE_LOG(ERR, L3L4, "failed to allocate sflow socket\n"); return NULL; } return socket; }
void chpl_mem_layerInit(void) { void* heap_base_; size_t heap_size_; chpl_comm_desired_shared_heap(&heap_base_, &heap_size_); if (heap_base_ != NULL && heap_size_ == 0) { chpl_internal_error("if heap address is specified, size must be also"); } // If we have a shared heap, initialize our shared heap. This will take care // of initializing jemalloc. If we're not using a shared heap, do a first // allocation to allow jemaloc to set up: // // jemalloc 4.0.4 man: "Once, when the first call is made to one of the // memory allocation routines, the allocator initializes its internals" if (heap_base_ != NULL) { heap_base = heap_base_; heap_size = heap_size_; cur_heap_offset = 0; if (pthread_mutex_init(&chunk_alloc_lock, NULL) != 0) { chpl_internal_error("cannot init chunk_alloc lock"); } initializeSharedHeap(); } else { void* p; if ((p = je_malloc(1)) == NULL) { chpl_internal_error("cannot init heap: je_malloc() failed"); } je_free(p); } }
~KMerVector() { #ifdef SPADES_USE_JEMALLOC je_free(storage_); #else delete[] storage_; #endif }
ElTy *realloc() { #ifdef SPADES_USE_JEMALLOC // First, try to expand in-place if (storage_ && sizeof(ElTy) * capacity_ * el_sz_ > 4096 && je_rallocm((void**)&storage_, NULL, sizeof(ElTy) * capacity_ * el_sz_, 0, ALLOCM_NO_MOVE) == ALLOCM_SUCCESS) return storage_; // Failed, do usual malloc / memcpy / free cycle ElTy *res = (ElTy*) je_malloc(sizeof(ElTy) * capacity_ * el_sz_); if (storage_) std::memcpy(res, storage_, size_ * sizeof(ElTy) * el_sz_); je_free(storage_); storage_ = res; #else // No JEMalloc, no cookies ElTy *res = new ElTy[capacity_ * el_sz_]; if (storage_) std:: memcpy(res, storage_, size_ * sizeof(ElTy) * el_sz_); delete[] storage_; storage_ = res; #endif return storage_; }
char* ss_conf_path_get() { size_t path_size = PATH_MAX; char* program_directory = NULL; char* program_path = je_calloc(1, path_size); if (program_path == NULL) { goto error_out; } ssize_t rv = readlink(PROGRAM_PATH, program_path, path_size); if (rv < 0) { goto error_out; } char* directory = dirname(program_path); program_directory = je_calloc(1, path_size); if (program_directory == NULL) { goto error_out; } strlcpy(program_directory, directory, path_size); strlcat(program_directory, CONF_PATH, path_size); error_out: if (program_path) je_free(program_path); return program_directory; }
utString utString::substr(const size_type start, size_type len_orig) const { utString s; size_type len = strlen(p); if (len_orig == npos) { len_orig = len - start; } if (start > len) { abort(); } if (len > len_orig) { len = len_orig; } je_free(s.p); s.p = malloc_never_null(len + 1); memcpy(s.p, p + start, len); s.p[len] = '\0'; return s; }
extern "C" void wrap(free)(void *p) { void* p1 = STUB_PTR(p); if (p1) fibjs::MemPool::global().remove(p1); je_free(p1); }
void mem_alloc::free(void * ptr, uint64_t size) { if (NO_FREE) {} DEBUG_M("free %ld 0x%lx\n",size,(uint64_t)ptr); #ifdef N_MALLOC std::free(ptr); #else je_free(ptr); #endif }
void free_impl(void *ptr) { if (MOZ_UNLIKELY(!replace_malloc_initialized)) init(); if (MOZ_LIKELY(!replace_free)) je_free(ptr); else replace_free(ptr); }
void fs_free(void* ptr){ #ifdef JEMALLOC_HAVE_ATTR je_free(ptr); #else free(ptr); #endif }
void DefaultAllocator::Free(void *p) { untrackAlloc(p); OVR_ALLOC_BENCHMARK_START(); #ifdef OVR_USE_JEMALLOC je_free(p); #else // OVR_USE_JEMALLOC free(p); #endif // OVR_USE_JEMALLOC OVR_ALLOC_BENCHMARK_END(); }
void _rmt_free(void *ptr, const char *name, int line) { ASSERT(ptr != NULL); log_debug(LOG_VVVERB, "free(%p) @ %s:%d", ptr, name, line); #ifdef RMT_JEMALLOC je_free(ptr); #else free(ptr); #endif }
static void zone_free(malloc_zone_t *zone, void *ptr) { if (ivsalloc(ptr, config_prof) != 0) { je_free(ptr); return; } free(ptr); }
static void zone_free_definite_size(malloc_zone_t *zone, void *ptr, size_t size) { if (ivsalloc(ptr, config_prof) != 0) { assert(ivsalloc(ptr, config_prof) == size); je_free(ptr); return; } free(ptr); }
void utString::fromBytes(const void *bytes, int len) { // Free old value if any. if(p != NULL) je_free(p); // Copy the bytes into p. p = (char*)je_malloc(len+1); memcpy(p, bytes, len); p[len] = 0; // Make sure we are NULL terminated. }
utString& utString::operator=(const char *s) { if (p != s) { // this should work with overlapping memory char *copy = strdup_never_null(s); je_free(p); p = copy; } return *this; }
static void zone_free_definite_size(malloc_zone_t *zone, void *ptr, size_t size) { size_t alloc_size; alloc_size = ivsalloc(tsdn_fetch(), ptr, config_prof); if (alloc_size != 0) { assert(alloc_size == size); je_free(ptr); return; } free(ptr); }
void Allocator::FreeAligned(void* p) { OVR_ALLOC_BENCHMARK_START(); #ifdef OVR_USE_JEMALLOC je_free(p); #else // OVR_USE_JEMALLOC if (p) { size_t src = size_t(p) - *(((size_t*)p) - 1); Free((void*)src); } #endif // OVR_USE_JEMALLOC OVR_ALLOC_BENCHMARK_END(); }
int sflow_socket_delete(sflow_key_t* key, bool is_locked) { sflow_key_dump("delete socket for key", key); if (likely(!is_locked)) rte_rwlock_write_lock(&sflow_hash_lock); int32_t socket_id = rte_hash_del_key(sflow_hash, key); sflow_socket_t* socket = ((int32_t) socket_id) < 0 ? NULL : sflow_sockets[socket_id]; if (likely(!is_locked)) rte_rwlock_write_unlock(&sflow_hash_lock); if (!socket) return -1; je_free(socket); sflow_sockets[socket_id] = NULL; return 0; }
int main (void) { size_t i; char * ptrs[num]; MALLOC_INIT(); for (i = 0; i < num; i++) { size_t size = (rand() % (max_size - min_size)) + min_size; ptrs[i] = je_malloc(size); memset(ptrs[i], 1, size); } for (i = 0; i < num; i++) je_free(ptrs[i]); return 0; }
void Memory_Pool_Test_C() { int i; #ifdef _DEBUG const int nMaxLoop = 100000; const int nMaxAlloc = 16384; #else const int nMaxLoop = 1000000; const int nMaxAlloc = 16384; //const int nMaxAlloc = 10000; #endif unsigned int dwUsedTime1, dwUsedTime2; unsigned int dwStartTickCount = GetTickCount(); for (i = 0; i < nMaxLoop; i++) { char *p = (char *)malloc((i % nMaxAlloc) + 1); if (p) free(p); } dwUsedTime1 = GetTickCount() - dwStartTickCount; mem_pool_printf("Alloc Func : malloc() Alloc Size: 1-%d\n", nMaxAlloc); mem_pool_printf("Alloc Count: %d\n", nMaxLoop); mem_pool_printf("Total Cost : %d ms.\n", dwUsedTime1); //system("pause"); mem_pool_printf("\n"); set_errno(0); je_init(); dwStartTickCount = GetTickCount(); for (i = 0; i < nMaxLoop; i++) { void *p = je_malloc((i % nMaxAlloc) + 1); if (p) je_free(p); } je_uninit(); dwUsedTime2 = GetTickCount() - dwStartTickCount; mem_pool_printf("Alloc Func : je_malloc() Alloc Size: 1-%d\n", nMaxAlloc); mem_pool_printf("Alloc Count: %d\n", nMaxLoop); mem_pool_printf("Total Cost : %d ms.\t Speed up: %0.2f %%\n", dwUsedTime2, ((double)(dwUsedTime1 * 10000 / dwUsedTime2) / 100.0)); //system("pause"); //mem_pool_printf("\n"); }
void free_all(allocs_info* info) { for (std::size_t i = 0; i < info->count; ++i) { #if defined(CRT_LFH) || defined(TBBMALLOC) || defined(TCMALLOC) delete[] info->memory[i]; #elif defined(MS_CONCURRENCY) Concurrency::Free(info->memory[i]); #elif defined(MY_ALLOCATOR) allocator.free(info->memory[i]); #elif defined(JEMALLOC) je_free(info->memory[i]); #endif info->memory[i] = nullptr; } info->count = 0; }
int ss_conf_destroy() { // XXX: destroy everything in ss_conf_t wordfree(&ss_conf->eal_vector); if (ss_conf->json) { json_object_put(ss_conf->json); ss_conf->json = NULL; } ss_pcap_chain_destroy(); ss_cidr_table_destroy(&ss_conf->cidr_table); ss_dns_chain_destroy(); ss_re_chain_destroy(); ss_ioc_chain_destroy(); // XXX: destroy ss_ioc_entry_t* tables je_free(ss_conf); return 0; }
extern "C" void* wrap(realloc)(void* p, size_t sz) { fibjs::MemPool& mp = fibjs::MemPool::global(); if (p == 0) { void* p1 = je_malloc(FULL_SIZE(sz)); if (p1) { memset(p1, 0, STUB_SIZE); mp.add(p1, sz); } return MEM_PTR(p1); } if (sz == 0) { void* p1 = STUB_PTR(p); if (p1) mp.remove(p1); je_free(p1); return 0; } void* p1 = STUB_PTR(p); mp.remove(p1); void* p2 = je_realloc(p1, FULL_SIZE(sz)); if (p2) { memset(p2, 0, STUB_SIZE); mp.add(p2, sz); } else mp.add(p1); return MEM_PTR(p2); }
void chpl_mem_layerInit(void) { void* start; size_t size; chpl_comm_desired_shared_heap(&start, &size); // // TODO (EJR 12/17/15): add support for shared heaps. I think we basically // need to create a custom chunk allocator. // // HPX-5 did this so I think we can too: // http://jemalloc-discuss.canonware.narkive.com/FzSQ4Qv4/need-help-in-porting-jemalloc // // http://www.canonware.com/pipermail/jemalloc-discuss/2015-October/001179.html // // TODO (EJR 12/17/15): when we support shared heaps, I need to remember to // update the third-party README // if (start || size) chpl_error("set CHPL_MEM to a more appropriate mem type", 0, 0); // // Do a first allocation, to allow jemalloc to set up: // // """ // Once, when the first call is made to one of the memory allocation // routines, the allocator initializes its internals based in part on various // options that can be specified at compile- or run-time. // """ // { void* p; if ((p = je_malloc(1)) == NULL) chpl_internal_error("cannot init heap: je_malloc() failed"); je_free(p); } }
int main(int argc,char * argv[]) { int i = 0, j = 0, rc; MDB_env *env; MDB_dbi dbi; MDB_val key, data; MDB_txn *txn; MDB_stat mst; MDB_cursor *cursor; int count; int *values; char sval[32]; char kval[sizeof(int)]; srand(time(NULL)); memset(sval, 0, sizeof(sval)); count = (rand()%384) + 64; values = (int *)je_malloc(count*sizeof(int)); for(i = 0;i<count;i++) { values[i] = rand()%1024; } E(mdb_env_create(&env)); E(mdb_env_set_mapsize(env, 10485760)); E(mdb_env_set_maxdbs(env, 4)); E(mdb_env_open(env, "/tmp/testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664)); E(mdb_txn_begin(env, NULL, 0, &txn)); E(mdb_open(txn, "id2", MDB_CREATE|MDB_DUPSORT, &dbi)); key.mv_size = sizeof(int); key.mv_data = kval; data.mv_size = sizeof(sval); data.mv_data = sval; printf("Adding %d values\n", count); for (i=0;i<count;i++) { if (!(i & 0x0f)) sprintf(kval, "%03x", values[i]); sprintf(sval, "%03x %d foo bar", values[i], values[i]); if (RES(MDB_KEYEXIST, mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA))) j++; } if (j) printf("%d duplicates skipped\n", j); E(mdb_txn_commit(txn)); E(mdb_env_stat(env, &mst)); E(mdb_txn_begin(env, NULL, 1, &txn)); E(mdb_cursor_open(txn, dbi, &cursor)); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { printf("key: %p %.*s, data: %p %.*s\n", key.mv_data, (int) key.mv_size, (char *) key.mv_data, data.mv_data, (int) data.mv_size, (char *) data.mv_data); } CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get"); mdb_cursor_close(cursor); mdb_txn_abort(txn); j=0; for (i= count - 1; i > -1; i-= (rand()%5)) { j++; txn=NULL; E(mdb_txn_begin(env, NULL, 0, &txn)); sprintf(kval, "%03x", values[i & ~0x0f]); sprintf(sval, "%03x %d foo bar", values[i], values[i]); key.mv_size = sizeof(int); key.mv_data = kval; data.mv_size = sizeof(sval); data.mv_data = sval; if (RES(MDB_NOTFOUND, mdb_del(txn, dbi, &key, &data))) { j--; mdb_txn_abort(txn); } else { E(mdb_txn_commit(txn)); } } je_free(values); printf("Deleted %d values\n", j); E(mdb_env_stat(env, &mst)); E(mdb_txn_begin(env, NULL, 1, &txn)); E(mdb_cursor_open(txn, dbi, &cursor)); printf("Cursor next\n"); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { printf("key: %.*s, data: %.*s\n", (int) key.mv_size, (char *) key.mv_data, (int) data.mv_size, (char *) data.mv_data); } CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get"); printf("Cursor prev\n"); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_PREV)) == 0) { printf("key: %.*s, data: %.*s\n", (int) key.mv_size, (char *) key.mv_data, (int) data.mv_size, (char *) data.mv_data); } CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get"); mdb_cursor_close(cursor); mdb_close(env, dbi); mdb_txn_abort(txn); mdb_env_close(env); return 0; }
uint8_t* ss_metadata_prepare_sflow( const char* source, const char* rule, nn_queue_t* nn_queue, sflow_sample_t* sample, ss_ioc_entry_t* iptr) { int irv; json_object* jobject = NULL; json_object* item = NULL; uint8_t* rv = NULL; uint8_t* jstring = NULL; jobject = json_object_new_object(); if (jobject == NULL) { RTE_LOG(ERR, EXTRACTOR, "could not allocate sflow json object\n"); goto error_out; } item = json_object_new_string(source); if (item == NULL) goto error_out; json_object_object_add(jobject, "source", item); item = json_object_new_int64((int64_t) __sync_add_and_fetch(&nn_queue->tx_messages, 1)); if (item == NULL) goto error_out; json_object_object_add(jobject, "seq_num", item); irv = ss_metadata_prepare_sflow_ip(jobject, "source_ip", &sample->source_ip); if (irv) goto error_out; irv = ss_metadata_prepare_sflow_ip(jobject, "agent_ip", &sample->agent_ip); if (irv) goto error_out; item = json_object_new_int((int32_t) sample->agent_sub_id); if (item == NULL) goto error_out; json_object_object_add(jobject, "agent_sub_id", item); item = json_object_new_int((int32_t) sample->packet_seq_num); if (item == NULL) goto error_out; json_object_object_add(jobject, "packet_seq_num", item); item = json_object_new_double(sample->sys_up_time / 1000.0); if (item == NULL) goto error_out; json_object_object_add(jobject, "sys_up_time", item); item = json_object_new_string(sflow_sample_type_dump(sample->sample_type)); if (item == NULL) goto error_out; json_object_object_add(jobject, "sample_type", item); item = json_object_new_string(sflow_sample_format_dump(sample->sample_type, sample->data_format)); if (item == NULL) goto error_out; json_object_object_add(jobject, "sample_format", item); item = json_object_new_int((int32_t) sample->sample_seq_num); if (item == NULL) goto error_out; json_object_object_add(jobject, "sample_seq_num", item); item = json_object_new_string(sflow_ds_type_dump(sample->ds_type)); if (item == NULL) goto error_out; json_object_object_add(jobject, "ds_type", item); item = json_object_new_int((int32_t) sample->ds_index); if (item == NULL) goto error_out; json_object_object_add(jobject, "ds_index", item); item = json_object_new_int((int32_t) sample->sample_rate); if (item == NULL) goto error_out; json_object_object_add(jobject, "sample_rate", item); item = json_object_new_int((int32_t) sample->sample_pool); if (item == NULL) goto error_out; json_object_object_add(jobject, "sample_pool", item); item = json_object_new_int((int32_t) sample->drop_count); if (item == NULL) goto error_out; json_object_object_add(jobject, "drop_count", item); item = json_object_new_string(sflow_port_id_dump(sample->input_port_format, sample->input_port)); if (item == NULL) goto error_out; json_object_object_add(jobject, "input_port", item); item = json_object_new_string(sflow_port_id_dump(sample->output_port_format, sample->output_port)); if (item == NULL) goto error_out; json_object_object_add(jobject, "output_port", item); item = json_object_new_string(sflow_header_protocol_dump(sample->header.protocol)); if (item == NULL) goto error_out; json_object_object_add(jobject, "sample_protocol", item); item = json_object_new_int((int32_t) sample->header.packet_size); if (item == NULL) goto error_out; json_object_object_add(jobject, "packet_length", item); item = json_object_new_int((int32_t) sample->header.stripped_size); if (item == NULL) goto error_out; json_object_object_add(jobject, "stripped_length", item); item = json_object_new_int((int32_t) sample->header.header_size); if (item == NULL) goto error_out; json_object_object_add(jobject, "header_length", item); item = json_object_new_int((int32_t) sample->eth_type); if (item == NULL) goto error_out; json_object_object_add(jobject, "eth_type", item); irv = ss_metadata_prepare_sflow_mac(jobject, "smac", (uint8_t*) &sample->src_eth); if (irv) goto error_out; irv = ss_metadata_prepare_sflow_mac(jobject, "dmac", (uint8_t*) &sample->src_eth); if (irv) goto error_out; item = json_object_new_int((int32_t) sample->rx_vlan); if (item == NULL) goto error_out; json_object_object_add(jobject, "rx_vlan", item); item = json_object_new_int((int32_t) sample->tx_vlan); if (item == NULL) goto error_out; json_object_object_add(jobject, "tx_vlan", item); irv = ss_metadata_prepare_sflow_ip(jobject, "sip", &sample->src_ip); if (irv == -1) goto error_out; irv = ss_metadata_prepare_sflow_ip(jobject, "dip", &sample->dst_ip); if (irv == -1) goto error_out; irv = ss_metadata_prepare_sflow_ip(jobject, "natsip", &sample->nat_src_ip); if (irv == -1) goto error_out; irv = ss_metadata_prepare_sflow_ip(jobject, "natdip", &sample->nat_dst_ip); if (irv == -1) goto error_out; item = json_object_new_int((int32_t) sample->ip_protocol); if (item == NULL) goto error_out; json_object_object_add(jobject, "ip_protocol", item); item = json_object_new_int((int32_t) sample->ip_tot_len); if (item == NULL) goto error_out; json_object_object_add(jobject, "ip_length", item); item = json_object_new_int((int32_t) sample->ip_ttl); if (item == NULL) goto error_out; json_object_object_add(jobject, "ttl", item); // XXX: fix this field item = json_object_new_int((int32_t) sample->udp_len); if (item == NULL) goto error_out; json_object_object_add(jobject, "l4_length", item); if (sample->ip_protocol == IPPROTO_ICMP || sample->ip_protocol == IPPROTO_ICMPV6) { item = json_object_new_int((int32_t) sample->src_port); if (item == NULL) goto error_out; json_object_object_add(jobject, "icmp_type", item); item = json_object_new_int((int32_t) sample->dst_port); if (item == NULL) goto error_out; json_object_object_add(jobject, "icmp_code", item); } else { item = json_object_new_int((int32_t) sample->src_port); if (item == NULL) goto error_out; json_object_object_add(jobject, "sport", item); item = json_object_new_int((int32_t) sample->dst_port); if (item == NULL) goto error_out; json_object_object_add(jobject, "dport", item); } item = json_object_new_int((int32_t) sample->nat_src_port); if (item == NULL) goto error_out; json_object_object_add(jobject, "natsport", item); item = json_object_new_int((int32_t) sample->nat_dst_port); if (item == NULL) goto error_out; json_object_object_add(jobject, "natdport", item); if (sample->ip_protocol == IPPROTO_TCP) { item = json_object_new_int((int32_t) sample->tcp_flags); if (item == NULL) goto error_out; json_object_object_add(jobject, "tcp_flags", item); } if (sample->src_user[0]) { item = json_object_new_string(sample->src_user); if (item == NULL) goto error_out; json_object_object_add(jobject, "src_user", item); } if (sample->dst_user[0]) { item = json_object_new_string(sample->dst_user); if (item == NULL) goto error_out; json_object_object_add(jobject, "dst_user", item); } if (iptr) { irv = ss_metadata_prepare_ioc(source, rule, nn_queue, iptr, jobject); if (irv) goto error_out; } item = NULL; // XXX: NOTE: String pointer is internal to JSON object. jstring = (uint8_t*) json_object_to_json_string_ext(jobject, JSON_C_TO_STRING_SPACED); rv = (uint8_t*) je_strdup((char*)jstring); if (!rv) goto error_out; json_object_put(jobject); jobject = NULL; return rv; error_out: fprintf(stderr, "could not serialize sflow metadata\n"); if (rv) { je_free(rv); rv = NULL; } if (jobject) { json_object_put(jobject); jobject = NULL; } if (item) { json_object_put(item); item = NULL; } return NULL; }
char* ss_conf_file_read(char* conf_path) { int is_ok = 1; int free_conf_path = 0; int rv; size_t srv; char* conf_content = NULL; if (conf_path == NULL) { conf_path = ss_conf_path_get(); free_conf_path = 1; } FILE* conf_file = fopen(conf_path, "rb"); if (conf_file == NULL) { is_ok = 0; fprintf(stderr, "error: could not open configuration file %s\n", conf_path); goto error_out; } rv = fseek(conf_file, 0L, SEEK_END); if (rv == -1) { is_ok = 0; fprintf(stderr, "error: could not seek to end of configuration file\n"); goto error_out; } long size = ftell(conf_file); if (size == -1) { is_ok = 0; fprintf(stderr, "error: could not get size of configuration file\n"); goto error_out; } rewind(conf_file); /* make room for terminating NUL */ conf_content = je_calloc(1, (size_t) (size + 1)); if (conf_content == NULL) { is_ok = 0; fprintf(stderr, "error: could not allocate configuration file buffer\n"); goto error_out; } srv = fread(conf_content, 1, (size_t) size, conf_file); if (srv != (size_t) size) { is_ok = 0; fprintf(stderr, "error: could not load configuration file\n"); goto error_out; } /* insert terminating NUL */ conf_content[size - 1] = '\0'; error_out: if (free_conf_path) { je_free(conf_path); conf_path = NULL; } if (conf_file) { fclose(conf_file); conf_file = NULL; } if (!is_ok && conf_content) { je_free(conf_content); conf_content = NULL; } return conf_content; }
ss_conf_t* ss_conf_file_parse(char* conf_path) { int is_ok = 1; int rv; char* conf_buffer = NULL; json_object* json_underlying = NULL; json_object* items = NULL; json_object* item = NULL; json_error_t json_error = json_tokener_success; conf_buffer = ss_conf_file_read(conf_path); if (conf_buffer == NULL) { fprintf(stderr, "conf file read error\n"); is_ok = 0; goto error_out; } json_underlying = json_tokener_parse_verbose(conf_buffer, &json_error); if (json_underlying == NULL) { is_ok = 0; fprintf(stderr, "json parse error: %s\n", json_tokener_error_desc(json_error)); is_ok = 0; goto error_out; } ss_conf = je_calloc(1, sizeof(ss_conf_t)); if (ss_conf == NULL) { fprintf(stderr, "could not allocate sdn_sensor configuration\n"); is_ok = 0; goto error_out; } ss_conf->json = json_object_get(json_underlying); is_ok = json_object_is_type(ss_conf->json, json_type_object); if (!is_ok) { is_ok = 0; fprintf(stderr, "json configuration root is not object\n"); is_ok = 0; goto error_out; } //const char* content = json_object_to_json_string_ext(ss_conf->json, JSON_C_TO_STRING_PRETTY); //fprintf(stderr, "json configuration:\n%s\n", content); TAILQ_INIT(&ss_conf->re_chain.re_list); TAILQ_INIT(&ss_conf->pcap_chain.pcap_list); TAILQ_INIT(&ss_conf->dns_chain.dns_list); TAILQ_INIT(&ss_conf->ioc_chain.ioc_list); items = ss_json_object_get(ss_conf->json, "network"); if (items == NULL) { fprintf(stderr, "could not load network configuration\n"); is_ok = 0; goto error_out; } if (!json_object_is_type(items, json_type_object)) { fprintf(stderr, "network configuration is not object\n"); is_ok = 0; goto error_out; } rv = ss_conf_network_parse(items); if (rv) { fprintf(stderr, "could not parse network configuration\n"); is_ok = 0; goto error_out; } items = ss_json_object_get(ss_conf->json, "dpdk"); if (items == NULL) { fprintf(stderr, "could not load dpdk configuration\n"); is_ok = 0; goto error_out; } if (!json_object_is_type(items, json_type_object)) { fprintf(stderr, "dpdk configuration is not object\n"); is_ok = 0; goto error_out; } rv = ss_conf_dpdk_parse(items); if (rv) { fprintf(stderr, "could not parse dpdk configuration\n"); is_ok = 0; goto error_out; } items = ss_json_object_get(ss_conf->json, "re_chain"); if (items) { is_ok = json_object_is_type(items, json_type_array); if (!is_ok) { fprintf(stderr, "re_chain is not an array\n"); goto error_out; } int length = json_object_array_length(items); for (int i = 0; i < length; ++i) { item = json_object_array_get_idx(items, i); ss_re_entry_t* entry = ss_re_entry_create(item); /* if (entry == NULL) { fprintf(stderr, "could not create re_chain entry\n"); if (entry) je_free(entry); is_ok = 0; goto error_out; } */ if (entry == NULL) { fprintf(stderr, "could not create re_chain entry %d\n", i); ss_re_entry_destroy(entry); is_ok = 0; goto error_out; } ss_re_chain_add(entry); } } items = ss_json_object_get(ss_conf->json, "pcap_chain"); if (items) { is_ok = json_object_is_type(items, json_type_array); if (!is_ok) { fprintf(stderr, "pcap_chain is not an array\n"); goto error_out; } int length = json_object_array_length(items); for (int i = 0; i < length; ++i) { item = json_object_array_get_idx(items, i); ss_pcap_entry_t* entry = ss_pcap_entry_create(item); if (entry == NULL) { fprintf(stderr, "could not create pcap_chain entry %d\n", i); ss_pcap_entry_destroy(entry); is_ok = 0; goto error_out; } ss_pcap_chain_add(entry); } } items = ss_json_object_get(ss_conf->json, "dns_chain"); if (items) { is_ok = json_object_is_type(items, json_type_array); if (!is_ok) { fprintf(stderr, "dns_chain is not an array\n"); goto error_out; } int length = json_object_array_length(items); for (int i = 0; i < length; ++i) { item = json_object_array_get_idx(items, i); ss_dns_entry_t* entry = ss_dns_entry_create(item); if (entry == NULL) { fprintf(stderr, "could not create dns_chain entry %d\n", i); ss_dns_entry_destroy(entry); is_ok = 0; goto error_out; } ss_dns_chain_add(entry); } } items = ss_json_object_get(ss_conf->json, "cidr_table"); if (items) { is_ok = json_object_is_type(items, json_type_array); if (!is_ok) { fprintf(stderr, "cidr_table is not an array\n"); goto error_out; } int length = json_object_array_length(items); for (int i = 0; i < length; ++i) { item = json_object_array_get_idx(items, i); ss_cidr_entry_t* entry = ss_cidr_entry_create(item); /* if (entry == NULL) { fprintf(stderr, "could not create cidr_table entry %d\n", i); ss_cidr_entry_destroy(entry); is_ok = 0; goto error_out; } */ ss_cidr_table_add(&ss_conf->cidr_table, entry); } } // XXX: do more stuff error_out: if (conf_buffer) { je_free(conf_buffer); conf_buffer = NULL; } if (!is_ok && ss_conf) { ss_conf_destroy(); ss_conf = NULL; } return ss_conf; }