// Const access and load on demand inline const ItemType& operator()( const int i ) { assert( i < static_cast<int>( forward_ptr.size() ) ); if ( -1 == forward_ptr[i] ) { assert( LAZY_BUFFER_INPUT == type ); if ( -1 != limit && static_cast<int>( backward_ptr.size() ) >= limit ) { int pos = fl.discard(); int unload = forward_ptr[pos]; forward_ptr[pos] = -1; assert( -1 != unload ); fseek( fdata, offsets[i], SEEK_SET ); items[unload] = std::move( ItemType( fdata ) ); forward_ptr[i] = unload; backward_ptr[unload] = i; } else { fseek( fdata, offsets[i], SEEK_SET ); items.push_back( std::move( ItemType( fdata ) ) ); forward_ptr[i] = static_cast<int>( items.size() - 1 ); backward_ptr.push_back( i ); assert( backward_ptr.size() == items.size() ); } fl.promote( i ); } else { assert( ( 0 <= forward_ptr[i] ) && ( forward_ptr[i] < static_cast<int>( items.size() ) ) ); } return items[forward_ptr[i]]; }
// The Constructor inline LazyBuffer( const string& offsetFile, const string& dataFile, int direction=LAZY_BUFFER_INPUT ) : limit(-1) { type = direction; datafile = dataFile; offsetfile = offsetFile; if ( LAZY_BUFFER_INPUT == direction ) { WITH_OPEN( in, offsetFile.c_str(), "rb" ); int len = 0; fread( &len, sizeof(int), 1, in ); forward_ptr.resize( len ); offsets.resize( len ); backward_ptr.clear(); items.clear(); hash.clear(); for ( auto& item : forward_ptr ) item = -1; // the forward_ptr is zero for ( auto& item : offsets ) fread( &item, sizeof(long), 1, in ); fdata = fopen( dataFile.c_str(), "rb" ); // Initialize the FreeList fl.init( len ); END_WITH( in ); } else if ( LAZY_BUFFER_CREATE == direction ) { fl.init(0); forward_ptr.clear(); backward_ptr.clear(); offsets.clear(); items.clear(); hash.clear(); fdata = nullptr; } else { Error( "LazyBuffer(): Wrong Parameter for direction in constructor." ); } }
void threadFunc(FreeList& pool, ros::atomic<bool>& done, ros::atomic<bool>& failed, boost::barrier& b) { b.wait(); //ROS_INFO_STREAM("Thread " << boost::this_thread::get_id() << " starting"); uint32_t* vals[10]; uint64_t alloc_count = 0; while (!done.load()) { for (uint32_t i = 0; i < 10; ++i) { vals[i] = static_cast<uint32_t*>(pool.allocate()); if (vals[i]) { ++alloc_count; *vals[i] = i; } else { ROS_ERROR_STREAM("Thread " << boost::this_thread::get_id() << " failed to allocate"); } } for (uint32_t i = 0; i < 10; ++i) { if (vals[i]) { if (*vals[i] != i) { ROS_ERROR_STREAM("Thread " << boost::this_thread::get_id() << " val " << vals[i] << " " << i << " = " << *vals[i]); failed.store(true); } pool.free(vals[i]); } } if (failed.load()) { #if FREE_LIST_DEBUG boost::mutex::scoped_lock lock(g_debug_mutex); g_debug.push_back(*pool.getDebug()); #endif return; } } //ROS_INFO_STREAM("Thread " << boost::this_thread::get_id() << " allocated " << alloc_count << " blocks"); }
void* malloc () { if (freeList.empty ()) { allocateChunk (); } FreeListNode *node = &freeList.front (); freeList.pop_front (); node->~slist_base_hook<> (); ++mallocCount; return reinterpret_cast<void*> (node); }
void free (void* t) { FreeListNode *node = reinterpret_cast<FreeListNode*> (t); new (node) FreeListNode; freeList.push_front (*node); ++freeCount; assert(([this]()->bool { // check we have no double-frees std::set<FreeListNode*> s; for(auto &n: freeList) s.insert(&n); return s.size() == freeList.size(); }) ()); }
void GLDrawInfo::StartScene() { sectorrenderflags.Resize(numsectors); ss_renderflags.Resize(numsubsectors); memset(§orrenderflags[0], 0, numsectors*sizeof(sectorrenderflags[0])); memset(&ss_renderflags[0], 0, numsubsectors*sizeof(ss_renderflags[0])); for(unsigned int i=0;i< otherfloorplanes.Size();i++) { gl_subsectorrendernode * node = otherfloorplanes[i]; while (node) { gl_subsectorrendernode * n = node; node = node->next; SSR_List.Release(n); } } otherfloorplanes.Clear(); for(unsigned int i=0;i< otherceilingplanes.Size();i++) { gl_subsectorrendernode * node = otherceilingplanes[i]; while (node) { gl_subsectorrendernode * n = node; node = node->next; SSR_List.Release(n); } } otherceilingplanes.Clear(); // clear all the lists that might not have been cleared already MissingUpperTextures.Clear(); MissingLowerTextures.Clear(); MissingUpperSegs.Clear(); MissingLowerSegs.Clear(); SubsectorHacks.Clear(); CeilingStacks.Clear(); FloorStacks.Clear(); HandledSubsectors.Clear(); next=gl_drawinfo; gl_drawinfo=this; for(int i=0;i<GLDL_TYPES;i++) drawlists[i].Reset(); }
//========================================================================== // // // //========================================================================== void GLDrawInfo::EndDrawInfo() { GLDrawInfo * di = gl_drawinfo; for(int i=0;i<GLDL_TYPES;i++) di->drawlists[i].Reset(); gl_drawinfo=di->next; if (di->temporary) di_list.Release(di); }
//========================================================================== // // Sets up a new drawinfo struct // //========================================================================== void GLDrawInfo::StartDrawInfo(GLDrawInfo * di) { if (!di) { di=di_list.GetNew(); di->temporary=true; } di->StartScene(); }
inline void evacuate() { assert( LAZY_BUFFER_INPUT == type ); for ( auto& ele : backward_ptr ) { forward_ptr[ele] = -1; fl.discard(); } backward_ptr.clear(); items.clear(); }
void allocateChunk () { chunks.push_back (new Chunk); // requires c++17 new with alignment for (Storage& storage : chunks.back ()) { FreeListNode *node = reinterpret_cast<FreeListNode*> (&storage); new (node) FreeListNode; freeList.push_front (*node); } }
void* MarkedAllocator::tryAllocateIn(MarkedBlock::Handle* block) { ASSERT(block); ASSERT(!block->hasAnyNewlyAllocated()); ASSERT(!block->isFreeListed()); FreeList freeList = block->sweep(MarkedBlock::Handle::SweepToFreeList); // It's possible to stumble on a completely full block. Marking tries to retire these, but // that algorithm is racy and may forget to do it sometimes. if (freeList.allocationWillFail()) { ASSERT(block->isFreeListed()); block->unsweepWithNoNewlyAllocated(); ASSERT(!block->isFreeListed()); ASSERT(!isEmpty(block)); ASSERT(!isCanAllocateButNotEmpty(block)); return nullptr; } m_currentBlock = block; setFreeList(freeList); void* result; if (m_freeList.remaining) { unsigned cellSize = m_cellSize; m_freeList.remaining -= cellSize; result = m_freeList.payloadEnd - m_freeList.remaining - cellSize; } else { FreeCell* head = m_freeList.head; m_freeList.head = head->next; result = head; } RELEASE_ASSERT(result); setIsEden(m_currentBlock, true); m_markedSpace->didAllocateInBlock(m_currentBlock); return result; }
void FDrawInfo::ClearBuffers() { for(unsigned int i=0; i< otherfloorplanes.Size(); i++) { gl_subsectorrendernode * node = otherfloorplanes[i]; while (node) { gl_subsectorrendernode * n = node; node = node->next; SSR_List.Release(n); } } otherfloorplanes.Clear(); for(unsigned int i=0; i< otherceilingplanes.Size(); i++) { gl_subsectorrendernode * node = otherceilingplanes[i]; while (node) { gl_subsectorrendernode * n = node; node = node->next; SSR_List.Release(n); } } otherceilingplanes.Clear(); // clear all the lists that might not have been cleared already MissingUpperTextures.Clear(); MissingLowerTextures.Clear(); MissingUpperSegs.Clear(); MissingLowerSegs.Clear(); SubsectorHacks.Clear(); CeilingStacks.Clear(); FloorStacks.Clear(); HandledSubsectors.Clear(); }
static FLightNode * DeleteLightNode(FLightNode * node) { FLightNode * tn; // next node on thing thread if (node) { *node->prevTarget = node->nextTarget; if (node->nextTarget) node->nextTarget->prevTarget=node->prevTarget; *node->prevLight = node->nextLight; if (node->nextLight) node->nextLight->prevLight=node->prevLight; // Return this node to the freelist tn=node->nextTarget; freelist.Release(node); return(tn); } return(NULL); } // phares 3/13/98
FLightNode * AddLightNode(FLightNode ** thread, void * linkto, ADynamicLight * light, FLightNode *& nextnode) { FLightNode * node; node = nextnode; while (node) { if (node->targ==linkto) // Already have a node for this sector? { node->lightsource = light; // Yes. Setting m_thing says 'keep it'. return(nextnode); } node = node->nextTarget; } // Couldn't find an existing node for this sector. Add one at the head // of the list. node = freelist.GetNew(); node->targ = linkto; node->lightsource = light; node->prevTarget = &nextnode; node->nextTarget = nextnode; if (nextnode) nextnode->prevTarget = &node->nextTarget; // Add new node at head of sector thread starting at s->touching_thinglist node->prevLight = thread; node->nextLight = *thread; if (node->nextLight) node->nextLight->prevLight=&node->nextLight; *thread = node; return(node); }
void FDrawInfo::HandleHackedSubsectors() { lowershcount=uppershcount=0; totalssms.Reset(); totalssms.Clock(); viewsubsector = R_PointInSubsector(viewx, viewy); // Each subsector may only be processed once in this loop! validcount++; for(unsigned int i=0; i<SubsectorHacks.Size(); i++) { subsector_t * sub = SubsectorHacks[i].sub; if (sub->validcount!=validcount && CheckAnchorFloor(sub)) { // Now collect everything that is connected with this subsector. HandledSubsectors.Clear(); inview=false; lowersegs.Clear(); if (CollectSubsectorsFloor(sub, sub->render_sector)) { for(unsigned int j=0; j<HandledSubsectors.Size(); j++) { gl_subsectorrendernode * node = SSR_List.GetNew(); node->sub = HandledSubsectors[j]; AddOtherFloorPlane(sub->render_sector->sectornum, node); } if (inview) for(unsigned int j=0; j<lowersegs.Size(); j++) { seg_t * seg=lowersegs[j]; GLRenderer->ProcessLowerMiniseg (seg, seg->Subsector->render_sector, seg->PartnerSeg->Subsector->render_sector); } lowershcount+=HandledSubsectors.Size(); } } } // Each subsector may only be processed once in this loop! validcount++; for(unsigned int i=0; i<SubsectorHacks.Size(); i++) { subsector_t * sub = SubsectorHacks[i].sub; if (sub->validcount!=validcount && CheckAnchorCeiling(sub)) { // Now collect everything that is connected with this subsector. HandledSubsectors.Clear(); if (CollectSubsectorsCeiling(sub, sub->render_sector)) { for(unsigned int j=0; j<HandledSubsectors.Size(); j++) { gl_subsectorrendernode * node = SSR_List.GetNew(); node->sub = HandledSubsectors[j]; AddOtherCeilingPlane(sub->render_sector->sectornum, node); } uppershcount+=HandledSubsectors.Size(); } } } SubsectorHacks.Clear(); totalssms.Unclock(); }
//========================================================================== // // Draws the fake planes // //========================================================================== void FDrawInfo::HandleMissingTextures() { sector_t fake; totalms.Clock(); totalupper=MissingUpperTextures.Size(); totallower=MissingLowerTextures.Size(); for(unsigned int i=0; i<MissingUpperTextures.Size(); i++) { if (!MissingUpperTextures[i].seg) continue; HandledSubsectors.Clear(); validcount++; if (MissingUpperTextures[i].planez > viewz) { // close the hole only if all neighboring sectors are an exact height match // Otherwise just fill in the missing textures. MissingUpperTextures[i].sub->validcount=validcount; if (DoOneSectorUpper(MissingUpperTextures[i].sub, MissingUpperTextures[i].planez)) { sector_t * sec = MissingUpperTextures[i].seg->backsector; // The mere fact that this seg has been added to the list means that the back sector // will be rendered so we can safely assume that it is already in the render list for(unsigned int j=0; j<HandledSubsectors.Size(); j++) { gl_subsectorrendernode * node = SSR_List.GetNew(); node->sub = HandledSubsectors[j]; AddOtherCeilingPlane(sec->sectornum, node); } if (HandledSubsectors.Size()!=1) { // mark all subsectors in the missing list that got processed by this for(unsigned int j=0; j<HandledSubsectors.Size(); j++) { for(unsigned int k=0; k<MissingUpperTextures.Size(); k++) { if (MissingUpperTextures[k].sub==HandledSubsectors[j]) { MissingUpperTextures[k].seg=NULL; } } } } else MissingUpperTextures[i].seg=NULL; continue; } } if (!MissingUpperTextures[i].seg->PartnerSeg) continue; if (!MissingUpperTextures[i].seg->PartnerSeg->Subsector) continue; validcount++; HandledSubsectors.Clear(); { // It isn't a hole. Now check whether it might be a fake bridge sector_t * fakesector = gl_FakeFlat(MissingUpperTextures[i].seg->frontsector, &fake, false); fixed_t planez = fakesector->GetPlaneTexZ(sector_t::ceiling); MissingUpperTextures[i].seg->PartnerSeg->Subsector->validcount=validcount; if (DoFakeCeilingBridge(MissingUpperTextures[i].seg->PartnerSeg->Subsector, planez)) { // The mere fact that this seg has been added to the list means that the back sector // will be rendered so we can safely assume that it is already in the render list for(unsigned int j=0; j<HandledSubsectors.Size(); j++) { gl_subsectorrendernode * node = SSR_List.GetNew(); node->sub = HandledSubsectors[j]; AddOtherCeilingPlane(fakesector->sectornum, node); } } continue; } } for(unsigned int i=0; i<MissingLowerTextures.Size(); i++) { if (!MissingLowerTextures[i].seg) continue; HandledSubsectors.Clear(); validcount++; if (MissingLowerTextures[i].planez < viewz) { // close the hole only if all neighboring sectors are an exact height match // Otherwise just fill in the missing textures. MissingLowerTextures[i].sub->validcount=validcount; if (DoOneSectorLower(MissingLowerTextures[i].sub, MissingLowerTextures[i].planez)) { sector_t * sec = MissingLowerTextures[i].seg->backsector; // The mere fact that this seg has been added to the list means that the back sector // will be rendered so we can safely assume that it is already in the render list for(unsigned int j=0; j<HandledSubsectors.Size(); j++) { gl_subsectorrendernode * node = SSR_List.GetNew(); node->sub = HandledSubsectors[j]; AddOtherFloorPlane(sec->sectornum, node); } if (HandledSubsectors.Size()!=1) { // mark all subsectors in the missing list that got processed by this for(unsigned int j=0; j<HandledSubsectors.Size(); j++) { for(unsigned int k=0; k<MissingLowerTextures.Size(); k++) { if (MissingLowerTextures[k].sub==HandledSubsectors[j]) { MissingLowerTextures[k].seg=NULL; } } } } else MissingLowerTextures[i].seg=NULL; continue; } } if (!MissingLowerTextures[i].seg->PartnerSeg) continue; if (!MissingLowerTextures[i].seg->PartnerSeg->Subsector) continue; validcount++; HandledSubsectors.Clear(); { // It isn't a hole. Now check whether it might be a fake bridge sector_t * fakesector = gl_FakeFlat(MissingLowerTextures[i].seg->frontsector, &fake, false); fixed_t planez = fakesector->GetPlaneTexZ(sector_t::floor); MissingLowerTextures[i].seg->PartnerSeg->Subsector->validcount=validcount; if (DoFakeBridge(MissingLowerTextures[i].seg->PartnerSeg->Subsector, planez)) { // The mere fact that this seg has been added to the list means that the back sector // will be rendered so we can safely assume that it is already in the render list for(unsigned int j=0; j<HandledSubsectors.Size(); j++) { gl_subsectorrendernode * node = SSR_List.GetNew(); node->sub = HandledSubsectors[j]; AddOtherFloorPlane(fakesector->sectornum, node); } } continue; } } totalms.Unclock(); showtotalms=totalms; totalms.Reset(); }
void FDrawInfo::ProcessSectorStacks() { unsigned int i; validcount++; for (i=0; i<CeilingStacks.Size (); i++) { subsector_t * sub = CeilingStacks[i]; HandledSubsectors.Clear(); for(DWORD j=0; j<sub->numlines; j++) { seg_t * seg = sub->firstline + j; if (seg->PartnerSeg) { subsector_t * backsub = seg->PartnerSeg->Subsector; if (backsub->validcount!=validcount) CollectSectorStacksCeiling (backsub, sub->render_sector); } } for(unsigned int j=0; j<HandledSubsectors.Size(); j++) { ss_renderflags[DWORD(HandledSubsectors[j]-subsectors)] &= ~SSRF_RENDERCEILING; if (sub->render_sector->CeilingSkyBox->PlaneAlpha!=0) { gl_subsectorrendernode * node = SSR_List.GetNew(); node->sub = HandledSubsectors[j]; AddOtherCeilingPlane(sub->render_sector->sectornum, node); } } } validcount++; for (i=0; i<FloorStacks.Size (); i++) { subsector_t * sub = FloorStacks[i]; HandledSubsectors.Clear(); for(DWORD j=0; j<sub->numlines; j++) { seg_t * seg = sub->firstline + j; if (seg->PartnerSeg) { subsector_t * backsub = seg->PartnerSeg->Subsector; if (backsub->validcount!=validcount) CollectSectorStacksFloor (backsub, sub->render_sector); } } for(unsigned int j=0; j<HandledSubsectors.Size(); j++) { //Printf("%d: ss %d, sec %d\n", j, HandledSubsectors[j]-subsectors, HandledSubsectors[j]->render_sector->sectornum); ss_renderflags[DWORD(HandledSubsectors[j]-subsectors)] &= ~SSRF_RENDERFLOOR; if (sub->render_sector->FloorSkyBox->PlaneAlpha!=0) { gl_subsectorrendernode * node = SSR_List.GetNew(); node->sub = HandledSubsectors[j]; AddOtherFloorPlane(sub->render_sector->sectornum, node); } } } FloorStacks.Clear(); CeilingStacks.Clear(); }
void FDrawInfo::ProcessSectorStacks() { unsigned int i; sector_t fake; validcount++; for (i=0;i<CeilingStacks.Size (); i++) { sector_t *sec = gl_FakeFlat(CeilingStacks[i], &fake, false); FPortal *portal = sec->portals[sector_t::ceiling]; if (portal != NULL) for(int k=0;k<sec->subsectorcount;k++) { subsector_t * sub = sec->subsectors[k]; if (ss_renderflags[sub-subsectors] & SSRF_PROCESSED) { HandledSubsectors.Clear(); for(DWORD j=0;j<sub->numlines;j++) { seg_t * seg = sub->firstline + j; if (seg->PartnerSeg) { subsector_t * backsub = seg->PartnerSeg->Subsector; if (backsub->validcount!=validcount) CollectSectorStacksCeiling (backsub, sec); } } for(unsigned int j=0;j<HandledSubsectors.Size();j++) { subsector_t *sub = HandledSubsectors[j]; ss_renderflags[DWORD(sub-subsectors)] &= ~SSRF_RENDERCEILING; if (sub->portalcoverage[sector_t::ceiling].subsectors == NULL) { gl_BuildPortalCoverage(&sub->portalcoverage[sector_t::ceiling], sub, portal); } portal->GetGLPortal()->AddSubsector(sub); if (sec->GetAlpha(sector_t::ceiling) != 0 && sec->GetTexture(sector_t::ceiling) != skyflatnum) { gl_subsectorrendernode * node = SSR_List.GetNew(); node->sub = sub; AddOtherCeilingPlane(sec->sectornum, node); } } } } } validcount++; for (i=0;i<FloorStacks.Size (); i++) { sector_t *sec = gl_FakeFlat(FloorStacks[i], &fake, false); FPortal *portal = sec->portals[sector_t::floor]; if (portal != NULL) for(int k=0;k<sec->subsectorcount;k++) { subsector_t * sub = sec->subsectors[k]; if (ss_renderflags[sub-subsectors] & SSRF_PROCESSED) { HandledSubsectors.Clear(); for(DWORD j=0;j<sub->numlines;j++) { seg_t * seg = sub->firstline + j; if (seg->PartnerSeg) { subsector_t * backsub = seg->PartnerSeg->Subsector; if (backsub->validcount!=validcount) CollectSectorStacksFloor (backsub, sec); } } for(unsigned int j=0;j<HandledSubsectors.Size();j++) { subsector_t *sub = HandledSubsectors[j]; ss_renderflags[DWORD(sub-subsectors)] &= ~SSRF_RENDERFLOOR; if (sub->portalcoverage[sector_t::floor].subsectors == NULL) { gl_BuildPortalCoverage(&sub->portalcoverage[sector_t::floor], sub, portal); } GLSectorStackPortal *glportal = portal->GetGLPortal(); glportal->AddSubsector(sub); if (sec->GetAlpha(sector_t::floor) != 0 && sec->GetTexture(sector_t::floor) != skyflatnum) { gl_subsectorrendernode * node = SSR_List.GetNew(); node->sub = sub; AddOtherFloorPlane(sec->sectornum, node); } } } } } FloorStacks.Clear(); CeilingStacks.Clear(); }
void decRef() { ptr->decRef(); }
void incRef() { ptr->incRef(); }