Recent

Author Topic: exists a filemaping in pas?  (Read 1410 times)

Laur

  • New Member
  • *
  • Posts: 35
exists a filemaping in pas?
« on: July 18, 2024, 06:41:45 pm »
In c++ for win I use simple class/object:

Quote
class TFileMap : public TFileIO // TFileIO is a general class for files
{
  public:   TFileMap(filename);
           ~TFileMap() { endMap(); } // in c++ destructors are automatic

//            BYTE* doMap(fnam); // or create a map further
            void endMap(); // clear

   HANDLE hMap; // some system number
   int sizf; // size of file
   BYTE *ptr; // the usable pointer for file data
};


and now I simply use this ie.:

readWords(file)
{
  TFileMap map(file); // open everything at once...

  p = map.ptr;

  c = p[0]; // first char on the file, ect.
 
 c500 = p[499];
....

  // map.end(); in pas only, because in c++ it's auto
}

So , is something like this implemented in fp?
« Last Edit: July 18, 2024, 06:46:34 pm by Laur »

Thaddy

  • Hero Member
  • *****
  • Posts: 16152
  • Censorship about opinions does not belong here.
Re: exists a filemaping in pas?
« Reply #1 on: July 18, 2024, 06:47:10 pm »
fpmmap. silly question.
https://www.freepascal.org/docs-html/rtl/baseunix/fpmmap.html

When do we get reasonable questions from you?
Everything you asked over the past weeks is either answered or rebuted. >:D
« Last Edit: July 18, 2024, 06:49:27 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

cdbc

  • Hero Member
  • *****
  • Posts: 1646
    • http://www.cdbc.dk
Re: exists a filemaping in pas?
« Reply #2 on: July 18, 2024, 06:49:06 pm »
Hi
edit: sorry Thaddy was quicker and right'er  :D
If you're on winders, I'm pretty sure it's there, 'cause it's winapi...
IIRC it's called 'MapViewOfFile' the winapi function to call, try typing that in laz' editor and ctrl+click on it...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Thaddy

  • Hero Member
  • *****
  • Posts: 16152
  • Censorship about opinions does not belong here.
Re: exists a filemaping in pas?
« Reply #3 on: July 18, 2024, 06:50:44 pm »
Benny, he did not even mention platform, but you are correct: on windows it is slightly different.
If I smell bad code it usually is bad code and that includes my own code.

440bx

  • Hero Member
  • *****
  • Posts: 4729
Re: exists a filemaping in pas?
« Reply #4 on: July 18, 2024, 07:12:33 pm »
Benny, he did not even mention platform, but you are correct: on windows it is slightly different.
I pretty much agree with your comments about the post but, in all fairness, he _did_ mention the platform.  IOW, he can't be faulted for that.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Laur

  • New Member
  • *
  • Posts: 35
Re: exists a filemaping in pas?
« Reply #5 on: July 18, 2024, 07:24:56 pm »
fpmmap. silly question.
https://www.freepascal.org/docs-html/rtl/baseunix/fpmmap.html

When do we get reasonable questions from you?
Everything you asked over the past weeks is either answered or rebuted. >:D

there is an error in code probably: a file is cosed before the map on the file. :)

it's stupid idea creating a map on a handle;
 my method creates map on file(name), then it's full encapsulation.
« Last Edit: July 18, 2024, 07:28:30 pm by Laur »

Thaddy

  • Hero Member
  • *****
  • Posts: 16152
  • Censorship about opinions does not belong here.
Re: exists a filemaping in pas?
« Reply #6 on: July 18, 2024, 07:27:51 pm »
Where? show us.... I have little patience left with you, but if you show an error - in that very tried and tested code - I will fix it and apologize. (no chance).

Je n’ai plus de patience pour vous. Comprends?
« Last Edit: July 18, 2024, 07:35:36 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

Laur

  • New Member
  • *
  • Posts: 35
Re: exists a filemaping in pas?
« Reply #7 on: July 18, 2024, 07:34:12 pm »
Where? show us.... I have little patience left with you, but if you show an error - in that very tried and tested code - I will fix it and apologize. (no chance).

the file can not be closed on opened map, so it's error...

hence the lack of memory and other effects popular in... naive programing. :)


Thaddy

  • Hero Member
  • *****
  • Posts: 16152
  • Censorship about opinions does not belong here.
