[DUG] Pointers help

Ross Levis ross at stationplaylist.com
Sun Nov 5 20:49:49 NZDT 2017


I’ve been doing this unsafe operation for a few years with no problems with
probably 200 users.

 

I did miss out the fact that I set the memory size using
TMemorySream.SetSize as I know the maximum size in advance, so the position
of the data in RAM will not change.  I took the risk of doing this unsafely
to speed up the operations and it works fine.

 

I could switch to using critical sections and then I could store the
Position, manipulate it and restore it before leaving the section, and that
would get around the move operation, but I suspect the Indy code is doing
data movements in RAM so not saving anything.

 

Ross.

 

From: Stefan Mueller [mailto:muellers at orcl-toolbox.com] 
Sent: Sunday, 5 November 2017 4:13 p.m.
To: 'NZ Borland Developers Group - Delphi List'
Cc: 'Ross Levis'
Subject: RE: [DUG] Pointers help

 

Just noticed this one here:

 

>>There is a IOHandler.Write function that supports TStream but the memory
stream is being appended to in a separate thread so I can’t go changing the
Position I want to access the data from.

 

You are aware that tmemorystream isn’t threadsafe, right? You will need to
use a TCriticalSection to lock concurrent writing and reading threads 
 if
you only have 2 locations in your code (one read, one write) then you can
wrap them in tcriticalsection there .. if you have more locations than I
suggest you think about what is called the RCU-pattern (read-copy-update)
for passing data between threads in a safe manner. 

 

Kind regards,

Stefan Müller,
R&D Manager

ORCL Toolbox Ltd. 
Auckland, New Zealand 


P Please consider the environment before printing this email

This message is intended for the addressee named above and may contain
privileged or confidential information.
If you are not the intended recipient of this message you must not use,
copy, distribute or disclose it to anyone.

 

From: delphi-bounces at listserver.123.net.nz
[mailto:delphi-bounces at listserver.123.net.nz] On Behalf Of Stefan Mueller
Sent: Sunday, 5 November 2017 3:25 p.m.
To: 'NZ Borland Developers Group - Delphi List'
Subject: Re: [DUG] Pointers help

 

Ross,

 

“array of bytes” isn’t the same as a “pointer” (nor is the same as a static
array with a fixed length).  Dynamic arrays are a managed data structure in
Delphi that contain information such as length and a reference count (so
Delphi can manage memory and free them if no longer used). Converting data
from a memory-pointer to a dynamic array isn’t possible by typecasting
alone, you will need to copy the data as in your existing code – Indies
idGlobal.pas contains a helper function which does the same as your code:
function RawToBytes(const AValue; const ASize: Integer): TIdBytes;

 

 

Kind regards,

Stefan Müller,
R&D Manager

ORCL Toolbox Ltd. 
Auckland, New Zealand 


P Please consider the environment before printing this email

This message is intended for the addressee named above and may contain
privileged or confidential information.
If you are not the intended recipient of this message you must not use,
copy, distribute or disclose it to anyone.

 

From: delphi-bounces at listserver.123.net.nz
[mailto:delphi-bounces at listserver.123.net.nz] On Behalf Of Ross Levis
Sent: Sunday, 5 November 2017 2:19 p.m.
To: delphi at delphi.org.nz
Subject: [DUG] Pointers help

 

I’m writing data to an Indy IOHandler by moving the data from a
TMemoryStream.Memory to a TIDBytes (array of byte) before writing it as I
cannot find how to do it otherwise.

 

This works...

 

AudioMS: TMemoryStream;

Buffer: TIdBytes; // array of byte

MemPos: PByte;

....

MemPos := AudioMS.Memory;

Inc(MemPos,FromPos);

ToPos := Player.AudioMS.Position;

SetLength(Buffer,ToPos-FromPos);

Move(MemPos^,Buffer[0],Length(Buffer));

IOHandler.WriteDirect(Buffer);

FromPos := ToPos;

 

 

But there has to be a better way of writing direct from the AudioMS.Memory
and getting rid of the Move operation.

 

I’ve tried the following but produces pointer errors or access violations.

 

IOHandler.WriteDirect(TIdBytes(MemPos),ToPos-FromPos);

 

Also tried...

IOHandler.WriteDirect(TIdBytes(MemPos^),ToPos-FromPos);

 

There is a IOHandler.Write function that supports TStream but the memory
stream is being appended to in a separate thread so I can’t go changing the
Position I want to access the data from. 

 

 

How should it be done?

 

Cheers,

Ross.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://listserver.123.net.nz/pipermail/delphi/attachments/20171105/55ec85ff/attachment-0001.html 


More information about the Delphi mailing list