Read only memory stream, given start memory pointer and length lazarus / 62-General ---------------------------------------------------------------------- Чебурашка · 2021-11-12 12:39 Hi everybody, I have the following situation: 1. there is a packed record type variable populated by some entity Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  type  TMyRecord = packed record   f1: Int   f2: Int   f3: Int   f4: Single   f5: Single   f6: Single   f7: Single end var  x: TMyRecord   2. This variable is passed to a procedure by means of two parameters: StartPointer: Pointer, MemoryLength: Int64 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  procedure MakeSomething(StartPointer: Pointer, MemoryLength: Int64); [...] MakeSomething(Addr(x), SizeOf(TMyRecord));   In this way I get into the procedure with the memory area where the variable x lives. I would like to use a TStream, because inside the procedure there is an activity that writes to db, that would like to get a TStream as input, something like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  ParamByName('samplingData').LoadFromStream(stream, ftBlob); // <--- it would be nice if stream contains the memory area used by variable x   I need to have a sort of TStream able to read from the memory area from StartPointer to StartPointer + MemoryLength - 1 so that no extra memory is allocated because the memory area used by x will exist during the call to the procedure, and the procedure is called billion of billion times per hour. Is there anything ready or do I need to implement a custom stream?? Thank you very much! ---------------------------------------------------------------------- Zvoni · 2021-11-12 13:06 I remember doing a very small sample-project (more like proof of concept) with SQLite for storing and retrieving blobs https://forum.lazarus.freepascal.org/index.php/topic,47482.msg340894.h tml#msg340894 ---------------------------------------------------------------------- BrunoK · 2021-11-12 13:24 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1; {$mode objfpc}{$H+} uses {$IFDEF UNIX}  cthreads,     {$ENDIF}  Classes { you can add units after this }; type   TMyRecord = packed record    f1: integer;    f2: integer;    f3: integer;    { etc ... }  end;  PMyRecord = ^TMyRecord;  procedure MakeSomething(apMyRecord : PMyRecord);var  lSumOfInt : integer;begin  // Do something with the pointed record  lSumOfInt := apMyRecord^.f1 + apMyRecord^.f2 + apMyRecord^.f3;end; var  vInStream : TMemoryStream; // or another TStream descendent begin  { Whatever needed to create and load the data to vInStream  ...  }  begin    vInStream.Position := 0; // go to stream begining    repeat      if (vInStream.Position + SizeOf(TMyRecord)) > vInStream.Size then        break;      MakeSomething(PMyRecord(vInStream.Memory + vInStream.Position));      vInStream.Position := vInStream.Position + SizeOf(TMyRecord); // Move forward    until False;  end;end. ---------------------------------------------------------------------- ASerge · 2021-11-12 16:59 Quote from: tt on November 12, 2021, 01:39:09 pmI need to have a sort of TStream able to read from the memory area from StartPointer to StartPointer + MemoryLength - 1 so that no extra memory is allocated because the memory area used by x will exists during the call to the procedure, and the procedure is called billion of billion times per hour. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TStreamOnStaticMemory = class(TCustomMemoryStream)  public    constructor Create(AMemory: Pointer; ASize: SizeInt);  end; constructor TStreamOnStaticMemory.Create(AMemory: Pointer; ASize: SizeInt);begin  inherited Create;  SetPointer(AMemory, ASize);end; ---------------------------------------------------------------------- cdbc · 2021-11-12 17:25 Hi You could do a sliding window technique, of sorts. TMemorystream exposes a property "Memory", it's a pointer. PMyRecord = ^TMyRecord; Calculate the offset and place PMyRecord there, and read the values you need. Regards Benny ---------------------------------------------------------------------- Thaddy · 2021-11-12 20:02 That is called a windowed stream( nothing to do with windows). There are many implementations for that. It is a very basic concept. ---------------------------------------------------------------------- Чебурашка · 2021-11-15 07:32 Thank you all very much for the help. Regards, Tomaso ---------------------------------------------------------------------- Чебурашка · 2021-11-15 09:57 Quote from: ASerge on November 12, 2021, 05:59:19 pmQuote from: tt on November 12, 2021, 01:39:09 pmI need to have a sort of TStream able to read from the memory area from StartPointer to StartPointer + MemoryLength - 1 so that no extra memory is allocated because the memory area used by x will exists during the call to the procedure, and the procedure is called billion of billion times per hour. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TStreamOnStaticMemory = class(TCustomMemoryStream)  public    constructor Create(AMemory: Pointer; ASize: SizeInt);  end; constructor TStreamOnStaticMemory.Create(AMemory: Pointer; ASize: SizeInt);begin  inherited Create;  SetPointer(AMemory, ASize);end; Thank you ASerge. So the answer is: there is nothing ready, I must subclass on my own. Despite no already available stream class type exists in the standard library, the class you suggested is exactly what I was looking for. ---------------------------------------------------------------------- Чебурашка · 2021-11-15 12:32 Quote from: jamie on November 15, 2021, 12:43:13 pmHave looked at TmemoryStreamEx ? Where can I find it? ---------------------------------------------------------------------- marcov · 2021-11-15 14:57 I can't find it with a quick grep. I do know the principle, a few years ago I derived a version from Indy's TidMemoryStream for this purpose (pretty much like Serge's). ---------------------------------------------------------------------- dsiders · 2021-11-15 15:52 Quote from: tt on November 15, 2021, 10:57:14 amQuote from: ASerge on November 12, 2021, 05:59:19 pmQuote from: tt on November 12, 2021, 01:39:09 pmI need to have a sort of TStream able to read from the memory area from StartPointer to StartPointer + MemoryLength - 1 so that no extra memory is allocated because the memory area used by x will exists during the call to the procedure, and the procedure is called billion of billion times per hour. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TStreamOnStaticMemory = class(TCustomMemoryStream)  public    constructor Create(AMemory: Pointer; ASize: SizeInt);  end; constructor TStreamOnStaticMemory.Create(AMemory: Pointer; ASize: SizeInt);begin  inherited Create;  SetPointer(AMemory, ASize);end; Thank you ASerge. So the answer is: there is nothing ready, I must subclass on my own. Despite no already available stream class type exists in the standard library, the class you suggested is exactly what I was looking for. Perhaps this will help: TWindowedStream in fpc/3.2.2/source/packages/fcl-base/src/streamex.pp ---------------------------------------------------------------------- marcov · 2021-11-16 09:13 Quote from: jamie on November 16, 2021, 02:33:49 am I don't know but that kind of defeats what the memory stream is for I guess but who am i... In Indy a lot of streams are used as arguments. (buffer to send etc) If you have some buffer you would have to write it to a tstream first, resulting in bufferbloat. This way circumvents that. But the Indy situation is IMHO not ideal, but I guess due to past .NET experiments.