Chapter8MemoryMemoryLayoutLOCAL_MEM_LOCAL_ADRSRAM_LOW-ADRSFREE_RAM_ADRSVxWorksSYSTEMMEMORYPOOLsysMemTop()WDB_POOL_SIZEUSER_RESERVED_MEMsysPhysMemTop()TargetServerMemoryPoolApoolofmemoryonthetargetreservedforusebytheTornadotools:Dynamicloadingofobjectmodules.SpawningtaskfromWindShorCrossWind.CreationofvariablesfromWindSh.TheinitialsizeofthetargetservermemorypoolisconfiguredbyWDB_POOL_SIZE.Thedefaultis1/16ofsysMemTop()-FREE_RAM_ADRS.Thetargetservermanagesthepool,keepingoverheadsuchasblocklistsonthehost.Additionalmemoryissilentlyallocatedfromthesystemmemorypool,ifneeded.SystemMemoryPoolInitializedatsystemstartup.Usedfordynamicmemoryallocationinprograms:malloc().Creatingtasks(stackandTCB).CanmodifyUSER_RESERVED_MEMtoreservememoryforapplication-specificuse.MayneedtomodifysysPhysMemTop()(orjustLOCAL_MEM_SIZE)whenaddingmemorytoyourboard.Toaddoffboardmemory:voidmemAddToPool(pPool,poolSize)pPoolmustbethelocaladdressofthememory.VxWorksmemoryrequests.Allocating/ReleasingMemoryUsesfirst-fitalgorithm.Todynamicallyallocatememory:void*malloc(nBytes)ReturnsapointertothenewlyallocatedmemoryorNULLonerror.Freememoryisstoredinalinkedlist.Some(small)overheadforeachmalloc().Toreleaseallocatedmemory:voidfree(ptr)Adjacentblocksarecoalesced.DebuggingOptionsDefaultfree()debugging:Defaultmalloc()debugging:Ifrequesttoolarge,loganerrormessage.Checkblockforconsistency.Ifcorrupted:suspendtask,logerrormessage.Canchangedefaultdebuggingoptionswith:voidmemOptionsSet(options)Optionscanbe:+MEM_ALLOC_ERROR_LOG_FLAG-MEM_ALLOC_ERROR_SUSPEND_FLAG+MEM_BLOCK_CHECK+MEM_BLOCK_ERROR_LOG_FLAG+MEM_BLOCK_ERROR_SUSPEND_FLAGExaminingMemoryEnterthememorypartitionIDintheShowbox.Usethebrowser.FreeListmv152-external@mekong:MempartOxTotalbytes=3870840Allocatedblocks=86bytes=467224Freeblocks=7bytes=3403584Cummulativeblocks=88bytes=467832FreeList0addr=0x3fd8f8size=96041addr=0x4ef90size=3355432AdditionalSystemMemoryManagementRoutinesvoid*calloc(nElems,size)Allocatezeroedmemoryforanarray.void*realloc(ptr,newSize)Resizeanallocatedblock.Theblockmaybemoved.intmemFindMax()Returnsthesizeofthelargestfreeblockinsystemmemory.FineTuningForfast,deterministicallocationoffixedsizebuffers,usemessagequeuesinsteadofmalloc().messagequeueptrptrptr....bufGet()bufReturn()....buffersGenericPartitionManagerVxWorksprovideslowlevelroutinestocreateandmanipulatealternatememorypools.Highlevelroutineslikemalloc()andfree()calltheselowerlevelroutines,specifyingthesystemmemorypool.Applicationmayusealternatememorypartitionstoreducefragmentation.Applicationsmayusealternatememorypartitionstomanagememorywithdifferentproperties.CreatingamemoryPartitionReturnsapartitionid(PART_ID)orNULLorerror.Thememoryforthispartition(pPool)maybetakenfrom:PART_IDmemPartCreate(pPool,size)pPoolPointertomemoryforthispartitionsizeSizeofmemorypartitioninbytes.Aseparatememoryboard.Ablockallocatedfromthesystemmemorypartition.ThetopoftheCPUboard’sRAM.ManagingMemoryPartitionsSystempartitionmanagementroutinescallroutineslistedbelow,specifyingthePART_IDasmemSysPartId.GenericSystemMemoryPoolmemPartAlloc()malloc()memPartFree()free()memPartShow()memShow()memPartAddToPool()memAddToPool()memPartOptionsSet()memOptionsSet()memPartRealloc()realloc()memPartFindMax()memFindMax()ExampleCreatingaMemoryPartition--partId=memPartCreate(pMemory,100000)newsymbol“partId”addedtosymbltable.partId=0x23ff318:value=37745448=0x23ff328=partId+0x10--ptr=memPartAlloc(partId,200)newsymbol“ptr”addedtosymbltable.ptr=0x23ff2ec:value=37652632=0x23e8898--showpartIdstatusbytesblocksaveblockmaxblockcurrentfree9977619977699776alloc2081208-cumulativealloc2081208-SummaryToconfigurethesystemmemorypool:StandardCroutinesusedfordynamicmemoryallocation.ModifysysPhysMemTop()ModifyUSER_RESERVED_RAMModifymemAddToPool()Forfast,deterministicallocationoffixedsizebuffers,usemessagequeuesinsteadofmalloc().Createseparatememorypartitionforoff-boardmemory,ortohelpreducefragmentation.Chapter9Exceptions,InterruptsandTimersExceptions,InterruptsandTimersExceptionHandlingandSignalsInterruptServiceRoutinesTimers(Signals)•VxWorksISR••••Wind–UNIXBSD–POSIX(Signals)•31sigvec()sigaction()intConnect()(ISR)kill()sigsetmask()sigblocksigprocmask()•sigInit()..........N/Asigpending()pause()sigsuspend()sigvec()sigaction()N/Araise()kill()kill()signal()signal()UNIXBSDPOSIX1003.1bintsigvec(intsig,/**/conststructsigvec*pVec,/**/structsigvec*pOvec/**/)intkill(inttid,/**/intsigno/**/)sigqueuekill()(inttid,/**/intsigno,/**/constunionsigvalvalue/**/)SignalsnormalCode(){......}mySignalHandler(){......}mySignalUNIX:UNIXvs.VxWorksSIgnalsSignalisignoredifnohandlerisinstalled‘‘Automaticfunctionrestarting‘‘CaninstallahandlertocatchSIGKILL.NoSIGCHLD,SIGPIPE,orSIGURG.taskDelay()setserrno=EINTRandreturnsERRORifinterruptedbyasignal.PendQReadyQSignalRunsignal-handlersemTake(....OrgDelay)CaveatsSIgLibcontainsbothPOSIXandBSDUNIXinterfaces.Donotmixthem.Signalsarenotrecommendedforgeneralintertaskcommunication.Asignal:Maybehandledattoohighapriorityifitarrivesduringapriorityinheritence.Disruptsatask’snormalexecution