Janilabo

Janilabo | MSSL New Memory Management

Aug 5th, 2012
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 9.07 KB | None | 0 0
  1. {
  2.   type MSSL_TMMObjectKind = (mmok_Bitmap, mmok_DTM, mmok_Font);
  3.   type MSSL_TMMBitmap
  4.   type MSSL_TMMDTM
  5.   type MSSL_TMMFont
  6.   type MSSL_TMemoryManagement
  7.   function MSSL_CreateMMSet(var MM: MSSL_TMemoryManagement; name, genre, description: string): Integer;
  8.   function MSSL_AddMMObject(var MM: MSSL_TMemoryManagement; set_ID: Integer; objKind: MSSL_TMMObjectKind; name, genre, description, D: string): Integer;
  9.   function MSSL_LoadMMObject(var MM: MSSL_TMemoryManagement; set_ID: Integer; objKind: MSSL_TMMObjectKind; obj_ID: Integer): Boolean;
  10.   function MSSL_FreeMMObject(var MM: MSSL_TMemoryManagement; set_ID: Integer; objKind: MSSL_TMMObjectKind; obj_ID: Integer): Boolean;
  11.   procedure MSSL_FreeMMObjectMulti(var MM: MSSL_TMemoryManagement; set_ID: Integer; objKind: MSSL_TMMObjectKind; obj_IDs: TIntArray);
  12.   procedure MSSL_FreeMMObjects(var MM: MSSL_TMemoryManagement);
  13. }
  14.  
  15. type
  16.   MSSL_TMMObjectKind = (mmok_Bitmap, mmok_DTM, mmok_Font);
  17.   MSSL_TMMBitmap = record
  18.     name, genre, description, data: string;
  19.     obj: TSCARBitmap;                      
  20.     loaded: Boolean;
  21.   end;
  22.   MSSL_TMMDTM = record
  23.     name, genre, description, data: string;
  24.     obj: Integer;
  25.     loaded: Boolean;
  26.   end;
  27.   MSSL_TMMFont = record
  28.     name, genre, description, directory: string;
  29.     obj: Integer;
  30.     loaded: Boolean;
  31.   end;
  32.   MSSL_TMemoryManagement = record
  33.     st: array of record
  34.       bitmap: array of MSSL_TMMBitmap;
  35.       DTM: array of MSSL_TMMDTM;
  36.       font: array of MSSL_TMMFont;
  37.       name, genre, description: string;
  38.     end;
  39.   end;
  40.  
  41. function MSSL_CreateMMSet(var MM: MSSL_TMemoryManagement; name, genre, description: string): Integer;
  42. begin
  43.   Result := (High(MM.st) + 1);
  44.   SetLength(MM.st, (Result + 1));
  45.   MM.st[Result].name := name;  
  46.   MM.st[Result].genre := genre;
  47.   MM.st[Result].description := description;
  48. end;
  49.  
  50. function MSSL_AddMMObject(var MM: MSSL_TMemoryManagement; set_ID: Integer; objKind: MSSL_TMMObjectKind; name, genre, description, D: string): Integer;
  51. var
  52.   h: Integer;
  53. begin
  54.   h := High(MM.st);
  55.   if not InRange(set_ID, 0, h) or (D = '') then
  56.     Exit;
  57.   case objKind of
  58.     mmok_Bitmap:
  59.     begin
  60.       h := High(MM.st[set_ID].bitmap);
  61.       SetLength(MM.st[set_ID].bitmap, (h + 2));
  62.       MM.st[set_ID].bitmap[(h + 1)].name := name;    
  63.       MM.st[set_ID].bitmap[(h + 1)].genre := genre;  
  64.       MM.st[set_ID].bitmap[(h + 1)].description := description;
  65.       MM.st[set_ID].bitmap[(h + 1)].data := D;
  66.     end;
  67.     mmok_DTM:
  68.     begin
  69.       h := High(MM.st[set_ID].DTM);
  70.       SetLength(MM.st[set_ID].DTM, (h + 2));
  71.       MM.st[set_ID].DTM[(h + 1)].name := name;
  72.       MM.st[set_ID].DTM[(h + 1)].genre := genre;
  73.       MM.st[set_ID].DTM[(h + 1)].description := description;
  74.       MM.st[set_ID].DTM[(h + 1)].data := D;    
  75.     end;
  76.     mmok_Font:
  77.     begin
  78.       h := High(MM.st[set_ID].font);
  79.       SetLength(MM.st[set_ID].font, (h + 2));
  80.       MM.st[set_ID].font[(h + 1)].name := name;        
  81.       MM.st[set_ID].font[(h + 1)].genre := genre;
  82.       MM.st[set_ID].font[(h + 1)].description := description;
  83.       MM.st[set_ID].font[(h + 1)].directory := D;
  84.     end;
  85.   end;
  86.   Result := (h + 1);
  87. end;
  88.  
  89. function MSSL_LoadMMObject(var MM: MSSL_TMemoryManagement; set_ID: Integer; objKind: MSSL_TMMObjectKind; obj_ID: Integer): Boolean;
  90. begin
  91.   case objKind of
  92.     mmok_Bitmap:
  93.     if InRange(set_ID, 0, High(MM.st)) and InRange(obj_ID, 0, High(MM.st[set_ID].bitmap)) then
  94.       if not MM.st[set_ID].bitmap[obj_ID].loaded then
  95.       try
  96.         MM.st[set_ID].bitmap[obj_ID].obj.LoadFromStr(MM.st[set_ID].bitmap[obj_ID].data);
  97.         MM.st[set_ID].bitmap[obj_ID].loaded := True;
  98.         Result := True;
  99.       except
  100.         try
  101.           MM.st[set_ID].bitmap[obj_ID].obj.Free;
  102.         except
  103.         end;
  104.         MM.st[set_ID].bitmap[obj_ID].loaded := False;
  105.         Result := False;
  106.       end;
  107.     mmok_DTM:
  108.     if InRange(set_ID, 0, High(MM.st)) and InRange(obj_ID, 0, High(MM.st[set_ID].DTM)) then
  109.       if not MM.st[set_ID].DTM[obj_ID].loaded then
  110.       try
  111.         MM.st[set_ID].DTM[obj_ID].obj := DTMFromString(MM.st[set_ID].DTM[obj_ID].data);
  112.         MM.st[set_ID].DTM[obj_ID].loaded := True;
  113.         Result := True;
  114.       except
  115.         try
  116.           FreeDTM(MM.st[set_ID].DTM[obj_ID].obj);
  117.         except
  118.         end;
  119.         MM.st[set_ID].DTM[obj_ID].loaded := False;
  120.         Result := False;
  121.       end;    
  122.     mmok_Font:
  123.     if (InRange(set_ID, 0, High(MM.st)) and InRange(obj_ID, 0, High(MM.st[set_ID].font))) then
  124.       if not MM.st[set_ID].font[obj_ID].loaded then
  125.       try                                                  
  126.         if not DirectoryExists(MM.st[set_ID].font[obj_ID].directory) then
  127.           Exit;  
  128.         MM.st[set_ID].font[obj_ID].obj := LoadChars2(MM.st[set_ID].font[obj_ID].directory);
  129.         MM.st[set_ID].font[obj_ID].loaded := True;
  130.         Result := True;
  131.       except
  132.         try
  133.           FreeChars2(MM.st[set_ID].font[obj_ID].obj);
  134.         except
  135.         end;
  136.         MM.st[set_ID].font[obj_ID].loaded := False;
  137.         Result := False;
  138.       end;
  139.   end;
  140. end;
  141.  
  142. function MSSL_FreeMMObject(var MM: MSSL_TMemoryManagement; set_ID: Integer; objKind: MSSL_TMMObjectKind; obj_ID: Integer): Boolean;
  143. begin
  144.   case objKind of
  145.     mmok_Bitmap:
  146.     if MM.st[set_ID].bitmap[obj_ID].loaded then
  147.     try
  148.       MM.st[set_ID].bitmap[obj_ID].obj.Free;
  149.       MM.st[set_ID].bitmap[obj_ID].loaded := False;
  150.       Result := True;
  151.     except
  152.       MM.st[set_ID].bitmap[obj_ID].loaded := True;
  153.       Result := False;
  154.     end;
  155.     mmok_DTM:
  156.     if InRange(set_ID, 0, High(MM.st)) and InRange(obj_ID, 0, High(MM.st[set_ID].DTM)) then
  157.       if MM.st[set_ID].DTM[obj_ID].loaded then
  158.       try
  159.         FreeDTM(MM.st[set_ID].DTM[obj_ID].obj);
  160.         MM.st[set_ID].DTM[obj_ID].loaded := False;
  161.         Result := True;
  162.       except
  163.         MM.st[set_ID].DTM[obj_ID].loaded := True;
  164.         Result := False;
  165.       end;  
  166.     mmok_Font:
  167.     if (InRange(set_ID, 0, High(MM.st)) and InRange(obj_ID, 0, High(MM.st[set_ID].font))) then
  168.       if MM.st[set_ID].font[obj_ID].loaded then
  169.       try
  170.         FreeChars2(MM.st[set_ID].font[obj_ID].obj);
  171.         MM.st[set_ID].font[obj_ID].loaded := False;
  172.         Result := True;
  173.       except
  174.         MM.st[set_ID].font[obj_ID].loaded := True;
  175.         Result := False;
  176.       end;
  177.   end;
  178. end;
  179.  
  180. procedure MSSL_FreeMMObjectMulti(var MM: MSSL_TMemoryManagement; set_ID: Integer; objKind: MSSL_TMMObjectKind; obj_IDs: TIntArray);
  181. var
  182.   h, h2, i: Integer;
  183. begin
  184.   h2 := High(obj_IDs);
  185.   if ((h2 < 0) or not InRange(set_ID, 0, High(MM.st))) then
  186.     Exit;
  187.   case objKind of
  188.     mmok_Bitmap:
  189.     begin
  190.       h := High(MM.st[set_ID].bitmap);
  191.       if (h < 0) then
  192.         Exit;
  193.       for i := 0 to h2 do
  194.       try      
  195.         if InRange(obj_IDs[i], 0, h) then
  196.           if MM.st[set_ID].bitmap[obj_IDs[i]].loaded then
  197.           begin
  198.             MM.st[set_ID].bitmap[obj_IDs[i]].obj.Free;
  199.             MM.st[set_ID].bitmap[obj_IDs[i]].loaded := False;
  200.           end;
  201.       except
  202.         MM.st[set_ID].bitmap[obj_IDs[i]].loaded := True;
  203.       end;
  204.     end;    
  205.     mmok_DTM:
  206.     begin
  207.       h := High(MM.st[set_ID].DTM);
  208.       for i := 0 to h do
  209.       try
  210.         if MM.st[set_ID].DTM[obj_IDs[i]].loaded then
  211.         begin
  212.           FreeDTM(MM.st[set_ID].DTM[obj_IDs[i]].obj);
  213.           MM.st[set_ID].DTM[obj_IDs[i]].loaded := False;
  214.         end;
  215.       except
  216.         MM.st[set_ID].DTM[obj_IDs[i]].loaded := True;
  217.       end;
  218.     end;    
  219.     mmok_Font:
  220.     begin
  221.       h := High(MM.st[set_ID].font);
  222.       if (h < 0) then
  223.         Exit;
  224.       for i := 0 to h2 do
  225.       try      
  226.         if InRange(obj_IDs[i], 0, h) then
  227.           if MM.st[set_ID].font[obj_IDs[i]].loaded then
  228.           begin
  229.             FreeChars2(MM.st[set_ID].font[obj_IDs[i]].obj);
  230.             MM.st[set_ID].font[obj_IDs[i]].loaded := False;
  231.           end;
  232.       except
  233.         MM.st[set_ID].font[obj_IDs[i]].loaded := True;
  234.       end;
  235.     end;
  236.   end;  
  237. end;
  238.  
  239. procedure MSSL_FreeMMObjects(var MM: MSSL_TMemoryManagement);
  240. var
  241.   set_ID, h, h2, i, i2: Integer;
  242.   tmpOK: array of MSSL_TMMObjectKind;
  243.   tmpWM: TStrArray;
  244. begin
  245.   h2 := High(MM.st);
  246.   if (h2 < 0) then
  247.     Exit;  
  248.   tmpOK := [mmok_Bitmap, mmok_DTM, mmok_Font];
  249.   for set_ID := 0 to h2 do
  250.   begin
  251.     tmpWM := ['Bitmap "' + MM.st[set_ID].bitmap[i].name + '" (set[' + IntToStr(set_ID) + '] - bitmap[' + IntToStr(i) + '])',
  252.               'DTM "' + MM.st[set_ID].DTM[i].name + '" (set[' + IntToStr(set_ID) + '] - DTM[' + IntToStr(i) + '])',
  253.               'Font "' + MM.st[set_ID].font[i].name + '" (set[' + IntToStr(set_ID) + '] - font[' + IntToStr(i) + '])'];
  254.     for i2 := 0 to 2 do
  255.     begin
  256.       case tmpOK of
  257.         mmok_Bitmap: h := High(MM.st[set_ID].bitmap);
  258.         mmok_DTM: h := High(MM.st[set_ID].DTM);
  259.         mmok_Font: h := High(MM.st[set_ID].font);
  260.       end;
  261.       for i := 0 to h do
  262.         if not MSSL_FreeMMObject(MM, set_ID, tmpOK[i], i) then
  263.           WriteLn('WARNING: Failed to free ' + tmpWM[i] + '!');
  264.     end;              
  265.     SetLength(tmpWM, 0);
  266.   end;  
  267.   SetLength(tmpOK, 0);
  268. end;
Advertisement
Add Comment
Please, Sign In to add comment