static int add_new_word(index_struct *idx, char *word) { int word_len = strlen(word); int w; int left; char *word_pos; word_struct* ws; if(idx->next_word == idx->dict_len){ /* must reallocate the dictionnary */ int old_size = idx->dict_len; int new_size = old_size * 2; word_struct** new_dict = index_alloc(sizeof(word_struct*) * new_size); memcpy(new_dict, idx->dict, sizeof(word_struct*) * old_size); index_free(idx->dict); idx->dict = new_dict; idx->dict_len = new_size; } w = idx->next_word; idx->next_word++; ws = index_alloc(sizeof(word_struct) + word_len + 1); idx->dict[w] = ws; strncpy(ws->word_string, word, word_len); ws->word_string[word_len] = 0; return w; }
index_struct *create_index(int range, converter f) { index_struct *idx = index_alloc(sizeof(index_struct)); idx->dict = index_alloc(sizeof(word_struct*) * DICT_INITIAL_SIZE); idx->next_word = 0; idx->dict_len = DICT_INITIAL_SIZE; idx->tree = NULL; idx->range = range; idx->f = f; return idx; }
struct usb_dev *usb_dev_alloc(struct usb_hcd *hcd) { struct usb_dev *dev = pool_alloc(&usb_devs); size_t idx; if (!dev) { return NULL; } idx = index_alloc(&hcd->enumerator, INDEX_MIN); assert(idx != INDEX_NONE); assert(idx < USB_HC_MAX_DEV); memset(dev, 0, sizeof(struct usb_dev)); dev->hcd = hcd; dev->idx = idx; dev->bus_idx = 0; if (!usb_endp_alloc(dev, &usb_desc_endp_control_default)) { usb_dev_free(dev); return NULL; } dlist_head_init(&dev->dev_link); return dev; }
static array_struct *alloc_array(index_struct *idx) { array_struct *array= index_alloc(sizeof(tree_struct) + 2 * MAX_LINEAR_SEARCH * sizeof(suffix_struct)); array->array_type = WORD_ARRAY; array->array_word = -1; array->array_nprefixes = 0; array->array_nsuffixes = 0; return array; }
static int uart_fill_name(struct uart *dev) { dev->idx = index_alloc(&serial_indexator, INDEX_MIN); if(dev->idx < 0) { return -EBUSY; } snprintf(dev->dev_name, UART_NAME_MAXLEN, "ttyS%d", dev->idx); return 0; }
void alloc_index_spec(index_spec_t* s) { int i; s->index = index_alloc(0,s->ndims); for (i = 0; i < s->ndims; ++i) { if (s->dim_size[i] > 0) { s->index[i] = size_alloc(0,s->dim_size[i]); } else { s->index[i] = 0; } } }
mdb_handle_t mdb_handle_add(mdb_handle_map_t *hmap, void *data) { int index; MDB_CHECKARG(hmap && data, MDB_HANDLE_INVALID); if ((index = freemap_alloc(&hmap->freemap)) == HANDLE_INDEX_INVALID) { return MDB_HANDLE_INVALID; } return index_alloc(&hmap->indextbl, index, data); }
void create_index_spec(index_spec_t* dest, int nridx, ...) { int i; va_list ap; va_start(ap,nridx); dest->ndims = nridx; dest->dim_size = size_alloc(0,nridx); dest->index = index_alloc(0,nridx); dest->index_type = char_alloc(0,nridx); for (i = 0; i < nridx; ++i) { dest->dim_size[i] = va_arg(ap, _index_t); dest->index[i] = va_arg(ap, _index_t*); dest->index_type[i] = (char) va_arg(ap,_index_t); /* char is cast to int by va_arg.*/ } va_end(ap); }
static tree_struct* find_in_tree(index_struct *idx, char *suffix, tree_struct *tree) { if(tree == NULL) return NULL; if(suffix[0] == 0) return tree; if(tree->tree_type == WORD_TREE){ tree_struct * new_tree = tree->tree_subtrees[ (int) suffix[0]]; return find_in_tree(idx, suffix+1, new_tree); } else { array_struct * array = (array_struct*) tree; int nprefixes = array->array_nprefixes; int nsuffixes = array->array_nsuffixes; int suffix_len = strlen(suffix); int i; hits_struct *all_hits; nhits = 0; for(i=0; i<nprefixes; i++){ char *word = array->array_suffixes[i].suffix_word; if(!strncmp(suffix, word, suffix_len)){ add_hit(array->array_suffixes[i].suffix_num); } } for(i=0; i<nsuffixes; i++){ char *word = array->array_suffixes[MAX_LINEAR_SEARCH+i].suffix_word; if(!strncmp(suffix, word, suffix_len)){ add_hit(array->array_suffixes[MAX_LINEAR_SEARCH+i].suffix_num); } } if(nhits == 0) return NULL; all_hits = index_alloc(sizeof(hits_struct) + sizeof(int) * nhits); all_hits->hits_type = WORD_HITS; all_hits->hits_nbr = nhits; memcpy(all_hits->hits_words, hits, nhits * sizeof(int)); return (tree_struct*) all_hits; } }
static tmpfs_file_info_t *tmpfs_create_file(struct nas *nas) { tmpfs_file_info_t *fi; size_t fi_index; fi = pool_alloc(&tmpfs_file_pool); if (!fi) { return NULL; } fi_index = index_alloc(&tmpfs_file_idx, INDEX_MIN); if (fi_index == INDEX_NONE) { pool_free(&tmpfs_file_pool, fi); return NULL; } fi->index = fi_index; nas->fi->ni.size = 0; return fi; }
static int add_prefix_in_tree(index_struct *idx, char *suffix, tree_struct *tree, tree_struct **tree_ptr) { if(tree == NULL){ tree = (tree_struct*) alloc_array(idx); *tree_ptr = tree; } if(suffix[0] == 0) { if(tree->tree_word == (-1)) tree->tree_word = add_stemmed_word(idx); return tree->tree_word; } if(tree->tree_type == WORD_TREE){ tree_struct * new_tree = tree->tree_subtrees[ (int) suffix[0]]; return add_prefix_in_tree(idx, suffix+1, new_tree, & (tree->tree_subtrees[(int) suffix[0]])); } else { array_struct * array = (array_struct*) tree; int nprefixes = array->array_nprefixes; int suffix_len = strlen(suffix); int i; for(i=0; i<nprefixes; i++){ char *word = array->array_suffixes[i].suffix_word; if(!strcmp(suffix, word)){ return array->array_suffixes[i].suffix_num; } } /* not found */ if(nprefixes == MAX_LINEAR_SEARCH){ int size = idx->range * sizeof(tree_struct*); int nsuffixes = array->array_nsuffixes; tree_struct *new_tree = index_alloc(sizeof(tree_struct) + size); bzero(new_tree->tree_subtrees, size); new_tree->tree_type = WORD_TREE; new_tree->tree_word = -1; *tree_ptr = new_tree; for(i=0; i<nprefixes; i++){ char *word = array->array_suffixes[i].suffix_word; int num = array->array_suffixes[i].suffix_num; tree_struct *next_tree = new_tree->tree_subtrees[(int)word[0]]; array_struct *nr = (array_struct*) next_tree; if(nr == NULL){ nr = alloc_array(idx); new_tree->tree_subtrees[(int) word[0]] = (tree_struct*) nr; } nr->array_suffixes[nr->array_nprefixes].suffix_word = word+1; nr->array_suffixes[nr->array_nprefixes].suffix_num = num; nr->array_nprefixes++; } for(i=0; i<nsuffixes; i++){ char *word = array->array_suffixes[MAX_LINEAR_SEARCH+i].suffix_word; int num = array->array_suffixes[i].suffix_num; tree_struct *next_tree = new_tree->tree_subtrees[(int)word[0]]; array_struct *nr = (array_struct*) next_tree; if(nr == NULL){ nr = alloc_array(idx); new_tree->tree_subtrees[(int) word[0]] = (tree_struct*) nr; } nr->array_suffixes[MAX_LINEAR_SEARCH+nr->array_nsuffixes].suffix_word = word+1; nr->array_suffixes[MAX_LINEAR_SEARCH+nr->array_nsuffixes].suffix_num = num; nr->array_nsuffixes++; } index_free(array); return add_prefix_in_tree(idx, suffix+1, new_tree->tree_subtrees[(int) suffix[0]], & new_tree->tree_subtrees[(int) suffix[0]]); } else { int w = add_stemmed_word(idx); array->array_nprefixes = nprefixes+1; array->array_suffixes[nprefixes].suffix_num = w; array->array_suffixes[nprefixes].suffix_word = idx->dict[w]->word_string + word_len - suffix_len; return w; } } }
void *salloc(int id, int type) { if(ammount[type] == max_ammount[type]) return NULL; if(type == SALLOC_TSK || type == SALLOC_THR || type == SALLOC_SMO) { switch(type) { case SALLOC_TSK: if(TST_PTR(id,tsk)) return NULL; break; case SALLOC_THR: if(TST_PTR(id,thr)) return NULL; break; case SALLOC_SMO: if(TST_PTR(id,smo)) return NULL; break; default: break; } // allocate index first, for csalloc is not always atomic if(!index_alloc(id, ALLOC2IDX(type))) return NULL; ammount[type]++; // increment now, for atomicity might be broken on csalloc void * ptr = csalloc(SALLOC2CONT_TYPE(type)); if(ptr == NULL) { ammount[type]--; index_free(id, ALLOC2IDX(type)); return NULL; } switch(type) { case SALLOC_TSK: SET_PTR(id,tsk,ptr); break; case SALLOC_THR: SET_PTR(id,thr,ptr); break; case SALLOC_SMO: SET_PTR(id,smo,ptr); break; default: break; } return ptr; } else { // messages and ports wont have an ID ammount[type]++; // increment now, for atomicity might be broken on csalloc void *ptr = csalloc(SALLOC2CONT_TYPE(type)); if(ptr == NULL) ammount[type]--; return ptr; } }