by markstuartwalker » Wed Nov 27, 2013 6:33 am
OK, I think that I can see that now. I have created a few functions to encapsulate this capability. Please review and confirm.
Q1: Do I need Critical sections in my code, I understood the plugins to be single threaded?
Q2: ZeroMemory() is not in standard libraries but I think that is of negligible benefit
Q3: Doesn't MM dispose of the memory once it has consumed the information? I do not retain a pointer the the results that I return from either of these functions.
Code: Select all
unit ScanResultUnit;
interface
uses sysutils,DeviceCommon;
function createScanResult(count:integer):PScanResult;
procedure addScanResult(res:PScanResult;
i:Integer;
name:string ;
filesize:integer ;
lastModified:TDateTime ;
track_cookie:integer);
implementation
function createScanResult(count:integer):PScanResult;
var
size:integer;
begin
new(Result);
Result.FileCount := count;
size := count * sizeof(Result.Files[0]);
GetMem(Result.Files,size);
(*
New( ScannedPlaylists);
GetMem( ScannedPlaylists.Files, playlists.Count*sizeof(ScannedPlaylists.Files[0]));
*)
end;
procedure addScanResult(res:PScanResult;
i:Integer;
name:string ;
filesize:integer ;
lastModified:TDateTime ;
track_cookie:integer);
type
PFileName = ^TFileName;
TFileName = array [0 .. 255] of WideChar;
var
// name2 : PfileName ;
rec : PScanRecord;
begin
if i < res.FileCount then
begin
new(res.Files[i]);
rec:=res.files[i]; // for debug
// ZeroMemory( res.Files[i], sizeof(res.Files[i]^));
//new(name2);
//StrCopy(@name2[1], PWideChar(name));
GetMem( res.Files[i].filename, length(name)*2+2);
StrCopy( res.Files[i].filename, PWideChar(name));
// res.Files[i].filename := @name2[1];
res.Files[i].filesize := filesize;
res.Files[i].lastmodified := lastModified;
res.Files[i].track_cookie := track_cookie;
end;
end;
(*
function TiPodDB.GetScannedPlaylists: PScanResult;
var
i : integer;
s : String;
plst : TiPodPlaylist;
begin
EnterCriticalSection( iPodCS);
try
ClearScannedPlaylists;
New( ScannedPlaylists);
GetMem( ScannedPlaylists.Files, playlists.Count*sizeof(ScannedPlaylists.Files[0]));
ScannedPlaylists.FileCount := 0;
for i:=0 to playlists.Count-1 do
begin
plst := TiPodPlaylist( playlists[i]);
if plst.MHYP.podcast_flag = 0 then
begin
New( ScannedPlaylists.Files[ScannedPlaylists.FileCount]);
ZeroMemory( ScannedPlaylists.Files[ScannedPlaylists.FileCount], sizeof(ScannedPlaylists.Files[ScannedPlaylists.FileCount]^));
s := plst.Title;
GetMem( ScannedPlaylists.Files[ScannedPlaylists.FileCount].filename, length(s)*2+2);
StrCopy( ScannedPlaylists.Files[ScannedPlaylists.FileCount].filename, PWideChar( s));
inc( ScannedPlaylists.FileCount);
end;
end;
result := ScannedPlaylists;
finally
LeaveCriticalSection( iPodCS);
end;
end;
procedure TiPodDB.ClearScannedPlaylists;
var
i : integer;
begin
EnterCriticalSection( iPodCS);
try
if ScannedPlaylists<>nil then
begin
for i:=0 to ScannedPlaylists^.FileCount-1 do
begin
Dispose( ScannedPlaylists^.Files^[i].filename);
Dispose( ScannedPlaylists^.Files^[i]);
end;
Dispose( ScannedPlaylists^.Files);
Dispose( ScannedPlaylists);
ScannedPlaylists := nil;
end;
finally
LeaveCriticalSection( iPodCS);
end;
end;*)
end.
OK, I think that I can see that now. I have created a few functions to encapsulate this capability. Please review and confirm.
Q1: Do I need Critical sections in my code, I understood the plugins to be single threaded?
Q2: ZeroMemory() is not in standard libraries but I think that is of negligible benefit
Q3: Doesn't MM dispose of the memory once it has consumed the information? I do not retain a pointer the the results that I return from either of these functions.
[code]unit ScanResultUnit;
interface
uses sysutils,DeviceCommon;
function createScanResult(count:integer):PScanResult;
procedure addScanResult(res:PScanResult;
i:Integer;
name:string ;
filesize:integer ;
lastModified:TDateTime ;
track_cookie:integer);
implementation
function createScanResult(count:integer):PScanResult;
var
size:integer;
begin
new(Result);
Result.FileCount := count;
size := count * sizeof(Result.Files[0]);
GetMem(Result.Files,size);
(*
New( ScannedPlaylists);
GetMem( ScannedPlaylists.Files, playlists.Count*sizeof(ScannedPlaylists.Files[0]));
*)
end;
procedure addScanResult(res:PScanResult;
i:Integer;
name:string ;
filesize:integer ;
lastModified:TDateTime ;
track_cookie:integer);
type
PFileName = ^TFileName;
TFileName = array [0 .. 255] of WideChar;
var
// name2 : PfileName ;
rec : PScanRecord;
begin
if i < res.FileCount then
begin
new(res.Files[i]);
rec:=res.files[i]; // for debug
// ZeroMemory( res.Files[i], sizeof(res.Files[i]^));
//new(name2);
//StrCopy(@name2[1], PWideChar(name));
GetMem( res.Files[i].filename, length(name)*2+2);
StrCopy( res.Files[i].filename, PWideChar(name));
// res.Files[i].filename := @name2[1];
res.Files[i].filesize := filesize;
res.Files[i].lastmodified := lastModified;
res.Files[i].track_cookie := track_cookie;
end;
end;
(*
function TiPodDB.GetScannedPlaylists: PScanResult;
var
i : integer;
s : String;
plst : TiPodPlaylist;
begin
EnterCriticalSection( iPodCS);
try
ClearScannedPlaylists;
New( ScannedPlaylists);
GetMem( ScannedPlaylists.Files, playlists.Count*sizeof(ScannedPlaylists.Files[0]));
ScannedPlaylists.FileCount := 0;
for i:=0 to playlists.Count-1 do
begin
plst := TiPodPlaylist( playlists[i]);
if plst.MHYP.podcast_flag = 0 then
begin
New( ScannedPlaylists.Files[ScannedPlaylists.FileCount]);
ZeroMemory( ScannedPlaylists.Files[ScannedPlaylists.FileCount], sizeof(ScannedPlaylists.Files[ScannedPlaylists.FileCount]^));
s := plst.Title;
GetMem( ScannedPlaylists.Files[ScannedPlaylists.FileCount].filename, length(s)*2+2);
StrCopy( ScannedPlaylists.Files[ScannedPlaylists.FileCount].filename, PWideChar( s));
inc( ScannedPlaylists.FileCount);
end;
end;
result := ScannedPlaylists;
finally
LeaveCriticalSection( iPodCS);
end;
end;
procedure TiPodDB.ClearScannedPlaylists;
var
i : integer;
begin
EnterCriticalSection( iPodCS);
try
if ScannedPlaylists<>nil then
begin
for i:=0 to ScannedPlaylists^.FileCount-1 do
begin
Dispose( ScannedPlaylists^.Files^[i].filename);
Dispose( ScannedPlaylists^.Files^[i]);
end;
Dispose( ScannedPlaylists^.Files);
Dispose( ScannedPlaylists);
ScannedPlaylists := nil;
end;
finally
LeaveCriticalSection( iPodCS);
end;
end;*)
end.[/code]