Go Back   Velocity Reviews > Newsgroups > VHDL
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

VHDL - std.textio, readline and memory deallocation

 
Thread Tools Search this Thread
Old 09-01-2006, 06:34 PM   #1
Default std.textio, readline and memory deallocation


Hello
I was curious about how variable length strings were implemented in the
textio package and had a look at ModelSim's VHDL source (found in
$MODELTECH\vhdl_src directory)
I noticed that the readline procedure (ModelSim 6.0) did not deallocate
the line if it was not empty (actually, the line parameter is an input
only). The deallocation part is commented out.
Does anyone have an explanation for this ?

Nicolas


Nicolas Matringe
  Reply With Quote
Old 09-01-2006, 08:40 PM   #2
Paul Uiterlinden
 
Posts: n/a
Default Re: std.textio, readline and memory deallocation
Nicolas Matringe wrote:

> Hello
> I was curious about how variable length strings were implemented in
> the textio package and had a look at ModelSim's VHDL source (found
> in $MODELTECH\vhdl_src directory)
> I noticed that the readline procedure (ModelSim 6.0) did not
> deallocate the line if it was not empty (actually, the line
> parameter is an input only).


Surely you mean output only. In ModelSim 6.1d I see:

procedure READLINE(file f: TEXT; L: out LINE)
--procedure READLINE(variable f: in TEXT; L : inout LINE)

> The deallocation part is commented out.
> Does anyone have an explanation for this ?


Deallocation would make sense if parameter L was mode inout (as in the
commented out declaration line). A mode out parameter has the "left
most value" of its type as initial value; NULL in this case. So no
need for deallocation.

--
Paul.




Paul Uiterlinden
  Reply With Quote
Old 09-02-2006, 01:56 PM   #3
Nicolas Matringe
 
Posts: n/a
Default Re: std.textio, readline and memory deallocation
Paul Uiterlinden a écrit :
> Nicolas Matringe wrote:

[...]
>> deallocate the line if it was not empty (actually, the line
>> parameter is an input only).

> Surely you mean output only.


