T
Tricky
Guest
This cropped up today while writing a testbench. Please look at the following code - it manages a linked list:
type queue is protected
procedure add_data(a : unsigned);
procedure get_data(a : out unsigned);
end protected queue;
type queue is protected body
type link_t;
type link_ptr_t is access link_t;
type link_t is record
data : unsigned;
next_link : link_ptr_t;
end record link_t;
variable start_of_list : link_ptr_t;
procedure add_data(a : unsigned) is
variable ptr : link_ptr_t := null;
begin
ptr := new link_t'( (a, start_of_list) );
start_of_list := ptr;
end procedure add_data;
procedure get_data(a : out unsigned) is
variable ptr : link_ptr_t := null;
begin
a := start_of_list.data;
ptr := start_of_list.next_link;
DEALLOCATE(start_of_list);
start_of_list := ptr;
end procedure get_data;
end protected body queue;
Ideally, the get_data procedure would actually be a function (as I have done in the past) but as the data field in the link_t is unconstrained in the type, I cannot create a temporary variable to place the data into before returning it from the function, as I dont know the length of the data field before pulling it out of the list.
Would the above be the best way of doing it, or can some others of you work out a way a function would work instead?
type queue is protected
procedure add_data(a : unsigned);
procedure get_data(a : out unsigned);
end protected queue;
type queue is protected body
type link_t;
type link_ptr_t is access link_t;
type link_t is record
data : unsigned;
next_link : link_ptr_t;
end record link_t;
variable start_of_list : link_ptr_t;
procedure add_data(a : unsigned) is
variable ptr : link_ptr_t := null;
begin
ptr := new link_t'( (a, start_of_list) );
start_of_list := ptr;
end procedure add_data;
procedure get_data(a : out unsigned) is
variable ptr : link_ptr_t := null;
begin
a := start_of_list.data;
ptr := start_of_list.next_link;
DEALLOCATE(start_of_list);
start_of_list := ptr;
end procedure get_data;
end protected body queue;
Ideally, the get_data procedure would actually be a function (as I have done in the past) but as the data field in the link_t is unconstrained in the type, I cannot create a temporary variable to place the data into before returning it from the function, as I dont know the length of the data field before pulling it out of the list.
Would the above be the best way of doing it, or can some others of you work out a way a function would work instead?