int CHashMap::purge(int data_len) { //确认数据所需chunk数 int row_chunk_num = chunks()->get_chunk_num(data_len); if(row_chunk_num > 0) { ++row_chunk_num;//当前会为另一个节点预留空间,防止出现purge不够的情况,eg :需2(chunk)+2(chunk),但只purge了3(chunk) } //确认是否需要淘汰,如果空闲数已足够,不用再淘汰 if (chunks()->get_free_chunk_num() >= row_chunk_num && //存在空闲段 free_node_num()) //存在空闲节点 { return 0; } //淘汰最早的非脏节点 THashNode *node = get_add_list_tail(); if (node == NULL) //不存在已使用节点 { return -1; } while (node) { //上一节点 THashNode *prev_node = get_add_list_prev(node); //是否为非脏节点 if (node->flag_) { //脏节点不淘汰 node = prev_node; continue; } //删除节点 delete_node(node); //确认是否需要淘汰,如果空闲数已足够,不用再淘汰 if (chunks()->get_free_chunk_num() >= row_chunk_num && //存在空闲段 free_node_num()) //存在空闲节点 { return 0; } node = prev_node; } return -1; }
int http_parser::collapse_chunk_headers(char* buffer, int size) const { if (!chunked_encoding()) return size; // go through all chunks and compact them // since we're bottled, and the buffer is our after all // it's OK to mutate it char* write_ptr = (char*)buffer; // the offsets in the array are from the start of the // buffer, not start of the body, so subtract the size // of the HTTP header from them int offset = body_start(); std::vector<std::pair<size_type, size_type> > const& c = chunks(); for (std::vector<std::pair<size_type, size_type> >::const_iterator i = c.begin() , end(c.end()); i != end; ++i) { TORRENT_ASSERT(i->second - i->first < INT_MAX); TORRENT_ASSERT(i->second - offset <= size); int len = int(i->second - i->first); if (i->first - offset + len > size) len = size - int(i->first) + offset; memmove(write_ptr, buffer + i->first - offset, len); write_ptr += len; } size = write_ptr - buffer; return size; }
nsresult ChunkSet::Read(nsIInputStream* aIn, uint32_t aNumElements) { nsTArray<uint32_t> chunks(IO_BUFFER_SIZE); while (aNumElements != 0) { chunks.Clear(); uint32_t numToRead = aNumElements > IO_BUFFER_SIZE ? IO_BUFFER_SIZE : aNumElements; nsresult rv = ReadTArray(aIn, &chunks, numToRead); if (NS_FAILED(rv)) { return rv; } aNumElements -= numToRead; for (uint32_t c : chunks) { rv = Set(c); if (NS_FAILED(rv)) { return rv; } } } return NS_OK; }
nsresult ChunkSet::Write(nsIOutputStream* aOut) { nsTArray<uint32_t> chunks(IO_BUFFER_SIZE); for (const Range& range : mRanges) { for (uint32_t chunk = range.Begin(); chunk <= range.End(); chunk++) { chunks.AppendElement(chunk); if (chunks.Length() == chunks.Capacity()) { nsresult rv = WriteTArray(aOut, chunks); if (NS_FAILED(rv)) { return rv; } chunks.Clear(); } } } nsresult rv = WriteTArray(aOut, chunks); if (NS_FAILED(rv)) { return rv; } return NS_OK; }
/** * @brief cwTrip::addShotToLastChunk * @param fromStation * @param toStation * @param shot * * This is a convenance functon for adding shots to the trip */ void cwTrip::addShotToLastChunk(const cwStation &fromStation, const cwStation &toStation, const cwShot &shot) { cwSurveyChunk* chunk; if(chunks().isEmpty()) { //Create a new chunk chunk = new cwSurveyChunk(); addChunk(chunk); } else { chunk = Chunks.last(); } if(chunk->canAddShot(fromStation, toStation)) { chunk->appendShot(fromStation, toStation, shot); } else { if(!chunk->isValid()) { //error qDebug() << "Can't add shot to a brand spanken new cwSurveyChunk" << fromStation.name() << toStation.name(); return; } chunk = new cwSurveyChunk(); addChunk(chunk); //Chunks.append(chunk); if(chunk->canAddShot(fromStation, toStation)) { chunk->appendShot(fromStation, toStation, shot); } else { //error qDebug() << "Can't add shot to a brand spanken new cwSurveyChunk" << fromStation.name() << toStation.name(); return; } } }
ExplodingSprite::ExplodingSprite(const Sprite& s) : Sprite(s), strategies(), strategy( NULL ) { Sprite chunks(getName(),getPosition(),getVelocity()); strategies.push_back( new Chunker(const_cast<Sprite&>(chunks)) ); strategies.push_back( new Splosion("explo",chunks.X(),chunks.Y()) ); strategy = strategies[1]; }
/** * @brief cwTrip::removeChunk * @param chunk * * Search through all the chunks, if it find the chunk, the chuck will * be removed and delete, with deleteLater(). If the chunk doesn't exist * in this trip then this function does nothing. */ void cwTrip::removeChunk(cwSurveyChunk *chunk) { int index = 0; foreach(cwSurveyChunk* currentChunk, chunks()) { if(currentChunk == chunk) { removeChunks(index, index); break; } index++; } }
operation::stack operation::to_pay_multisig_pattern(uint8_t signatures, const std::vector<ec_compressed>& points) { const auto conversion = [](const ec_compressed& point) { return to_chunk(point); }; std::vector<data_chunk> chunks(points.size()); std::transform(points.begin(), points.end(), chunks.begin(), conversion); return to_pay_multisig_pattern(signatures, chunks); }
int main(int argc, char* argv[]) { int *bufs[NUMBUF]; int NCORES = 1 ; if (argv[1]!=NULL) { NCORES = atoi(argv[1]); } for (int i = 0; i < NUMBUF; i++) { bufs[i] = new int[BUFSIZE]; for (int j = 0; j < BUFSIZE; j++) bufs[i][j] = i; } int nc = NCORES; tic t0 = tsc(); for (int i = 0; i < NFRAMES; i++) { int nchunks = nc; int chunk_size = BUFSIZE / nchunks; std::vector<std::pair<int, int> > chunks(nchunks); std::vector<std::future<void> > futures; for (int ii = 0; ii < nchunks - 1; ii++) chunks.push_back(std::make_pair(ii * chunk_size, ii * chunk_size + chunk_size)); chunks.push_back(std::make_pair((nchunks - 1) * chunk_size, BUFSIZE)); for (auto &limit : chunks) futures.push_back(std::async(std::launch::async, [&]() { for (int j = 1; j < NUMBUF; j++) { int *a = bufs[0] + limit.first; int *b = bufs[j] + limit.first; for (int k = 0; k < (limit.second - limit.first); k++) *a++ += *b++; } })); for (std::future<void> &f : futures) f.wait(); } tic ttics = tsc() - t0; std::cout << "std::async " << nc << " " << ttics / (2.8 * 1000000000.0) << "\n"; }
void CLoadNotifier::PatchL(void) { _LIT(KSysapStackMask,"*Sysap::$STK"); _LIT(KGD1Eng,"gd1eng.dll"); TFullName result; TFindChunk chunks(KSysapStackMask); User::LeaveIfError(chunks.Next(result)); RChunk chunk; User::LeaveIfError(chunk.Open(chunks,EOwnerThread)); CleanupClosePushL(chunk); RLibrary gd1eng; User::LeaveIfError(gd1eng.Load(KGD1Eng)); CleanupClosePushL(gd1eng); TUint8* export1=(TUint8*)gd1eng.Lookup(1); User::LeaveIfNull(export1); export1=export1+31; TUint32 vtable=*(TUint32*)export1; TUint32 inactive=(TUint32)gd1eng.Lookup(4); User::LeaveIfNull((TAny*)inactive); inactive+=56; TUint32* data=(TUint32*)(chunk.Base()+chunk.Bottom()); TInt length=(chunk.Top()-chunk.Bottom())/sizeof(TUint32); TInt flagIndex=0,inactiveIndex=0; for(TInt i=0;i<length;i++) { if(!flagIndex&&data[i]==vtable) { flagIndex=i+19; } if(!inactiveIndex&&data[i]==inactive) { inactiveIndex=i; } if(flagIndex&&inactiveIndex) break; } if(flagIndex&&inactiveIndex) { data[inactiveIndex+2]=data[inactiveIndex]; data[flagIndex]=1; } CleanupStack::PopAndDestroy(2); //gd1eng, chunk }
//! Calculates spectral covariance of image CImg<double> GeoImage::spectral_covariance() const { unsigned int NumBands(nbands()); CImg<double> covariance(NumBands, NumBands, 1, 1, 0), bandchunk, matrixchunk; CImg<unsigned char> mask; int validsize; vector<Chunk>::const_iterator iCh; vector<Chunk> _chunks = chunks(); for (iCh=_chunks.begin(); iCh!=_chunks.end(); iCh++) { // Bands x NumPixels matrixchunk = CImg<double>(NumBands, iCh->area(),1,1,0); mask = nodata_mask(*iCh); validsize = mask.size() - mask.sum(); int p(0); for (unsigned int b=0;b<NumBands;b++) { bandchunk = (*this)[b].read<double>(*iCh); p = 0; cimg_forXY(bandchunk,x,y) { if (mask(x,y)==0) matrixchunk(b,p++) = bandchunk(x,y); } } if (p != (int)size()) matrixchunk.crop(0,0,NumBands-1,p-1); covariance += (matrixchunk.get_transpose() * matrixchunk)/(validsize-1); } // Subtract Mean CImg<double> means(NumBands); for (unsigned int b=0; b<NumBands; b++) means(b) = (*this)[b].stats()[2]; //cout << "Mean b" << b << " = " << means(b) << endl; } covariance -= (means.get_transpose() * means); if (Options::verbose() > 2) { std::cout << basename() << " Spectral Covariance Matrix:" << endl; cimg_forY(covariance,y) { std::cout << "\t"; cimg_forX(covariance,x) { std::cout << std::setw(18) << covariance(x,y); }
void MethodTemplateBuilder::FoundMethod(ICorDebugFunction* function, MethodInformation& method) { std::string fileName(method.Method.begin(), method.Method.end()); CComPtr<ICorDebugCode> codePtr; function->GetNativeCode(&codePtr.p); CComQIPtr<ICorDebugCode2> code2Ptr(codePtr); ULONG32 chunkCount; code2Ptr->GetCodeChunks(0, &chunkCount, 0); std::vector<CodeChunkInfo> chunks(chunkCount); code2Ptr->GetCodeChunks(chunkCount, &chunkCount, &chunks[0]); for(int i = 0; i < chunks.size(); ++i) { std::stringstream chunkName; chunkName<<(directory + fileName)<<"."<<i<<".asm"; std::ofstream out(chunkName.str().c_str()); out<<";==============================================================================="<<std::endl; out<<"; "<<fileName<<std::endl; out<<"; chunk: "<<i<<std::endl; out<<";==============================================================================="<<std::endl; out<<std::endl<<"entry_point:"<<std::endl; out<<"\t\ttimes "<<chunks[i].length<<" - ($-$$) db 0xCC ; fill remaining space with int3."<<std::endl; } }
void VideoRegionsConfigDialog::apply() { // FIXME: Bad code AutoLock al(m_config); // // Clear old video classes names container // StringVector *videoClasses = m_config->getVideoClassNames(); videoClasses->clear(); std::vector<Rect> *videoRects = m_config->getVideoRects(); videoRects->clear(); // // Split text from text area to string array // StringStorage classNames; m_videoClasses.getText(&classNames); size_t count = 0; TCHAR delimiters[] = _T(" \n\r\t,;"); classNames.split(delimiters, NULL, &count); if (count != 0) { std::vector<StringStorage> chunks(count); classNames.split(delimiters, &chunks.front(), &count); for (size_t i = 0; i < count; i++) { if (!chunks[i].isEmpty()) { videoClasses->push_back(chunks[i].getString()); } } } StringStorage videoRectsStringStorage; m_videoRects.getText(&videoRectsStringStorage); count = 0; videoRectsStringStorage.split(delimiters, NULL, &count); if (count != 0) { std::vector<StringStorage> chunks(count); videoRectsStringStorage.split(delimiters, &chunks.front(), &count); for (size_t i = 0; i < count; i++) { if (!chunks[i].isEmpty()) { try { videoRects->push_back(RectSerializer::toRect(&chunks[i])); } catch (...) { // Ignore wrong formatted strings } } } } // // TODO: Create parseUInt method // StringStorage vriss; m_videoRecognitionInterval.getText(&vriss); int interval; StringParser::parseInt(vriss.getString(), &interval); m_config->setVideoRecognitionInterval((unsigned int)interval); }
Value *DataflowAnalyzer::computeValue(const Term *term, const MemoryLocation &memoryLocation, const ReachingDefinitions &definitions) { assert(term); assert(term->isRead()); assert(memoryLocation || definitions.empty()); auto value = dataflow().getValue(term); if (definitions.empty()) { return value; } auto byteOrder = architecture()->getByteOrder(memoryLocation.domain()); /* * Merge abstract values. */ auto abstractValue = value->abstractValue(); foreach (const auto &chunk, definitions.chunks()) { assert(memoryLocation.covers(chunk.location())); /* * Mask of bits inside abstractValue which are covered by chunk's location. */ auto mask = bitMask<ConstantValue>(chunk.location().size()); if (byteOrder == ByteOrder::LittleEndian) { mask = bitShift(mask, chunk.location().addr() - memoryLocation.addr()); } else { mask = bitShift(mask, memoryLocation.endAddr() - chunk.location().endAddr()); } foreach (auto definition, chunk.definitions()) { auto definitionLocation = dataflow().getMemoryLocation(definition); assert(definitionLocation.covers(chunk.location())); auto definitionValue = dataflow().getValue(definition); auto definitionAbstractValue = definitionValue->abstractValue(); /* * Shift definition's abstract value to match term's location. */ if (byteOrder == ByteOrder::LittleEndian) { definitionAbstractValue.shift(definitionLocation.addr() - memoryLocation.addr()); } else { definitionAbstractValue.shift(memoryLocation.endAddr() - definitionLocation.endAddr()); } /* Project the value to the defined location. */ definitionAbstractValue.project(mask); /* Update term's value. */ abstractValue.merge(definitionAbstractValue); } } value->setAbstractValue(abstractValue.resize(term->size())); /* * Merge stack offset and product flags. * * Heuristic: merge information only from terms that define lower bits of the term's value. */ const std::vector<const Term *> *lowerBitsDefinitions = nullptr; if (byteOrder == ByteOrder::LittleEndian) { if (definitions.chunks().front().location().addr() == memoryLocation.addr()) { lowerBitsDefinitions = &definitions.chunks().front().definitions(); } } else { if (definitions.chunks().back().location().endAddr() == memoryLocation.endAddr()) { lowerBitsDefinitions = &definitions.chunks().back().definitions(); } } if (lowerBitsDefinitions) { foreach (auto definition, *lowerBitsDefinitions) { auto definitionValue = dataflow().getValue(definition); if (definitionValue->isNotStackOffset()) { value->makeNotStackOffset(); } else if (definitionValue->isStackOffset()) { value->makeStackOffset(definitionValue->stackOffset()); } if (definitionValue->isNotProduct()) { value->makeNotProduct(); } else if (definitionValue->isProduct()) { value->makeProduct(); } } } /* * Merge return address flag. */ if (definitions.chunks().front().location() == memoryLocation) { foreach (auto definition, definitions.chunks().front().definitions()) { auto definitionValue = dataflow().getValue(definition); if (definitionValue->isNotReturnAddress()) { value->makeNotReturnAddress(); } else if (definitionValue->isReturnAddress()) { value->makeReturnAddress(); } } }
typename util::detail::algorithm_result<ExPolicy, OutIter>::type set_operation(ExPolicy policy, RanIter1 first1, RanIter1 last1, RanIter2 first2, RanIter2 last2, OutIter dest, F && f, Combiner && combiner, SetOp && setop) { typedef typename std::iterator_traits<RanIter1>::difference_type difference_type1; typedef typename std::iterator_traits<RanIter2>::difference_type difference_type2; // allocate intermediate buffers difference_type1 len1 = std::distance(first1, last1); difference_type2 len2 = std::distance(first2, last2); typedef typename set_operations_buffer<OutIter>::type buffer_type; boost::shared_array<buffer_type> buffer( new buffer_type[combiner(len1, len2)]); typedef typename ExPolicy::executor_type executor_type; std::size_t cores = executor_information_traits<executor_type>:: processing_units_count(policy.executor(), policy.parameters()); std::size_t step = (len1 + cores - 1) / cores; boost::shared_array<set_chunk_data> chunks(new set_chunk_data[cores]); // fill the buffer piecewise return parallel::util::partitioner<ExPolicy, OutIter, void>::call( policy, chunks.get(), cores, // first step, is applied to all partitions [=](set_chunk_data* curr_chunk, std::size_t part_size) { HPX_ASSERT(part_size == 1); // find start in sequence 1 std::size_t start1 = (curr_chunk - chunks.get()) * step; std::size_t end1 = (std::min)(start1 + step, std::size_t(len1)); bool first_partition = (start1 == 0); bool last_partition = (end1 == std::size_t(len1)); // all but the last chunk require special handling if (!last_partition) { // this chunk will be handled by the next one if all // elements of this partition are equal if (!f(first1[start1], first1[end1 + 1])) return; // move backwards to find earliest element which is equal to // the last element of the current chunk while (end1 != 0 && !f(first1[end1 - 1], first1[end1])) --end1; } // move backwards to find earliest element which is equal to // the first element of the current chunk while (start1 != 0 && !f(first1[start1 - 1], first1[start1])) --start1; // find start and end in sequence 2 std::size_t start2 = 0; if (!first_partition) { start2 = std::lower_bound( first2, first2 + len2, first1[start1], f ) - first2; } std::size_t end2 = len2; if (!last_partition) { end2 = std::lower_bound( first2 + start2, first2 + len2, first1[end1], f ) - first2; } // perform requested set-operation into the proper place of the // intermediate buffer curr_chunk->start = combiner(start1, start2); auto buffer_dest = buffer.get() + curr_chunk->start; curr_chunk->len = setop(first1 + start1, first1 + end1, first2 + start2, first2 + end2, buffer_dest, f ) - buffer_dest; }, // second step, is executed after all partitions are done running [buffer, chunks, cores, dest](std::vector<future<void> >&&) -> OutIter { // accumulate real length set_chunk_data* chunk = chunks.get(); chunk->start_index = 0; for (size_t i = 1; i != cores; ++i) { set_chunk_data* curr_chunk = chunk++; chunk->start_index = curr_chunk->start_index + curr_chunk->len; } // finally, copy data to destination parallel::util::foreach_partitioner< hpx::parallel::parallel_execution_policy >::call(par, chunks.get(), cores, [buffer, dest]( set_chunk_data* chunk, std::size_t, std::size_t) { std::copy(buffer.get() + chunk->start, buffer.get() + chunk->start + chunk->len, dest + chunk->start_index); }, [](set_chunk_data* last) -> set_chunk_data* { return last; }); return dest; }); }