Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; ########################################## Dokumentation ##########################################
  2. ;
  3. ;
  4. ;
  5. ;
  6. ;
  7. ;
  8. ;
  9. ; ########################################## Variablen ##########################################
  10.  
  11.  
  12.  
  13. Structure Main
  14.   Quit.a
  15. EndStructure
  16. Global Main.Main
  17.  
  18. Structure Window_Main
  19.   ID.i
  20.   Width.u
  21.   Height.u
  22.   Bytes_Per_Pixel.d
  23.   Image.i
  24.   Image_ID.i
  25. EndStructure
  26. Global Window_Main.Window_Main
  27.  
  28. Structure Heap_Entry
  29.   Type.a              ; 0:Allocated-Block 1:Region 2:Uncommited-Range 3:Block
  30.   *Pointer
  31.   Size.l
  32.   Region_Index.a
  33. EndStructure
  34. Global NewList Heap_Entry.Heap_Entry()
  35.  
  36. Structure PROCESS_HEAP_ENTRY_BLOCK
  37.   *hMem
  38.   dwReserved.l  [3]
  39. EndStructure
  40. Structure PROCESS_HEAP_ENTRY_REGION
  41.   dwCommittedSize.l
  42.   dwUnCommittedSize.l
  43.   *lpFirstBlock
  44.   *lpLastBlock
  45. EndStructure
  46. Structure PROCESS_HEAP_ENTRY
  47.   *lpData
  48.   cbData.l
  49.   cbOverhead.a
  50.   iRegionIndex.a
  51.   wFlags.w
  52.   StructureUnion
  53.     Block.PROCESS_HEAP_ENTRY_BLOCK
  54.     Region.PROCESS_HEAP_ENTRY_REGION
  55.   EndStructureUnion
  56. EndStructure
  57.  
  58. Global NewList Temp()
  59.  
  60. ; ########################################## Declares ############################################
  61.  
  62. ; ########################################## Ladekram ############################################
  63.  
  64. ; ########################################## Macros ##############################################
  65.  
  66. Macro Get_X(Position, Width)
  67.   ((Position) % Width)
  68. EndMacro
  69.  
  70. Macro Get_Y(Position, Width)
  71.   ((Position) / Width)
  72. EndMacro
  73.  
  74. ; ########################################## Proceduren ##########################################
  75.  
  76. Procedure Heap_Walk()
  77.   Protected Entry.PROCESS_HEAP_ENTRY
  78.  
  79.   Protected Heaps = GetProcessHeaps_(0, 0)
  80.   If Heaps = 0
  81.     ProcedureReturn GetLastError_()
  82.   EndIf
  83.  
  84.   ClearList(Heap_Entry())
  85.  
  86.   Protected *Heaps_Array = AllocateMemory(Heaps*4)
  87.  
  88.   Protected Heaps_Temp = Heaps
  89.  
  90.   Heaps = GetProcessHeaps_(Heaps_Temp, *Heaps_Array)
  91.   If Heaps = 0
  92.     ProcedureReturn GetLastError_()
  93.   ElseIf Heaps > Heaps_Temp
  94.     ProcedureReturn -1
  95.   EndIf
  96.  
  97.   For i = 0 To Heaps-1
  98.     Protected Heap = PeekL(*Heaps_Array+i*4)
  99.    
  100.     If HeapLock_(Heap) = #False
  101.       ProcedureReturn GetLastError_()
  102.     EndIf
  103.    
  104.     Entry\lpData = #Null
  105.     While HeapWalk_(Heap, @Entry)
  106.       AddElement(Heap_Entry())
  107.      
  108.       If Entry\wFlags & #PROCESS_HEAP_ENTRY_BUSY
  109.         Heap_Entry()\Type = 0
  110.         ;PrintN("Allocated block")
  111.         If Entry\wFlags & #PROCESS_HEAP_ENTRY_MOVEABLE
  112.           ;PrintN("movable with HANDLE "+Str(Entry\Block\hMem))
  113.         EndIf
  114.         If Entry\wFlags & #PROCESS_HEAP_ENTRY_DDESHARE
  115.           ;PrintN("DDESHARE")
  116.         EndIf
  117.       ElseIf Entry\wFlags & #PROCESS_HEAP_REGION
  118.         Heap_Entry()\Type = 1
  119.         ;PrintN("Region")
  120.         ;PrintN("  "+Str(Entry\Region\dwCommittedSize)+" bytes committed")
  121.         ;PrintN("  "+Str(Entry\Region\dwUnCommittedSize)+" bytes uncommitted")
  122.         ;PrintN("  First block address: "+Str(Entry\Region\lpFirstBlock)+"")
  123.         ;PrintN("  Last block address: "+Str(Entry\Region\lpLastBlock)+"")
  124.       ElseIf Entry\wFlags & #PROCESS_HEAP_UNCOMMITTED_RANGE
  125.         Heap_Entry()\Type = 2
  126.         ;PrintN("Uncommitted range")
  127.       Else
  128.         Heap_Entry()\Type = 3
  129.         ;PrintN("Block")
  130.       EndIf
  131.      
  132.       Heap_Entry()\Pointer = Entry\lpData
  133.       Heap_Entry()\Size = Entry\cbData
  134.       Heap_Entry()\Region_Index = Entry\iRegionIndex
  135.      
  136.       ;PrintN("  Data portion begins at: "+Str(Entry\lpData))
  137.       ;PrintN("  Size: "+Str(Entry\cbData)+" bytes")
  138.       ;PrintN("  Overhead: "+Str(Entry\cbOverhead)+" bytes")
  139.       ;PrintN("  Region index: "+Str(Entry\iRegionIndex))
  140.       ;PrintN("")
  141.     Wend
  142.    
  143.     Protected LastError = GetLastError_()
  144.     If LastError <> #ERROR_NO_MORE_ITEMS
  145.       ProcedureReturn LastError
  146.     EndIf
  147.    
  148.     If HeapUnlock_(Heap) = #False
  149.       ProcedureReturn GetLastError_()
  150.     EndIf
  151.    
  152.   Next
  153.  
  154.   FreeMemory(*Heaps_Array)
  155.  
  156.   ProcedureReturn 0
  157. EndProcedure
  158.  
  159. Procedure Window_Main(Open, Width, Height)
  160.   If Open = 1 And Window_Main\ID = 0
  161.     Window_Main(0, Width, Height)
  162.   EndIf
  163.  
  164.   If Open = 1 And Window_Main\ID = 0
  165.    
  166.     Window_Main\Width  = Width
  167.     Window_Main\Height = Height
  168.     Window_Main\Bytes_Per_Pixel = (4294967296)/(Window_Main\Width*Window_Main\Height)
  169.    
  170.     Window_Main\ID = OpenWindow(#PB_Any, 0, 0, Window_Main\Width, Window_Main\Height, "Heap-Walk",  #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget)
  171.     If Window_Main\ID
  172.       Window_Main\Image_ID = CreateImage(#PB_Any, Window_Main\Width, Window_Main\Height)
  173.       Window_Main\Image = ImageGadget(#PB_Any, 0, 0, Window_Main\Width, Window_Main\Height, ImageID(Window_Main\Image_ID))
  174.     EndIf
  175.   ElseIf Open = 0 And Window_Main\ID <> 0
  176.     CloseWindow(Window_Main\ID)
  177.     FreeImage(Window_Main\Image_ID)
  178.     Window_Main\ID = 0
  179.   EndIf
  180. EndProcedure
  181.  
  182. Procedure Window_Main_Draw()
  183.   If Window_Main\ID
  184.     If StartDrawing(ImageOutput(Window_Main\Image_ID))
  185.      
  186.       Box(0, 0, Window_Main\Width, Window_Main\Height, 0)
  187.      
  188.       Allocated = 0
  189.      
  190.       ForEach Heap_Entry()
  191.         If Heap_Entry()\Type = 0
  192.           *Pointer = Heap_Entry()\Pointer
  193.           *Pointer_Start = *Pointer
  194.           Allocated + Heap_Entry()\Size
  195.           Size = Round(Heap_Entry()\Size/Window_Main\Bytes_Per_Pixel, 1)
  196.           Real_Size = Heap_Entry()\Size
  197.           While *Pointer-*Pointer_Start < Real_Size
  198.             Position = *Pointer / Window_Main\Bytes_Per_Pixel
  199.             Byte.a = PeekA(*Pointer)
  200.             Color = RGB(50,Byte,Byte)
  201.             X = Get_X(Position, Window_Main\Width)
  202.             Y = Get_Y(Position, Window_Main\Width)
  203.             Plot(X, Y, Color)
  204.             *Pointer + Window_Main\Bytes_Per_Pixel
  205.           Wend
  206.         EndIf
  207.       Next
  208.      
  209.       DrawingMode(#PB_2DDrawing_Transparent )
  210.       X = 0
  211.       Y = Window_Main\Height/2
  212.       DrawText(X, Y, "Allocated: "+StrD(Allocated/1000000,3)+"MB", RGB(255,255,255)) : Y + 15
  213.       DrawText(X, Y, StrD(Allocated*100/4294967296,1)+"%", RGB(255,255,255)) : Y + 15
  214.       DrawText(X, Y, "Allocations: "+Str(ListSize(Heap_Entry())), RGB(255,255,255)) : Y + 15
  215.      
  216.       StopDrawing()
  217.       SetGadgetState(Window_Main\Image, ImageID(Window_Main\Image_ID))
  218.     EndIf
  219.   EndIf
  220. EndProcedure
  221.  
  222. ; ####################################################### Initkram #########################################################
  223.  
  224. Window_Main(1, 800, 600)
  225.  
  226. ; ####################################################### Hauptschleife ####################################################
  227.  
  228. Repeat
  229.  
  230.   Repeat
  231.     Window_Event = WindowEvent()
  232.     Event_Window = EventWindow()
  233.     Event_Gadget = EventGadget()
  234.     Event_Type = EventType()
  235.    
  236.     If Event_Window = Window_Main\ID
  237.       Select Window_Event
  238.         Case #PB_Event_Gadget
  239.           Select EventGadget()
  240.            
  241.           EndSelect
  242.          
  243.         Case #PB_Event_SizeWindow
  244.           Window_Main\Width  = WindowWidth(Event_Window)
  245.           Window_Main\Height = WindowHeight(Event_Window)
  246.           If Window_Main\Width And Window_Main\Height
  247.             ResizeImage(Window_Main\Image_ID, Window_Main\Width, Window_Main\Height)
  248.             Window_Main\Bytes_Per_Pixel = (4294967296)/(Window_Main\Width*Window_Main\Height)
  249.           EndIf
  250.        
  251.         Case #PB_Event_CloseWindow
  252.           Main\Quit = 1
  253.          
  254.         Case 0
  255.           Break
  256.          
  257.       EndSelect
  258.     EndIf
  259.   ForEver
  260.  
  261.   Delay(10)
  262.  
  263.   Heap_Walk()
  264.   Window_Main_Draw()
  265.  
  266.   ; ##### Allocate and Free...
  267.   For i = 1 To 2
  268.     If Random(1)
  269.       AddElement(Temp())
  270.       Result = AllocateMemory(Random(100000000))
  271.       If Result
  272.         Temp() = Result
  273.       EndIf
  274.     ElseIf ListSize(Temp())
  275.       If SelectElement(Temp(), Random(ListSize(Temp())-1))
  276.         FreeMemory(Temp())
  277.         DeleteElement(Temp())
  278.       EndIf
  279.     EndIf
  280.   Next
  281.  
  282. Until Main\Quit
  283.  
  284. ; ######################################################## Ende ##############################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement