void ObjectPool::releaseObjectAtPos(PoolItem *item, size_t pos) { if (!isused(pos)) printf ("error: release of an unused item %p [location: %ld]\n", item, pos); else setused(pos, 0); }
int get(int key) { mymap::iterator it = m.find(key); if (it == m.end()) { return -1; } setused(it->second); return it->second->value; }
/** releases all items in pool */ void ObjectPool::destroyAllPoolItems() { for (int i = 0; i < MAX_OBJECT_POOL_ITEMS; ++i) { if (_items[i]) delete _items[i]; setused(i,0); } }
bool ObjectPool::obtainPoolItemAndRelease() { for (int i = 0; i < _max_pool_count; ++i) { if (grabunused(i)) { PoolItem *temp = getObjectAtPos(i); setused(i,0); return true; } } return false; }
void insertelement(int key, int value) { mymap::iterator it = m.find(key); if (it == m.end()) { Node* n = new Node(key, value); insertathead(n); m.insert(std::pair<int, Node*>(key, n)); if (m.size() > maxsize) { invalidate(); } } else { it->second->value = value; setused(it->second); } }
// undo effect of last deepening operation static void state_restore(void) {int i; for(i=0;i<nw;i++) { if(scommit[sdep][i]!=-1) { // word to uncommit? // printf("uncommitting word %d (%s)\n",i,lts[scommit[sdep][i]].s);fflush(stdout); setused(scommit[sdep][i],0); words[i].commit=-1; scommit[sdep][i]=-1; } if(sflistlen[sdep][i]!=-1&&words[i].flist!=0) { // word feasible list to free? free(words[i].flist); ct_free++; words[i].flist=sflist[sdep][i]; words[i].flistlen=sflistlen[sdep][i]; } } for(i=0;i<ne;i++) entries[i].flbm=sentryfl[sdep][i]; }
// check updated entries and rebuild feasible word lists // returns -2 for infeasible, -1 for out of memory, 0 if no feasible word lists affected, >=1 otherwise static int settleents(void) {struct entry*e;struct word*w;int f,i,l; int*p; DEB1 printf("settleents() sdep=%d\n",sdep); f=0; for(i=0;i<nc;i++) cells[i].upd=0; for(i=0;i<nc;i++) cells[i].upd|=cells[i].e->upd; // generate cell updated flags from entry updated flags for(i=0;i<nc;i++) if(cells[i].upd) { e=cells[i].e; w=cells[i].w; p=w->flist; l=w->flistlen; if(sflistlen[sdep][w-words]==-1) { // then we mustn't trash words[].flist sflist [sdep][w-words]=w->flist; sflistlen[sdep][w-words]=w->flistlen; w->flist=(int*)malloc(l*sizeof(int)); // new list can be at most as long as old one if(w->flist==NULL) return -1; // out of memory ct_malloc++; } w->flistlen=listisect(w->flist,p,l,cells[i].wp,e->flbm); // generate new feasible word list if(w->flistlen!=l) {w->upd=1;f++;} // word list has changed: feasible letter lists will need updating if(w->flistlen==0&&!w->fe) return -2; // no options left, not a fully-entered word if(w->flistlen==1&&w->commit==-1) { // down to a single word? if(afunique&&isused(w->flist[0])) { // in no-duplicates mode and only answer left is already used? (could check all on list) w->flistlen=0; // abort return -2; } else { // otherwise, if down to one word, commit it // printf("committing word %d (%s)\n",w,lts[w->flist[0]].s);fflush(stdout); setused(w->flist[0],1); // flag as used w->commit=w->flist[0]; scommit[sdep][w-words]=w->flist[0]; } } } for(i=0;i<ne;i++) entries[i].upd=0; // all entry update effects now propagated into word updates // printf("settleents returns %d\n",f);fflush(stdout); return f; }
/** releases all items in pool */ void ObjectPool::releaseAllPoolItems() { for (int i = 0; i < _max_pool_count; ++i) setused(i, 0); }
buffer *getblk(int fs, ino_t inode, int block) { debug(1 + BUFFER_DL, "getblk(fs:%d, inode:%d, block:%d) {\n", fs, (int)inode, block); // wait for semaphore to start while (!semWait(semid)) { // skip signal interruptions if (errno != EINTR) { perror("getblk, semWait"); debug(2 + BUFFER_DL, "ERROR en getblk, semWait."); exit(1); } } // search for specified buffer or return a new one buffer *buff = NULL; while (buff == NULL) { // search hash list for block int pos = bc->header.hash[block % MAXHASH]; buffer *hl_buff = (pos > -1) ? &(bc->buffers[pos]) : NULL; int times = 0; while(pos > -1 && (hl_buff->inode != inode || hl_buff->block != block)) { times ++; if (times == MAXBUFF) { printf("[%d] times: %d ... \n", getpid(), times); int end = pos; do { bdebug(0, &(bc->buffers[pos])); pos = bc->buffers[pos].hashnext; } while (end != pos); exit(0); } pos = hl_buff->hashnext; hl_buff = (pos > -1) ? &(bc->buffers[pos]) : NULL; } // return the buffer if already loaded if (hl_buff != NULL && hl_buff->inode == inode && hl_buff->block == block) { // sleep until the targeted buffer becomes free if (BS_CMP(hl_buff->status, BS_LOCKED)) { bwait(hl_buff); continue; } // take ownership off buffer hl_buff->fd = getfd(inode); hl_buff->pid = getpid(); // lock the found buffer BS_SET(hl_buff->status, BS_LOCKED); // will return found buffer buff = hl_buff; // allocate the block in a new buffer } else { buffer *new_buff = getfree(); // sleep until any buffer becomes free if (new_buff == NULL) { bwait(NULL); continue; } // asynchronous write buffer to disk while (BS_CMP(new_buff->status, BS_DELAYED)) { basyncwrite(new_buff); // wait for free buffer to be written usleep(1000); } // set new buffer properties and proper hash list drophash(new_buff); new_buff->valid = 0; new_buff->inode = inode; new_buff->block = block; new_buff->fd = getfd(inode); new_buff->pid = getpid(); rehash(new_buff); // lock the new buffer and mark it as not valid BS_SET(new_buff->status, BS_LOCKED); BS_CLR(new_buff->status, BS_VALID); // will return the new buffer buff = new_buff; } } // extract the buffer from free list setused(buff); // free the semaphore if (!semSignal(semid)) { perror("getblk, semSignal"); debug(2 + BUFFER_DL, "ERROR en getblk, semSignal."); exit(1); } bdebug(2 + BUFFER_DL, buff); debug(1 + BUFFER_DL, "} -> %p\n", buff); return buff; }