/** * Start/stop workers. * */ static void engine_create_workers(engine_type* engine) { size_t i = 0; ods_log_assert(engine); ods_log_assert(engine->config); ods_log_assert(engine->allocator); engine->workers = (worker_type**) allocator_alloc(engine->allocator, ((size_t)engine->config->num_worker_threads) * sizeof(worker_type*)); for (i=0; i < (size_t) engine->config->num_worker_threads; i++) { engine->workers[i] = worker_create(engine->allocator, i, WORKER_WORKER); } return; }
struct sym * symnew(struct allocator *al) { struct sym *sym; if ((sym = allocator_alloc(al, sizeof *sym, 1)) == NULL) { AMSG(""); return NULL; } if (syminit(sym, al) == -1) { AMSG(""); return NULL; } return sym; }
static iterator ollrb_itr_clone(const_iterator citr) { struct ollrb_itr* itr = (struct ollrb_itr*)citr; struct ollrb_itr* n_itr = NULL; dbg_assert(__is_object(citr)); dbg_assert(itr->__cast == ollrb_itr_cast); /* destroy itself */ n_itr = (struct ollrb_itr*)allocator_alloc(itr->allocator, sizeof(struct ollrb_itr)); memcpy (n_itr, itr, sizeof(struct ollrb_itr)); /* TODO: this is error prone */ n_itr->__offset = n_itr; return (iterator)n_itr; }
/** * Create notify structure. * */ notify_type* notify_create(void* xfrhandler, void* zone) { notify_type* notify = NULL; allocator_type* allocator = NULL; if (!xfrhandler || !zone) { return NULL; } allocator = allocator_create(malloc, free); if (!allocator) { ods_log_error("[%s] unable to create notify structure: " "allocator_create() failed", notify_str); return NULL; } notify = (notify_type*) allocator_alloc(allocator, sizeof(notify_type)); if (!notify) { ods_log_error("[%s] unable to create notify structure: " " allocator_alloc() failed", notify_str); allocator_cleanup(allocator); return NULL; } notify->allocator = allocator; notify->zone = zone; notify->xfrhandler = xfrhandler; notify->waiting_next = NULL; notify->secondary = NULL; notify->soa = NULL; notify->tsig_rr = tsig_rr_create(allocator); if (!notify->tsig_rr) { notify_cleanup(notify); return NULL; } notify->retry = 0; notify->query_id = 0; notify->is_waiting = 0; notify->handler.fd = -1; notify->timeout.tv_sec = 0; notify->timeout.tv_nsec = 0; notify->handler.timeout = NULL; notify->handler.user_data = notify; notify->handler.event_types = NETIO_EVENT_READ|NETIO_EVENT_TIMEOUT; notify->handler.event_handler = notify_handle_zone; return notify; }
static ham_status_t _remote_fun_txn_begin(ham_env_t *env, ham_db_t *db, ham_txn_t **txn, ham_u32_t flags) { ham_status_t st; proto_wrapper_t *request, *reply; request=proto_init_txn_begin_request(db_get_remote_handle(db), flags); st=_perform_request(env, env_get_curl(env), request, &reply); proto_delete(request); if (st) { if (reply) proto_delete(reply); return (st); } ham_assert(reply!=0, ("")); ham_assert(proto_has_txn_begin_reply(reply), ("")); st=proto_txn_begin_reply_get_status(reply); if (st) { proto_delete(reply); return (st); } *txn=(ham_txn_t *)allocator_alloc(env_get_allocator(env), sizeof(ham_txn_t)); if (!(*txn)) return (HAM_OUT_OF_MEMORY); st=txn_begin(*txn, env, flags); if (st) { allocator_free(env_get_allocator(env), *txn); *txn=0; } else { txn_set_remote_handle(*txn, proto_txn_begin_reply_get_txn_handle(reply)); } proto_delete(reply); return (st); }
/** * Create listener. * */ listener_type* listener_create(allocator_type* allocator) { listener_type* listener = NULL; if (!allocator) { return NULL; } listener = (listener_type*) allocator_alloc(allocator, sizeof(listener_type)); if (!listener) { ods_log_error("[%s] create listener failed: allocator_alloc() failed", listener_str); return NULL; } listener->allocator = allocator; listener->count = 0; listener->interfaces = NULL; return listener; }
static void engine_create_drudgers(engine_type* engine) { #if HAVE_DRUDGERS size_t i = 0; #endif ods_log_assert(engine); ods_log_assert(engine->config); ods_log_assert(engine->allocator); #if HAVE_DRUDGERS engine->drudgers = (worker_type**) allocator_alloc(engine->allocator, ((size_t)engine->config->num_signer_threads) * sizeof(worker_type*)); for (i=0; i < (size_t) engine->config->num_signer_threads; i++) { engine->drudgers[i] = worker_create(engine->allocator, i, WORKER_DRUDGER); } #endif return; }
int main(int argc, char* argv[]) { int i = 0; int n = 100; Allocator* allocator = allocator_normal_create(); for(i = 0; i < n; i++) { char* ptr = allocator_alloc(allocator, i); ptr = allocator_realloc(allocator, ptr, i+4); allocator_free(allocator, ptr); ptr = allocator_calloc(allocator, i+4, 4); allocator_free(allocator, ptr); } allocator_destroy(allocator); return 0; }
/** * Create engine. * */ static engine_type* engine_create(void) { engine_type* engine; allocator_type* allocator = allocator_create(malloc, free); if (!allocator) { return NULL; } engine = (engine_type*) allocator_alloc(allocator, sizeof(engine_type)); if (!engine) { allocator_cleanup(allocator); return NULL; } engine->allocator = allocator; engine->config = NULL; engine->workers = NULL; engine->drudgers = NULL; engine->cmdhandler = NULL; engine->cmdhandler_done = 0; engine->pid = -1; engine->uid = -1; engine->gid = -1; engine->daemonize = 0; engine->need_to_exit = 0; engine->need_to_reload = 0; engine->signal = SIGNAL_INIT; lock_basic_init(&engine->signal_lock); lock_basic_set(&engine->signal_cond); engine->taskq = schedule_create(engine->allocator); if (!engine->taskq) { engine_cleanup(engine); return NULL; } engine->signq = fifoq_create(engine->allocator); if (!engine->signq) { engine_cleanup(engine); return NULL; } return engine; }
extern int clock_init_base( struct Clock** clockp, struct Allocator* allocator, uint64_t numerator, uint64_t denominator) { struct Clock* clock = static_cast<struct Clock*>(allocator_alloc(allocator, sizeof *clock)); if (!clock) { return -1; } clock->allocator = allocator; clock->microseconds_per_tick[0] = numerator; clock->microseconds_per_tick[1] = denominator; *clockp = clock; return 0; }
/** * Create new FIFO queue. * */ fifoq_type* fifoq_create(allocator_type* allocator) { fifoq_type* fifoq; if (!allocator) { return NULL; } fifoq = (fifoq_type*) allocator_alloc(allocator, sizeof(fifoq_type)); if (!fifoq) { ods_log_error("[%s] unable to create fifoq: allocator_alloc() failed", fifoq_str); return NULL; } fifoq->allocator = allocator; fifoq_wipe(fifoq); lock_basic_init(&fifoq->q_lock); lock_basic_set(&fifoq->q_threshold); lock_basic_set(&fifoq->q_nonfull); return fifoq; }
obj_t _string(allocator_t *al, char *str, int copy) { int hsize = sizeof(string_t); int ssize = strlen(str)+1; if (copy) hsize += ssize; void *mem = allocator_alloc(al, hsize, t_string); string_t *string = mem; string->size = ssize; if (copy) { string->str = mem + sizeof(string_t); memcpy(string->str, str, ssize); string->allocated = 1; } else { string->allocated = 0; string->str = str; } return MAKE_TAGGED_PTR(string, al->id); }
/** * Create DNS output adapter. * */ dnsout_type* dnsout_create(void) { dnsout_type* addns = NULL; allocator_type* allocator = allocator_create(malloc, free); if (!allocator) { ods_log_error("[%s] unable to create dnsout: allocator_create() " " failed", adapter_str); return NULL; } addns = (dnsout_type*) allocator_alloc(allocator, sizeof(dnsout_type)); if (!addns) { ods_log_error("[%s] unable to create dnsout: allocator_alloc() " " failed", adapter_str); allocator_cleanup(allocator); return NULL; } addns->allocator = allocator; addns->provide_xfr = NULL; addns->do_notify = NULL; addns->tsig = NULL; return addns; }
object* ollrb_create_v(pf_ref_compare ref_compare, allocator alc) { struct ollrb* ollrb = NULL; bool managed_allocator = false; if (alc == NULL) { alc = allocator_mpool_spawn(__global_default_allocator, 10); managed_allocator = true; } ollrb = (struct ollrb*)allocator_alloc(alc, sizeof(struct ollrb)); ollrb->__offset = ollrb; ollrb->__cast = ollrb_cast; ollrb->__iftable[e_set].__offset = (address)e_set; ollrb->__iftable[e_set].__vtable = &__iset_vtable; ollrb->__iftable[e_mset].__offset = (address)e_mset; ollrb->__iftable[e_mset].__vtable = &__imset_vtable; ollrb->size = 0; ollrb->ref_comp = ref_compare; ollrb->root = NULL; ollrb->sentinel.left = NULL; ollrb->sentinel.right = NULL; ollrb->sentinel.parent = NULL; ollrb->sentinel.color = 55; /* TODO: how to handle this color? */ ollrb->allocator = alc; ollrb->allocator_join_ondispose = managed_allocator; /* initialize begin/end iterators, the position is reassigned when each query */ ollrb_itr_com_init(&ollrb->itr_begin, ollrb); ollrb_itr_com_init(&ollrb->itr_end, ollrb); return (object*)ollrb; }
/** * Create a new zone list. * */ zonelist_type* zonelist_create(allocator_type* allocator) { zonelist_type* zlist = NULL; if (allocator) { zlist = (zonelist_type*) allocator_alloc(allocator, sizeof(zonelist_type)); } if (!zlist) { ods_log_error("[%s] unable to create zonelist: allocator_alloc() " "failed", zl_str); return NULL; } zlist->allocator = allocator; zlist->zones = ldns_rbtree_create(zone_compare); if (!zlist->zones) { ods_log_error("[%s] unable to create zonelist: ldns_rbtree_create() " "failed", zl_str); allocator_deallocate(allocator, (void*) zlist); return NULL; } zlist->last_modified = 0; lock_basic_init(&zlist->zl_lock); return zlist; }
/** * Configure engine. * */ engineconfig_type* engine_config(allocator_type* allocator, const char* cfgfile, int cmdline_verbosity) { engineconfig_type* ecfg; const char* rngfile = ODS_SE_RNGDIR "/conf.rng"; FILE* cfgfd = NULL; if (!allocator) { ods_log_error("[%s] failed to read: no allocator available", conf_str); return NULL; } ods_log_assert(allocator); if (!cfgfile) { ods_log_error("[%s] failed to read: no filename given", conf_str); return NULL; } ods_log_assert(cfgfile); ods_log_verbose("[%s] read cfgfile: %s", conf_str, cfgfile); ecfg = (engineconfig_type*) allocator_alloc(allocator, sizeof(engineconfig_type)); if (!ecfg) { ods_log_error("[%s] failed to read: allocator failed", conf_str); return NULL; } ecfg->allocator = allocator; /* check syntax (slows down parsing configuration file) */ if (parse_file_check(cfgfile, rngfile) != ODS_STATUS_OK) { ods_log_error("[%s] failed to read: unable to parse file %s", conf_str, cfgfile); return NULL; } /* open cfgfile */ cfgfd = ods_fopen(cfgfile, NULL, "r"); if (cfgfd) { /* get values */ ecfg->cfg_filename = allocator_strdup(allocator, cfgfile); ecfg->policy_filename = parse_conf_policy_filename(allocator, cfgfile); ecfg->zonelist_filename = parse_conf_zonelist_filename(allocator, cfgfile); ecfg->zonefetch_filename = parse_conf_zonefetch_filename(allocator, cfgfile); ecfg->log_filename = parse_conf_log_filename(allocator, cfgfile); ecfg->pid_filename = parse_conf_pid_filename(allocator, cfgfile); ecfg->delegation_signer_submit_command = parse_conf_delegation_signer_submit_command(allocator, cfgfile); ecfg->delegation_signer_retract_command = parse_conf_delegation_signer_retract_command(allocator, cfgfile); ecfg->clisock_filename = parse_conf_clisock_filename(allocator, cfgfile); ecfg->working_dir = parse_conf_working_dir(allocator, cfgfile); ecfg->username = parse_conf_username(allocator, cfgfile); ecfg->group = parse_conf_group(allocator, cfgfile); ecfg->chroot = parse_conf_chroot(allocator, cfgfile); ecfg->datastore = parse_conf_datastore(allocator, cfgfile); ecfg->db_host = parse_conf_db_host(allocator,cfgfile); ecfg->db_username = parse_conf_db_username(allocator,cfgfile); ecfg->db_password = parse_conf_db_password(allocator,cfgfile); ecfg->use_syslog = parse_conf_use_syslog(cfgfile); ecfg->num_worker_threads = parse_conf_worker_threads(cfgfile); ecfg->manual_keygen = parse_conf_manual_keygen(cfgfile); /* If any verbosity has been specified at cmd line we will use that */ if (cmdline_verbosity > 0) { ecfg->verbosity = cmdline_verbosity; } else { ecfg->verbosity = parse_conf_verbosity(cfgfile); } ecfg->db_port = parse_conf_db_port(cfgfile); ecfg->automatic_keygen_duration = parse_conf_automatic_keygen_period(cfgfile); /* done */ ods_fclose(cfgfd); return ecfg; } ods_log_error("[%s] failed to read: unable to open file %s", conf_str, cfgfile); return NULL; }
static void _ensure_allocator_traced_alloc_returns_null_for_empty_allocation( void ) { allocator_traced_t alloc; allocator_traced_init_stdout( &alloc, allocator_default( ) ); TEST_REQUIRE( allocator_alloc( 0, allocator_traced_get( &alloc ) ) == 0 ); }
int HashmapExercise0(int verbose, struct cfg *cfg, char *args[]) { struct hashmap0 *h; int rate = 0, i, n = 0; int count = atoi(args[0]); int interval = atoi(args[1]); iter_t riter; char *str; clock_t t0; void *mem; struct allocator *al; iter_t iter; int chkpnt = 0; if ((mem = malloc(0xFFFFFFF)) == NULL || (al = suba_init(mem, 0xFFFFFFF, 1, 0)) == NULL || (h = hashmap0_new(HASHMAP_LARGE, hash_str, (compare_fn)strcmp, NULL, allocator_free, NULL, al)) == NULL) { AMSG(""); return -1; } srand(1); fputc('\n', stderr); t0 = clock(); fprintf(stderr, " time count size mem\n"); hashmap0_iterate(h, &iter); rate_iterate(&riter); for (i = 0; i < count; i++) { if ((i % interval) == 0) { fprintf(stderr, "%2d %8ld %8d %8d %8d %d\n", chkpnt++, (clock() - t0) / 1000, hashmap0_size(h), HASHMAP_LARGE, h->al->size_total - h->al->free_total, rate); rate = rate_next(&riter); } if (rand() % 10 < rate) { str = allocator_alloc(NULL, 16, 0); sprintf(str, "%015d", n++); if (hashmap0_put(h, str, str) == -1) { AMSG(""); return -1; } else { /*fputc('+', stderr); */ tcase_printf(verbose, "put %s %d\n", str, hashmap0_size(h)); } } else { if (hashmap0_is_empty(h)) { tcase_printf(verbose, "hashmap0 empty\n"); } else { str = hashmap0_next(h, &iter); if (str) { tcase_printf(verbose, "get %s %d\n", str, hashmap0_size(h)); if ((str = hashmap0_remove(h, str)) == NULL) { errno = EFAULT; PMNO(errno); return -1; } /*fputc('-', stderr); */ tcase_printf(verbose, "remove %s %d\n", str, hashmap0_size(h)); allocator_free(NULL, str); } else { /*fputc('x', stderr); */ tcase_printf(verbose, "nothing left to iterate over\r"); hashmap0_iterate(h, &iter); } } } } fprintf(stderr, "%2d %8ld %8d %8d %8d\n", chkpnt++, (clock() - t0) / 1000, hashmap0_size(h), HASHMAP_LARGE, h->al->size_total); hashmap0_del(h); tcase_printf(verbose, "hashmap0 done\n"); cfg = NULL; return 0; }
/** * Create ACL. * */ acl_type* acl_create(allocator_type* allocator, char* address, char* port, char* tsig_name, tsig_type* tsig) { ods_status status = ODS_STATUS_OK; acl_type* acl = NULL; char* p = NULL; if (!allocator) { return NULL; } acl = (acl_type*) allocator_alloc(allocator, sizeof(acl_type)); if (!acl) { ods_log_error("[%s] unable to create acl: allocator_alloc() " "failed", acl_str); return NULL; } acl->address = NULL; acl->next = NULL; acl->tsig = NULL; if (tsig_name) { acl->tsig = tsig_lookup_by_name(tsig, tsig_name); if (!acl->tsig) { ods_log_error("[%s] unable to create acl: tsig %s not found", acl_str, tsig_name); acl_cleanup(acl, allocator); return NULL; } } acl->port = 0; if (port) { acl->port = atoi((const char*) port); } memset(&acl->addr, 0, sizeof(union acl_addr_storage)); memset(&acl->range_mask, 0, sizeof(union acl_addr_storage)); if (address) { acl->family = acl_parse_family(address); acl->range_type = acl_parse_range_type(address, &p); acl->address = allocator_strdup(allocator, address); if (!acl->address) { ods_log_error("[%s] unable to create acl: allocator_strdup() " "failed", acl_str); acl_cleanup(acl, allocator); return NULL; } if (acl->family == AF_INET6) { if (inet_pton(AF_INET6, acl->address, &acl->addr.addr6) != 1) { ods_log_error("[%s] unable to create acl: bad ipv6 address " "(%s)", acl_str, acl->address); acl_cleanup(acl, allocator); return NULL; } if (acl->range_type == ACL_RANGE_MASK || acl->range_type == ACL_RANGE_MINMAX) { if (inet_pton(AF_INET6, p, &acl->range_mask.addr6) != 1) { ods_log_error("[%s] unable to create acl: bad ipv6 address" " mask (%s)", acl_str, p); acl_cleanup(acl, allocator); return NULL; } } else if (acl->range_type == ACL_RANGE_SUBNET) { status = acl_parse_range_subnet(p, &acl->range_mask.addr6, 128); if (status != ODS_STATUS_OK) { ods_log_error("[%s] unable to create acl: %s (%s)", acl_str, ods_status2str(status), p); acl_cleanup(acl, allocator); return NULL; } } } else if (acl->family == AF_INET) { if (inet_pton(AF_INET, acl->address, &acl->addr.addr) != 1) { ods_log_error("[%s] unable to create acl: bad ipv4 address " "(%s)", acl_str, acl->address); acl_cleanup(acl, allocator); return NULL; } if (acl->range_type == ACL_RANGE_MASK || acl->range_type == ACL_RANGE_MINMAX) { if (inet_pton(AF_INET, p, &acl->range_mask.addr) != 1) { ods_log_error("[%s] unable to create acl: bad ipv4 address" " mask (%s)", acl_str, p); acl_cleanup(acl, allocator); return NULL; } } else if (acl->range_type == ACL_RANGE_SUBNET) { status = acl_parse_range_subnet(p, &acl->range_mask.addr, 32); if (status != ODS_STATUS_OK) { ods_log_error("[%s] unable to create acl: %s (%s)", acl_str, ods_status2str(status), p); acl_cleanup(acl, allocator); return NULL; } } } } acl->ixfr_disabled = 0; return acl; }
static ham_status_t _remote_fun_get_parameters(ham_db_t *db, ham_parameter_t *param) { static char filename[1024]; ham_status_t st; ham_env_t *env=db_get_env(db); proto_wrapper_t *request, *reply; ham_size_t i, num_names=0; ham_u32_t *names; ham_parameter_t *p; /* count number of parameters */ p=param; if (p) { for (; p->name; p++) { num_names++; } } /* allocate a memory and copy the parameter names */ names=(ham_u32_t *)allocator_alloc(env_get_allocator(env), num_names*sizeof(ham_u32_t)); if (!names) return (HAM_OUT_OF_MEMORY); p=param; if (p) { for (i=0; p->name; p++) { names[i]=p->name; i++; } } request=proto_init_db_get_parameters_request(db_get_remote_handle(db), names, num_names); st=_perform_request(env, env_get_curl(env), request, &reply); proto_delete(request); allocator_free(env_get_allocator(env), names); if (st) { if (reply) proto_delete(reply); return (st); } ham_assert(reply!=0, ("")); ham_assert(proto_has_db_get_parameters_reply(reply), ("")); st=proto_db_get_parameters_reply_get_status(reply); if (st) { proto_delete(reply); return (st); } p=param; while (p && p->name) { switch (p->name) { case HAM_PARAM_CACHESIZE: ham_assert(proto_db_get_parameters_reply_has_cachesize(reply), ("")); p->value=proto_db_get_parameters_reply_get_cachesize(reply); break; case HAM_PARAM_PAGESIZE: ham_assert(proto_db_get_parameters_reply_has_pagesize(reply), ("")); p->value=proto_db_get_parameters_reply_get_pagesize(reply); break; case HAM_PARAM_MAX_ENV_DATABASES: ham_assert(proto_db_get_parameters_reply_has_max_env_databases(reply), ("")); p->value=proto_db_get_parameters_reply_get_max_env_databases(reply); break; case HAM_PARAM_GET_FLAGS: ham_assert(proto_db_get_parameters_reply_has_flags(reply), ("")); p->value=proto_db_get_parameters_reply_get_flags(reply); break; case HAM_PARAM_GET_FILEMODE: ham_assert(proto_db_get_parameters_reply_has_filemode(reply), ("")); p->value=proto_db_get_parameters_reply_get_filemode(reply); break; case HAM_PARAM_GET_FILENAME: ham_assert(proto_db_get_parameters_reply_has_filename(reply), ("")); strncpy(filename, proto_db_get_parameters_reply_get_filename(reply), sizeof(filename)); p->value=PTR_TO_U64(&filename[0]); break; case HAM_PARAM_KEYSIZE: ham_assert(proto_db_get_parameters_reply_has_keysize(reply), ("")); p->value=proto_db_get_parameters_reply_get_keysize(reply); break; case HAM_PARAM_GET_DATABASE_NAME: ham_assert(proto_db_get_parameters_reply_has_dbname(reply), ("")); p->value=proto_db_get_parameters_reply_get_dbname(reply); break; case HAM_PARAM_GET_KEYS_PER_PAGE: ham_assert(proto_db_get_parameters_reply_has_keys_per_page(reply), ("")); p->value=proto_db_get_parameters_reply_get_keys_per_page(reply); break; case HAM_PARAM_GET_DATA_ACCESS_MODE: ham_assert(proto_db_get_parameters_reply_has_dam(reply), ("")); p->value=proto_db_get_parameters_reply_get_dam(reply); break; default: ham_trace(("unknown parameter %d", (int)p->name)); break; } p++; } proto_delete(reply); return (st); }
/** * Create a new zone. * */ zone_type* zone_create(char* name, ldns_rr_class klass) { allocator_type* allocator = NULL; zone_type* zone = NULL; if (!name || !klass) { return NULL; } allocator = allocator_create(malloc, free); if (!allocator) { ods_log_error("[%s] unable to create zone %s: allocator_create() " "failed", zone_str, name); return NULL; } zone = (zone_type*) allocator_alloc(allocator, sizeof(zone_type)); if (!zone) { ods_log_error("[%s] unable to create zone %s: allocator_alloc()", "failed", zone_str, name); allocator_cleanup(allocator); return NULL; } zone->allocator = allocator; /* [start] PS 9218653: Drop trailing dot in domain name */ if (strlen(name) > 1 && name[strlen(name)-1] == '.') { name[strlen(name)-1] = '\0'; } /* [end] PS 9218653 */ zone->name = allocator_strdup(allocator, name); if (!zone->name) { ods_log_error("[%s] unable to create zone %s: allocator_strdup() " "failed", zone_str, name); zone_cleanup(zone); return NULL; } zone->klass = klass; zone->default_ttl = 3600; /* TODO: configure --default-ttl option? */ zone->apex = ldns_dname_new_frm_str(name); /* check zone->apex? */ zone->notify_command = NULL; zone->notify_ns = NULL; zone->notify_args = NULL; zone->policy_name = NULL; zone->signconf_filename = NULL; zone->adinbound = NULL; zone->adoutbound = NULL; zone->zl_status = ZONE_ZL_OK; zone->task = NULL; zone->xfrd = NULL; zone->notify = NULL; zone->db = namedb_create((void*)zone); if (!zone->db) { ods_log_error("[%s] unable to create zone %s: namedb_create() " "failed", zone_str, name); zone_cleanup(zone); return NULL; } zone->ixfr = ixfr_create((void*)zone); if (!zone->ixfr) { ods_log_error("[%s] unable to create zone %s: ixfr_create() " "failed", zone_str, name); zone_cleanup(zone); return NULL; } zone->signconf = signconf_create(); if (!zone->signconf) { ods_log_error("[%s] unable to create zone %s: signconf_create() " "failed", zone_str, name); zone_cleanup(zone); return NULL; } zone->stats = stats_create(); lock_basic_init(&zone->zone_lock); lock_basic_init(&zone->xfr_lock); return zone; }
static ham_status_t _remote_fun_open_db(ham_env_t *env, ham_db_t *db, ham_u16_t dbname, ham_u32_t flags, const ham_parameter_t *param) { ham_status_t st; proto_wrapper_t *request, *reply; ham_size_t i=0, num_params=0; ham_u32_t *names; ham_u64_t *values; const ham_parameter_t *p; /* count number of parameters */ p=param; if (p) { for (; p->name; p++) { num_params++; } } /* allocate a memory and copy the parameter names */ names=(ham_u32_t *)allocator_alloc(env_get_allocator(env), num_params*sizeof(ham_u32_t)); values=(ham_u64_t *)allocator_alloc(env_get_allocator(env), num_params*sizeof(ham_u64_t)); if (!names || !values) return (HAM_OUT_OF_MEMORY); p=param; if (p) { for (; p->name; p++) { names[i]=p->name; values[i]=p->value; i++; } } request=proto_init_env_open_db_request(dbname, flags, names, values, num_params); st=_perform_request(env, env_get_curl(env), request, &reply); proto_delete(request); allocator_free(env_get_allocator(env), names); allocator_free(env_get_allocator(env), values); if (st) { if (reply) proto_delete(reply); return (st); } ham_assert(reply!=0, ("")); ham_assert(proto_has_env_open_db_reply(reply), ("")); st=proto_env_open_db_reply_get_status(reply); if (st) { proto_delete(reply); return (st); } /* * store the env pointer in the database */ db_set_env(db, env); db_set_remote_handle(db, proto_env_open_db_reply_get_db_handle(reply)); db_set_rt_flags(db, proto_env_open_db_reply_get_flags(reply)); proto_delete(reply); /* * on success: store the open database in the environment's list of * opened databases */ db_set_next(db, env_get_list(env)); env_set_list(env, db); /* * initialize the remaining function pointers in ham_db_t */ return (db_initialize_remote(db)); }
/** * Start dns handler. * */ void dnshandler_start(dnshandler_type* dnshandler) { size_t i = 0; engine_type* engine = NULL; netio_handler_type* tcp_accept_handlers = NULL; ods_log_assert(dnshandler); ods_log_assert(dnshandler->engine); ods_log_debug("[%s] start", dnsh_str); /* udp */ for (i=0; i < dnshandler->interfaces->count; i++) { struct udp_data* data = NULL; netio_handler_type* handler = NULL; data = (struct udp_data*) allocator_alloc(dnshandler->allocator, sizeof(struct udp_data)); if (!data) { ods_log_error("[%s] unable to start: allocator_alloc() " "failed", dnsh_str); dnshandler->thread_id = 0; engine->need_to_exit = 1; break; } data->query = dnshandler->query; data->engine = dnshandler->engine; data->socket = &dnshandler->socklist->udp[i]; handler = (netio_handler_type*) allocator_alloc( dnshandler->allocator, sizeof(netio_handler_type)); if (!handler) { ods_log_error("[%s] unable to start: allocator_alloc() " "failed", dnsh_str); allocator_deallocate(dnshandler->allocator, (void*)data); dnshandler->thread_id = 0; engine->need_to_exit = 1; break; } handler->fd = dnshandler->socklist->udp[i].s; handler->timeout = NULL; handler->user_data = data; handler->event_types = NETIO_EVENT_READ; handler->event_handler = sock_handle_udp; ods_log_debug("[%s] add udp network handler fd %u", dnsh_str, (unsigned) handler->fd); netio_add_handler(dnshandler->netio, handler); } /* tcp */ tcp_accept_handlers = (netio_handler_type*) allocator_alloc( dnshandler->allocator, dnshandler->interfaces->count * sizeof(netio_handler_type)); for (i=0; i < dnshandler->interfaces->count; i++) { struct tcp_accept_data* data = NULL; netio_handler_type* handler = NULL; data = (struct tcp_accept_data*) allocator_alloc( dnshandler->allocator, sizeof(struct tcp_accept_data)); if (!data) { ods_log_error("[%s] unable to start: allocator_alloc() " "failed", dnsh_str); dnshandler->thread_id = 0; engine->need_to_exit = 1; return; } data->engine = dnshandler->engine; data->socket = &dnshandler->socklist->udp[i]; data->tcp_accept_handler_count = dnshandler->interfaces->count; data->tcp_accept_handlers = tcp_accept_handlers; handler = &tcp_accept_handlers[i]; handler->fd = dnshandler->socklist->tcp[i].s; handler->timeout = NULL; handler->user_data = data; handler->event_types = NETIO_EVENT_READ; handler->event_handler = sock_handle_tcp_accept; ods_log_debug("[%s] add tcp network handler fd %u", dnsh_str, (unsigned) handler->fd); netio_add_handler(dnshandler->netio, handler); } /* service */ while (dnshandler->need_to_exit == 0) { ods_log_deeebug("[%s] netio dispatch", dnsh_str); if (netio_dispatch(dnshandler->netio, NULL, NULL) == -1) { if (errno != EINTR) { ods_log_error("[%s] unable to dispatch netio: %s", dnsh_str, strerror(errno)); break; } } } /* shutdown */ ods_log_debug("[%s] shutdown", dnsh_str); for (i=0; i < dnshandler->interfaces->count; i++) { if (dnshandler->socklist->udp[i].s != -1) { close(dnshandler->socklist->udp[i].s); freeaddrinfo((void*)dnshandler->socklist->udp[i].addr); } if (dnshandler->socklist->tcp[i].s != -1) { close(dnshandler->socklist->tcp[i].s); freeaddrinfo((void*)dnshandler->socklist->tcp[i].addr); } } return; }
/** * Allocate space in storage for and write the content references by 'data' * (and length 'size') to storage. * * Conditions will apply whether the data is written through cache or direct * to device. * * The content is, of course, prefixed by a BLOB header. * * Partial writes are handled in this function. */ ham_status_t blob_allocate(ham_env_t *env, ham_db_t *db, ham_record_t *record, ham_u32_t flags, ham_offset_t *blobid) { ham_status_t st; ham_page_t *page=0; ham_offset_t addr; blob_t hdr; ham_u8_t *chunk_data[2]; ham_size_t alloc_size; ham_size_t chunk_size[2]; ham_device_t *device=env_get_device(env); ham_bool_t freshly_created = HAM_FALSE; *blobid=0; /* * PARTIAL WRITE * * if offset+partial_size equals the full record size, then we won't * have any gaps. In this case we just write the full record and ignore * the partial parameters. */ if (flags&HAM_PARTIAL) { if (record->partial_offset==0 && record->partial_offset+record->partial_size==record->size) flags&=~HAM_PARTIAL; } /* * in-memory-database: the blobid is actually a pointer to the memory * buffer, in which the blob (with the blob-header) is stored */ if (env_get_rt_flags(env)&HAM_IN_MEMORY_DB) { blob_t *hdr; ham_u8_t *p=(ham_u8_t *)allocator_alloc(env_get_allocator(env), record->size+sizeof(blob_t)); if (!p) { return HAM_OUT_OF_MEMORY; } /* initialize the header */ hdr=(blob_t *)p; memset(hdr, 0, sizeof(*hdr)); blob_set_self(hdr, (ham_offset_t)PTR_TO_U64(p)); blob_set_alloc_size(hdr, record->size+sizeof(blob_t)); blob_set_size(hdr, record->size); /* do we have gaps? if yes, fill them with zeroes */ if (flags&HAM_PARTIAL) { ham_u8_t *s=p+sizeof(blob_t); if (record->partial_offset) memset(s, 0, record->partial_offset); memcpy(s+record->partial_offset, record->data, record->partial_size); if (record->partial_offset+record->partial_size<record->size) memset(s+record->partial_offset+record->partial_size, 0, record->size-(record->partial_offset+record->partial_size)); } else { memcpy(p+sizeof(blob_t), record->data, record->size); } *blobid=(ham_offset_t)PTR_TO_U64(p); return (0); } memset(&hdr, 0, sizeof(hdr)); /* * blobs are CHUNKSIZE-allocated */ alloc_size=sizeof(blob_t)+record->size; alloc_size += DB_CHUNKSIZE - 1; alloc_size -= alloc_size % DB_CHUNKSIZE; /* * check if we have space in the freelist */ st = freel_alloc_area(&addr, env, db, alloc_size); if (!addr) { if (st) return st; /* * if the blob is small AND if logging is disabled: load the page * through the cache */ if (__blob_from_cache(env, alloc_size)) { st = db_alloc_page(&page, db, PAGE_TYPE_BLOB, PAGE_IGNORE_FREELIST); ham_assert(st ? page == NULL : 1, (0)); ham_assert(!st ? page != NULL : 1, (0)); if (st) return st; /* blob pages don't have a page header */ page_set_npers_flags(page, page_get_npers_flags(page)|PAGE_NPERS_NO_HEADER); addr=page_get_self(page); /* move the remaining space to the freelist */ (void)freel_mark_free(env, db, addr+alloc_size, env_get_pagesize(env)-alloc_size, HAM_FALSE); blob_set_alloc_size(&hdr, alloc_size); } else { /* * otherwise use direct IO to allocate the space */ ham_size_t aligned=alloc_size; aligned += env_get_pagesize(env) - 1; aligned -= aligned % env_get_pagesize(env); st=device->alloc(device, aligned, &addr); if (st) return (st); /* if aligned!=size, and the remaining chunk is large enough: * move it to the freelist */ { ham_size_t diff=aligned-alloc_size; if (diff > SMALLEST_CHUNK_SIZE) { (void)freel_mark_free(env, db, addr+alloc_size, diff, HAM_FALSE); blob_set_alloc_size(&hdr, aligned-diff); } else { blob_set_alloc_size(&hdr, aligned); } } freshly_created = HAM_TRUE; } ham_assert(HAM_SUCCESS == freel_check_area_is_allocated(env, db, addr, alloc_size), (0)); } else { ham_assert(!st, (0)); blob_set_alloc_size(&hdr, alloc_size); } blob_set_size(&hdr, record->size); blob_set_self(&hdr, addr); /* * PARTIAL WRITE * * are there gaps at the beginning? If yes, then we'll fill with zeros */ if ((flags&HAM_PARTIAL) && (record->partial_offset)) { ham_u8_t *ptr; ham_size_t gapsize=record->partial_offset; ptr=allocator_calloc(env_get_allocator(env), gapsize > env_get_pagesize(env) ? env_get_pagesize(env) : gapsize); if (!ptr) return (HAM_OUT_OF_MEMORY); /* * first: write the header */ chunk_data[0]=(ham_u8_t *)&hdr; chunk_size[0]=sizeof(hdr); st=__write_chunks(env, page, addr, HAM_TRUE, freshly_created, chunk_data, chunk_size, 1); if (st) return (st); addr+=sizeof(hdr); /* now fill the gap; if the gap is bigger than a pagesize we'll * split the gap into smaller chunks */ while (gapsize>=env_get_pagesize(env)) { chunk_data[0]=ptr; chunk_size[0]=env_get_pagesize(env); st=__write_chunks(env, page, addr, HAM_TRUE, freshly_created, chunk_data, chunk_size, 1); if (st) break; gapsize-=env_get_pagesize(env); addr+=env_get_pagesize(env); } /* fill the remaining gap */ if (gapsize) { chunk_data[0]=ptr; chunk_size[0]=gapsize; st=__write_chunks(env, page, addr, HAM_TRUE, freshly_created, chunk_data, chunk_size, 1); if (st) return (st); addr+=gapsize; } allocator_free(env_get_allocator(env), ptr); /* now write the "real" data */ chunk_data[0]=(ham_u8_t *)record->data; chunk_size[0]=record->partial_size; st=__write_chunks(env, page, addr, HAM_TRUE, freshly_created, chunk_data, chunk_size, 1); if (st) return (st); addr+=record->partial_size; } else { /* * not writing partially: write header and data, then we're done */ chunk_data[0]=(ham_u8_t *)&hdr; chunk_size[0]=sizeof(hdr); chunk_data[1]=(ham_u8_t *)record->data; chunk_size[1]=(flags&HAM_PARTIAL) ? record->partial_size : record->size; st=__write_chunks(env, page, addr, HAM_TRUE, freshly_created, chunk_data, chunk_size, 2); if (st) return (st); addr+=sizeof(hdr)+ ((flags&HAM_PARTIAL) ? record->partial_size : record->size); } /* * store the blobid; it will be returned to the caller */ *blobid=blob_get_self(&hdr); /* * PARTIAL WRITES: * * if we have gaps at the end of the blob: just append more chunks to * fill these gaps. Since they can be pretty large we split them into * smaller chunks if necessary. */ if (flags&HAM_PARTIAL) { if (record->partial_offset+record->partial_size < record->size) { ham_u8_t *ptr; ham_size_t gapsize=record->size - (record->partial_offset+record->partial_size); /* now fill the gap; if the gap is bigger than a pagesize we'll * split the gap into smaller chunks * * we split this loop in two - the outer loop will allocate the * memory buffer, thus saving some allocations */ while (gapsize>env_get_pagesize(env)) { ham_u8_t *ptr=allocator_calloc(env_get_allocator(env), env_get_pagesize(env)); if (!ptr) return (HAM_OUT_OF_MEMORY); while (gapsize>env_get_pagesize(env)) { chunk_data[0]=ptr; chunk_size[0]=env_get_pagesize(env); st=__write_chunks(env, page, addr, HAM_TRUE, freshly_created, chunk_data, chunk_size, 1); if (st) break; gapsize-=env_get_pagesize(env); addr+=env_get_pagesize(env); } allocator_free(env_get_allocator(env), ptr); if (st) return (st); } /* now write the remainder, which is less than a pagesize */ ham_assert(gapsize<env_get_pagesize(env), ("")); chunk_size[0]=gapsize; ptr=chunk_data[0]=allocator_calloc(env_get_allocator(env), gapsize); if (!ptr) return (HAM_OUT_OF_MEMORY); st=__write_chunks(env, page, addr, HAM_TRUE, freshly_created, chunk_data, chunk_size, 1); allocator_free(env_get_allocator(env), ptr); if (st) return (st); } } return (0); }
int HashmapExercise(int verbose, struct cfg *cfg, char *args[]) { struct hashmap *h; int rate = 0, i, n = 0; int count = atoi(args[0]); int interval = atoi(args[1]); iter_t riter; iter_t iter; char *str; struct allocator *al = NULL; char *mem; clock_t t0; int chkpnt = 0; struct allocator *hal; if ((mem = malloc(0xFFFFFF)) == NULL || (al = suba_init(mem, 0xFFFFFF, 1, 0)) == NULL || (h = hashmap_new(hash_str, cmp_str, NULL, al)) == NULL) { AMSG(""); return -1; } hal = AL(h); /* if ((h = hashmap_new(hash_str, cmp_str, NULL, al)) == NULL) { AMSG(""); return -1; } */ srand(0); t0 = clock(); if (verbose) fprintf(stderr, "\n time count size mem\n"); hashmap_iterate(h, &iter); rate_iterate(&riter); for (i = 0; i < count; i++) { if ((i % interval) == 0) { if (verbose) fprintf(stderr, "%2d %8ld %8d %8d %8d\n", chkpnt++, (clock() - t0) / 1000, hashmap_size(h), table_sizes[h->table_size_index], hal->alloc_total - hal->free_total); rate = rate_next(&riter); } if (rand() % 10 < rate) { str = allocator_alloc(NULL, 8, 0); sprintf(str, "%07d", n++); if (hashmap_put(h, str, str) == -1) { AMSG(""); return -1; } else { /*fputc('+', stderr); */ tcase_printf(verbose, "put %s %d\r", str, hashmap_size(h)); } } else { if (hashmap_is_empty(h)) { /*fputc('0', stderr); */ tcase_printf(verbose, "hashmap empty\r"); } else { str = hashmap_next(h, &iter); if (str) { char *data; tcase_printf(verbose, "get %s %d\r", str, hashmap_size(h)); if (hashmap_remove(h, (void **)&str, (void **)&data) == -1) { AMSG(""); return -1; } /*fputc('-', stderr); */ tcase_printf(verbose, "rem %s %d\r", str, hashmap_size(h)); allocator_free(NULL, str); } else { /*fputc('x', stderr); */ tcase_printf(verbose, "nothing left to iterate over\r"); hashmap_iterate(h, &iter); } } } } if (verbose) fprintf(stderr, "%2d %8ld %8d %8d %8d\n", chkpnt++, (clock() - t0) / 1000, hashmap_size(h), table_sizes[h->table_size_index], hal->alloc_total - hal->free_total); hashmap_del(h, allocator_free, NULL, NULL); /* free(mem); */ if (verbose) fprintf(stderr, "bytes outstanding from allocator: %d\n", hal->alloc_total - hal->free_total); cfg = NULL; return 0; }
static void _ensure_allocator_traced_alloc_returns_null_when_parent_allocator_null( void ) { allocator_traced_t alloc; allocator_traced_init( &alloc, 0, stdout ); TEST_REQUIRE( allocator_alloc( 1024, allocator_traced_get( &alloc ) ) == 0 ); }