tt_sshmsg_t *tt_sshmsg_create(IN tt_u32_t msg_id, IN tt_u32_t msg_len, IN tt_sshmsg_itf_t *itf) { tt_sshmsg_t *msg; TT_ASSERT(itf != NULL); TT_ASSERT(itf->render_prepare != NULL); TT_ASSERT(itf->render != NULL); TT_ASSERT(itf->parse != NULL); msg_len += sizeof(tt_sshmsg_t); msg = (tt_sshmsg_t *)tt_malloc(msg_len); tt_memset(msg, 0, msg_len); if (msg != NULL) { msg->msg_id = msg_id; msg->msg_private = 0; tt_atomic_s32_set(&msg->ref, 1); tt_buf_init(&msg->buf, NULL); msg->itf = itf; // flags are all set to 0 if ((msg->itf->create != NULL) && !TT_OK(msg->itf->create(msg))) { tt_buf_destroy(&msg->buf); tt_free(msg); return NULL; } } return msg; }
tt_result_t tt_dns_create_ntv(IN ares_channel ch) { if (ch->nservers != 0) { __dskt_t *dskt; int i; dskt = tt_malloc(sizeof(__dskt_t) * __DSKT_NUM(ch)); if (dskt == NULL) { TT_ERROR("no mem for dns wov"); return TT_FAIL; } for (i = 0; i < __DSKT_NUM(ch); ++i) { __dskt_init(&dskt[i], ch); } // save dskt in ch->sock_create_cb_data. note ares_dup() // would copy the pointer, then two ares_channels would // share same dskts, which is not expected, so must do a // "deep copy" if ares_dup() is called ares_set_socket_callback(ch, NULL, dskt); } ares_set_socket_functions(ch, &__dskt_itf, ch); return TT_SUCCESS; }
// // Parse Get_Locale reply and pass it to user callback. // Also used to parse Set_Locale request and pass it to _ttDtApplyLocale(). // Tt_message _ttDtGetLocaleCB( Tt_message msg, Tt_pattern , void *clientCB, void *clientData ) { _TttkItem2Free fuse = msg; int numArgs = tt_message_args_count( msg ); Tt_status status = tt_int_error( numArgs ); if (status != TT_OK) { return (Tt_message)tt_error_pointer( status ); } _TttkList2Free fuses( numArgs + 2 ); int n = numArgs / 2; char **categories = (char **)tt_malloc( (n + 1) * sizeof(char *) ); char **locales = (char **)tt_malloc( (n + 1) * sizeof(char *) ); categories[ n ] = 0; locales[ n ] = 0; // // We only need these guys until after we call clientCB // fuses += (caddr_t)categories; fuses += (caddr_t)locales; for (int i = 0; i < n; i++) { categories[i] = tt_message_arg_val( msg, 2 * i ); status = tt_ptr_error( categories[i] ); if (status != TT_OK) { return (Tt_message)tt_error_pointer( status ); } fuses += categories[i]; locales[i] = tt_message_arg_val( msg, 2 * i + 1 ); status = tt_ptr_error( locales[i] ); if (status != TT_OK) { return (Tt_message)tt_error_pointer( status ); } fuses += locales[i]; } fuse = (caddr_t)0; // aborts message destruction Ttdt_Get_Locale_msg_cb _cb = (Ttdt_Get_Locale_msg_cb)clientCB; return (*_cb)( msg, clientData, (const char **)categories, (const char **)locales ); }
t_cone *create_cone(t_vec3 p, double r, double h) { t_cone *c; c = tt_malloc(sizeof(t_cone)); c->p = p; c->r = r; c->h = h; return (c); }
t_plane *create_plane(t_vec3 n, double d) { t_plane *plane; plane = tt_malloc(sizeof(t_plane)); plane->n = normalize(n); plane->d = d; plane->p = vec_scalar(plane->n, d); printf("%f, %f, %f\n", plane->p.x, plane->p.y, plane->p.z); return (plane); }
t_data *init(void) { t_data *d; if(!(d = tt_malloc(sizeof(t_data)))) exit(tt_pl("Failed to alloc data")); d->r.o = VEC3(0, 0, 0); tt_pl("Init mlx"); init_mlx(d); tt_pl("Init obj"); init_obj(d); tt_pl("Init light"); init_light(d); return (d); }
tt_netif_addr_t *tt_netif_addr_create(IN tt_net_family_t family) { tt_netif_addr_t *addr; addr = (tt_netif_addr_t *)tt_malloc(sizeof(tt_netif_addr_t)); if (addr == NULL) { TT_ERROR("no mem for new netif addr"); return NULL; } tt_lnode_init(&addr->node); addr->internal_flag = 0; tt_sktaddr_init(&addr->addr, TT_NET_AF_INET); tt_sktaddr_init(&addr->netmask, TT_NET_AF_INET); tt_sktaddr_init(&addr->dstaddr, TT_NET_AF_INET); return addr; }
tt_logfltr_t *tt_logfltr_create(IN tt_u32_t size, IN tt_logfltr_itf_t *itf) { tt_logfltr_t *lf; if ((itf == NULL) || (itf->input == NULL)) { return NULL; } lf = tt_malloc(sizeof(tt_logfltr_t) + size); if (lf == NULL) { return NULL; } lf->itf = itf; tt_ptrq_init(&lf->io_q, NULL); tt_atomic_s32_set(&lf->ref, 1); return lf; }
tt_result_t tt_sshctx_pubk_create(IN tt_sshctx_t *sshctx) { tt_sshpubk_t *pubk; if (sshctx->pubk != NULL) { TT_ERROR("sshctx already has pubk"); return TT_FAIL; } pubk = (tt_sshpubk_t *)tt_malloc(sizeof(tt_sshpubk_t)); if (pubk == NULL) { TT_ERROR("no mem for sshctx pubk"); return TT_FAIL; } tt_sshpubk_init(pubk); sshctx->pubk = pubk; return TT_SUCCESS; }
tt_result_t tt_sshctx_kex_create(IN tt_sshctx_t *sshctx) { tt_sshkex_t *kex; if (sshctx->kex != NULL) { TT_ERROR("sshctx already has kex"); return TT_FAIL; } kex = (tt_sshkex_t *)tt_malloc(sizeof(tt_sshkex_t)); if (kex == NULL) { TT_ERROR("no mem for sshctx kex"); return TT_FAIL; } if (!TT_OK(tt_sshkex_create(kex))) { tt_free(kex); return TT_FAIL; } sshctx->kex = kex; return TT_SUCCESS; }
char *_DtEnv_tt_host_netfile_file( const char *host, const char *filename) { static int first_time = 1; static int fragListAvail = CACHE_FILEFRAG_SIZE_START; static int fragListCnt = 0; static cachedNetfileFrag *fragList; static int cacheGen = 0; static int hitIdxStart = 0; char *newfile; int hitval, hitIdx, i; cachedNetfileFrag *tmpCffP; char *tmpStr; int newCount = fragListCnt; _DtSvcProcessLock(); if (first_time) { fragList = (cachedNetfileFrag *) calloc( fragListAvail, sizeof(cachedNetfileFrag) ); first_time = 0; } /* * Take care of the obvious. */ if (!filename) { _DtSvcProcessUnlock(); return( (char *) NULL ); } if (!host) { /* * Return a tt_free-able un-mapped copy. */ tmpStr = tt_malloc( strlen(filename) + 1 ); strcpy( tmpStr, filename ); _DtSvcProcessUnlock(); return(tmpStr); } /* * Look for existing answer in cache. * * While at it, also look for least used entry just in case. */ if (fragListCnt) hitIdxStart = (hitIdxStart + 7) % fragListCnt; else hitIdxStart = 0; hitIdx = hitIdxStart; hitval = fragList[hitIdx].cacheHit; tmpCffP = fragList; /* walk rather than index */ for ( i = 0; i < fragListCnt; i++ ) { if (tmpCffP->cacheHit && !strcmp( filename, tmpCffP->pathFragOrig ) ) { if (!strcmp( host, tmpCffP->targetHost ) ) { break; } } /* * Save index of least used entry */ if (tmpCffP->cacheHit < hitval) { hitIdx = i; hitval = tmpCffP->cacheHit; } tmpCffP++; } /* * Decide what was found. */ if ( i != fragListCnt ) { /* * Found a cached entry. */ hitIdx = i; if ( fragList[hitIdx].cacheHit++ > CACHE_FILEFRAG_REMAP_AFTER ) { /* * This looks like an old entry, so re-compute it. */ freeAndNull( fragList[hitIdx].targetHost ); freeAndNull( fragList[hitIdx].pathFragOrig ); ttfreeAndNull( fragList[hitIdx].pathFragMapped ); fragList[hitIdx].cacheHit = 0; /* 0 means remap below */ } } else { /* * Did not find a cache entry, so scrounge around for * a new entry. */ if ( fragListCnt < fragListAvail ) { /* * Use next already-malloc'ed cacheEntry. */ hitIdx = fragListCnt; newCount = fragListCnt + 1; } else if ( fragListCnt < CACHE_FILEFRAG_SIZE_MAX ) { /* * Can grow fragList[] */ fragListAvail += CACHE_FILEFRAG_SIZE_BUMP; fragList = (cachedNetfileFrag *) realloc( (char *) fragList, sizeof(cachedNetfileFrag) * fragListAvail); /* * Zero out new memory. */ memset( fragList + (fragListAvail-CACHE_FILEFRAG_SIZE_BUMP), 0, CACHE_FILEFRAG_SIZE_BUMP*sizeof(cachedNetfileFrag) ); hitIdx = fragListCnt; newCount = fragListCnt + 1; } else { /* * Last resort - bump out the least used entry. */ freeAndNull( fragList[hitIdx].targetHost ); freeAndNull( fragList[hitIdx].pathFragOrig ); ttfreeAndNull( fragList[hitIdx].pathFragMapped ); /* * Since the cache is 100% full, ocassionally reset * everyone's cacheHit rate so entries that were only * popular long ago don't get locked in. */ if ( cacheGen++ > CACHE_FILEFRAG_RESET_PRI ) { cacheGen = 0; tmpCffP = fragList; for ( i = 0; i < fragListCnt; i++ ) { tmpCffP->cacheHit = 1; tmpCffP++; } } } fragList[hitIdx].cacheHit = 0; /* 0 means remap below */ } if ( ! fragList[hitIdx].cacheHit ) { /* * Need to perform mapping. */ newfile = tt_host_netfile_file( host, filename ); #ifdef _DTENV_SUPPORT_MAPERROR_CACHING fragList[hitIdx].targetHost = strdup ( host ); fragList[hitIdx].pathFragOrig = strdup( filename ); fragList[hitIdx].cacheHit = 1; fragList[hitIdx].pathFragMapped = newfile; fragListCnt = newCount; #else if ( tt_ptr_error(newfile) == TT_OK ) { fragList[hitIdx].targetHost = strdup ( host ); fragList[hitIdx].pathFragOrig = strdup( filename ); fragList[hitIdx].cacheHit = 1; fragList[hitIdx].pathFragMapped = newfile; /* * Only change the count if we are successful in adding * a new entry. */ fragListCnt = newCount; } else { /* * Don't cache errors. Leave this cache slot empty * and it will be rediscovered and used in the future. */ fragList[hitIdx].cacheHit = 0; /* * Do not change the fragListCount since we are not saving * error entries. */ } #endif /* _DTENV_SUPPORT_MAPERROR_CACHING */ } /* * Dig out answer and return it. */ #ifdef _DTENV_SUPPORT_MAPERROR_CACHING if ( tt_ptr_error(newfile) == TT_OK ) #else if ( fragList[hitIdx].cacheHit ) #endif /* _DTENV_SUPPORT_MAPERROR_CACHING */ { /* * Return a tt_free-able copy of the answer. */ tmpStr = tt_malloc( strlen(fragList[hitIdx].pathFragMapped) + 1 ); strcpy( tmpStr, fragList[hitIdx].pathFragMapped ); _DtSvcProcessUnlock(); return(tmpStr); } else { /* * See XXX comment. * * Since newfile is an error code, return as is. */ _DtSvcProcessUnlock(); return(newfile); } }
tt_bool_t __do_accept(IN tt_io_ev_t *io_ev) { __skt_accept_t *skt_accept = (__skt_accept_t *)io_ev; socklen_t len = sizeof(struct sockaddr_storage); int s, flag; struct epoll_event event; // tell caller that kq returned skt_accept->done = TT_TRUE; skt_accept->new_skt = tt_malloc(sizeof(tt_skt_t)); if (skt_accept->new_skt == NULL) { TT_ERROR("no mem for new skt"); return TT_TRUE; } again: s = accept4(skt_accept->skt->s, (struct sockaddr *)skt_accept->addr, &len, SOCK_NONBLOCK | SOCK_CLOEXEC); if (s == -1) { if (errno == EINTR) { goto again; } else { TT_ERROR_NTV("accept fail"); goto fail; } } #if 0 if (((flag = fcntl(s, F_GETFL, 0)) == -1) || (fcntl(s, F_SETFL, flag | O_NONBLOCK) == -1)) { TT_ERROR_NTV("fail to set O_NONBLOCK"); goto fail; } if (((flag = fcntl(s, F_GETFD, 0)) == -1) || (fcntl(s, F_SETFD, flag | FD_CLOEXEC) == -1)) { TT_ERROR_NTV("fail to set FD_CLOEXEC"); goto fail; } #endif event.events = EPOLLRDHUP | EPOLLONESHOT; event.data.ptr = &__s_null_io_ev; if (epoll_ctl(skt_accept->ep, EPOLL_CTL_ADD, s, &event) != 0) { TT_ERROR_NTV("fail to add skt to epoll"); goto fail; } skt_accept->new_skt->sys_skt.s = s; return TT_TRUE; fail: if (s != -1) { __RETRY_IF_EINTR(close(s)); } return TT_TRUE; }