Advertisement
twixraider81

MemoryMap Code

Dec 2nd, 2013
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 1.41 KB | None | 0 0
  1. struct Tag
  2. {
  3.     public uint tagType;
  4.     public uint tagSize;
  5. };
  6.  
  7. struct TagMemoryMap
  8. {
  9.     public uint tagType;
  10.     public uint tagSize;
  11.     public uint entrySize;
  12.     public uint entryVersion;
  13.  
  14.     public struct Entry
  15.     {
  16.         align(1):
  17.         ulong baseAddress;
  18.         ulong length;
  19.         uint memoryType;
  20.         uint zero;
  21.     }
  22.     public Entry entries[0];
  23. };
  24.  
  25. @property
  26. {
  27.     public static TagMemoryMap* memoryMap()
  28.     {
  29.         TagMemoryMap* memMap = getTag!( TagMemoryMap* )( tagType.MEMMAP );
  30.  
  31.         ulong entryAddr = cast(ulong)(memMap + memMap.tagType.sizeof + memMap.tagSize.sizeof + memMap.entrySize.sizeof + memMap.entryVersion.sizeof);
  32.         ulong entrylimit = cast(ulong)(memMap + memMap.tagSize);
  33.         TagMemoryMap.Entry* mmapEntries;
  34.    
  35.         while( entryAddr < entrylimit ) {
  36.             TagMemoryMap.Entry* entry = cast(TagMemoryMap.Entry*) entryAddr;
  37.  
  38.             if( entry.memoryType < memoryType.UNKNOWN ) {
  39.                 Trace.printf( " * * %s memory: %x; length: %d\n", memoryTypeNames[entry.memoryType], entry.baseAddress, entry.length );
  40.             }
  41.  
  42.             entryAddr += memMap.entrySize;
  43.         }
  44.  
  45.         return memMap;
  46.     }
  47. }
  48.  
  49. private static T getTag( T )( uint tType )
  50. {
  51.     if( tType <= tagType.TERMINATOR || tType > tagType.NETWORK ) {
  52.         return null;
  53.     }
  54.  
  55.  
  56.     for( Tag* tag = cast(Tag*)(infoAddr + 8); tag.tagType != tagType.TERMINATOR; tag = cast(Tag*)( (cast(ubyte*)tag) + ((tag.tagSize + 7) & ~7))) {
  57.         if( tType == tag.tagType ) {
  58.             return cast(T)(tag);
  59.         }
  60.     }
  61.  
  62.     return null;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement