/** * Create query. * */ query_type* query_create(void) { allocator_type* allocator = NULL; query_type* q = NULL; allocator = allocator_create(malloc, free); if (!allocator) { return NULL; } q = (query_type*) allocator_alloc(allocator, sizeof(query_type)); if (!q) { allocator_cleanup(allocator); return NULL; } q->allocator = allocator; q->buffer = NULL; q->tsig_rr = NULL; q->buffer = buffer_create(allocator, PACKET_BUFFER_SIZE); if (!q->buffer) { query_cleanup(q); return NULL; } q->tsig_rr = tsig_rr_create(allocator); if (!q->tsig_rr) { query_cleanup(q); return NULL; } q->edns_rr = edns_rr_create(allocator); if (!q->edns_rr) { query_cleanup(q); return NULL; } query_reset(q, UDP_MAX_MESSAGE_LEN, 0); return q; }
/** * Create a new 'instant' duration. * */ duration_type* duration_create(void) { duration_type* duration; allocator_type* allocator = allocator_create(malloc, free); if (!allocator) { ods_log_error("[%s] cannot create: no allocator available", duration_str); return NULL; } duration = (duration_type*) allocator_alloc(allocator, sizeof(duration_type)); if (!duration) { ods_log_error("[%s] cannot create: allocator failed", duration_str); allocator_cleanup(allocator); return NULL; } duration->allocator = allocator; duration->years = 0; duration->months = 0; duration->weeks = 0; duration->days = 0; duration->hours = 0; duration->minutes = 0; duration->seconds = 0; return duration; }
//*************************************************************************// //* initialize the av heap. //*************************************************************************// int av_heap_init(int fd_ve) { //* 1. allocate a heap context. if(heap_ctx != NULL) { loge("av heap context already initialized, return fail."); return -1; } heap_ctx = (heap_ctx_t*)malloc(sizeof(heap_ctx_t)); if(heap_ctx == NULL) { loge("can not malloc memory for av heap context, return fail."); return -1; } memset(heap_ctx, 0, sizeof(heap_ctx_t)); //* 2. initialize the mutex. if(pthread_mutex_init(&heap_ctx->mutex, NULL) != 0) { loge("can not initialize mutex for the av heap context, return fail."); free(heap_ctx); heap_ctx = NULL; return -1; } //* 3. open the driver. heap_ctx->fd = fd_ve; if(heap_ctx->fd == -1 || heap_ctx->fd == 0) { loge("file descriptor of driver '/dev/cedar_dev' is invalid, return fail."); pthread_mutex_destroy(&heap_ctx->mutex); free(heap_ctx); heap_ctx = NULL; return -1; } //* 4. get memory information. ioctl(heap_ctx->fd, IOCTL_GET_ENV_INFO, (unsigned int)&heap_ctx->env_info); //* 5. create a allocator. heap_ctx->allocator = allocator_create(heap_ctx->env_info.phymem_total_size); if(heap_ctx->allocator == NULL) { loge("can not create allocator for the heap context, return fail."); pthread_mutex_destroy(&heap_ctx->mutex); free(heap_ctx); heap_ctx = NULL; return -1; } return 0; }
/* * Create an object allocator managing the root CNode's free slots */ static void create_bootstrap_allocator(struct allocator *allocator) { /* Fetch seL4 bootinfo. */ seL4_BootInfo *bootinfo = seL4_GetBootInfo(); /* Create the allocator. */ allocator_create( allocator, seL4_CapInitThreadCNode, seL4_WordBits, 0, bootinfo->empty.start, bootinfo->empty.end - bootinfo->empty.start, NULL, 0 ); /* Give the allocator all of our free memory. */ fill_allocator_with_resources(allocator, bootinfo); #ifdef CONFIG_DEBUG_BUILD /* Test out everything. */ allocator_self_test(allocator); #endif }
int cpeer_create_out(int fd,struct net_client_t *nc) { allocator_t *allocator; if(allocator_create(&allocator) < 0) return -1; struct cpeer *p = (struct cpeer *)mmalloc(nc->allocator,sizeof(struct cpeer)); memset(p,0,sizeof(struct cpeer)); p->sd = fd; p->id = 0; p->refcount = 0; p->status = CPEER_CONNECTED; p->nc = nc; p->allocator = allocator; thread_mutex_create(&(p->mpool_mutex),0); allocator_mutex_set(allocator,p->mpool_mutex); p->sendbuf = iobuf_init(p->allocator,512); p->recvbuf = iobuf_init(p->allocator,512); thread_mutex_create(&(p->sq_mutex),0); BTPDQ_INIT(&p->send_queue); nc->peer = p; fdev_new(nc->epfd,&p->ioev,fd,EV_READ,net_io_cb,p); return 0; }
/** * Create a new task. * */ task_type* task_create(task_id what, time_t when, void* zone) { allocator_type* allocator = NULL; task_type* task = NULL; if (!zone) { return NULL; } allocator = allocator_create(malloc, free); if (!allocator) { ods_log_error("[%s] unable to create task: allocator_create() failed", task_str); return NULL; } task = (task_type*) allocator_alloc(allocator, sizeof(task_type)); if (!task) { ods_log_error("[%s] unable to create task: allocator_alloc() failed", task_str); allocator_cleanup(allocator); return NULL; } task->allocator = allocator; task->what = what; task->interrupt = TASK_NONE; task->halted = TASK_NONE; task->when = when; task->halted_when = 0; task->backoff = 0; task->flush = 0; task->zone = zone; return task; }
int display_matrix_init(void) { if (!matrixdef_allocator) { matrixdef_allocator = allocator_create(256, sizeof(lua_matrix_def_t), "mat_def"); } if (!matrixref_allocator) { matrixref_allocator = allocator_create(256, sizeof(lua_matrix_t), "mat_ref"); } if (!matrixdef_allocator || !matrixref_allocator) { display_matrix_shutdown(); return -1; } return 0; }
allocator_t* allocator_get_global() { static allocator_t* allocator = NULL; if (!allocator) { allocator = allocator_create(); assert(allocator); } return allocator; }
int nc_connect(net_client_t **nc,const nc_arg_t *nc_arg) { //忽略SIGPIPE 信号 struct sigaction sa; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sa.sa_handler = SIG_IGN; sigaction(SIGPIPE,&sa,NULL); int rv,sd; rv = nc_nbconnect(&sd,nc_arg); if(rv < 0) return -1; int on=1; setsockopt(sd, IPPROTO_TCP, TCP_NODELAY,(void *)&on,(socklen_t)sizeof(on)); allocator_t *allocator; if(allocator_create(&allocator) < 0) { close(sd); return -1; } (*nc) = (net_client_t *)malloc(sizeof(net_client_t)); memset((*nc),0,sizeof(net_client_t)); thread_mutex_create(&((*nc)->mpool_mutex),0); allocator_mutex_set(allocator,(*nc)->mpool_mutex); (*nc)->allocator = allocator; queue_create(&((*nc)->recv_queue),C_MAX_QUEUE_CAPACITY); // BTPDQ_INIT(&(*nc)->recv_queue); // thread_mutex_create(&((*nc)->recv_mutex),0); thread_mutex_create(&((*nc)->peer_mutex),0); if(nc_arg->data_func) (*nc)->data_func = nc_arg->data_func; else (*nc)->data_func = default_process_func; if(nc_arg->msg_func) (*nc)->msg_func = nc_arg->msg_func; int epfd = evloop_init(); (*nc)->epfd = epfd; cpeer_create_out(sd,*nc); pthread_t td; pthread_create(&td,NULL,&start_threads,(*nc)); (*nc)->td_start = td; return 0; }
/** * Update zone list. * */ ods_status zonelist_update(zonelist_type* zl, const char* zlfile) { zonelist_type* new_zlist = NULL; allocator_type* tmp_alloc = NULL; time_t st_mtime = 0; ods_status status = ODS_STATUS_OK; char* datestamp = NULL; ods_log_debug("[%s] update zone list", zl_str); if (!zl|| !zl->zones || !zlfile) { return ODS_STATUS_ASSERT_ERR; } /* is the file updated? */ st_mtime = ods_file_lastmodified(zlfile); if (st_mtime <= zl->last_modified) { (void)time_datestamp(zl->last_modified, "%Y-%m-%d %T", &datestamp); ods_log_debug("[%s] zonelist file %s is unchanged since %s", zl_str, zlfile, datestamp?datestamp:"Unknown"); free((void*)datestamp); return ODS_STATUS_UNCHANGED; } /* create new zonelist */ tmp_alloc = allocator_create(malloc, free); if (!tmp_alloc) { return ODS_STATUS_MALLOC_ERR; } new_zlist = zonelist_create(tmp_alloc); if (!new_zlist) { ods_log_error("[%s] unable to update zonelist: zonelist_create() " "failed", zl_str); allocator_cleanup(tmp_alloc); return ODS_STATUS_ERR; } /* read zonelist */ status = zonelist_read(new_zlist, zlfile); if (status == ODS_STATUS_OK) { zl->just_removed = 0; zl->just_added = 0; zl->just_updated = 0; new_zlist->last_modified = st_mtime; zonelist_merge(zl, new_zlist); (void)time_datestamp(zl->last_modified, "%Y-%m-%d %T", &datestamp); ods_log_debug("[%s] file %s is modified since %s", zl_str, zlfile, datestamp?datestamp:"Unknown"); free((void*)datestamp); } else { ods_log_error("[%s] unable to update zonelist: read file %s failed " "(%s)", zl_str, zlfile, ods_status2str(status)); } zonelist_free(new_zlist); allocator_cleanup(tmp_alloc); return status; }
/** * Create engine. * */ static engine_type* engine_create(void) { engine_type* engine; allocator_type* allocator = allocator_create(malloc, free); if (!allocator) { ods_log_error("[%s] unable to create engine: allocator_create() " "failed", engine_str); return NULL; } engine = (engine_type*) allocator_alloc(allocator, sizeof(engine_type)); if (!engine) { ods_log_error("[%s] unable to create engine: allocator_alloc() " "failed", engine_str); 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->dnshandler = NULL; engine->xfrhandler = NULL; engine->pid = -1; engine->uid = -1; engine->gid = -1; engine->daemonize = 0; engine->need_to_exit = 0; engine->need_to_reload = 0; lock_basic_init(&engine->signal_lock); lock_basic_set(&engine->signal_cond); lock_basic_lock(&engine->signal_lock); engine->signal = SIGNAL_INIT; lock_basic_unlock(&engine->signal_lock); engine->zonelist = zonelist_create(engine->allocator); if (!engine->zonelist) { engine_cleanup(engine); return NULL; } 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; }
/** * 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; }
/** * Main. start interface tool. * */ int main(int argc, char* argv[]) { char* cmd = NULL; int ret = 0; allocator_type* clialloc = allocator_create(malloc, free); if (!clialloc) { fprintf(stderr,"error, malloc failed for client\n"); exit(1); } /* argv[0] is always the executable name so argc is always 1 or higher */ ods_log_assert(argc >= 1); /* concat arguments an add 1 extra char for adding '\n' char later on, but only when argc > 1 */ cmd = ods_str_join(clialloc,argc-1,&argv[1],' '); if (argc > 1 && !cmd) { fprintf(stderr, "memory allocation failed\n"); exit(1); } /* main stuff */ if (!cmd) { ret = interface_start(cmd); } else { if (ods_strcmp(cmd, "-h") == 0 || ods_strcmp(cmd, "--help") == 0) { usage(stdout); ret = 1; } else { strcat(cmd,"\n"); ret = interface_start(cmd); } } /* done */ allocator_deallocate(clialloc, (void*) cmd); allocator_cleanup(clialloc); return ret; }
/** * 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; }
/** * 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; }
int peer_create_in(int fd,struct sockaddr_in addr,struct net_server_t *ns) { allocator_t *allocator; if(allocator_create(&allocator) < 0) return -1; struct peer *p = (struct peer *)mmalloc(ns->allocator,sizeof(struct peer)); memset(p,0,sizeof(struct peer)); p->sd = fd; gen_peerid(ns,&p->id); p->refcount = 0; p->status = PEER_CONNECTED; p->ns = ns; p->addr = addr; p->allocator = allocator; thread_mutex_create(&(p->mpool_mutex),0); allocator_mutex_set(allocator,p->mpool_mutex); p->sendbuf = iobuf_init(p->allocator,512); p->recvbuf = iobuf_init(p->allocator,512); thread_mutex_create(&(p->sq_mutex),0); BTPDQ_INIT(&p->send_queue); thread_mutex_lock(ns->ptbl_mutex); ptbl_insert(ns->ptbl,p); ns->npeers++; thread_mutex_unlock(ns->ptbl_mutex); fdev_new(ns->epfd,&p->ioev,fd,EV_READ,net_io_cb,p); char *peer_ip = inet_ntoa(p->addr.sin_addr); log_error(ns->log,"ns accept new connection from:%s.",peer_ip); return 0; }
/** * 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; }