void IMovable::ApplyForce(VDir force) { if (!NonZero(force)) return; if (!NonZero(force_)) ForceManager::Get().Add(GetId()); force_.x += force.x; force_.y += force.y; force_.z += force.z; }
void IMovable::LoadInForceManager() { if (NonZero(force_)) { ForceManager::Get().Add(GetId()); } }
void CMemTest::Zerofilling() { TestStruct* TSMas; size_t CountElement; CountErrors=0; if (FullLog) REPORT("\nzeroings elements of array...."); //test struct for (int i=0; i<COUNTEXPERIMENT; i++) { CountElement=rand()%MAX_SIZE; TSMas=(TestStruct*)Tcalloc(CountElement,sizeof(TestStruct)); if (NULL == TSMas) continue; for (size_t j=0; j<CountElement; j++) { if (NonZero(TSMas+j, sizeof(TestStruct))) { CountErrors++; if (ShouldReportError()) REPORT("detect nonzero element at TestStruct\n"); } } Tfree(TSMas); } if (CountErrors) REPORT("%s\n",strError); else if (FullLog) REPORT("%s\n",strOk); error_occurred |= ( CountErrors>0 ) ; }
//template <class R> VALUE PowerSymLag::SparseRowLag() { LastWasRow= 1; const unsigned char NewLag= LastOrder + 1; INDEX Current; Iterator<INDEX> SaveLast= NonZero(); Iterator<WPair> it; Iterator<INDEX> Inz; for (Inz= SaveLast; Inz; ++Inz) if (OrderLag[Current= *Inz] == LastOrder) { VALUE Nbr= RowLag[Current]; for (it= mt[Current](); it; ++it) { INDEX IxNbr= (*it).first, OrderIxNbr= Order[IxNbr]; if (OrderIxNbr == NewLag) Row[IxNbr] += (*it).second * Nbr; else { Row[IxNbr] = (*it).second * Nbr; if (OrderIxNbr == 0 && OrderLag[IxNbr] == 0) NonZero << IxNbr; Order[IxNbr]= NewLag; }; }; }; VALUE Product= 0; for (Inz= SaveLast; Inz; ++Inz) { Current= *Inz; if (OrderLag[Current] == LastOrder && Order[Current] == NewLag) Product += RowLag[Current] * Row[Current]; }; return Product; };
void ForceManager::Process() { Timer timer, remove_timer; timer.Start(); for (auto movable = to_add_.begin(); movable != to_add_.end(); ++movable) { if (!(*movable).valid()) continue; if (!NonZero((*movable)->force_)) continue; auto to_add = std::find(under_force_.begin(), under_force_.end(), *movable); if (to_add == under_force_.end()) { under_force_.push_back(*movable); } } if (to_add_.size()) { std::sort(under_force_.begin(), under_force_.end(), [](id_ptr_on<IMovable> item1, id_ptr_on<IMovable> item2) { return item1.ret_id() < item2.ret_id(); }); to_add_.clear(); } // SYSTEM_STREAM << "Remove size: " << to_remove.size() << " Force size: " << under_force_.size() << std::endl; remove_timer.Start(); for (auto movable = under_force_.begin(); movable != under_force_.end(); ++movable) { if ( !(*movable) || !NonZero((*movable)->force_)) { continue; } (*movable)->ProcessForce(); } // SYSTEM_STREAM << "Remove take: " << (remove_timer.Get() * 1.0 / timer.Get()) * 100.0 << "%" << std::endl; // SYSTEM_STREAM << "Remove size: " << to_remove.size() << " Force size: " << under_force_.size() << std::endl; //SYSTEM_STREAM << "Force take: " << timer.Get() / 1000.0 << std::endl; }
//template <class R> VALUE PowerSymLag::SparseColumnLag() { LastWasRow= 0; const unsigned char NewLag= LastOrder + 1; INDEX Current; VALUE Product= 0; for (Iterator<INDEX> Inz= NonZero(); Inz; ++Inz) if (Order[Current= *Inz] == NewLag) Product += Row[Current] * Row[Current]; AdvanceLag(); return Product; };
void ForceManager::Process() { for (auto movable = to_add_.begin(); movable != to_add_.end(); ++movable) { if (!(*movable).valid()) { continue; } if (!NonZero((*movable)->force_)) { continue; } auto to_add = std::find(under_force_.begin(), under_force_.end(), *movable); if (to_add == under_force_.end()) { under_force_.push_back(*movable); } } if (to_add_.size()) { std::sort(under_force_.begin(), under_force_.end(), [](id_ptr_on<IMovable> item1, id_ptr_on<IMovable> item2) { return item1.ret_id() < item2.ret_id(); }); to_add_.clear(); } for (auto movable = under_force_.begin(); movable != under_force_.end(); ++movable) { if ( !(*movable) || !NonZero((*movable)->force_)) { continue; } (*movable)->ProcessForce(); } }
void ForceManager::Clear() { std::vector<id_ptr_on<IMovable>> to_remove; for (auto movable = under_force_.begin(); movable != under_force_.end(); ++movable) { if ( !(*movable) || !NonZero((*movable)->force_)) { to_remove.push_back(*movable); } } for (auto it = to_remove.begin(); it != to_remove.end(); ++it) under_force_.erase(std::find(under_force_.begin(), under_force_.end(), *it)); }
void IMovable::ProcessForce() { Dir step = VDirToDir(force_); TryMove(step); if (!NonZero(force_)) { return; } VDir vstep = DirToVDir[step]; force_.x -= (vstep.x * Friction::CombinedFriction(GetTurf())) / Friction::BASE_FRICTION; force_.y -= (vstep.y * Friction::CombinedFriction(GetTurf())) / Friction::BASE_FRICTION; force_.z -= (vstep.z * Friction::CombinedFriction(GetTurf())) / Friction::BASE_FRICTION; }
void IMovable::ProcessForce() { Dir step = VDirToDir(force_); checkMove(step); if (!NonZero(force_)) return; // qDebug() << "Process force: " << GetId(); // qDebug() << force_.x; // qDebug() << force_.y; VDir vstep = DirToVDir[step]; force_.x -= (vstep.x * Friction::CombinedFriction(GetTurf())) / Friction::BASE_FRICTION; force_.y -= (vstep.y * Friction::CombinedFriction(GetTurf())) / Friction::BASE_FRICTION; force_.z -= (vstep.z * Friction::CombinedFriction(GetTurf())) / Friction::BASE_FRICTION; }
void CMemTest::UniquePointer() { CountErrors=0; int **MasPointer = (int **)Tmalloc(sizeof(int*)*COUNT_ELEM); size_t *MasCountElem = (size_t*)Tmalloc(sizeof(size_t)*COUNT_ELEM); if (FullLog) REPORT("\nUnique pointer using 0\n"); ASSERT(MasCountElem && MasPointer, NULL); // //------------------------------------------------------- //malloc for (int i=0; i<COUNT_ELEM; i++) { MasCountElem[i]=rand()%MAX_SIZE; MasPointer[i]=(int*)Tmalloc(MasCountElem[i]*sizeof(int)); if (NULL == MasPointer[i]) MasCountElem[i]=0; memset(MasPointer[i], 0, sizeof(int)*MasCountElem[i]); } if (FullLog) REPORT("malloc...."); for (UINT i=0; i<COUNT_ELEM-1; i++) { if (size_t badOff = NonZero(MasPointer[i], sizeof(int)*MasCountElem[i])) { CountErrors++; if (ShouldReportError()) REPORT("error, detect non-zero at %p\n", (char*)MasPointer[i]+badOff-1); } memset(MasPointer[i], 1, sizeof(int)*MasCountElem[i]); } if (CountErrors) REPORT("%s\n",strError); else if (FullLog) REPORT("%s\n",strOk); error_occurred |= ( CountErrors>0 ) ; //---------------------------------------------------------- //calloc for (int i=0; i<COUNT_ELEM; i++) Tfree(MasPointer[i]); CountErrors=0; for (long i=0; i<COUNT_ELEM; i++) { MasPointer[i]=(int*)Tcalloc(MasCountElem[i]*sizeof(int),2); if (NULL == MasPointer[i]) MasCountElem[i]=0; } if (FullLog) REPORT("calloc...."); for (int i=0; i<COUNT_ELEM-1; i++) { if (size_t badOff = NonZero(MasPointer[i], sizeof(int)*MasCountElem[i])) { CountErrors++; if (ShouldReportError()) REPORT("error, detect non-zero at %p\n", (char*)MasPointer[i]+badOff-1); } memset(MasPointer[i], 1, sizeof(int)*MasCountElem[i]); } if (CountErrors) REPORT("%s\n",strError); else if (FullLog) REPORT("%s\n",strOk); error_occurred |= ( CountErrors>0 ) ; //--------------------------------------------------------- //realloc CountErrors=0; for (int i=0; i<COUNT_ELEM; i++) { MasCountElem[i]*=2; *(MasPointer+i)= (int*)Trealloc(*(MasPointer+i),MasCountElem[i]*sizeof(int)); if (NULL == MasPointer[i]) MasCountElem[i]=0; memset(MasPointer[i], 0, sizeof(int)*MasCountElem[i]); } if (FullLog) REPORT("realloc...."); for (int i=0; i<COUNT_ELEM-1; i++) { if (NonZero(MasPointer[i], sizeof(int)*MasCountElem[i])) CountErrors++; memset(MasPointer[i], 1, sizeof(int)*MasCountElem[i]); } if (CountErrors) REPORT("%s\n",strError); else if (FullLog) REPORT("%s\n",strOk); error_occurred |= ( CountErrors>0 ) ; for (int i=0; i<COUNT_ELEM; i++) Tfree(MasPointer[i]); Tfree(MasCountElem); Tfree(MasPointer); }
void CMemTest::NULLReturn(UINT MinSize, UINT MaxSize, int total_threads) { const int MB_PER_THREAD = TOTAL_MB_ALLOC / total_threads; // find size to guarantee getting NULL for 1024 B allocations const int MAXNUM_1024 = (MB_PER_THREAD + (MB_PER_THREAD>>2)) * 1024; std::vector<MemStruct> PointerList; void *tmp; CountErrors=0; int CountNULL, num_1024; if (FullLog) REPORT("\nNULL return & check errno:\n"); UINT Size; Limit limit_total(TOTAL_MB_ALLOC), no_limit(0); void **buf_1024 = (void**)Tmalloc(MAXNUM_1024*sizeof(void*)); ASSERT(buf_1024, NULL); /* We must have space for pointers when memory limit is hit. Reserve enough for the worst case, taking into account race for limited space between threads. */ PointerList.reserve(TOTAL_MB_ALLOC*MByte/MinSize); /* There is a bug in the specific version of GLIBC (2.5-12) shipped with RHEL5 that leads to erroneous working of the test on Intel64 and IPF systems when setrlimit-related part is enabled. Switching to GLIBC 2.5-18 from RHEL5.1 resolved the issue. */ if (perProcessLimits) limitBarrier->wait(limit_total); else limitMem(MB_PER_THREAD); /* regression test against the bug in allocator when it dereference NULL while lack of memory */ for (num_1024=0; num_1024<MAXNUM_1024; num_1024++) { buf_1024[num_1024] = Tcalloc(1024, 1); if (! buf_1024[num_1024]) { ASSERT_ERRNO(errno == ENOMEM, NULL); break; } } for (int i=0; i<num_1024; i++) Tfree(buf_1024[i]); Tfree(buf_1024); do { Size=rand()%(MaxSize-MinSize)+MinSize; tmp=Tmalloc(Size); if (tmp != NULL) { myMemset(tmp, 0, Size); PointerList.push_back(MemStruct(tmp, Size)); } } while(tmp != NULL); ASSERT_ERRNO(errno == ENOMEM, NULL); if (FullLog) REPORT("\n"); // preparation complete, now running tests // malloc if (FullLog) REPORT("malloc...."); CountNULL = 0; while (CountNULL==0) for (int j=0; j<COUNT_TESTS; j++) { Size=rand()%(MaxSize-MinSize)+MinSize; errno = ENOMEM+j+1; tmp=Tmalloc(Size); if (tmp == NULL) { CountNULL++; if ( CHECK_ERRNO(errno != ENOMEM) ) { CountErrors++; if (ShouldReportError()) REPORT("NULL returned, error: errno (%d) != ENOMEM\n", errno); } } else { // Technically, if malloc returns a non-NULL pointer, it is allowed to set errno anyway. // However, on most systems it does not set errno. bool known_issue = false; #if __linux__ if( CHECK_ERRNO(errno==ENOMEM) ) known_issue = true; #endif /* __linux__ */ if ( CHECK_ERRNO(errno != ENOMEM+j+1) && !known_issue) { CountErrors++; if (ShouldReportError()) REPORT("error: errno changed to %d though valid pointer was returned\n", errno); } myMemset(tmp, 0, Size); PointerList.push_back(MemStruct(tmp, Size)); } } if (FullLog) REPORT("end malloc\n"); if (CountErrors) REPORT("%s\n",strError); else if (FullLog) REPORT("%s\n",strOk); error_occurred |= ( CountErrors>0 ) ; CountErrors=0; //calloc if (FullLog) REPORT("calloc...."); CountNULL = 0; while (CountNULL==0) for (int j=0; j<COUNT_TESTS; j++) { Size=rand()%(MaxSize-MinSize)+MinSize; errno = ENOMEM+j+1; tmp=Tcalloc(COUNT_ELEM_CALLOC,Size); if (tmp == NULL) { CountNULL++; if ( CHECK_ERRNO(errno != ENOMEM) ){ CountErrors++; if (ShouldReportError()) REPORT("NULL returned, error: errno(%d) != ENOMEM\n", errno); } } else { // Technically, if calloc returns a non-NULL pointer, it is allowed to set errno anyway. // However, on most systems it does not set errno. bool known_issue = false; #if __linux__ if( CHECK_ERRNO(errno==ENOMEM) ) known_issue = true; #endif /* __linux__ */ if ( CHECK_ERRNO(errno != ENOMEM+j+1) && !known_issue ) { CountErrors++; if (ShouldReportError()) REPORT("error: errno changed to %d though valid pointer was returned\n", errno); } PointerList.push_back(MemStruct(tmp, Size)); } } if (FullLog) REPORT("end calloc\n"); if (CountErrors) REPORT("%s\n",strError); else if (FullLog) REPORT("%s\n",strOk); error_occurred |= ( CountErrors>0 ) ; CountErrors=0; if (FullLog) REPORT("realloc...."); CountNULL = 0; if (PointerList.size() > 0) while (CountNULL==0) for (size_t i=0; i<(size_t)COUNT_TESTS && i<PointerList.size(); i++) { errno = 0; tmp=Trealloc(PointerList[i].Pointer,PointerList[i].Size*2); if (PointerList[i].Pointer == tmp) // the same place { bool known_issue = false; #if __linux__ if( errno==ENOMEM ) known_issue = true; #endif /* __linux__ */ if (errno != 0 && !known_issue) { CountErrors++; if (ShouldReportError()) REPORT("valid pointer returned, error: errno not kept\n"); } PointerList[i].Size *= 2; } else if (tmp != PointerList[i].Pointer && tmp != NULL) // another place { bool known_issue = false; #if __linux__ if( errno==ENOMEM ) known_issue = true; #endif /* __linux__ */ if (errno != 0 && !known_issue) { CountErrors++; if (ShouldReportError()) REPORT("valid pointer returned, error: errno not kept\n"); } // newly allocated area have to be zeroed myMemset((char*)tmp + PointerList[i].Size, 0, PointerList[i].Size); PointerList[i].Pointer = tmp; PointerList[i].Size *= 2; } else if (tmp == NULL) { CountNULL++; if ( CHECK_ERRNO(errno != ENOMEM) ) { CountErrors++; if (ShouldReportError()) REPORT("NULL returned, error: errno(%d) != ENOMEM\n", errno); } // check data integrity if (NonZero(PointerList[i].Pointer, PointerList[i].Size)) { CountErrors++; if (ShouldReportError()) REPORT("NULL returned, error: data changed\n"); } } } if (FullLog) REPORT("realloc end\n"); if (CountErrors) REPORT("%s\n",strError); else if (FullLog) REPORT("%s\n",strOk); error_occurred |= ( CountErrors>0 ) ; for (UINT i=0; i<PointerList.size(); i++) { Tfree(PointerList[i].Pointer); } if (perProcessLimits) limitBarrier->wait(no_limit); else limitMem(0); }