Board index » delphi » Req : What is the Delphi equivalent for Turbo Pascal's MARK, MAXAVAIL

Req : What is the Delphi equivalent for Turbo Pascal's MARK, MAXAVAIL

Hi everybody,

As a Delphi programmer, I have been given the task to rewrite a program
that was written several years ago in Turbo Pascal by another programmer.
Fortunately that programmer left the company some 6 years ago, and the only
thing I have here is his source code.  As I didn't have a clue of what his
program did, I started typing everything again in Delphi.  After making
some changes, I started to get an Idea of what it does, but still I have a
little question.

In his program I found the following commands :

        Mark(Dummypntr)             // where dummypntr : ^byte

and some further

        If maxavail > 0 then    // where maxavail is not some kind of variable,
but some
                                      kind of turbo pascal function

and then again a little further

        Release(Dummypntr)

Some of these functions, like Mark and Maxavail are not available anymore
in Delphi, so I wanted to know which commands are substituting those
functions.  What are the new commands ?

If anybody could help me, I would really appreciate it.

Thanks in advance,

                        Stefaan

P.S. : If you need the full source code before you can help me, I can send
it by e-mail.

--

Stefaan Lesage
Fotek nv; Belgium
Stef...@fotek.com

 

Re:Req : What is the Delphi equivalent for Turbo Pascal's MARK, MAXAVAIL


On 11 Jun 1997 14:33:16 GMT, "Stefaan Lesage" <stef...@fotek.com>
wrote:

Quote
>Hi everybody,

>    Mark(Dummypntr)             // where dummypntr : ^byte

>and some further

>    If maxavail > 0 then    // where maxavail is not some kind of variable,
>but some
>                                  kind of turbo pascal function

>and then again a little further

>    Release(Dummypntr)

>Some of these functions, like Mark and Maxavail are not available anymore
>in Delphi, so I wanted to know which commands are substituting those
>functions.  What are the new commands ?

>If anybody could help me, I would really appreciate it.

>Thanks in advance,

When I translated a program from Turbo Pascal, I replaced the Mark()
Release() with Getmem() and Freemem(). The usuage should be available
in the help system for Delphi.
As with release, you must be very careful not to release memory that
was not allocated with Getmem().

--
Peter Neuendorffer  
pet...@user1.channel1.com
Green Line Software  
http://www.channel1.com/users/petern

Re:Req : What is the Delphi equivalent for Turbo Pascal's MARK, MAXAVAIL


On 11 Jun 1997 14:33:16 GMT, "Stefaan Lesage" <stef...@fotek.com>
wrote:

Quote
>As a Delphi programmer, I have been given the task to rewrite a program
>that was written several years ago in Turbo Pascal by another programmer.
>Fortunately that programmer left the company some 6 years ago, and the only
>thing I have here is his source code.  As I didn't have a clue of what his
>program did, I started typing everything again in Delphi.  After making
>some changes, I started to get an Idea of what it does, but still I have a
>little question.

>In his program I found the following commands :

>    Mark(Dummypntr)             // where dummypntr : ^byte

>and some further

>    If maxavail > 0 then    // where maxavail is not some kind of variable,
>but some
>                                  kind of turbo pascal function

>and then again a little further

>    Release(Dummypntr)

>Some of these functions, like Mark and Maxavail are not available anymore
>in Delphi, so I wanted to know which commands are substituting those
>functions.  What are the new commands ?

Actually, I don't know if there are any functions which would replace
the old TP memory functions, but with a little effort you can replace
the functions with more efficient code.

The Mark and Release procedures were used to save the state of the
heap. For example, when you called Mark, a pointer to the current heap
top was saved. After that you could allocate more memory, and when you
finally called Release, everything allocated after the call to Mark
was automatically deallocated.

MaxAvail was simply a function to determine if enough memory was
available (under the 640k DOS memory limit... ;-)

To make your Delphi code work, you must manually save and then free
all the allocated dynamic variables between the Mark/Release calls. Of
course, this can be very difficult in the worst case, but on the
average, this is not a very painful process.

Good luck!

Regards,

Jani J?rvinen
Helsinki Finland

Tools, information, tips, reviews & bug lists
for professional Delphi, Win32 and WinHelp developers.

mailto:ja...@dystopia.fi
http://www.dystopia.fi/~janij/

Re:Req : What is the Delphi equivalent for Turbo Pascal's MARK, MAXAVAIL


On 11 Jun 1997 14:33:16 GMT, "Stefaan Lesage" <stef...@fotek.com>
wrote:

Quote
>Hi everybody,

>As a Delphi programmer, I have been given the task to rewrite a program
>that was written several years ago in Turbo Pascal by another programmer.
>Fortunately that programmer left the company some 6 years ago, and the only
>thing I have here is his source code.  As I didn't have a clue of what his
>program did, I started typing everything again in Delphi.  After making
>some changes, I started to get an Idea of what it does, but still I have a
>little question.

>In his program I found the following commands :

>    Mark(Dummypntr)             // where dummypntr : ^byte

>and some further

>    If maxavail > 0 then    // where maxavail is not some kind of variable,
>but some
>                                  kind of turbo pascal function

>and then again a little further

>    Release(Dummypntr)

>Some of these functions, like Mark and Maxavail are not available anymore
>in Delphi, so I wanted to know which commands are substituting those
>functions.  What are the new commands ?

>If anybody could help me, I would really appreciate it.

>Thanks in advance,

>                    Stefaan

>P.S. : If you need the full source code before you can help me, I can send
>it by e-mail.

>--

>Stefaan Lesage
>Fotek nv; Belgium
>Stef...@fotek.com

Stefaan,

From the Borland Pascal help:

