int main(int argc, char *argv[]) { int arr_ints[] = {1,2,3,4,5}; double arr_doubles[] = {1.0, 2.0, 3.0, 4.0, 5.0}; int key_int = 5; double key_double = 3.0; double *dptr = NULL; int *iptr = NULL; iptr = (int *)lfind(&key_int, arr_ints, sizeof(arr_ints)/sizeof(arr_ints[0]), sizeof(arr_ints[0]), compare); if(iptr == NULL) { printf("Key not found in the array\n"); } else { printf("Key %d found at index %ld\n", *iptr, (long int)(iptr - arr_ints)); } dptr = (double *)lfind(&key_double, arr_doubles, sizeof(arr_doubles)/sizeof(arr_doubles[0]), sizeof(arr_doubles[0]), compare); if(dptr == NULL) { printf("Key not found in the array\n"); } else { printf("Key %f found at index %ld\n", *dptr, (long int)(dptr - arr_doubles)); } exit(EXIT_SUCCESS); }
TEST(search, lfind_lsearch) { int xs[10]; memset(xs, 0, sizeof(xs)); size_t x_size = 0; int needle; // lfind(3) can't find '2' in the empty table. needle = 2; ASSERT_EQ(nullptr, lfind(&needle, xs, &x_size, sizeof(xs[0]), int_cmp)); ASSERT_EQ(0U, x_size); // lsearch(3) will add it. ASSERT_EQ(&xs[0], lsearch(&needle, xs, &x_size, sizeof(xs[0]), int_cmp)); ASSERT_EQ(2, xs[0]); ASSERT_EQ(1U, x_size); // And then lfind(3) can find it. ASSERT_EQ(&xs[0], lfind(&needle, xs, &x_size, sizeof(xs[0]), int_cmp)); ASSERT_EQ(1U, x_size); // Inserting a duplicate does nothing (but returns the existing element). ASSERT_EQ(&xs[0], lsearch(&needle, xs, &x_size, sizeof(xs[0]), int_cmp)); ASSERT_EQ(1U, x_size); }
void main(void) { int int_values[] = {1, 3, 2, 4, 5}; float float_values[] = {1.1, 3.3, 2.2, 4.4, 5.5}; int *int_ptr, int_value = 2, elements = 5; float *float_ptr, float_value = 33.3; int_ptr = lfind(&int_value, int_values, &elements, sizeof(int), (int (*) (const void *, const void *)) compare_int); if (*int_ptr) printf("Value %d found\n", int_value); else printf("Value %d not found\n", int_value); float_ptr = lfind(&float_value, float_values, &elements, sizeof(float), (int (*) (const void *, const void *)) compare_float); if (*float_ptr) printf("Value %3.1f found\n", float_value); else printf("Value %3.1f not found\n", float_value); }
/* * Return a looped back vnode for the given vnode. * If no lnode exists for this vnode create one and put it * in a table hashed by vnode. If the lnode for * this vnode is already in the table return it (ref count is * incremented by lfind). The lnode will be flushed from the * table when lo_inactive calls freelonode. The creation of * a new lnode can be forced via the LOF_FORCE flag even if * the vnode exists in the table. This is used in the creation * of a terminating lnode when looping is detected. A unique * lnode is required for the correct evaluation of the current * working directory. * NOTE: vp is assumed to be a held vnode. */ struct vnode * makelonode(struct vnode *vp, struct loinfo *li, int flag) { lnode_t *lp, *tlp; struct vfs *vfsp; vnode_t *nvp; lp = NULL; TABLE_LOCK_ENTER(vp, li); if (flag != LOF_FORCE) lp = lfind(vp, li); if ((flag == LOF_FORCE) || (lp == NULL)) { /* * Optimistically assume that we won't need to sleep. */ lp = kmem_cache_alloc(lnode_cache, KM_NOSLEEP); nvp = vn_alloc(KM_NOSLEEP); if (lp == NULL || nvp == NULL) { TABLE_LOCK_EXIT(vp, li); /* The lnode allocation may have succeeded, save it */ tlp = lp; if (tlp == NULL) { tlp = kmem_cache_alloc(lnode_cache, KM_SLEEP); } if (nvp == NULL) { nvp = vn_alloc(KM_SLEEP); } lp = NULL; TABLE_LOCK_ENTER(vp, li); if (flag != LOF_FORCE) lp = lfind(vp, li); if (lp != NULL) { kmem_cache_free(lnode_cache, tlp); vn_free(nvp); VN_RELE(vp); goto found_lnode; } lp = tlp; } atomic_inc_32(&li->li_refct); vfsp = makelfsnode(vp->v_vfsp, li); lp->lo_vnode = nvp; VN_SET_VFS_TYPE_DEV(nvp, vfsp, vp->v_type, vp->v_rdev); nvp->v_flag |= (vp->v_flag & (VNOMOUNT|VNOMAP|VDIROPEN)); vn_setops(nvp, lo_vnodeops); nvp->v_data = (caddr_t)lp; lp->lo_vp = vp; lp->lo_looping = 0; lsave(lp, li); vn_exists(vp); } else { VN_RELE(vp); } found_lnode: TABLE_LOCK_EXIT(vp, li); return (ltov(lp)); }
void insert(int intarget) { size_t nmemb; PATTERN_ *p; char **p2; str2pattern (); nmemb = n_patterns; p = lfind (buffer2, patterns, &nmemb, sizeof (PATTERN_), lfindpat); if (! p) { if (n_patterns == max_patterns) { max_patterns += 32; patterns = (PATTERN_ *) s_realloc (patterns, max_patterns * sizeof (PATTERN_)); } patterns [n_patterns].s = s_strdup (buffer2); patterns [n_patterns].n_forms = 0; patterns [n_patterns].max_forms = 0; patterns [n_patterns].i = 0; patterns [n_patterns].o = 0; patterns [n_patterns].used = 0; patterns [n_patterns].rejected = 0; patterns [n_patterns].forms = NULL; p = &(patterns [n_patterns]); n_patterns++; } if (intarget) p->i++; else p->o++; if (p->i + p->o >= minvar) p->used = 1; p2 = NULL; if (p->n_forms > 0) { nmemb = p->n_forms; p2 = lfind (buffer, p->forms, &nmemb, sizeof (char *), searchcmp); } if (! p2) { if (p->n_forms == p->max_forms) { p->max_forms += 32; p->forms = (char **) s_realloc (p->forms, p->max_forms * sizeof (char *)); } p->forms[p->n_forms] = s_strdup (buffer); p->n_forms++; } }
const TIFFFieldInfo* _TIFFFindFieldInfoByName(TIFF* tif, const char *field_name, TIFFDataType dt) { int i, n; if (tif->tif_foundfield && streq(tif->tif_foundfield->field_name, field_name) && (dt == TIFF_ANY || dt == tif->tif_foundfield->field_type)) return (tif->tif_foundfield); /* NB: use sorted search (e.g. binary search) */ if(dt != TIFF_ANY) { TIFFFieldInfo key = {0, 0, 0, TIFF_NOTYPE, 0, 0, 0, 0}; TIFFFieldInfo* pkey = &key; const TIFFFieldInfo **ret; key.field_name = (char *)field_name; key.field_type = dt; ret = (const TIFFFieldInfo **) lfind(&pkey, tif->tif_fieldinfo, (void *) &tif->tif_nfields, sizeof(TIFFFieldInfo *), tagNameCompare); return (ret) ? (*ret) : NULL; } else for (i = 0, n = tif->tif_nfields; i < n; i++) { const TIFFFieldInfo* fip = tif->tif_fieldinfo[i]; if (streq(fip->field_name, field_name) && (dt == TIFF_ANY || fip->field_type == dt)) return (tif->tif_foundfield = fip); } return ((const TIFFFieldInfo *)0); }
int compare_contexts(const context_t *x, const context_t *y) { int *result = NULL; int ret_val = 0; // check if each entry in the 'x' context is in the 'y' context // for (uint i = 0; i < x->num_values; i++) { result = (int *) lfind (&(x->values[i]), y->values, &(y->num_values), sizeof(int), (int(*) (const void *, const void *))compare); if (result) { // if we got a non-null value then the value was found // ret_val = 1; } else { // if at any time we did not find a value we can give up // ret_val = 0; break; } } return ret_val; }
int VectorSearch(const vector *v, const void *key, VectorCompareFunction searchFn, int startIndex, bool isSorted) { if (isSorted) { const void *startSearch = (char*)v->elems + startIndex*v->elemSize; int nMembers = v->logicalLen - startIndex; void *found = bsearch(key, startSearch, nMembers, v->elemSize, searchFn); if (found == NULL) return -1; int idx = ((char*)found - (char*)v->elems)/v->elemSize; return idx; } else { void *startSearch = (char*)v->elems + startIndex*v->elemSize; int nMembers = v->logicalLen - startIndex; void *found = lfind(key, startSearch, &nMembers, v->elemSize, searchFn); if (found == NULL) return -1; int idx = ((char*)found - (char*)v->elems)/v->elemSize; return idx; } }
static const char *var_InheritModulation (vlc_object_t *obj, const char *var) { char *mod = var_InheritString (obj, var); if (mod == NULL) return ""; size_t n = sizeof (modulation_vlc) / sizeof (modulation_vlc[0]); const char *const *p = lfind (mod, modulation_vlc, &n, sizeof (mod), modcmp); if (p != NULL) { free (mod); return *p; } /* Backward compatibility with VLC < 1.2 */ const char *str; switch (atoi (mod)) { case -1: str = "QPSK"; break; case 0: str = "QAM"; break; case 8: str = "8VSB"; break; case 16: str = "16QAM"; break; case 32: str = "32QAM"; break; case 64: str = "64QAM"; break; case 128: str = "128QAM"; break; case 256: str = "256QAM"; break; default: return ""; } msg_Warn (obj, "\"modulation=%s\" option is obsolete. " "Use \"modulation=%s\" instead.", mod, str); free (mod); return str; }
int VectorSearch(const vector *v, const void *key, VectorCompareFunction searchFn, int startIndex, bool isSorted) { assert (v!=NULL); if(v->realSize == 0) return kNotFound; assert (searchFn!=NULL); assert (startIndex < v->realSize); assert (key !=NULL); assert (startIndex >=0); void * start = (char*)v->dataP+startIndex*v->elemSize; size_t size = v->realSize-startIndex; char* position; if(isSorted==true) position = bsearch(key,start,size,v->elemSize, searchFn); else position = lfind(key, start, &size,v->elemSize, searchFn); if(position !=NULL) return(position - (char*)v->dataP)/v->elemSize; return kNotFound; }
void main() { int Comparacion (const void *p, const void *q); char aux[10]; int n,*p; unsigned int Num = sizeof (a) / sizeof (int); cout << "De numero a buscar" << endl; gets (aux); n = atoi (aux); while ( n != 9999 ) { p = (int *) lfind ( (void *) &n, // Direccion de la llave (void *)a, // Arreglo &Num, // Numero de elementos sizeof (int), // Longitud de cada // elemento Comparacion ); if (p != NULL) { cout << "Si existe, "; printf ("en la posicion = %d\n",p-q); } else { cout << "No existe. " << endl; } cout << "De numero a buscar. 9999 para terminar " << endl; gets (aux); n = atoi (aux); } }
const TIFFFieldInfo* _TIFFFindFieldInfoByName(TIFF* tif, const char *field_name, TIFFDataType dt) { TIFFFieldInfo key = {0, 0, 0, TIFF_NOTYPE, 0, 0, 0, 0}; TIFFFieldInfo* pkey = &key; const TIFFFieldInfo **ret; if (tif->tif_foundfield && streq(tif->tif_foundfield->field_name, field_name) && (dt == TIFF_ANY || dt == tif->tif_foundfield->field_type)) return (tif->tif_foundfield); /* If we are invoked with no field information, then just return. */ if ( !tif->tif_fieldinfo ) { return NULL; } /* NB: use sorted search (e.g. binary search) */ key.field_name = (char *)field_name; key.field_type = dt; ret = (const TIFFFieldInfo **) lfind(&pkey, tif->tif_fieldinfo, &tif->tif_nfields, sizeof(TIFFFieldInfo *), tagNameCompare); return tif->tif_foundfield = (ret ? *ret : NULL); }
int CWatchdogMngr::LoadPidList() { CIniParser oIniParser; if ( !oIniParser.Load(m_pPidListFile) ) { throw CWatchdog::exception::exception("Unable to load pidListFile") ; } LOG("Loaded pidListFile [%s]", m_pPidListFile); char* currentPidList[WTD_PIDLIST_ENTRIES]={0,}; size_t currentPidListLen=0; char pidFile[256]; for ( unsigned pos=0 ; oIniParser.GetVar("WTD", "watch", pidFile, sizeof(pidFile), pos) && currentPidListLen < WTD_PIDLIST_ENTRIES ; ++pos ) { void*key=pidFile; void *el=lfind( &key,currentPidList,¤tPidListLen,sizeof(currentPidList[0]),qsortcmp ) ; if ( NULL == el ) { currentPidList[currentPidListLen] = strdup(pidFile); LOG( WTD"fw.cfg:[%s]", currentPidList[currentPidListLen] ); ++currentPidListLen ; } } qsort( currentPidList, currentPidListLen, sizeof(currentPidList[0]), qsortcmp ); /// Check if any pidFile from m_ppPidList is missing from currentPidList. for ( unsigned j=0; j < m_nPidListLength && m_ppPidList[j]; ++j ) { void *el=bsearch( &m_ppPidList[j], currentPidList, currentPidListLen, sizeof(currentPidList[0]), qsortcmp) ; if ( NULL == el ) { LOG( WTD "STOP watching pid[%s]", m_ppPidList[j]); systemf_to( 20, "log2flash 'WTD: STOP watching pid[%s]'&", m_ppPidList[j]); } } /// Free the old list to prepare the copy of the new one. clearPidList() ; m_nPidListLength=currentPidListLen ; m_ppPidList[m_nPidListLength] = 0; LOG( WTD"WATCH:%d modules", m_nPidListLength ); if ( !m_nPidListLength ) { return true ; } memmove(m_ppPidList, currentPidList, WTD_PIDLIST_ENTRIES*sizeof(currentPidList[0]) ); for ( unsigned j = 0; j< m_nPidListLength ; j++) { LOG( WTD "WATCH:[%s]", m_ppPidList[j]); } return true ; }
/* DESCRIPTION deletes a node as identified by hashnr/keey/keylen from the list that starts from 'head' RETURN 0 - ok 1 - not found NOTE it uses pins[0..2], on return all pins are removed. */ static int ldelete(LF_SLIST * volatile *head, CHARSET_INFO *cs, uint32 hashnr, const uchar *key, uint keylen, LF_PINS *pins) { CURSOR cursor; int res; for (;;) { if (!lfind(head, cs, hashnr, key, keylen, &cursor, pins)) { res= 1; /* not found */ break; } else { /* mark the node deleted */ if (my_atomic_casptr((void **) (char*) &(cursor.curr->link), (void **) (char*) &cursor.next, (void *)(((intptr)cursor.next) | 1))) { /* and remove it from the list */ if (my_atomic_casptr((void **)cursor.prev, (void **)(char*)&cursor.curr, cursor.next)) _lf_alloc_free(pins, cursor.curr); else { /* somebody already "helped" us and removed the node ? Let's check if we need to help that someone too! (to ensure the number of "set DELETED flag" actions is equal to the number of "remove from the list" actions) */ lfind(head, cs, hashnr, key, keylen, &cursor, pins); } res= 0; break; } } } _lf_unpin(pins, 0); _lf_unpin(pins, 1); _lf_unpin(pins, 2); return res; }
int symtab_findFun() /* util_string() holds the name of an existing */ { /* function, returns -1 if not */ size_t nmemb = gs_functions.nFunctions; void *ret = lfind(util_string(), gs_functions.info, &nmemb, sizeof(FunInfo), st_findFun); return ret == NULL ? -1 : (FunInfo *)ret - gs_functions.info; }
/* * stress_lsearch() * stress lsearch */ static int stress_lsearch(const args_t *args) { int32_t *data, *root; size_t i, max; uint64_t lsearch_size = DEFAULT_LSEARCH_SIZE; if (!get_setting("lsearch-size", &lsearch_size)) { if (g_opt_flags & OPT_FLAGS_MAXIMIZE) lsearch_size = MAX_LSEARCH_SIZE; if (g_opt_flags & OPT_FLAGS_MINIMIZE) lsearch_size = MIN_LSEARCH_SIZE; } max = (size_t)lsearch_size; if ((data = calloc(max, sizeof(*data))) == NULL) { pr_fail_dbg("malloc"); return EXIT_NO_RESOURCE; } if ((root = calloc(max, sizeof(*data))) == NULL) { free(data); pr_fail_dbg("malloc"); return EXIT_NO_RESOURCE; } do { size_t n = 0; /* Step #1, populate with data */ for (i = 0; g_keep_stressing_flag && i < max; i++) { void *ptr; data[i] = ((mwc32() & 0xfff) << 20) ^ i; ptr = lsearch(&data[i], root, &n, sizeof(*data), cmp); (void)ptr; } /* Step #2, find */ for (i = 0; g_keep_stressing_flag && i < n; i++) { int32_t *result; result = lfind(&data[i], root, &n, sizeof(*data), cmp); if (g_opt_flags & OPT_FLAGS_VERIFY) { if (result == NULL) pr_fail("%s: element %zu could not be found\n", args->name, i); else if (*result != data[i]) pr_fail("%s: element %zu found %" PRIu32 ", expecting %" PRIu32 "\n", args->name, i, *result, data[i]); } } inc_counter(args); } while (keep_stressing()); free(root); free(data); return EXIT_SUCCESS; }
/* Get the set of key mappings for a single modifier combination. Mappings are appended to "head". Returns 0 on success and -1 on failure. */ static int getkeys(name_mapping_t *keys, int max, int mod) { sequence_t *current, *new_seq; int i; for (i = 0; i < max; i++) { /* Check if the key is blocked from the command line. */ if (blocked_keys != NULL) { name_mapping_t key_mod[2] = {keys[i], modifiers[mod]}; if (lfind(&key_mod, blocked_keys, &blocked_keys_fill, sizeof(char *), (int (*)(const void *, const void *))check_key) != NULL) { printf("Skipping blocked key %s%s\n", modifiers[mod].name, keys[i].name); continue; } } printf("%s%s ", modifiers[mod].name, keys[i].name); fflush(stdout); #ifndef NO_AUTOLEARN /* When auto learning, just send the appropriate events to the terminal. */ if (option_auto_learn) { send_event(keys[i].keysym, modifiers[mod].keysym); } #endif /* Retrieve key sequence. */ new_seq = get_sequence(); if (new_seq == NULL) { printf("\n"); continue; } else if (new_seq == (void *)-1) { return -1; } printf("%s ", new_seq->seq); current = head; /* Check for duplicate sequences, and set the duplicate field if one is found. */ while (current != NULL) { if (strcmp(current->seq, new_seq->seq) == 0 && current->duplicate == NULL) { printf("(duplicate for %s%s)", current->modifiers->name, current->keynames->name); new_seq->duplicate = current; break; } else { current = current->next; } } new_seq->modifiers = modifiers + mod; new_seq->keynames = keys + i; new_seq->next = head; head = new_seq; printf("\n"); } return 0; }
/* DESCRIPTION searches for a node as identified by hashnr/keey/keylen in the list that starts from 'head' RETURN 0 - not found node - found NOTE it uses pins[0..2], on return the pin[2] keeps the node found all other pins are removed. */ static LF_SLIST *lsearch(LF_SLIST * volatile *head, CHARSET_INFO *cs, uint32 hashnr, const uchar *key, uint keylen, LF_PINS *pins) { CURSOR cursor; int res= lfind(head, cs, hashnr, key, keylen, &cursor, pins); if (res) _lf_pin(pins, 2, cursor.curr); _lf_unpin(pins, 0); _lf_unpin(pins, 1); return res ? cursor.curr : 0; }
char getFilterData(int tile, int read, int cycle, int region) { int itile, i, cycleLength=0, totalCycleLength=0; void *pitile; for (i=0; i < read; i++) cycleLength += _hdr->readLength[i]; for (i=0; i < _hdr->nreads; i++) totalCycleLength += _hdr->readLength[i]; size_t nelem = _hdr->ntiles; pitile = lfind(&tile, _hdr->tileArray, &nelem, sizeof(int), &keyComp); if (!pitile) return 0; // if tile not found in filter itile = ((int*)pitile - _hdr->tileArray); return filterData[ (itile * totalCycleLength * _hdr->nregions) + ((cycleLength + cycle) * _hdr->nregions) + region]; }
int maxk(int A[], int N) { int i, k, ind, mask, prefix; int bits[102], freq[102], len; mask = 0; len = 0; for (k = 31; k >= 0; k--) { len = 0; memset(bits, 0, sizeof(bits[0])*N); memset(freq, 0, sizeof(freq[0])*N); mask = mask | (1<<k); for (i = 0; i < N; i++) { prefix = A[i] & mask; // for this problem linear search is fine ind = lfind(prefix, bits, len); if (ind == -1) { bits[len] = prefix; freq[len]++; len++; } else { freq[ind]++; } } // second pass: find the sequence of bits whose frequency exceeds 50% for (i = 0; i < N; i++) { prefix = A[i] & mask; ind = lfind(prefix, bits, len); if (freq[ind] > N/2) break; } if (i == N) return k; } return -1; }
static int add_event(struct acrd_handle *ah, enum acrd_event_type type, struct sd_node *node, void *buf, size_t buf_len) { int idx; struct sd_node *n; uint64_t *i; struct acrd_event ev; acrd_lock(ah); ev.type = type; ev.sender = *node; ev.buf_len = buf_len; if (buf) memcpy(ev.buf, buf, buf_len); ev.nr_nodes = get_nodes(ah, ev.nodes, ev.ids); switch (type) { case EVENT_JOIN_REQUEST: ev.nodes[ev.nr_nodes] = *node; ev.ids[ev.nr_nodes] = this_id; /* must be local node */ ev.nr_nodes++; break; case EVENT_LEAVE: n = lfind(node, ev.nodes, &ev.nr_nodes, sizeof(*n), node_id_cmp); if (!n) goto out; idx = n - ev.nodes; i = ev.ids + idx; ev.nr_nodes--; memmove(n, n + 1, sizeof(*n) * (ev.nr_nodes - idx)); memmove(i, i + 1, sizeof(*i) * (ev.nr_nodes - idx)); break; case EVENT_NOTIFY: case EVENT_BLOCK: break; case EVENT_JOIN_RESPONSE: abort(); } acrd_queue_push(ah, &ev); out: acrd_unlock(ah); return 0; }
u64 dictstat_find (hashcat_ctx_t *hashcat_ctx, dictstat_t *d) { hashconfig_t *hashconfig = hashcat_ctx->hashconfig; dictstat_ctx_t *dictstat_ctx = hashcat_ctx->dictstat_ctx; if (dictstat_ctx->enabled == false) return 0; if (hashconfig->dictstat_disable == true) return 0; dictstat_t *d_cache = (dictstat_t *) lfind (d, dictstat_ctx->base, &dictstat_ctx->cnt, sizeof (dictstat_t), sort_by_dictstat); if (d_cache == NULL) return 0; return d_cache->cnt; }
char *findparam( char *name ) { struct params *found, t; found = (struct params *)lfind( name, param, ¶mcount, sizeof(struct params), paramcompar ); /* found = (struct params *)qsearch( name, param, paramcount, sizeof(struct params), paramcompar ); */ if ( found ) { memcpy( &t, found, sizeof(t) ); memcpy( found, &(param[--paramcount]), sizeof(t) ); memcpy( &(param[paramcount]), &t, sizeof(t) ); return param[paramcount].value; } else return NULL; }
void main( int argc, const char *argv[] ) { unsigned num = 5; int compare( const void *, const void * ); if( argc <= 1 ) exit( EXIT_FAILURE ); if( lfind( &argv[1], keywords, &num, sizeof(char **), compare ) == NULL ) { printf( "'%s' is not a C keyword\n", argv[1] ); exit( EXIT_FAILURE ); } else { printf( "'%s' is a C keyword\n", argv[1] ); exit( EXIT_SUCCESS ); } }
int VectorSearch(const vector *v, const void *key, VectorCompareFunction searchFn, int startIndex, bool isSorted) { assert(startIndex >= 0 && startIndex <= v->length); assert(key != NULL && searchFn != NULL); char * loc; size_t nElems = v->length - startIndex; if(isSorted == true){ loc = bsearch(key, (char*)v->elems + startIndex*v->elemSize, nElems, v->elemSize, searchFn); } else{ loc = lfind(key, (char*)v->elems + startIndex*v->elemSize, &nElems, v->elemSize, searchFn); } if(loc == NULL) return -1; return loc- (char*)v->elems; }
void * lsearch (const void *key, void *base, size_t *nmemb, size_t size, __compar_fn_t compar) { void *result; /* Try to find it. */ result = lfind (key, base, nmemb, size, compar); if (result == NULL) { /* Not available. Insert at the end. */ memcpy (base + (*nmemb) * size, key, size); ++(*nmemb); } return result; }
static void add_event(enum local_event_type type, struct sheepdog_node_list_entry *node, void *buf, size_t buf_len, void (*block_cb)(void *arg)) { int idx; struct sheepdog_node_list_entry *n; pid_t *p; struct local_event ev = { .type = type, .sender = *node, }; ev.buf_len = buf_len; if (buf) memcpy(ev.buf, buf, buf_len); ev.nr_nodes = get_nodes(ev.nodes, ev.pids); switch (type) { case EVENT_JOIN: ev.blocked = 1; ev.nodes[ev.nr_nodes] = *node; ev.pids[ev.nr_nodes] = getpid(); /* must be local node */ ev.nr_nodes++; break; case EVENT_LEAVE: n = lfind(node, ev.nodes, &ev.nr_nodes, sizeof(*n), node_cmp); if (!n) panic("internal error\n"); idx = n - ev.nodes; p = ev.pids + idx; ev.nr_nodes--; memmove(n, n + 1, sizeof(*n) * (ev.nr_nodes - idx)); memmove(p, p + 1, sizeof(*p) * (ev.nr_nodes - idx)); break; case EVENT_NOTIFY: ev.blocked = !!block_cb; ev.block_cb = block_cb; break; } shm_queue_push(&ev); shm_queue_notify(); }
int main(int argc, char *argv[]) { struct sigevent ev; timer_t tid; timer_t *tids; size_t i; ev.sigev_notify = SIGEV_SIGNAL; ev.sigev_signo = SIGALRM; #if defined DEBUG && defined TIMER_MAX printf("Max timers is %ld\n", (long) TIMER_MAX); int max = TIMER_MAX; #else int max = 256; #endif tids = (timer_t *) malloc (max * sizeof (timer_t)); if (tids == NULL) { perror("malloc failed\n"); return PTS_UNRESOLVED; } for (i=0; i<max; i++) { if (timer_create(CLOCK_REALTIME, &ev, &tid) != 0) { #ifndef TIMER_MAX if (errno == EAGAIN) break; #endif perror("timer_create() did not return success\n"); return PTS_UNRESOLVED; } tids[i] = tid; if (lfind(&tid, tids, &i, sizeof(timer_t), compare) != NULL) { printf("Duplicate tid found %ld\n", (long)tid); printf("Test FAILED\n"); return PTS_FAIL; } } printf("No duplicate tids found\n"); printf("Test PASSED\n"); return PTS_PASS; }
static void add_event(enum local_event_type type, struct sd_node *node, void *buf, size_t buf_len) { int idx; struct sd_node *n; pid_t *p; struct local_event ev = { .type = type, .sender = *node, }; ev.buf_len = buf_len; if (buf) memcpy(ev.buf, buf, buf_len); ev.nr_nodes = get_nodes(ev.nodes, ev.pids); switch (type) { case EVENT_JOIN_REQUEST: ev.nodes[ev.nr_nodes] = *node; ev.pids[ev.nr_nodes] = getpid(); /* must be local node */ ev.nr_nodes++; break; case EVENT_LEAVE: n = lfind(node, ev.nodes, &ev.nr_nodes, sizeof(*n), node_id_cmp); if (!n) panic("internal error\n"); idx = n - ev.nodes; p = ev.pids + idx; ev.nr_nodes--; memmove(n, n + 1, sizeof(*n) * (ev.nr_nodes - idx)); memmove(p, p + 1, sizeof(*p) * (ev.nr_nodes - idx)); break; case EVENT_NOTIFY: case EVENT_BLOCK: break; case EVENT_JOIN_RESPONSE: abort(); } shm_queue_push(&ev); shm_queue_notify(); }
char * strtok_r(char * str, const char * delim, char ** saveptr) { char * token; if (str == NULL) { str = *saveptr; } str += strspn(str, delim); if (*str == '\0') { *saveptr = str; return 0; } token = str; str = strpbrk(token, delim); if (str == NULL) { *saveptr = (char *)lfind(token, '\0'); } else { *str = '\0'; *saveptr = str + 1; } return token; }