![]() |
|
|
|||||||
![]() |
VHDL - std.textio, readline and memory deallocation |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
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 |
|
|
|
|
#2 |
|
Posts: n/a
|
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 |
|
|
|
#3 |
|
Posts: n/a
|
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 |
|
|
|
#4 |
|
Posts: n/a
|
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 |
|
|
|
#5 |
|
Posts: n/a
|
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 |
|
|
|
#6 |
|
Posts: n/a
|
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 |
|
|
|
#7 |
|
Posts: n/a
|
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 |
|
|
|
#8 |
|
Posts: n/a
|
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 |
|
|
|
#9 |
|
Posts: n/a
|
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 |
|
|
|
#10 |
|
Posts: n/a
|
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 |
|