std.textio, readline and memory deallocation

Discussion in 'VHDL' started by Nicolas Matringe, Sep 1, 2006.

  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, Sep 1, 2006
    #1
    1. Advertising

  2. 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, Sep 1, 2006
    #2
    1. Advertising

  3. 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, Sep 2, 2006
    #3
  4. 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, Sep 3, 2006
    #4
  5. 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, Sep 3, 2006
    #5
  6. 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, Sep 3, 2006
    #6
  7. 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, Sep 3, 2006
    #7
  8. Nicolas Matringe

    Duane Clark Guest

    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, Sep 4, 2006
    #8
  9. 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, Sep 4, 2006
    #9
  10. 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-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.

    --
    Paul.
     
    Paul Uiterlinden, Sep 4, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qazmlp
    Replies:
    1
    Views:
    768
    John Harrison
    Mar 7, 2004
  2. Magne Munkejord
    Replies:
    1
    Views:
    3,752
    Magne Munkejord
    Aug 23, 2006
  3. Replies:
    13
    Views:
    1,088
  4. James Unterburger

    std.textio.read strange behaviour?!

    James Unterburger, Jun 26, 2008, in forum: VHDL
    Replies:
    3
    Views:
    543
    James Unterburger
    Jun 26, 2008
  5. Zhane
    Replies:
    0
    Views:
    625
    Zhane
    Apr 25, 2009
Loading...

Share This Page