Re: exists a filemaping in pas?
« Reply #8 on: July 18, 2024, 07:37:24 pm »
Where?
you make yourself ridiculous..
fpunmap?
You are a troll after all. I will ignore you.
Show us pascal code that you have written, never happened, because you can't.

And everybody on this forum knows that. Even beginners.
« Last Edit: July 18, 2024, 07:41:11 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

440bx

  • Hero Member
  • *****
  • Posts: 4729
Re: exists a filemaping in pas?
« Reply #9 on: July 18, 2024, 07:53:38 pm »
So , is something like this implemented in fp?
Trivial.

Here is some code I use in some of my programs which does everything your "class" does.  It can be made into a class (since I don't use any OOP, it won't be something I will do):
Code: Pascal  [Select][+][-]
  1. { --------------------------------------------------------------------------- }
  2.  
  3. function MapFile
  4.            (
  5.             const InFilename    : pchar;
  6.               out OutMapAddress : pointer;
  7.               out OutModuleSize : DWORD
  8.            )
  9.          : boolean;
  10.   { maps a file (should be a PE file)                                         }
  11.  
  12.   { NOTE: could use VirtualAlloc instead of a mapping (it would simplify the  }
  13.   {       function)                                                           }
  14.  
  15. var
  16.   FileHandle    : THANDLE = INVALID_HANDLE_VALUE;
  17.   FileSize      : int64   = 0;
  18.   BytesRead     : DWORD   = 0;
  19.  
  20.   MappingHandle : THANDLE = 0;
  21.  
  22. begin
  23.   result        := FALSE;
  24.   OutMapAddress := nil;
  25.   OutModuleSize := 0;
  26.  
  27.   repeat        { scope - not a loop                                          }
  28.  
  29.     { open the file to obtain a handle to it                                  }
  30.  
  31.     FileHandle := CreateFileA(InFilename,
  32.                               GENERIC_READ,
  33.                               FILE_SHARE_READ,
  34.                               nil,
  35.                               OPEN_EXISTING,
  36.                               FILE_ATTRIBUTE_NORMAL,
  37.                               0);
  38.  
  39.     if FileHandle = INVALID_HANDLE_VALUE then
  40.     begin
  41.       writeln('MapFile: CreateFile failed');
  42.       break;
  43.     end;
  44.  
  45.     if not GetFileSizeEx(FileHandle, @FileSize) then
  46.     begin
  47.       writeln('MapFile: GetFileSizeEx failed.');
  48.       break;
  49.     end;
  50.  
  51.     { the addition of a page (4 * 1024) is to ensure the file is null         }
  52.     { terminated and there is extra room which allows testing characters past }
  53.     { what would have been the eof.                                           }
  54.  
  55.     { However, note that the extra room is not used, it's there only to       }
  56.     { ensure string comparisons can go past the original end of file without  }
  57.     { causing an access violation.  Therefore, while the extra room is not    }
  58.     { used it is required for the program to operate properly.                }
  59.  
  60.     MappingHandle := CreateFileMappingA(INVALID_HANDLE_VALUE,
  61.                                         nil,                    { no security }
  62.                                         PAGE_READWRITE,
  63.                                         0,                      { size high   }
  64.                                         FileSize + (4 * 1024),  { size low    }
  65.                                         nil);
  66.     if MappingHandle = 0 then
  67.     begin
  68.       writeln('MapFile: CreateFileMapping failed.');
  69.       break;
  70.     end;
  71.  
  72.     { commit linear address space to the file mapping                         }
  73.  
  74.     OutMapAddress := MapViewOfFile(MappingHandle,
  75.                                    FILE_MAP_ALL_ACCESS,
  76.                                    0,
  77.                                    0,
  78.                                    0);
  79.  
  80.     if OutMapAddress = nil then
  81.     begin
  82.       writeln('MapFile: MapViewOfFile failed.');
  83.       break;
  84.  
  85.       if IsDebuggerPresent() then
  86.       begin
  87.         DebugBreak();
  88.       end;
  89.     end;
  90.  
  91.     { read the file into the mapping                                          }
  92.  
  93.     if not ReadFile(FileHandle,
  94.                     OutMapAddress,
  95.                     FileSize,
  96.                    @BytesRead,
  97.                     nil)        then
  98.     begin
  99.       writeln('MapFile: ReadFile failed.');
  100.       BytesRead := 0;
  101.  
  102.       if IsDebuggerPresent() then
  103.       begin
  104.         DebugBreak();
  105.       end;
  106.  
  107.       break;
  108.     end;
  109.  
  110.     { it would be "profilactic" to set the mapping to read only now that the  }
  111.     { file has been read into it.                                             }
  112.  
  113.   until TRUE;
  114.  
  115.   { we no longer need the FileHandle nor the MappingHandle                    }
  116.  
  117.   if FileHandle    <> INVALID_HANDLE_VALUE then CloseHandle(FileHandle);
  118.   if MappingHandle <> 0                    then CloseHandle(MappingHandle);
  119.  
  120.   if (OutMapAddress <> nil) and
  121.      (BytesRead     <> 0)   then
  122.   begin
  123.      result        := TRUE;
  124.      OutModuleSize := FileSize;
  125.   end;
  126. end;
  127.  
  128. { --------------------------------------------------------------------------- }
  129.  
  130. function UnmapFile(InMapAddress : pointer) : boolean;
  131.   { as its name indicates, it unmaps a previously mapped file                 }
  132.  
  133. begin
  134.   result := UnmapViewOfFile(InMapAddress);
  135. end;
  136.  
Note that the call to ReadFile isn't necessary.  It is there as an optimization, specifically, read the entire file in a single I/O request instead of having the file read every time a new page in the file is touched by a read.

Also, the file does not have to be a PE file but, I use that code in programs that deal with PE files, hence the comment.  Moreover, the function could be simplified by using VirtualAlloc instead of mapping the file but, it's low priority, haven't gotten to it.

Those two functions can be turned into some class' methods easy enough.



(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Laur

  • New Member
  • *
  • Posts: 35
Re: exists a filemaping in pas?
« Reply #10 on: July 18, 2024, 08:04:19 pm »
Where?
you make yourself ridiculous..
fpunmap?
You are a troll after all. I will ignore you.
Show us pascal code that you have written, never happened, because you can't.

And everybody on this forum knows that. Even beginners.

ye, ye, mr dogmatic... and there is your link fragment:

Quote
fpclose(fd);
  if fpMUnMap(P,Len)<>0 Then
    Halt(fpgeterrno);
the end.

Laur

  • New Member
  • *
  • Posts: 35
Re: exists a filemaping in pas?
« Reply #11 on: July 18, 2024, 08:19:09 pm »
So , is something like this implemented in fp?
Trivial.

Here is some code I use in some of my programs which does everything your "class" does.  It can be made into a class (since I don't use any OOP, it won't be something I will do):
Code: Pascal  [Select][+][-]
  1. { --------------------------------------------------------------------------- }
  2.  
  3. function MapFile
  4.            (
  5.             const InFilename    : pchar;
  6.               out OutMapAddress : pointer;
  7.               out OutModuleSize : DWORD
  8.            )
  9.          : boolean;
  10.   { maps a file (should be a PE file)                                         }
  11.  
  12.   { NOTE: could use VirtualAlloc instead of a mapping (it would simplify the  }
  13.   {       function)                                                           }
  14.  
  15. var
  16.   FileHandle    : THANDLE = INVALID_HANDLE_VALUE;
  17.   FileSize      : int64   = 0;
  18.   BytesRead     : DWORD   = 0;
  19.  
  20.   MappingHandle : THANDLE = 0;
  21.  
  22. begin
  23.   result        := FALSE;
  24.   OutMapAddress := nil;
  25.   OutModuleSize := 0;
  26.  
  27.   repeat        { scope - not a loop                                          }
  28.  
  29.     { open the file to obtain a handle to it                                  }
  30.  
  31.     FileHandle := CreateFileA(InFilename,
  32.                               GENERIC_READ,
  33.                               FILE_SHARE_READ,
  34.                               nil,
  35.                               OPEN_EXISTING,
  36.                               FILE_ATTRIBUTE_NORMAL,
  37.                               0);
  38.  
  39.     if FileHandle = INVALID_HANDLE_VALUE then
  40.     begin
  41.       writeln('MapFile: CreateFile failed');
  42.       break;
  43.     end;
  44.  
  45.     if not GetFileSizeEx(FileHandle, @FileSize) then
  46.     begin
  47.       writeln('MapFile: GetFileSizeEx failed.');
  48.       break;
  49.     end;
  50.  
  51.     { the addition of a page (4 * 1024) is to ensure the file is null         }
  52.     { terminated and there is extra room which allows testing characters past }
  53.     { what would have been the eof.                                           }
  54.  
  55.     { However, note that the extra room is not used, it's there only to       }
  56.     { ensure string comparisons can go past the original end of file without  }
  57.     { causing an access violation.  Therefore, while the extra room is not    }
  58.     { used it is required for the program to operate properly.                }
  59.  
  60.     MappingHandle := CreateFileMappingA(INVALID_HANDLE_VALUE,
  61.                                         nil,                    { no security }
  62.                                         PAGE_READWRITE,
  63.                                         0,                      { size high   }
  64.                                         FileSize + (4 * 1024),  { size low    }
  65.                                         nil);
  66.     if MappingHandle = 0 then
  67.     begin
  68.       writeln('MapFile: CreateFileMapping failed.');
  69.       break;
  70.     end;
  71.  
  72.     { commit linear address space to the file mapping                         }
  73.  
  74.     OutMapAddress := MapViewOfFile(MappingHandle,
  75.                                    FILE_MAP_ALL_ACCESS,
  76.                                    0,
  77.                                    0,
  78.                                    0);
  79.  
  80.     if OutMapAddress = nil then
  81.     begin
  82.       writeln('MapFile: MapViewOfFile failed.');
  83.       break;
  84.  
  85.       if IsDebuggerPresent() then
  86.       begin
  87.         DebugBreak();
  88.       end;
  89.     end;
  90.  
  91.     { read the file into the mapping                                          }
  92.  
  93.     if not ReadFile(FileHandle,
  94.                     OutMapAddress,
  95.                     FileSize,
  96.                    @BytesRead,
  97.                     nil)        then
  98.     begin
  99.       writeln('MapFile: ReadFile failed.');
  100.       BytesRead := 0;
  101.  
  102.       if IsDebuggerPresent() then
  103.       begin
  104.         DebugBreak();
  105.       end;
  106.  
  107.       break;
  108.     end;
  109.  
  110.     { it would be "profilactic" to set the mapping to read only now that the  }
  111.     { file has been read into it.                                             }
  112.  
  113.   until TRUE;
  114.  
  115.   { we no longer need the FileHandle nor the MappingHandle                    }
  116.  
  117.   if FileHandle    <> INVALID_HANDLE_VALUE then CloseHandle(FileHandle);
  118.   if MappingHandle <> 0                    then CloseHandle(MappingHandle);
  119.  
  120.   if (OutMapAddress <> nil) and
  121.      (BytesRead     <> 0)   then
  122.   begin
  123.      result        := TRUE;
  124.      OutModuleSize := FileSize;
  125.   end;
  126. end;
  127.  
  128. { --------------------------------------------------------------------------- }
  129.  
  130. function UnmapFile(InMapAddress : pointer) : boolean;
  131.   { as its name indicates, it unmaps a previously mapped file                 }
  132.  
  133. begin
  134.   result := UnmapViewOfFile(InMapAddress);
  135. end;
  136.  
Note that the call to ReadFile isn't necessary.  It is there as an optimization, specifically, read the entire file in a single I/O request instead of having the file read every time a new page in the file is touched by a read.

Also, the file does not have to be a PE file but, I use that code in programs that deal with PE files, hence the comment.  Moreover, the function could be simplified by using VirtualAlloc instead of mapping the file but, it's low priority, haven't gotten to it.

Those two functions can be turned into some class' methods easy enough.

Readfile is not any optimization, but rather de...generation.

and use round up to 4k, instead of adding 4k;
but for reading only it's auto, then nothing to do.


440bx

  • Hero Member
  • *****
  • Posts: 4729
Re: exists a filemaping in pas?
« Reply #12 on: July 18, 2024, 08:22:30 pm »
Readfile is not any optimization, but rather de...generation.
it has become apparent that the de...generation is not in ReadFile and is greater than I initially thought.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 16152
  • Censorship about opinions does not belong here.
Re: exists a filemaping in pas?
« Reply #13 on: July 18, 2024, 09:19:06 pm »
 :) O:-)
If I smell bad code it usually is bad code and that includes my own code.


 

TinyPortal © 2005-2018