int sock_comm_buffer_init(struct sock_conn *conn) { int optval; socklen_t size = SOCK_COMM_BUF_SZ; socklen_t optlen = sizeof(socklen_t); optval = 1; if (setsockopt(conn->sock_fd, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof optval)) SOCK_LOG_ERROR("setsockopt failed\n"); fd_set_nonblock(conn->sock_fd); rbinit(&conn->inbuf, SOCK_COMM_BUF_SZ); rbinit(&conn->outbuf, SOCK_COMM_BUF_SZ); if (setsockopt(conn->sock_fd, SOL_SOCKET, SO_RCVBUF, &size, optlen)) SOCK_LOG_ERROR("setsockopt failed\n"); if (setsockopt(conn->sock_fd, SOL_SOCKET, SO_SNDBUF, &size, optlen)) SOCK_LOG_ERROR("setsockopt failed\n"); if (!getsockopt(conn->sock_fd, SOL_SOCKET, SO_RCVBUF, &size, &optlen)) SOCK_LOG_INFO("SO_RCVBUF: %d\n", size); optlen = sizeof(socklen_t); if (!getsockopt(conn->sock_fd, SOL_SOCKET, SO_SNDBUF, &size, &optlen)) SOCK_LOG_INFO("SO_SNDBUF: %d\n", size); return 0; }
int sock_comm_buffer_init(struct sock_conn *conn) { int optval; uint64_t flags; socklen_t size = SOCK_COMM_BUF_SZ; socklen_t optlen = sizeof(socklen_t); optval = 1; setsockopt(conn->sock_fd, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof optval); flags = fcntl(conn->sock_fd, F_GETFL, 0); fcntl(conn->sock_fd, F_SETFL, flags | O_NONBLOCK); rbinit(&conn->inbuf, SOCK_COMM_BUF_SZ); rbinit(&conn->outbuf, SOCK_COMM_BUF_SZ); setsockopt(conn->sock_fd, SOL_SOCKET, SO_RCVBUF, &size, optlen); setsockopt(conn->sock_fd, SOL_SOCKET, SO_SNDBUF, &size, optlen); getsockopt(conn->sock_fd, SOL_SOCKET, SO_RCVBUF, &size, &optlen); SOCK_LOG_INFO("SO_RCVBUF: %d\n", size); optlen = sizeof(socklen_t); getsockopt(conn->sock_fd, SOL_SOCKET, SO_SNDBUF, &size, &optlen); SOCK_LOG_INFO("SO_SNDBUF: %d\n", size); return 0; }
void FS_InitCache (void) { #ifdef MAGIC_BTREE rb = rbinit (_compare, 1); #else #ifdef LINUX rb = rbinit ((int (EXPORT *)(const void *, const void *))strcmp, 0); #else rb = rbinit ((int (EXPORT *)(const void *, const void *))Q_stricmp, 0); #endif #endif if (!rb) Com_Error (ERR_FATAL, "FS_InitCache: rbinit failed"); }
int main() { int i, *ptr; const void *val; struct rbtree *rb; srand(getpid()); if ((rb=rbinit(compare, NULL))==NULL) { fprintf(stderr, "insufficient memory\n"); exit(1); } for (i = 0; i < 12; i++) { ptr = (int *)xmalloc(sizeof(int)); *ptr = rand()&0xff; val = rbsearch((void *)ptr, rb); if(val == NULL) { fprintf(stderr, "insufficient memory\n"); exit(1); } } for(val=rblookup(RB_LUFIRST, NULL, rb); val!=NULL; val=rblookup(RB_LUNEXT, val, rb)) { printf("%6d\n", *(int *)val); } rbdestroy(rb); return 0; }
/* ============ Cmd_Init ============ */ void Cmd_Init (void) { cmdtree = rbinit ((int (EXPORT *)(const void *, const void *))strcmp, 0); aliastree = rbinit ((int (EXPORT *)(const void *, const void *))strcmp, 0); // // register our commands // Cmd_AddCommand ("trigger",Cmd_Trigger_f); Cmd_AddCommand ("untrigger", Cmd_UnTrigger_f); Cmd_AddCommand ("cmdlist",Cmd_List_f); Cmd_AddCommand ("exec",Cmd_Exec_f); Cmd_AddCommand ("echo",Cmd_Echo_f); Cmd_AddCommand ("alias",Cmd_Alias_f); Cmd_AddCommand ("aliaslist",Cmd_Aliaslist_f); Cmd_AddCommand ("wait", Cmd_Wait_f); }
/* ============ Cvar_Init Reads in all archived cvars ============ */ void Cvar_Init (void) { cvartree = rbinit ((int (EXPORT *)(const void *, const void *))strcmp, 0); Cmd_AddCommand ("set", Cvar_Set_f); Cmd_AddCommand ("cvarlist", Cvar_List_f); Cmd_AddCommand ("cvarhelp", Cvar_Help_f); developer = Cvar_Get ("developer", "0", 0); }
lv_t *lisp_create_hash(void) { lv_t *result; result = safe_malloc(sizeof(lv_t)); result->type = l_hash; L_HASH(result) = rbinit(s_hash_cmp, NULL); assert(L_HASH(result)); return result; }
stasis_aggregate_min_t * stasis_aggregate_min_init(int large) { stasis_aggregate_min_t * ret = malloc(sizeof(*ret)); if(large) { ret->tree = rbinit(cmp_lsn_t,0); } else { ret->tree = 0; ret->vals = 0; ret->num_entries = 0; pthread_key_create(&ret->key, free_key); ret->memo = 0; } return ret; }
int main(int argc, char** argv) { ITEM *ptr, *result; struct rbtree *rb; ITEM c; FILE *f; char *dbName = "f_inc.0.u.test"; c.w = atof(argv[1]); c.value = -1; if ((rb=rbinit(compare, NULL))==NULL) { fprintf(stderr, "insufficient memory\n"); exit(1); } f = fopen(dbName, "r"); ptr = (ITEM *)xmalloc(sizeof(ITEM)); while (fscanf(f, "%lf", &ptr->w) != EOF) { fscanf(f, "%lf", &ptr->value); rbsearch(ptr, rb); ptr = (ITEM *)xmalloc(sizeof(ITEM)); } result = (ITEM *)rbsearch(&c, rb); fprintf(stdout, "%.8lf\t%.8lf\n", result->w, result->value); /* for (i = 50000; i > 0; i--) { ptr = (ITEM *)xmalloc(sizeof(ITEM)); ptr->w = i; ptr->value = (double)i/7; val = rbsearch((void *)ptr, rb); if(val == NULL) { fprintf(stderr, "insufficient memory\n"); exit(1); } } rbwalk(rb, walkact, "No"); printf("Minimum branch length: %d\n", minleaf); printf("Maximum branch length: %d\n", maxleaf); */ rbdestroy(rb); return 0; }
void free_metadata_list (struct ushare_t *ut) { ut->init = 0; if (ut->root_entry) upnp_entry_free (ut, ut->root_entry); ut->root_entry = NULL; ut->nr_entries = 0; if (ut->rb) { rbdestroy (ut->rb); ut->rb = NULL; } ut->rb = rbinit (rb_compare, NULL); if (!ut->rb) log_error (_("Cannot create RB tree for lookups\n")); }
skPDUSource_t * skPDUSourceCreate( const skpc_probe_t *probe, const skFlowSourceParams_t *params) { skPDUSource_t *source; assert(probe); /* Create and initialize source */ source = (skPDUSource_t*)calloc(1, sizeof(*source)); if (source == NULL) { goto ERROR; } source->probe = probe; source->name = skpcProbeGetName(probe); source->logopt = skpcProbeGetLogFlags(probe); source->engine_info_tree = rbinit(pdu_engine_compare, NULL); if (source->engine_info_tree == NULL) { goto ERROR; } source->source = skUDPSourceCreate(probe, params, V5PDU_LEN, &pduSourceRejectPacket, source); if (NULL == source->source) { goto ERROR; } pthread_mutex_init(&source->stats_mutex, NULL); return source; ERROR: if (source) { if (source->engine_info_tree) { rbdestroy(source->engine_info_tree); } free(source); } return NULL; }
int main() { int i, *ptr; const void *val; struct rbtree *rb; RBLIST *rblist; if ((rb=rbinit(compare, NULL))==NULL) { fprintf(stderr, "insufficient memory from rbinit()\n"); exit(1); } for (i = 200; i > 0; i--) { ptr = (int *)xmalloc(sizeof(int)); *ptr = i; val = rbsearch((void *)ptr, rb); if(val == NULL) { fprintf(stderr, "insufficient memory from rbsearch()\n"); exit(1); } } if ((rblist=rbopenlist(rb))==NULL) { fprintf(stderr, "insufficient memory from rbopenlist()\n"); exit(1); } while((val=rbreadlist(rblist))) { printf("%6d\n", *(int *)val); } rbcloselist(rblist); rbdestroy(rb); return 0; }
int_dict_t * int_dict_create( size_t value_size) { int_dict_t *d; d = (int_dict_t*)malloc(sizeof(int_dict_t)); if (d == NULL) { return NULL; } d->tree = rbinit(int_node_compare, NULL); if (d->tree == NULL) { free(d); return NULL; } d->tnode = NULL; d->value_size = value_size; d->count = 0; RW_MUTEX_INIT(&d->mutex); return d; }
int main() { int i,N,q,a; int cases; scanf("%d",&cases); while(cases--) { scanf("%d %d",&N,&q); rbinit(); for(i=1;i<=N;i++) value[i]=i,rbindex[i]=i; rbbuildtree(N); /* for(i=1;i<=N;i++) rbinsert(i);*/ q--; scanf("%d",&a); printf("%d",process(a)); while(q--) { scanf("%d",&a); printf(" %d",process(a)); } putchar('\n'); } return 0; }
int sock_cq_open(struct fid_domain *domain, struct fi_cq_attr *attr, struct fid_cq **cq, void *context) { struct sock_domain *sock_dom; struct sock_cq *sock_cq; struct fi_wait_attr wait_attr; struct sock_fid_list *list_entry; struct sock_wait *wait; int ret; sock_dom = container_of(domain, struct sock_domain, dom_fid); ret = sock_cq_verify_attr(attr); if (ret) return ret; sock_cq = calloc(1, sizeof(*sock_cq)); if (!sock_cq) return -FI_ENOMEM; atomic_initialize(&sock_cq->ref, 0); sock_cq->cq_fid.fid.fclass = FI_CLASS_CQ; sock_cq->cq_fid.fid.context = context; sock_cq->cq_fid.fid.ops = &sock_cq_fi_ops; sock_cq->cq_fid.ops = &sock_cq_ops; if (attr == NULL) sock_cq->attr = _sock_cq_def_attr; else { sock_cq->attr = *attr; if (attr->size == 0) sock_cq->attr.size = _sock_cq_def_attr.size; } sock_cq->domain = sock_dom; sock_cq->cq_entry_size = sock_cq_entry_size(sock_cq); sock_cq_set_report_fn(sock_cq); dlist_init(&sock_cq->tx_list); dlist_init(&sock_cq->rx_list); dlist_init(&sock_cq->ep_list); dlist_init(&sock_cq->overflow_list); if ((ret = rbfdinit(&sock_cq->cq_rbfd, sock_cq->attr.size * sock_cq->cq_entry_size))) goto err1; if ((ret = rbinit(&sock_cq->addr_rb, sock_cq->attr.size * sizeof(fi_addr_t)))) goto err2; if ((ret = rbinit(&sock_cq->cqerr_rb, sock_cq->attr.size * sizeof(struct fi_cq_err_entry)))) goto err3; fastlock_init(&sock_cq->lock); switch (sock_cq->attr.wait_obj) { case FI_WAIT_NONE: case FI_WAIT_UNSPEC: case FI_WAIT_FD: break; case FI_WAIT_MUTEX_COND: wait_attr.flags = 0; wait_attr.wait_obj = FI_WAIT_MUTEX_COND; ret = sock_wait_open(&sock_dom->fab->fab_fid, &wait_attr, &sock_cq->waitset); if (ret) { ret = -FI_EINVAL; goto err4; } sock_cq->signal = 1; break; case FI_WAIT_SET: if (!attr) { ret = -FI_EINVAL; goto err4; } sock_cq->waitset = attr->wait_set; sock_cq->signal = 1; wait = container_of(attr->wait_set, struct sock_wait, wait_fid); list_entry = calloc(1, sizeof(*list_entry)); dlist_init(&list_entry->entry); list_entry->fid = &sock_cq->cq_fid.fid; dlist_insert_after(&list_entry->entry, &wait->fid_list); break; default: break; } *cq = &sock_cq->cq_fid; atomic_inc(&sock_dom->ref); fastlock_init(&sock_cq->list_lock); return 0; err4: rbfree(&sock_cq->cqerr_rb); err3: rbfree(&sock_cq->addr_rb); err2: rbfdfree(&sock_cq->cq_rbfd); err1: free(sock_cq); return ret; }
/** * scan an iTunes xml music database file, augmenting * the metainfo with that found in the xml file * * @param filename xml file to parse * @returns TRUE if playlist parsed successfully, FALSE otherwise */ int scan_xml_playlist(char *filename) { char *working_base; const void *val; int retval=TRUE; SCAN_XML_RB *lookup_ptr; SCAN_XML_RB lookup_val; RXMLHANDLE xml_handle; MAYBEFREE(scan_xml_itunes_version); MAYBEFREE(scan_xml_itunes_base_path); MAYBEFREE(scan_xml_itunes_decoded_base_path); MAYBEFREE(scan_xml_real_base_path); scan_xml_file = filename; /* initialize the redblack tree */ if((scan_xml_db = rbinit(scan_xml_rb_compare,NULL)) == NULL) { DPRINTF(E_LOG,L_SCAN,"Could not initialize red/black tree\n"); return FALSE; } /* find the base dir of the itunes playlist itself */ working_base = strdup(filename); if(strrchr(working_base,'/')) { *(strrchr(working_base,'/') + 1) = '\x0'; scan_xml_real_base_path = strdup(working_base); } else { scan_xml_real_base_path = strdup("/"); } free(working_base); DPRINTF(E_SPAM,L_SCAN,"Parsing xml file: %s\n",filename); if(!rxml_open(&xml_handle,filename,scan_xml_handler,NULL)) { DPRINTF(E_LOG,L_SCAN,"Error opening xml file %s: %s\n", filename,rxml_errorstring(xml_handle)); } else { if(!rxml_parse(xml_handle)) { retval=FALSE; DPRINTF(E_LOG,L_SCAN,"Error parsing xml file %s: %s\n", filename,rxml_errorstring(xml_handle)); } } rxml_close(xml_handle); /* destroy the redblack tree */ val = rblookup(RB_LUFIRST,NULL,scan_xml_db); while(val) { lookup_val.itunes_index = ((SCAN_XML_RB*)val)->itunes_index; lookup_ptr = (SCAN_XML_RB *)rbdelete((void*)&lookup_val,scan_xml_db); if(lookup_ptr) free(lookup_ptr); val = rblookup(RB_LUFIRST,NULL,scan_xml_db); } rbdestroy(scan_xml_db); MAYBEFREE(scan_xml_itunes_version); MAYBEFREE(scan_xml_itunes_base_path); MAYBEFREE(scan_xml_itunes_decoded_base_path); MAYBEFREE(scan_xml_real_base_path); return retval; }
/* ================= FS_LoadPackFile Takes an explicit (not game tree related) path to a pak file. Loads the header and directory, adding the files at the beginning of the list so they override previous pack files. ================= */ static pack_t /*@null@*/ *FS_LoadPackFile (const char *packfile, const char *ext) { int i; void **newitem; pack_t *pack = NULL; packfile_t *info; if (!strcmp (ext, "pak")) { unsigned pakLen; int numpackfiles; FILE *packhandle; dpackheader_t header; packhandle = fopen(packfile, "rb"); if (!packhandle) return NULL; fseek (packhandle, 0, SEEK_END); pakLen = ftell (packhandle); rewind (packhandle); if (fread (&header, sizeof(header), 1, packhandle) != 1) Com_Error (ERR_FATAL, "FS_LoadPackFile: Couldn't read pak header from %s", packfile); if (LittleLong(header.ident) != IDPAKHEADER) Com_Error (ERR_FATAL, "FS_LoadPackFile: %s is not a valid pak file.", packfile); #if YOU_HAVE_A_BROKEN_COMPUTER header.dirofs = LittleLong (header.dirofs); header.dirlen = LittleLong (header.dirlen); #endif if (header.dirlen % sizeof(packfile_t)) Com_Error (ERR_FATAL, "FS_LoadPackFile: Bad pak file %s (directory length %u is not a multiple of %d)", packfile, header.dirlen, (int)sizeof(packfile_t)); numpackfiles = header.dirlen / sizeof(packfile_t); if (numpackfiles > MAX_FILES_IN_PACK) //Com_Error (ERR_FATAL, "FS_LoadPackFile: packfile %s has %i files (max allowed %d)", packfile, numpackfiles, MAX_FILES_IN_PACK); Com_Printf ("WARNING: Pak file %s has %i files (max allowed %d) - may not be compatible with other clients\n", LOG_GENERAL, packfile, numpackfiles, MAX_FILES_IN_PACK); if (!numpackfiles) { fclose (packhandle); Com_Printf ("WARNING: Empty packfile %s\n", LOG_GENERAL|LOG_WARNING, packfile); return NULL; } //newfiles = Z_TagMalloc (numpackfiles * sizeof(packfile_t), TAGMALLOC_FSLOADPAK); info = Z_TagMalloc (numpackfiles * sizeof(packfile_t), TAGMALLOC_FSLOADPAK); if (fseek (packhandle, header.dirofs, SEEK_SET)) Com_Error (ERR_FATAL, "FS_LoadPackFile: fseek() to offset %u in %s failed. Pak file is possibly corrupt.", header.dirofs, packfile); if ((int)fread (info, 1, header.dirlen, packhandle) != header.dirlen) Com_Error (ERR_FATAL, "FS_LoadPackFile: Error reading packfile directory from %s (failed to read %u bytes at %u). Pak file is possibly corrupt.", packfile, header.dirofs, header.dirlen); pack = Z_TagMalloc (sizeof (pack_t), TAGMALLOC_FSLOADPAK); pack->type = PAK_QUAKE; pack->rb = rbinit ((int (EXPORT *)(const void *, const void *))strcmp, numpackfiles); //entry = Z_TagMalloc (sizeof(packfile_t) * numpackfiles, TAGMALLOC_FSLOADPAK); for (i=0 ; i<numpackfiles ; i++) { fast_strlwr (info[i].name); #if YOU_HAVE_A_BROKEN_COMPUTER info[i].filepos = LittleLong(info[i].filepos); info[i].filelen = LittleLong(info[i].filelen); #endif if (info[i].filepos + info[i].filelen >= pakLen) Com_Error (ERR_FATAL, "FS_LoadPackFile: File '%.64s' in pak file %s has illegal offset %u past end of file %u. Pak file is possibly corrupt.", MakePrintable (info[i].name, 0), packfile, info[i].filepos, pakLen); newitem = rbsearch (info[i].name, pack->rb); *newitem = &info[i]; } Q_strncpy (pack->filename, packfile, sizeof(pack->filename)-1); pack->h.handle = packhandle; pack->numfiles = numpackfiles; Com_Printf ("Added packfile %s (%i files)\n", LOG_GENERAL, packfile, numpackfiles); } #ifndef NO_ZLIB else if (!strcmp (ext, "pkz")) { unzFile f; unz_global_info zipinfo; char zipFileName[56]; unz_file_info fileInfo; f = unzOpen (packfile); if (!f) return NULL; if (unzGetGlobalInfo (f, &zipinfo) != UNZ_OK) Com_Error (ERR_FATAL, "FS_LoadPackFile: Couldn't read .zip info from '%s'", packfile); info = Z_TagMalloc (zipinfo.number_entry * sizeof(*info), TAGMALLOC_FSLOADPAK); pack = Z_TagMalloc (sizeof (pack_t), TAGMALLOC_FSLOADPAK); pack->type = PAK_ZIP; pack->rb = rbinit ((int (EXPORT *)(const void *, const void *))strcmp, zipinfo.number_entry); if (unzGoToFirstFile (f) != UNZ_OK) Com_Error (ERR_FATAL, "FS_LoadPackFile: Couldn't seek to first .zip file in '%s'", packfile); zipFileName[sizeof(zipFileName)-1] = 0; i = 0; do { if (unzGetCurrentFileInfo (f, &fileInfo, zipFileName, sizeof(zipFileName)-1, NULL, 0, NULL, 0) == UNZ_OK) { //directory, ignored if (fileInfo.external_fa & 16) continue; strcpy (info[i].name, zipFileName); info[i].filepos = unzGetOffset (f); info[i].filelen = fileInfo.uncompressed_size; newitem = rbsearch (info[i].name, pack->rb); *newitem = &info[i]; i++; } } while (unzGoToNextFile (f) == UNZ_OK); pack->h.zhandle = f; Com_Printf ("Added zpackfile %s (%i files)\n", LOG_GENERAL, packfile, i); } #endif else { Com_Error (ERR_FATAL, "FS_LoadPackFile: Unknown type %s", ext); } return pack; }
void connect_init(void) { state_tbl=rbinit(111); return; }
int sock_cq_open(struct fid_domain *domain, struct fi_cq_attr *attr, struct fid_cq **cq, void *context) { struct sock_domain *sock_dom; struct sock_cq *sock_cq; int ret; sock_dom = container_of(domain, struct sock_domain, dom_fid); ret = sock_cq_verify_attr(attr); if (ret) return ret; sock_cq = calloc(1, sizeof(*sock_cq)); if (!sock_cq) return -FI_ENOMEM; atomic_init(&sock_cq->ref, 0); sock_cq->cq_fid.fid.fclass = FI_CLASS_CQ; sock_cq->cq_fid.fid.context = context; sock_cq->cq_fid.fid.ops = &sock_cq_fi_ops; sock_cq->cq_fid.ops = &sock_cq_ops; atomic_inc(&sock_dom->ref); if(attr == NULL) memcpy(&sock_cq->attr, &_sock_cq_def_attr, sizeof(struct fi_cq_attr)); else memcpy(&sock_cq->attr, attr, sizeof(struct fi_cq_attr)); sock_cq->domain = sock_dom; sock_cq->cq_entry_size = sock_cq_entry_size(sock_cq); sock_cq_set_report_fn(sock_cq); dlist_init(&sock_cq->tx_list); dlist_init(&sock_cq->rx_list); dlist_init(&sock_cq->ep_list); if((ret = rbfdinit(&sock_cq->cq_rbfd, sock_cq->attr.size))) goto err1; if((ret = rbinit(&sock_cq->addr_rb, (sock_cq->attr.size/sock_cq->cq_entry_size) * sizeof(fi_addr_t)))) goto err2; if((ret = rbinit(&sock_cq->cqerr_rb, sock_cq->attr.size))) goto err3; fastlock_init(&sock_cq->lock); *cq = &sock_cq->cq_fid; atomic_inc(&sock_dom->ref); return 0; err3: rbfree(&sock_cq->addr_rb); err2: rbfdfree(&sock_cq->cq_rbfd); err1: free(sock_cq); return ret; }