int cal_alloc_chunk(_sbinfo sbinfo, vnode * v, unsigned long long blocks) { int nr, i = 0, sum = 0; nr = find_first_zero(sbinfo->bitmap, MaxBlocks * 8); while (sum < blocks && i < MaxchunkCount) { v->block[i][0] = nr; sum++; nr++; while (sum < blocks && !bit(sbinfo->bitmap, nr)) { nr++; sum++; } v->block[i][1] = nr; if (i == 0 && sum < 2 && sum < blocks) sum = 0; else i++; while (bit(sbinfo->bitmap, nr)) nr++; } if (i == MaxchunkCount || sum < blocks) { ErrorFlag = TOO_FLAGS; //_Debug("too frags\n",__LINE__,__FILE__); for (i = 0; i < MaxchunkCount; i++) v->block[i][0] = CHUNKNULL; return -1; } while (i < MaxchunkCount) { v->block[i][0] = CHUNKNULL; i++; } return 0; }
/* * get the specified goal block * if alread alloced find it by looking forward */ int ext2_grab_block(char *bitmap, unsigned int goal) { if ( !ext2_test_bit(bitmap, goal) ) goto got_block; /* else looking forward */ goal = find_first_zero (bitmap, goal + 1, EXT2_BLOCKS_PER_GROUP); got_block: return goal; }
DEF_TEST(Paint_cmap, reporter) { // need to implement charsToGlyphs on other backends (e.g. linux, win) // before we can run this tests everywhere return; static const int NGLYPHS = 64; SkUnichar src[NGLYPHS]; SkUnichar dst[NGLYPHS]; // used for utf8, utf16, utf32 storage static const struct { size_t (*fSeedTextProc)(const SkUnichar[], void* dst, int count); SkPaint::TextEncoding fEncoding; } gRec[] = { { uni_to_utf8, SkPaint::kUTF8_TextEncoding }, { uni_to_utf16, SkPaint::kUTF16_TextEncoding }, { uni_to_utf32, SkPaint::kUTF32_TextEncoding }, }; SkRandom rand; SkPaint paint; paint.setTypeface(SkTypeface::RefDefault())->unref(); SkTypeface* face = paint.getTypeface(); for (int i = 0; i < 1000; ++i) { // generate some random text for (int j = 0; j < NGLYPHS; ++j) { src[j] = ' ' + j; } // inject some random chars, to sometimes abort early src[rand.nextU() & 63] = rand.nextU() & 0xFFF; for (size_t k = 0; k < SK_ARRAY_COUNT(gRec); ++k) { paint.setTextEncoding(gRec[k].fEncoding); size_t len = gRec[k].fSeedTextProc(src, dst, NGLYPHS); uint16_t glyphs0[NGLYPHS], glyphs1[NGLYPHS]; bool contains = paint.containsText(dst, len); int nglyphs = paint.textToGlyphs(dst, len, glyphs0); int first = face->charsToGlyphs(dst, paint2encoding(paint), glyphs1, NGLYPHS); int index = find_first_zero(glyphs1, NGLYPHS); REPORTER_ASSERT(reporter, NGLYPHS == nglyphs); REPORTER_ASSERT(reporter, index == first); REPORTER_ASSERT(reporter, 0 == memcmp(glyphs0, glyphs1, NGLYPHS * sizeof(uint16_t))); if (contains) { REPORTER_ASSERT(reporter, NGLYPHS == first); } else { REPORTER_ASSERT(reporter, NGLYPHS > first); } } } }
_vnodeInfo alloc_vi(_sbinfo sbinfo) { int nr; while (spin_trywrlock(sbinfo->_bh->spin)); if ((nr = find_first_zero(sbinfo->_bh->map, MaxBufBitmapLen * 8)) == MaxBufBitmapLen * 8) { ErrorFlag = MAX_RDWR_COUNT; spin_rwunlock(sbinfo->_bh->spin); return NULL; } spin_rwunlock(sbinfo->_bh->spin); return (_vnodeInfo) (sbinfo->_bf + sizeof(struct vnodeInfo) * nr); }
struct m_inode * new_inode(int dev) { struct m_inode * inode; struct super_block * sb; struct buffer_head * bh; int i,j; inode = alloc_inode(); if (!inode) return NULL; if (!(sb = get_super(dev))) panic("new_inode with unknown device"); j = 8192; for (i=0 ; i<8 ; i++) { if ((bh = sb->s_imap[i])) { if ((j=find_first_zero(bh->b_data))<8192) break; } } if (!bh || j >= 8192 || j+i*8192 > sb->s_ninodes) { panic("not implemented!\n"); //iput(inode); return NULL; } if (test_and_set_bit(j,bh->b_data)) panic("new_inode: bit already set"); bh->b_dirt = 1; inode->i_count=1; inode->i_nlinks=1; inode->i_dev=dev; // inode->i_uid=current->euid; // inode->i_gid=current->egid; inode->i_dirt=1; inode->i_num = j + i*8192; // inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; return inode; }
int new_block(int dev) { struct buffer_head * bh; struct super_block * sb; int i,j; if (!(sb = get_super(dev))) panic("trying to get new block from nonexistant device"); j = 8192; for (i = 0 ; i < 8 ; i++) { if ((bh = sb->s_zmap[i])) { if ((j = find_first_zero(bh->b_data)) < 8192) break; } } if (i>=8 || !bh || j>=8192) return 0; if (test_and_set_bit(j,bh->b_data)) panic("new_block: bit already set"); bh->b_dirt = 1; j += i*8192 + sb->s_firstdatazone-1; if (j >= sb->s_nzones) return 0; if (!(bh= buffer_get(dev,j))) panic("new_block: cannot get block"); if (bh->b_count != 1) panic("new block: count is != 1"); //clear_block(bh->b_data); bh->b_uptodate = 1; bh->b_dirt = 1; //brelse(bh); return j; }
char * extend_buf(vnode * v, _sbinfo sbinfo, char mode) { int nr; uint8_t key = 0; _vnodeInfo vi, p; while (spin_trywrlock(sbinfo->_bh->spin)); if ((nr = find_first_zero(sbinfo->_bh->map, MaxBufBitmapLen * 8)) == MaxBufBitmapLen * 8) { ErrorFlag = MAX_RDWR_COUNT; spin_rwunlock(sbinfo->_bh->spin); return NULL; } setbit_(sbinfo->_bh->map, nr); spin_rwunlock(sbinfo->_bh->spin); // if(!v->_bf) spin_rwinit(v->spin); while (spin_wrlock(v->spin)); vi = v->_bf; if (vi == NULL || vi->key > key) { //分配key是有序的。 p = v->_bf; v->_bf = (_vnodeInfo) (sbinfo->_bf + sizeof(struct vnodeInfo) * nr); v->_bf->next = p; vi = v->_bf; p = NULL; } else { while (vi) { key++; if (!vi->next || vi->next->key > key) { p = vi->next; vi->next = (_vnodeInfo) (sbinfo->_bf + sizeof(struct vnodeInfo) * nr); vi->next->next = p; p = vi; vi = vi->next; break; } vi = vi->next; } } pthread_mutex_lock(&sbinfo->mutex); vi->fd = open(sbinfo->volName, O_RDWR); pthread_mutex_unlock(&sbinfo->mutex); pthread_mutex_init(&vi->mutex, NULL); if (vi->fd <= 2) { pthread_mutex_lock(&sbinfo->mutex); vi->fd = open(sbinfo->volName, O_RDWR); pthread_mutex_unlock(&sbinfo->mutex); } spin_rwunlock(v->spin); // } /* else { ErrorFlag=LOCK_ERR; return NULL; } */ vi->key = key; vi->ID = ((char *)v - (char *)sbinfo->vnodeTable) / sizeof(vnode); if (mode == ReadRECORD) { vi->status = ReadRECORD; vi->nextTimeAddr = 0; vi->count = TimeNULL; //vi->fd=open(sbinfo->volName,O_RDWR); // if(vi->fd<=2){ // vi->fd=open(sbinfo->volName,O_RDWR); // } } else { vi->count = 0; vi->status = WriteRECORD; vi->is_starttime = NOTSET; // vi->fd=open(sbinfo->volName,O_RDWR); // if(vi->fd<=2){ //close(vi->fd); // vi->fd=open(sbinfo->volName,O_RDWR); // } } ////////////////////////////////////////////// if (vi->fd < 0) { while (spin_trywrlock(sbinfo->_bh->spin)); clrbit_(sbinfo->_bh->map, nr); spin_rwunlock(sbinfo->_bh->spin); while (spin_trywrlock(v->spin)); if (p) p->next = p->next->next; else v->_bf = vi->next; spin_rwunlock(v->spin); pthread_mutex_destroy(&vi->mutex); ErrorFlag = ALLOC_FD_ERR; return NULL; } // v->status++; return (char *)vi; }