/****************************************************************************** * * * BYTE * _Init_Alloc( BYTE *pRamStart, DWORD dwRamSize ) * * * ******************************************************************************* * * This function initializes a buffer for the internal memory allocation pool. * * Where: * pRamStart - beginning address of the linear buffer memory * dwRamTop - size in bytes of the buffer memory * * Returns: * Memory handle to be passed to subsequent mallocHeap() and freeHeap() * functions. That is the "heap". * This is actually the starting address pRamStart. * * NULL - memory could not be initialized (invalid size/address) * ******************************************************************************/ static BYTE * _Init_Alloc( BYTE *pRamStart, DWORD dwRamSize ) { Tmalloc *pMalloc; BYTE * pFree; // Some sanity checking if( dwRamSize < 32 ) return( NULL ); // Set the dummy free structure at the beginning of the free block to // easily traverse the linked list (a sentinel) pMalloc = TM(pRamStart); // Get the buffer start pFree = (char*)pMalloc; // Set the free list beginning pMalloc->size = 0; // No one can request that much! pMalloc->next = STM(pMalloc + 1); // Next structure immediately follows pMalloc = TM(pMalloc->next); // Next free block header pMalloc->size = dwRamSize - HEADER_SIZE; // That's how much is really free pMalloc->next = NULL; // Last block in the list // Return the address of a heap that is now initialized return( pFree ); }
// Since `96 :-P static int _ReAlloc( BYTE * pHeap, void * ptr, size_t new_size ) { Tmalloc *pLast; Tmalloc *pMem; Tmalloc *pMalloc; // Return if anything is NULL (should not happen) if( (pHeap==NULL) || (ptr==NULL) || (new_size==0) ) return( NULL ); // Check that the ptr pointer is valid pMalloc = (Tmalloc*)((int)mPtr - HEADER_SIZE); // Check for the magic number to ensure that the right block was passed if( (int)pMalloc->next != MALLOC_COOKIE ) { /* Should print some error message in the future */ //printf(" *** ERROR - Magic Number Wrong: %08X ***\n",(int)pMalloc->next ); return NULL; } // We need to find the last free node before this one pLast = TM(pHeap); pMem = TM(pLast->next); // Traverse the free list and find the last node before the `ptr'-one while( (pMem != NULL) && (pMem < pMalloc) ) { pLast = pMem; pMem = TM(pMem->next); } // If the new size if smaller, this function always succeeds if( new_size < pMalloc->size ) { return( ptr ); } else if( new_size > pMalloc->size ) { // If the new size is larger than the old size, check if there // is enough free space after this block return( NULL ); } // Return ok return( ptr ); }
/****************************************************************************** * * * void DumpHeap(BYTE *pHeap) * * * ******************************************************************************* * * Dumps the free list of a heap nodes. This is used for debugging of the heap * allocations. * * Where: * pHeap is the requested heap * ******************************************************************************/ void DumpHeap(BYTE *pHeap) { Tmalloc *p; // Traverse the free list and dump it p = TM(pHeap); while( p ) { dprinth(1, "%08X %X (%d)", (DWORD)p, p->size, p->size); p = TM(p->next); } }
void fillWall(struct TILE *map, int sy, int sx, int ft, int wt){ int y, x; for(y = 1; y < sy - 1; y++){ for(x = 1; x < sx - 1; x++){ if(map[CM(y,sx,x)].tT == TILE_BLANK){ if(map[TL(y,sx,x)].tT == ft || map[TM(y,sx,x)].tT == ft || map[TR(y,sx,x)].tT == ft || map[CL(y,sx,x)].tT == ft || map[CR(y,sx,x)].tT == ft || map[BL(y,sx,x)].tT == ft || map[BM(y,sx,x)].tT == ft || map[BR(y,sx,x)].tT == ft){ map[CM(y,sx,x)].tT = wt; } } } } for(y = 0; y < sy; y++){ for(x = 0; x < sx; x++){ if(y == 0){ if(map[S(y,sx,x)].tT == ft) map[C(y,sx,x)].tT = wt; } if(x == 0){ if(map[E(y,sx,x)].tT == ft) map[C(y,sx,x)].tT = wt; } if(y == sy - 1){ if(map[N(y,sx,x)].tT == ft) map[C(y,sx,x)].tT = wt; } if(x == sx - 1){ if(map[W(y,sx,x)].tT == ft) map[C(y,sx,x)].tT = wt; } } } }
void test_memcpy_bounds_memarray_range (void) { #undef TM #define TM(mem, dst, src, n) \ do { \ struct MA { char a5[5]; int i; } ma; \ sink (&ma); /* Initialize arrays. */ \ memcpy (dst, src, n); \ sink (&ma); \ } while (0) ptrdiff_t i = SR (1, 2); TM (ma.a5, ma.a5 + i, ma.a5, 1); TM (ma.a5, ma.a5 + i, ma.a5, 3); TM (ma.a5, ma.a5 + i, ma.a5, 5); TM (ma.a5, ma.a5 + i, ma.a5, 7); /* diagnosed with -Warray-bounds=2 */ }
static int _Alloc_Check( BYTE *pHeap, DWORD dwInitSize ) { Tmalloc *pLast; Tmalloc *pNew; BYTE *pEnd; int nFree = 0; // Check the pointer to heap if( pHeap == NULL ) return( -1 ); // Check the size if( dwInitSize < 32 ) return( -2 ); pEnd = pHeap + dwInitSize; pLast = TM(pHeap); pNew = TM(pLast->next); if( ((BYTE *)pNew < pHeap) || ((BYTE *)pNew > pEnd) ) return( -3 ); // Traverse the list and check all the nodes while( pNew != NULL ) { if( (pNew->size < 4) || (pNew->size + (BYTE *)pNew > pEnd) ) return( -4 ); // Add the free size nFree += pNew->size; pLast = pNew; pNew = TM(pNew->next); if( pNew != NULL ) if( ((BYTE *)pNew < pHeap) || ((BYTE *)pNew >= pEnd) ) return( -3 ); } return( nFree ); }
int main() { Nucleation nuc(5,10,50,1.,1.,2.5,0,10); Harmonic pe(nuc); TransferMatrixFunctions TM(pe); std::cout << TM.T(0,1) << std::endl; std::cout << TM.T_hat(0,1) << std::endl; return 0; }
inline TM to() const { switch(type) { case primitiveType::bool_ : return (TM) value.bool_; case primitiveType::uint8_ : return (TM) value.uint8_; case primitiveType::uint16_ : return (TM) value.uint16_; case primitiveType::uint32_ : return (TM) value.uint32_; case primitiveType::uint64_ : return (TM) value.uint64_; case primitiveType::int8_ : return (TM) value.int8_; case primitiveType::int16_ : return (TM) value.int16_; case primitiveType::int32_ : return (TM) value.int32_; case primitiveType::int64_ : return (TM) value.int64_; case primitiveType::float_ : return (TM) value.float_; case primitiveType::double_ : return (TM) value.double_; default: OCCA_FORCE_ERROR("Type not set"); } return TM(); }
Mm check_order(Mm n_in, i_o_t, Mm& n_out__o, Mm& w__o, Mm& trivalwin__o) { begin_scope n_in.setname("n_in"); dMm(n_out); dMm(w); dMm(trivalwin); call_stack_begin; // nargin, nargout entry code double old_nargin=nargin_val; if (!nargin_set) nargin_val=1.0; nargin_set=0; double old_nargout=nargout_val; if (!nargout_set) nargout_val=3.0; nargout_set=0; // translated code //CHECK_ORDER Checks the order passed to the window functions. // [N,W,TRIVALWIN] = CHECK_ORDER(N_ESTIMATE) will round N_ESTIMATE to the // nearest integer if it is not alreay an integer. In special cases (N is [], // 0, or 1), TRIVALWIN will be set to flag that W has been modified. // Copyright 1988-2002 The MathWorks, Inc. // $Revision: 1.6 $ $Date: 2002/04/15 01:07:36 $ w = nop_M; trivalwin = 0.0; // Special case of negative orders: if (istrue(n_in<0.0)) { error(TM("Order cannot be less than zero.")); } // Check if order is already an integer or empty // If not, round to nearest integer. if (istrue(isempty(n_in))||istrue(n_in==floor(n_in))) { n_out = n_in; } else { n_out = round(n_in); warning(TM("Rounding order to nearest integer.")); } // Special cases: if (istrue(isempty(n_out))||istrue(n_out==0.0)) { w = zeros(0.0,1.0); // Empty matrix: 0-by-1 trivalwin = 1.0; } else if (istrue(n_out==1.0)) { w = 1.0; trivalwin = 1.0; } call_stack_end; // nargin, nargout exit code nargin_val=old_nargin; nargout_val=old_nargout; // function exit code n_in.setname(NULL); n_out__o=n_out; w__o=w; trivalwin__o=trivalwin; return x_M; end_scope }
// takes the running content that has collected in the shadow module and dump it to disk // this builds the object file portion of the sysimage files for fast startup static void jl_dump_shadow(char *fname, int jit_model, const char *sysimg_data, size_t sysimg_len, bool dump_as_bc) { #ifdef LLVM36 std::error_code err; StringRef fname_ref = StringRef(fname); raw_fd_ostream OS(fname_ref, err, sys::fs::F_None); #elif defined(LLVM35) std::string err; raw_fd_ostream OS(fname, err, sys::fs::F_None); #else std::string err; raw_fd_ostream OS(fname, err); #endif #ifdef LLVM37 // 3.7 simplified formatted output; just use the raw stream alone raw_fd_ostream& FOS(OS); #else formatted_raw_ostream FOS(OS); #endif // We don't want to use MCJIT's target machine because // it uses the large code model and we may potentially // want less optimizations there. Triple TheTriple = Triple(jl_TargetMachine->getTargetTriple()); #if defined(_OS_WINDOWS_) && defined(FORCE_ELF) #ifdef LLVM35 TheTriple.setObjectFormat(Triple::COFF); #else TheTriple.setEnvironment(Triple::UnknownEnvironment); #endif #elif defined(_OS_DARWIN_) && defined(FORCE_ELF) #ifdef LLVM35 TheTriple.setObjectFormat(Triple::MachO); #else TheTriple.setEnvironment(Triple::MachO); #endif #endif #ifdef LLVM35 std::unique_ptr<TargetMachine> #else OwningPtr<TargetMachine> #endif TM(jl_TargetMachine->getTarget().createTargetMachine( TheTriple.getTriple(), jl_TargetMachine->getTargetCPU(), jl_TargetMachine->getTargetFeatureString(), jl_TargetMachine->Options, #if defined(_OS_LINUX_) || defined(_OS_FREEBSD_) Reloc::PIC_, #else jit_model ? Reloc::PIC_ : Reloc::Default, #endif jit_model ? CodeModel::JITDefault : CodeModel::Default, CodeGenOpt::Aggressive // -O3 TODO: respect command -O0 flag? )); #ifdef LLVM38 legacy::PassManager PM; #else PassManager PM; #endif #ifndef LLVM37 PM.add(new TargetLibraryInfo(Triple(TM->getTargetTriple()))); #else PM.add(new TargetLibraryInfoWrapperPass(Triple(TM->getTargetTriple()))); #endif // set up optimization passes #ifdef LLVM37 // No DataLayout pass needed anymore. #elif defined(LLVM36) PM.add(new DataLayoutPass()); #elif defined(LLVM35) PM.add(new DataLayoutPass(*jl_ExecutionEngine->getDataLayout())); #else PM.add(new DataLayout(*jl_ExecutionEngine->getDataLayout())); #endif addOptimizationPasses(&PM); if (!dump_as_bc) { if (TM->addPassesToEmitFile(PM, FOS, TargetMachine::CGFT_ObjectFile, false)) { jl_error("Could not generate obj file for this target"); } } // now copy the module, since PM.run may modify it ValueToValueMapTy VMap; #ifdef LLVM38 Module *clone = CloneModule(shadow_output, VMap).release(); #else Module *clone = CloneModule(shadow_output, VMap); #endif #ifdef LLVM37 // Reset the target triple to make sure it matches the new target machine clone->setTargetTriple(TM->getTargetTriple().str()); #ifdef LLVM38 clone->setDataLayout(TM->createDataLayout()); #else clone->setDataLayout(TM->getDataLayout()->getStringRepresentation()); #endif #endif // add metadata information jl_gen_llvm_globaldata(clone, VMap, sysimg_data, sysimg_len); // do the actual work PM.run(*clone); if (dump_as_bc) WriteBitcodeToFile(clone, FOS); delete clone; }
void lapic_dump(void) { int i; #define BOOL(a) ((a)?' ':'!') #define VEC(lvt) \ LAPIC_READ(lvt)&LAPIC_LVT_VECTOR_MASK #define DS(lvt) \ (LAPIC_READ(lvt)&LAPIC_LVT_DS_PENDING)?" SendPending" : "Idle" #define DM(lvt) \ DM_str[(LAPIC_READ(lvt)>>LAPIC_LVT_DM_SHIFT)&LAPIC_LVT_DM_MASK] #define MASK(lvt) \ BOOL(LAPIC_READ(lvt)&LAPIC_LVT_MASKED) #define TM(lvt) \ (LAPIC_READ(lvt)&LAPIC_LVT_TM_LEVEL)? "Level" : "Edge" #define IP(lvt) \ (LAPIC_READ(lvt)&LAPIC_LVT_IP_PLRITY_LOW)? "Low " : "High" kprintf("LAPIC %d at %p version 0x%x\n", (LAPIC_READ(ID)>>LAPIC_ID_SHIFT)&LAPIC_ID_MASK, (void *) lapic_start, LAPIC_READ(VERSION)&LAPIC_VERSION_MASK); kprintf("Priorities: Task 0x%x Arbitration 0x%x Processor 0x%x\n", LAPIC_READ(TPR)&LAPIC_TPR_MASK, LAPIC_READ(APR)&LAPIC_APR_MASK, LAPIC_READ(PPR)&LAPIC_PPR_MASK); kprintf("Destination Format 0x%x Logical Destination 0x%x\n", LAPIC_READ(DFR)>>LAPIC_DFR_SHIFT, LAPIC_READ(LDR)>>LAPIC_LDR_SHIFT); kprintf("%cEnabled %cFocusChecking SV 0x%x\n", BOOL(LAPIC_READ(SVR)&LAPIC_SVR_ENABLE), BOOL(!(LAPIC_READ(SVR)&LAPIC_SVR_FOCUS_OFF)), LAPIC_READ(SVR) & LAPIC_SVR_MASK); #if CONFIG_MCA if (mca_is_cmci_present()) kprintf("LVT_CMCI: Vector 0x%02x [%s] %s %cmasked\n", VEC(LVT_CMCI), DM(LVT_CMCI), DS(LVT_CMCI), MASK(LVT_CMCI)); #endif kprintf("LVT_TIMER: Vector 0x%02x %s %cmasked %s\n", VEC(LVT_TIMER), DS(LVT_TIMER), MASK(LVT_TIMER), (LAPIC_READ(LVT_TIMER)&LAPIC_LVT_PERIODIC)?"Periodic":"OneShot"); kprintf(" Initial Count: 0x%08x \n", LAPIC_READ(TIMER_INITIAL_COUNT)); kprintf(" Current Count: 0x%08x \n", LAPIC_READ(TIMER_CURRENT_COUNT)); kprintf(" Divide Config: 0x%08x \n", LAPIC_READ(TIMER_DIVIDE_CONFIG)); kprintf("LVT_PERFCNT: Vector 0x%02x [%s] %s %cmasked\n", VEC(LVT_PERFCNT), DM(LVT_PERFCNT), DS(LVT_PERFCNT), MASK(LVT_PERFCNT)); kprintf("LVT_THERMAL: Vector 0x%02x [%s] %s %cmasked\n", VEC(LVT_THERMAL), DM(LVT_THERMAL), DS(LVT_THERMAL), MASK(LVT_THERMAL)); kprintf("LVT_LINT0: Vector 0x%02x [%s][%s][%s] %s %cmasked\n", VEC(LVT_LINT0), DM(LVT_LINT0), TM(LVT_LINT0), IP(LVT_LINT0), DS(LVT_LINT0), MASK(LVT_LINT0)); kprintf("LVT_LINT1: Vector 0x%02x [%s][%s][%s] %s %cmasked\n", VEC(LVT_LINT1), DM(LVT_LINT1), TM(LVT_LINT1), IP(LVT_LINT1), DS(LVT_LINT1), MASK(LVT_LINT1)); kprintf("LVT_ERROR: Vector 0x%02x %s %cmasked\n", VEC(LVT_ERROR), DS(LVT_ERROR), MASK(LVT_ERROR)); kprintf("ESR: %08x \n", lapic_esr_read()); kprintf(" "); for(i=0xf; i>=0; i--) kprintf("%x%x%x%x",i,i,i,i); kprintf("\n"); kprintf("TMR: 0x"); for(i=7; i>=0; i--) kprintf("%08x",LAPIC_READ_OFFSET(TMR_BASE, i*0x10)); kprintf("\n"); kprintf("IRR: 0x"); for(i=7; i>=0; i--) kprintf("%08x",LAPIC_READ_OFFSET(IRR_BASE, i*0x10)); kprintf("\n"); kprintf("ISR: 0x"); for(i=7; i >= 0; i--) kprintf("%08x",LAPIC_READ_OFFSET(ISR_BASE, i*0x10)); kprintf("\n"); }
void test_strcpy_bounds_memarray_range (void) { #undef TM #define TM(mem, init, dst, src) \ do { \ struct MA ma; \ strcpy (ma.mem, init); \ strcpy (dst, src); \ sink (&ma); \ } while (0) ptrdiff_t i = SR (1, 2); TM (a5, "0", ma.a5 + i, ma.a5); TM (a5, "01", ma.a5 + i, ma.a5); TM (a5, "012", ma.a5 + i, ma.a5); TM (a5, "0123", ma.a5 + i, ma.a5); /* { dg-warning "offset 10 from the object at .ma. is out of the bounds of referenced subobject .\(MA::\)?a5. with type .char ?\\\[5]. at offset 4" "strcpy" } */ TM (a11, "0", ma.a5, ma.a11); TM (a11, "01", ma.a5, ma.a11); TM (a11, "012", ma.a5, ma.a11); TM (a11, "0123", ma.a5, ma.a11); TM (a11, "01234", ma.a5, ma.a11); /* { dg-warning "offset 10 from the object at .ma. is out of the bounds of referenced subobject .\(MA::\)?a5. with type .char ?\\\[5]' at offset 4" } */ TM (a11, "012345", ma.a5, ma.a11); /* { dg-warning "offset \\\[10, 11] from the object at .ma. is out of the bounds of referenced subobject .\(MA::\)?a5. with type .char ?\\\[5]' at offset 4" } */ TM (a11, "0123456", ma.a5, ma.a11); /* { dg-warning "offset \\\[10, 12] from the object at .ma. is out of the bounds of referenced subobject .\(MA::\)?a5. with type .char ?\\\[5]' at offset 4" } */ TM (a11, "0123456", ma.a11 + i, "789abcd"); }
bool NameFilter(const std::string & aSubD,cInterfChantierNameManipulateur * aICNM,const cNameFilter & aFilter,const std::string & aName) { std::string anEntete = aICNM->Dir()+ aSubD; std::string aFullName = anEntete + aName; int aSz = aFilter.SizeMinFile().Val(); if (aSz>=0) { if (sizeofile(aFullName.c_str()) < aSz) return false; } if ((aFilter.Min().IsInit())&&(aFilter.Min().Val()>aName)) return false; if ((aFilter.Max().IsInit())&&(aFilter.Max().Val()<aName)) return false; const std::list<Pt2drSubst> & aLFoc = aFilter.FocMm(); if (! aLFoc.empty()) { if (!IsInIntervalle(aLFoc,GetFocalMmDefined(aFullName),true)) { return false; } } for ( std::list<cKeyExistingFile>::const_iterator itKEF=aFilter.KeyExistingFile().begin(); itKEF!=aFilter.KeyExistingFile().end(); itKEF++ ) { bool OKGlob = itKEF->RequireForAll(); for ( std::list<std::string>::const_iterator itKA=itKEF->KeyAssoc().begin(); itKA!=itKEF->KeyAssoc().end(); itKA++ ) { std::string aNameF = anEntete + aICNM->Assoc1To1(*itKA,aName,true); bool fExists = ELISE_fp::exist_file(aNameF); // std::cout << "KEY-NF " << aNameF << "\n"; bool Ok = itKEF->RequireExist() ? fExists : (!fExists); if (itKEF->RequireForAll()) OKGlob = OKGlob && Ok; else OKGlob = OKGlob || Ok; } //std::cout << "KEY-NF " << aName << " " << OKGlob << "\n"; if (!OKGlob) return false; } if (aFilter.KeyLocalisation().IsInit()) { const cFilterLocalisation & aKLoc = aFilter.KeyLocalisation().Val(); std::string aNameCam = anEntete + aICNM->Assoc1To1(aKLoc.KeyAssocOrient(),aName,true); ElCamera * aCam = Cam_Gen_From_File(aNameCam,"OrientationConique",aICNM); Im2D_Bits<1> * aMasq = GetImRemanenteFromFile<Im2D_Bits<1> > (anEntete+ aKLoc.NameMasq()); TIm2DBits<1> TM(*aMasq); cFileOriMnt * anOri = RemanentStdGetObjFromFile<cFileOriMnt> ( anEntete+aKLoc.NameMTDMasq(), StdGetFileXMLSpec("ParamChantierPhotogram.xml"), "FileOriMnt", "FileOriMnt" ); // std::cout << "ADR MASQ " << aMasq << " " << anOri << "\n"; Pt3dr aPMnt = FromMnt(*anOri,aCam->OrigineProf()); Pt2di aP(round_ni(aPMnt.x),round_ni(aPMnt.y)); return ( TM.get(aP,0)==0 ); } return true; }
TuringPtr generateTM(const std::string& fileName) { TiXmlDocument doc; std::string path = DATADIR + fileName; if(!doc.LoadFile(path.c_str())) { throw std::runtime_error("Error generating TM from XML: File not found!"); } TiXmlElement* root = doc.FirstChildElement(); std::string rootName; if(root == NULL) { throw std::runtime_error("Error generating TM from XML: Failed to load file: No root element."); doc.Clear(); } rootName = root->Value(); if (rootName != "TM") { throw std::runtime_error("Error generating TM from XML: Not a Turing Machine XML file!"); } std::set<char> alphabet; std::set<char> tapeAlphabet; bool allFound = false; char blank = 0; for(TiXmlElement* elem = root->FirstChildElement(); elem != NULL; elem = elem->NextSiblingElement()) { //find alphabets and blank symbol std::string elemName = elem->Value(); if (elemName == "InputAlphabet") { for(TiXmlElement* elemOfSigma = elem->FirstChildElement(); elemOfSigma != NULL; elemOfSigma = elemOfSigma->NextSiblingElement()) { std::string elemOfSigmaName = elemOfSigma->Value(); if (elemOfSigmaName == "symbol") { TiXmlNode* e = elemOfSigma->FirstChild(); TiXmlText* text = e->ToText(); if(text == NULL) continue; std::string t = text->Value(); if (t.size() != 1) throw std::runtime_error("Error generating TM from XML: One input symbol per node please"); alphabet.insert(t.front()); } } } if (elemName == "TapeAlphabet") { for(TiXmlElement* elemOfGamma = elem->FirstChildElement(); elemOfGamma != NULL; elemOfGamma = elemOfGamma->NextSiblingElement()) { std::string elemOfGammaName = elemOfGamma->Value(); if (elemOfGammaName == "symbol") { TiXmlNode* e = elemOfGamma->FirstChild(); TiXmlText* text = e->ToText(); if(text == NULL) continue; std::string t = text->Value(); if (t.size() != 1) { throw std::runtime_error("Error generating TM from XML: One input symbol per node please"); } tapeAlphabet.insert(t.front()); } } } if (elemName == "Blank") { TiXmlNode* e = elem->FirstChild(); TiXmlText* text = e->ToText(); if(text == NULL) continue; std::string t = text->Value(); if (t.size() != 1) { throw std::runtime_error("Error generating TM from XML: One blank symbol please"); } blank = t.front(); } if (tapeAlphabet.size() && alphabet.size() && blank) { //All arguments necessary to construct TM found allFound = true; break; } } if (!allFound) { throw std::runtime_error("Error generating TM from XML: Alphabet, tape alphabet or blank symbol missing!"); return nullptr; } TuringPtr TM(new TuringMachine(alphabet, tapeAlphabet, blank)); for(TiXmlElement* elem = root->FirstChildElement(); elem != NULL; elem = elem->NextSiblingElement()) { //find alphabets and blank symbol std::string elemName = elem->Value(); if (elemName == "States") { const char* attr = elem->Attribute("storage"); bool hasStorage = false; std::vector<std::vector<char>> storages; if (attr) { std::string statesAttr(attr); if (statesAttr == "true") hasStorage = true; } if (hasStorage) { for(TiXmlElement* elemOfQ = elem->FirstChildElement(); elemOfQ != NULL; elemOfQ = elemOfQ->NextSiblingElement()) { std::string elemOfQName = elemOfQ->Value(); if (elemOfQName == "storage") { if (elemOfQ->FirstChild() == NULL) { storages.push_back(std::vector<char> ()); continue; } TiXmlNode* e = elemOfQ->FirstChild(); TiXmlText* text = e->ToText(); if(text == NULL) continue; std::string t = text->Value(); std::vector<char> thisStorage; for (auto i : t) thisStorage.push_back(i); storages.push_back(thisStorage); } } } for(TiXmlElement* elemOfQ = elem->FirstChildElement(); elemOfQ != NULL; elemOfQ = elemOfQ->NextSiblingElement()) { bool isStarting = false; bool isAccepting = false; std::string elemOfQName = elemOfQ->Value(); if (elemOfQName == "state") { const char* attr = elemOfQ->Attribute("start"); if (attr) { std::string stateAttr(attr); if (stateAttr == "true") isStarting = true; } attr = elemOfQ->Attribute("accept"); if (attr) { std::string stateAttr(attr); if (stateAttr == "true") isAccepting = true; } if (elemOfQ->FirstChild() == NULL) { throw std::runtime_error("Error generating TM from XML: State without name"); } TiXmlNode* e = elemOfQ->FirstChild(); TiXmlText* text = e->ToText(); if(text == NULL) continue; std::string t = text->Value(); if (!hasStorage) TM->addState(t, isStarting, isAccepting); else for (auto i : storages) TM->addState(t, isStarting, isAccepting, i); } } } if (elemName == "Transitions") { for(TiXmlElement* elemOfDelta = elem->FirstChildElement(); elemOfDelta != NULL; elemOfDelta = elemOfDelta->NextSiblingElement()) { std::string elemOfDeltaName = elemOfDelta->Value(); if (elemOfDeltaName == "transition") { std::string from = ""; std::string to = ""; std::vector<char> fromStorage; std::vector<char> toStorage; std::vector<char> read; std::vector<char> write; Direction dir = U; for(TiXmlElement* elemOfTransition = elemOfDelta->FirstChildElement(); elemOfTransition != NULL; elemOfTransition = elemOfTransition->NextSiblingElement()) { std::string elemOfTransitionName = elemOfTransition->Value(); if (elemOfTransitionName == "from") { if (elemOfTransition->FirstChild() == NULL) { continue; } TiXmlNode* e = elemOfTransition->FirstChild(); TiXmlText* text = e->ToText(); if(text == NULL) continue; std::string t = text->Value(); from = t; } if (elemOfTransitionName == "to") { if (elemOfTransition->FirstChild() == NULL) { continue; } TiXmlNode* e = elemOfTransition->FirstChild(); TiXmlText* text = e->ToText(); if(text == NULL) continue; std::string t = text->Value(); to = t; } if (elemOfTransitionName == "fromStorage") { if (elemOfTransition->FirstChild() == NULL) { continue; } TiXmlNode* e = elemOfTransition->FirstChild(); TiXmlText* text = e->ToText(); if(text == NULL) continue; std::string t = text->Value(); for (auto i : t) fromStorage.push_back(i); } if (elemOfTransitionName == "toStorage") { if (elemOfTransition->FirstChild() == NULL) { continue; } TiXmlNode* e = elemOfTransition->FirstChild(); TiXmlText* text = e->ToText(); if(text == NULL) continue; std::string t = text->Value(); for (auto i : t) toStorage.push_back(i); } if (elemOfTransitionName == "read") { if (elemOfTransition->FirstChild() == NULL) { continue; } TiXmlNode* e = elemOfTransition->FirstChild(); TiXmlText* text = e->ToText(); if(text == NULL) continue; std::string t = text->Value(); for (auto i : t) read.push_back(i); } if (elemOfTransitionName == "write") { if (elemOfTransition->FirstChild() == NULL) { continue; } TiXmlNode* e = elemOfTransition->FirstChild(); TiXmlText* text = e->ToText(); if(text == NULL) continue; std::string t = text->Value(); for (auto i : t) write.push_back(i); } if (elemOfTransitionName == "dir") { if (elemOfTransition->FirstChild() == NULL) { continue; } TiXmlNode* e = elemOfTransition->FirstChild(); TiXmlText* text = e->ToText(); if(text == NULL) continue; std::string t = text->Value(); if (t == "L") dir = L; else if (t == "R") dir = R; else throw std::runtime_error("Error generating TM from XML: invalid direction" ); } } if (from.size() && to.size() && read.size() && write.size() && (dir == L || dir == R)) TM->addTransition(from, to, read, write, dir, fromStorage, toStorage); else throw std::runtime_error("Error generating TM from XML: Incomplete transition"); } } } } for(TiXmlElement* elem = root->FirstChildElement(); elem != NULL; elem = elem->NextSiblingElement()) { //find alphabets and blank symbol std::string elemName = elem->Value(); if (elemName == "StartState") { std::string stateName = ""; std::vector<char> storage; for(TiXmlElement* elemOfSS = elem->FirstChildElement(); elemOfSS != NULL; elemOfSS = elemOfSS->NextSiblingElement()) { std::string elemOfSSName = elemOfSS->Value(); if (elemOfSSName == "name") { if (elemOfSS->FirstChild() == NULL) { continue; } TiXmlNode* e = elemOfSS->FirstChild(); TiXmlText* text = e->ToText(); if(text == NULL) continue; stateName = text->Value(); } if (elemOfSSName == "storage") { if (elemOfSS->FirstChild() == NULL) { continue; } TiXmlNode* e = elemOfSS->FirstChild(); TiXmlText* text = e->ToText(); if(text == NULL) continue; std::string t = text->Value(); for (auto i : t) storage.push_back(i); } } if (stateName.size() != 0) { if (storage.size() == 0) TM->indicateStartState(stateName); else TM->indicateStartState(stateName, storage); } else throw std::runtime_error("Error generating TM from XML: No name for start state specified"); } if (elemName == "AcceptingStates") { for(TiXmlElement* elemOfAccepting = elem->FirstChildElement(); elemOfAccepting != NULL; elemOfAccepting = elemOfAccepting->NextSiblingElement()) { std::string elemOfAcceptingName = elemOfAccepting->Value(); if (elemOfAcceptingName == "state") { std::string stateName = ""; std::vector<char> storage; for(TiXmlElement* elemOfAccState = elemOfAccepting->FirstChildElement(); elemOfAccState != NULL; elemOfAccState = elemOfAccState->NextSiblingElement()) { std::string elemOfAccStateName = elemOfAccState->Value(); if (elemOfAccStateName == "name") { if (elemOfAccState->FirstChild() == NULL) { continue; } TiXmlNode* e = elemOfAccState->FirstChild(); TiXmlText* text = e->ToText(); if(text == NULL) continue; stateName = text->Value(); } if (elemOfAccStateName == "storage") { if (elemOfAccState->FirstChild() == NULL) { continue; } TiXmlNode* e = elemOfAccState->FirstChild(); TiXmlText* text = e->ToText(); if(text == NULL) continue; std::string t = text->Value(); for (auto i : t) storage.push_back(i); } } if (stateName.size() != 0) { if (storage.size() == 0) TM->indicateAcceptingState(stateName); else TM->indicateAcceptingState(stateName, storage); } else throw std::runtime_error("Error generating TM from XML: No name for accepting state specified"); } } } } return TM; }
int tmo_ashikhmin02(pfs::Array2D* Y, pfs::Array2D* L, float maxLum, float minLum, float avLum, bool simple_flag, float lc_value, int eq) { assert(Y != NULL); assert(L != NULL); int nrows = Y->getRows(); // image size int ncols = Y->getCols(); assert(nrows == L->getRows() && ncols == L->getCols()); int im_size = nrows * ncols; // maxLum /= avLum; // normalize maximum luminance by average luminance // apply ToneMapping function only if (simple_flag) { for (int y = 0; y < nrows; y++) for (int x = 0; x < ncols; x++) { (*L)(x, y) = TM((*Y)(x, y), maxLum, minLum); //!! FIX: // to keep output values in range 0.01 - 1 // (*L)(x,y) /= 100.0f; } Normalize(L, nrows, ncols); return 0; } // applying the full functions.... GaussianPyramid *myPyramid = new GaussianPyramid(Y, nrows, ncols); // LAL calculation pfs::Array2D* la = new pfs::Array2DImpl(ncols, nrows); for (int y = 0; y < nrows; y++) { for (int x = 0; x < ncols; x++) { (*la)(x, y) = LAL(myPyramid, x, y, lc_value); if ((*la)(x, y) == 0.0) (*la)(x, y) == EPSILON; } } delete(myPyramid); // TM function pfs::Array2D* tm = new pfs::Array2DImpl(ncols, nrows); for (int y = 0; y < nrows; y++) for (int x = 0; x < ncols; x++) (*tm)(x, y) = TM((*la)(x, y), maxLum, minLum); // final computation for each pixel for (int y = 0; y < nrows; y++) for (int x = 0; x < ncols; x++) { switch (eq) { case 2: (*L)(x, y) = (*Y)(x, y) * (*tm)(x, y) / (*la)(x, y); break; case 4: (*L)(x, y) = (*tm)(x, y) + C((*tm)(x, y)) / C((*la)(x, y)) * ((*Y)(x, y) - (*la)(x, y)); break; default: exit(0); } //!! FIX: // to keep output values in range 0.01 - 1 //(*L)(x,y) /= 100.0f; } Normalize(L, nrows, ncols); // cleaning delete(la); delete(tm); return 0; }
void test_strcpy_bounds_memarray_var (struct MA *pma, struct MA2 *pma2, struct MA3 *pma3, const char *s, size_t n) { #undef TM #define TM(dst, src) do { \ strcpy (dst, src); \ sink (dst, src); \ } while (0) TM (pma->a5, s); TM (pma->a5 + 0, s); TM (pma->a5 + 1, s); TM (pma->a5 + 4, s); /* The following forms a pointer during the call that's outside the bounds of the array it was derived from (pma->a5) so it should be diagnosed but the representation of the pointer addition doesn't contain information to distinguish it from the valid pma->a11 + 1 so this is an XFAIL. */ TM (pma->a5 + 5, s); /* { dg-warning "offset 17 from the object at .pma. is out of the bounds of .struct MA." "strcpy" { xfail *-*-* } } */ /* The following also forms an out-of-bounds pointer but similar to the above, there is no reliable way to distinguish it from (char*)&pma[1].i + 1 so this too is not diagnosed. */ TM (pma->a5 + sizeof *pma + 1, s); /* { dg-warning "offset 17 from the object at .pma. is out of the bounds of .struct MA." "strcpy" { xfail *-*-* } } */ TM (pma->a5 - 1, s); /* { dg-warning "offset -1 from the object at .pma. is out of the bounds of .struct MA." "strcpy" { xfail *-*-* } } */ TM (pma[1].a5, s); TM (pma[2].a5 + 0, s); TM (pma[3].a5 + 1, s); TM (pma[4].a5 + 4, s); extern struct MA3 ma3[3]; TM (ma3[0].ma5[0].ma3[0].a5 + 6, s); }
/****************************************************************************** * * * char *mallocHeap(BYTE *pHeap, UINT size) * * * ******************************************************************************* * * Allocates a block of memory within the Linice internal heap. * * Where: * pHeap is the requested heap * size is the requested size of the memory block * * Returns: * Pointer to newly allocated block * NULL - memory could not be allocated * ******************************************************************************/ char *mallocHeap(BYTE *pHeap, UINT size) { Tmalloc *pLast; Tmalloc *pNew; // This should really be some sort of assert... if( pHeap == NULL ) return(NULL); // If the requested size is 0, do nothing. if( (size == 0) ) return NULL; // Set the size to be a multiple of 4 to keep the allignemnt // Also, add the size of the header to be allocated size = ((size+3) & 0xFFFFFFFC) + HEADER_SIZE; // Debug allocations: //dprinth(1, "malloc(%d)", size); // Traverse the free list and find the first block large enough for the // block of the requested size pLast = TM(pHeap); pNew = TM(pLast->next); // This effectively implements the first-fit memory allocation strategy while( (pNew != NULL) && (pNew->size < size )) { pLast = pNew; pNew = TM(pNew->next); } // Check if we could not find the block large enough and are at the end of // the list if( pNew==NULL ) return( NULL ); // A free memory block that was found is large enough for the request, but // maybe too large, so we may want to split it: // Two things can happen now: either we will link another free block // at the end of the allocated one, or not. Linking another block will // increase fragmentation so we will do it only if the space that remains // is larger or equal to 16 bytes (arbitrary value) if( pNew->size >= size+16+HEADER_SIZE ) { // Link in another free block pLast->next = (struct Tmalloc*)((int)pNew + size); pLast = TM(pLast->next); // Point to the new free block pLast->next = pNew->next; // Link in the next free block pLast->size = pNew->size - size; pNew->size = size; // Set the allocated block size pNew->next = STM(MALLOC_COOKIE); // And the debug cookie return (void*)((int)pNew + HEADER_SIZE); } else { // There was not enough free space to link in new free node, so just // allocate the whole block. pLast->next = pNew->next; // Skip the newly found space // pNew->size is all the size of a block. No need to change. pNew->next = STM(MALLOC_COOKIE); // Set the debug cookie return (void*)((int)pNew + HEADER_SIZE); } }
/****************************************************************************** * * * void freeHeap(BYTE *pHeap, void *mPtr ) * * * ******************************************************************************* * * Frees the memory that was allocated using mallocHeap() from the internal * memory allocation pool (heap). * * The pointer mPtr can be NULL. * * Where: * pHeap is the requested heap * pMem - pointer to a memory block to be freed * ******************************************************************************/ void freeHeap(BYTE *pHeap, void *mPtr ) { Tmalloc *pLast; Tmalloc *pMem; Tmalloc *pMalloc; // Return if pointer is NULL (should not happen) if( mPtr==NULL ) return; // Get the allocation structure pMalloc = (Tmalloc*)((int)mPtr - HEADER_SIZE); // Check for the magic number to ensure that the right block was passed if( (int)pMalloc->next != MALLOC_COOKIE ) { // Should print some error message in the future //printf(" *** ERROR - Magic Number Wrong: %08X ***\n",(int)pMalloc->next ); return; } // Now we have to return the block to the list of free blocks, so find the // place in the list to insert it. The free list is ordered by the address // of the blocks that it holds pLast = TM(pHeap); pMem = TM(pLast->next); // Traverse the free list and find where the new block should be inserted while( (pMem != NULL) && (pMem < pMalloc) ) { pLast = pMem; pMem = TM(pMem->next); } // If pMem is NULL, the block to be freed lies after the last node in the // free list, so link it at the end. if( pMem == NULL ) { pLast->next = STM(pMalloc); // Last node in the free list pMalloc->next = NULL; // Terminate it // The new, last free block may be merged with the preceeding one if( (int)pLast + pLast->size == (int)pMalloc ) { pLast->size += pMalloc->size; pLast->next = NULL; } return; } // Now pLast points to the last node before pMalloc, and pMem points to // the next node. They just have to be linked now. pLast->next = STM(pMalloc); pMalloc->next = STM(pMem); // If pMem node is immediately after pMalloc, they will be merged. if( (int)pMalloc + pMalloc->size == (int)pMem ) { // Merge new node and the successor pointed by pMem pMalloc->size += pMem->size; pMalloc->next = pMem->next; } // If the newly freed node is after another free node, merge them if( (int)pLast + pLast->size == (int)pMalloc ) { pLast->size += pMalloc->size; pLast->next = pMalloc->next; } return; }