std.textio, readline and memory deallocation

  • Thread starter Nicolas Matringe
  • Start date
N

Nicolas Matringe

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
 
P

Paul Uiterlinden

Nicolas said:
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.
 
N

Nicolas Matringe

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)

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
 
J

Jonathan Bromley

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
(e-mail address removed)
http://www.MYCOMPANY.com

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

Nicolas Matringe

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
 
P

Paul Uiterlinden

Nicolas said:
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...
 
P

Paul Uiterlinden

Jonathan said:
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.
 
D

Duane Clark

Paul said:
...
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".
 
N

Nicolas Matringe

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
 
P

Paul Uiterlinden

Duane said:
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-list/hm.pre2000/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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top