Yes of course (input only doesn't make sense)


>> The deallocation part is commented out.
>> Does anyone have an explanation for this ?

> Deallocation would make sense if parameter L was mode inout (as in the
> commented out declaration line). A mode out parameter has the "left
> most value" of its type as initial value; NULL in this case. So no
> need for deallocation.


The problem as I see it is that the line type is a pointer to a string.
If you reallocate the pointer, the string still exists but nothing
points to it.

What happens to the memory if I run this code :

while not endfile(my_file) loop
readline(my_file, a_line);
end loop;

All the lines should be somewhere in the memory but a_line will only
point to the last one. Or am I missing something ?

Nicolas
I'll have to


Nicolas Matringe
  Reply With Quote
Old 09-03-2006, 12:26 AM   #4
Jonathan Bromley
 
Posts: n/a
Default Re: std.textio, readline and memory deallocation
On Sat, 02 Sep 2006 14:56:09 +0200, Nicolas Matringe
<> wrote:

>>> The deallocation part is commented out.
>>> Does anyone have an explanation for this ?

>> Deallocation would make sense if parameter L was mode inout (as in the
>> commented out declaration line). A mode out parameter has the "left
>> most value" of its type as initial value; NULL in this case. So no
>> need for deallocation.

>
>The problem as I see it is that the line type is a pointer to a string.
>If you reallocate the pointer, the string still exists but nothing
>points to it.


Nicolas, I think you are right; but the standard packages,
including textio, are specially implemented in most simulators,
so the provided source code is only a reference implementation
and the internal implementation is probably quite different.
I have absolutely no idea why the deallocate is commented-out.

The optimised internal implementation is especially
important for textio, because the defined behaviour does
a great deal of very inefficient copying and allocating of
strings. I'm sure that the internal version is much more
efficient.

Maybe one of the tool vendors can comment in more
detail, and correct any misunderstandings in my
description?
--
Jonathan Bromley, Consultant

DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services

Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK

http://www.MYCOMPANY.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.


Jonathan Bromley
  Reply With Quote
Old 09-03-2006, 12:37 PM   #5
Nicolas Matringe
 
Posts: n/a
Default Re: std.textio, readline and memory deallocation
Jonathan Bromley a écrit :

> Nicolas, I think you are right; but the standard packages,
> including textio, are specially implemented in most simulators,
> so the provided source code is only a reference implementation
> and the internal implementation is probably quite different.


I thought so too, and I was indeed quite suprised to find VHDL source
for the standard, built-in, packages.


> I have absolutely no idea why the deallocate is commented-out.


So do I. Anyone at Mentor Graphics around ?


> The optimised internal implementation is especially
> important for textio, because the defined behaviour does
> a great deal of very inefficient copying and allocating of
> strings.


I noticed that too, since this is exactly what I was curious about.


> I'm sure that the internal version is much more efficient.


I hope so

Fun to talk about pointers after many years of NOT doing any software
programming

Nicolas


Nicolas Matringe
  Reply With Quote
Old 09-03-2006, 11:15 PM   #6
Paul Uiterlinden
 
Posts: n/a
Default Re: std.textio, readline and memory deallocation
Nicolas Matringe wrote:

> The problem as I see it is that the line type is a pointer to a
> string. If you reallocate the pointer, the string still exists but
> nothing points to it.


Yes, what you describe is the standard recipe for creating a memory
leak in VHDL.

> What happens to the memory if I run this code :
>
> while not endfile(my_file) loop
> readline(my_file, a_line);
> end loop;
>
> All the lines should be somewhere in the memory but a_line will only
> point to the last one.


You're absolutely right, so don't do that!
I guess you know by now the code should look something like:

while not endfile(my_file) loop
readline(my_file, a_line);

-- do something with a_line

-- done processing the line read from file
deallocate(a_line);
end loop;

> Or am I missing something ?


Just the deallocate. As for understanding what the code does, you're
spot on and not missing anything.

>
> Nicolas
> I'll have to


Nah...

--
Paul.



Paul Uiterlinden
  Reply With Quote
Old 09-03-2006, 11:16 PM   #7
Paul Uiterlinden
 
Posts: n/a
Default Re: std.textio, readline and memory deallocation
Jonathan Bromley wrote:

> Nicolas, I think you are right; but the standard packages,
> including textio, are specially implemented in most simulators,
> so the provided source code is only a reference implementation
> and the internal implementation is probably quite different.
> I have absolutely no idea why the deallocate is commented-out.


As far as I know the declaration of readline in the LRM is using mode
out for parameter l. So deallocation of the actual parameter by
readline just is not possible.

> The optimised internal implementation is especially
> important for textio, because the defined behaviour does
> a great deal of very inefficient copying and allocating of
> strings. I'm sure that the internal version is much more
> efficient.


Granted, but still they must stick to the LRM declaration of readline.

> Maybe one of the tool vendors can comment in more
> detail, and correct any misunderstandings in my
> description?


Or perhaps there is some action going on in the standardisation
comittee to change the declaration of readline and MTI put the
commented out code in as a reminder for future change. I don't know,
I'm just guessing here.

--
Paul.


Paul Uiterlinden
  Reply With Quote
Old 09-04-2006, 12:46 AM   #8
Duane Clark
 
Posts: n/a
Default Re: std.textio, readline and memory deallocation
Paul Uiterlinden wrote:
> ...
> Or perhaps there is some action going on in the standardisation
> comittee to change the declaration of readline and MTI put the
> commented out code in as a reminder for future change. I don't know,
> I'm just guessing here.
>


I don't have a copy of current standards, but the 1993 standard says
(page 204) in reference to readline "If parameter L contains a nonnull
access value at the start of the call, the object designated by that
value is deallocated before the new object is created".



Duane Clark
  Reply With Quote
Old 09-04-2006, 08:02 AM   #9
Nicolas Matringe
 
Posts: n/a
Default Re: std.textio, readline and memory deallocation
Duane Clark a écrit :

> I don't have a copy of current standards, but the 1993 standard says
> (page 204) in reference to readline "If parameter L contains a nonnull
> access value at the start of the call, the object designated by that
> value is deallocated before the new object is created".



Page 214 of the draft copy of the 2000 standard still says so (which I
think is a very sensible recommendation )

Nico


Nicolas Matringe
  Reply With Quote
Old 09-04-2006, 01:15 PM   #10
Paul Uiterlinden
 
Posts: n/a
Default Re: std.textio, readline and memory deallocation
Duane Clark wrote:

> I don't have a copy of current standards, but the 1993 standard says
> (page 204) in reference to readline "If parameter L contains a
> nonnull access value at the start of the call, the object designated
> by that value is deallocated before the new object is created".


Intriguing. How on earth could that work with an mode out parameter?
The subprogram (readline in this case) does not receive the value of
l.

Are you sure that the lines you quote refer to the readline procedure?
I don't have a copy of the LRM available right now.

Wow, hold on! I just put your quoted line into google, and guess what:
one hit (thank you for the exact quote):

http://www.vhdl.org/pub/vasg/vasg-li...2000/0039.html

-------------------- Quote --------------------

LCS ??/IR ????

This is an issue I identified when writing The Designer's Guide to
VHDL, but no IR was ever generated.

The procedure READLINE in TEXTIO has a parameter L : out LINE. 14.3
line 985 states

If parameter L contains a nonnull access value at the start of the
call, the object designated by that value is deallocated before the
new object is created.

For this to occur, the parameter should be INOUT.

Proposed resolution: change the mode of L in READLINE to INOUT.

-------------------- End quote --------------------

Well, there you have it: at some point in time, there was talk about
changing the mode of parameter l (and maybe there still is). That
might explain the commented out parts in MTIs code.

--
Paul.



Paul Uiterlinden
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46