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

Reply

VHDL - return a variable size string

 
Thread Tools Search this Thread
Old 11-28-2007, 08:13 PM   #1
Default return a variable size string


I am writing a to_string function for a type that I have defined. I
have used the STD.TEXTIO' write function to write a string
representation of the type that is of variable length to a line (L).
The function looks something like below:

impure function to_string( ... ) return string is
variable L : line;
begin
write( L, string'("+--------+") & CR );
for i in .... loop
write( L, ... );
end loop;
write( L, ... );
return L.all;
end function to_string;

L is an access type and write function keeps deallocating L and
allocating more storage (the string that L points to would grow in
size from write to write). My question is that, normally it is best
to release the storage pointed to by L by calling deallocate on L.
But in this case I cannot call deallocate(L) explicitly before the
function returns. Do simulators release the storage after the
function returns?

-- Amal


Amal
  Reply With Quote
Old 11-28-2007, 09:00 PM   #2
Jonathan Bromley
 
Posts: n/a
Default Re: return a variable size string
On Wed, 28 Nov 2007 12:13:05 -0800 (PST), Amal wrote:

>>L is an access type and write function keeps deallocating L and

>allocating more storage (the string that L points to would grow in
>size from write to write). My question is that, normally it is best
>to release the storage pointed to by L by calling deallocate on L.
>But in this case I cannot call deallocate(L) explicitly before the
>function returns. Do simulators release the storage after the
>function returns?


I'm afraid I don't know - in this particular case, presumably,
a sufficiently clever tool could see that the access variable
L cannot be "given away" by the function and therefore it
can safely be deallocated on exit. But I agree with
you that it feels wrong.

One trick for getting around this is to move variable L
outside your function, and then wrap the whole thing in
yet another function. Like this (in which the "to_string"
function merely makes a string of N asterisks, but you'll
get the idea):

impure function to_string(n: positive) return string is
variable L: line;
impure function to_string_inner(n: positive) return string is
begin
for i in 1 to n loop
write(L, character' ('*') );
end loop;
return L.all;
end;
constant s: string := to_string_inner(n);
begin
deallocate(L);
return s;
end;

Someone else may have some nicer ideas...
--
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 11-28-2007, 10:22 PM   #3
Mike Treseler
 
Posts: n/a
Default Re: return a variable size string
Amal wrote:

> But in this case I cannot call deallocate(L) explicitly before the
> function returns. Do simulators release the storage after the
> function returns?


I don't know, because I find that writing
my own string conversion functions is more
efficient than tinkering with std.textio.
Here's some examples:
http://home.comcast.net/~mike_treseler/min_vec_len.vhd

I do use std.textio.write to print strings to
modelsim's standard output (std.textio.output) like this:

procedure echo (
arg : in string := "") is
begin
std.textio.write(std.textio.output, arg);
end procedure echo;

Then I can say something like this in a testbench

echo("The answer is " & odd2string(my_oddity) & LF & LF);

-- Mike Treseler



Mike Treseler
  Reply With Quote
Old 11-29-2007, 03:13 AM   #4
kennheinrich@sympatico.ca
 
Posts: n/a
Default Re: return a variable size string
On Nov 28, 5:22 pm, Mike Treseler <mike_trese...@comcast.net> wrote:
> Amal wrote:
> > But in this case I cannot call deallocate(L) explicitly before the
> > function returns. Do simulators release the storage after the
> > function returns?

>


It shouldn't matter that write may repeatedly reallocate L; L is an
inout parameter and (although it's not explicitly stated as such in
the LRM), 'write' ought to be managing the allocated storage and
updating L appropriately. The only thing that matters is the L that
you are leaving as garbage when you exit.

The "wrapper" above makes sense. Alternately you could return L
directly and rely on the caller to free L (admittedly ugly).

The LRM states that "An implementation may (but need not) deallocate
the storage occupied by an object created by an allocator, once this
object has become inaccessible." So I think the answer to your final
question is "maybe".

If you don't trust your implementation to deallocate L for you, I
don't think you have a much better solution that using the wrapper.

Also, depending on how you want your data formatted, you might also be
able to make creative use of the 'image attribute with catenation to
avoid dynamically allocated objects completely.

HTH,

- Kenn



kennheinrich@sympatico.ca
  Reply With Quote
Old 11-29-2007, 08:51 PM   #5
Paul Uiterlinden
 
Posts: n/a
Default Re: return a variable size string
Jonathan Bromley wrote:

> One trick for getting around this is to move variable L
> outside your function, and then wrap the whole thing in
> yet another function. Like this (in which the "to_string"
> function merely makes a string of N asterisks, but you'll
> get the idea):
>
> impure function to_string(n: positive) return string is
> variable L: line;
> impure function to_string_inner(n: positive) return string is
> begin
> for i in 1 to n loop
> write(L, character' ('*') );
> end loop;
> return L.all;
> end;
> constant s: string := to_string_inner(n);
> begin
> deallocate(L);
> return s;
> end;
>
> Someone else may have some nicer ideas...


They don't come nicer than that one.
Elegant and simple.
I like it!

--
Paul Uiterlinden
www.aimvalley.nl
e-mail addres: remove the not.


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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Give you enough string functions in Java web reporting tool freezea Software 0 10-08-2009 09:03 AM
Java String Problems rbnbenjamin General Help Related Topics 0 02-03-2009 11:02 PM
Passing value with out using variable in query string in PHP! Ali_ggl General Help Related Topics 0 11-29-2008 12:22 PM
ASP.NET: Asign Users in Roles(Array.IndexOf(Of String) method) msandlana Software 0 04-25-2008 06:37 AM
Hidden linebreaks in string? VB.NET Jiggy Software 0 04-23-2008 02:18 PM




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