------------------------------------------------------------------------------------------------------------------------------
Mark (procedure)
  _________________
 Records the state of the heap in a pointer variable.

  Declaration:
  procedure Mark(var p: pointer);

  Restrictions:
 Should not be used with FreeMem or Dispose.

  Target:
 Real, Protected

  See Also:
  Dispose
  FreeMem
  GetMem
  New
  Release

  Sample Code:

  { Example for Mark and Release }

  uses Crt;

  var
    p : pointer;
    p1,p2,p3 : ^Integer;
  begin
    ClrScr;
    New(p1); { Allocate an Integer }
    Mark(p); { Save heap state }
    New(p2); { Allocate two more Integers }
    New(p3);
    Release(p); { Memory reserved for p2^ and
                  p3^ has been released; p1^
                  may still be used }
  end.
------------------------------------------------------------------------------------------------------------------------------
  Release (procedure)
  ____________________
 Returns the heap to a given state.

  Declaration:
  procedure Release(var p: pointer);

  Target:
 Real, Protected

  Remarks:
 Should not be used with FreeMem or Dispose.
------------------------------------------------------------------------------------------------------------------------------
  MaxAvail (function)
  ____________________
 Returns the size of the largest contiguous free block in the heap.

  Declaration:
  function MaxAvail: Longint;

  Target:
 Windows, Real, Protected

  Remarks:
 Returns the larger of:
  - the largest free blocks within the heap manager's sub-allocation
space
  - the Windows global heap

 The value corresponds to the size of the largest dynamic variable
that can
 be allocated at that time.

  See Also:
  MemAvail

  Sample Code:

  {FreeMem.PAS}

  {Sample code for the FreeMem and GetMem procedures and the MaxAvail
    function.}

  { For Windows: }
  { uses WinCrt; }

  type
    TFriendRec = record
                  Name: string[30];
                  Age : Byte;
                end;
  var
    p: pointer;
  begin
    if MaxAvail < SizeOf(TFriendRec) then
      Writeln('Not enough memory')
    else
    begin
      { Allocate memory on heap }
      GetMem(p, SizeOf(TFriendRec));
      { ...}
      { ...Use the memory... }
      { ...}
      { Then free it when done }
      FreeMem(p, SizeOf(TFriendRec));
    end;
  end.
------------------------------------------------------------------------------------------------------------------------------
These are all ways of controlling memory allocated on the heap. The
idea of Mark/Release (which is actually kinda-standard pascal) is that
you can return to the heap state 'before calling Mark' by calling
Release and automatically dispose of all memory allocated between the
Mark and Release. MaxAvail is used to check whether there is enough
memory, actually trying New. This is from the before-exception days.
(Note: The Windows mentioned in the Help is for Borland Pascal for
Windows, not Delphi).

The Delphi memory manager doesn't support Mark/Release any more (or
actually, the System unit still contains them, but will give you an
Invalid Pointer Operation exception if you use them), and there is no
equivalent, so if you want really really want to keep this code in,
you must write your own memory manager for Delphi. Maybe your program
allocates memory in such a way that this would be possible.

My preferred solution would be, remove the Mark/Release stuff , by
carefully recording every New allocation after each Mark, and making
sure all will be Disposed by the time Release is called. (You might
need to do some redesigning of the program). Since MaxAvail is not
particularly useful in a Windows environment, I  would replace the
tests by try..except blocks, to catch out-of-memory errors, like this:

if maxavail < MemoryNeeded
 then SomeErrorHandling
else Do SomeAllocating

would become

try
   DoSomeAllocating
except
   SomeErrorHandling
end

which would largely be equivalent

hth

David

David A. Schweizer

iec ProGAMMA, The Netherlands
d.a.schwei...@gamma.rug.nl

Re:Req : What is the Delphi equivalent for Turbo Pascal's MARK, MAXAVAIL


Quote
In article <339ebe9a.1947...@news.clinet.fi> ja...@dystopia.fi (Jani J?rvinen) writes:
>>As a Delphi programmer, I have been given the task to rewrite a program
>>that was written several years ago in Turbo Pascal by another programmer.
>>Fortunately that programmer left the company some 6 years ago, and the only
>>thing I have here is his source code.  As I didn't have a clue of what his
>>program did, I started typing everything again in Delphi.  After making
>>some changes, I started to get an Idea of what it does, but still I have a
>>little question.

>>In his program I found the following commands :

>>       Mark(Dummypntr)             // where dummypntr : ^byte

>>and some further

>>       If maxavail > 0 then       // where maxavail is not some kind of variable,
>>but some
>>                                     kind of turbo pascal function

>>and then again a little further

>>       Release(Dummypntr)

>>Some of these functions, like Mark and Maxavail are not available anymore
>>in Delphi, so I wanted to know which commands are substituting those
>>functions.  What are the new commands ?
>Actually, I don't know if there are any functions which would replace
>the old TP memory functions, but with a little effort you can replace
>the functions with more efficient code.
>The Mark and Release procedures were used to save the state of the
>heap. For example, when you called Mark, a pointer to the current heap
>top was saved. After that you could allocate more memory, and when you
>finally called Release, everything allocated after the call to Mark
>was automatically deallocated.
>MaxAvail was simply a function to determine if enough memory was
>available (under the 640k DOS memory limit... ;-)
>To make your Delphi code work, you must manually save and then free
>all the allocated dynamic variables between the Mark/Release calls. Of
>course, this can be very difficult in the worst case, but on the
>average, this is not a very painful process.
>Good luck!
>Regards,
>Jani J?rvinen
>Helsinki Finland

Yah, I don't think anyone seriously *misses* Mark and Release!  :-)

Other Threads