Added function "GetBinName" to CovPType

Why OSVVM™? Forums OSVVM Added function "GetBinName" to CovPType

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #1135

    Hello,

    In my testbench I ran into the requirement to have access to the “BinName”. Therfore I added the impure function GetBinName to CovPType.

    Here is the declaration:

    impure function GetBinName ( BinIndex : integer ) return string;

    Here is the implementation:

    impure function GetBinName ( BinIndex : integer ) return string is

      variable buf : line;

    begin

      if CovBinPtr(BinIndex).Name.all /= "" then

        swrite(buf, CovBinPtr(BinIndex).Name.all);

      else

        swrite(buf, "Nameless Bin");

      end if;

      return buf.all;

    end function GetBinName;

    #1136
    Jim Lewis
    Member

    Hi Sebastian,

    I can add that capability in the next release.  

    I note that in your code, you never deallocate buf and as a result have a memory leak.  You can fix this by simplifying your code to:

    impure function GetBinName ( BinIndex : integer ) return string is
    begin
      if CovBinPtr(BinIndex).Name.all /= "" then
        return CovBinPtr(BinIndex).Name.all) ;
      else
        return "Nameless Bin" ;
      end if;
    end function GetBinName;

    To be consistent with other OSVVM implementations, I will implement  something like the following:

    impure function GetBinName ( BinIndex : integer; DefaultName : string := "" ) return string is
    begin
      if CovBinPtr(BinIndex).Name.all /= "" then
        return CovBinPtr(BinIndex).Name.all) ;
      else
        return DefaultName ;
      end if;
    end function GetBinName;

    Cheers,
    Jim

    #1137
    Jim Lewis
    Member

    Hi Sebastian

    I have implemented my 2nd solution and pushed the solution to my GitHub development branch.  You can find it at:  https://github.com/JimLewis/OSVVM/tree/Dev

    Best Regards,

    Jim

    #1138

    Hi Jim,
    thank you very much for adding this function.

    But I am a little afraid that there seems to be no easy solution for handling strings with an unknown length. This is a standard issue in most of my procedures or functions.
    I always thought that a “local” variable inside a function or a procedure is stored on stack and is automatically freeed when running out of scope.
    So there must be something special about the type “line”. Probably some kind of a pointer, which allocates memory in the background.
    But what would be your advice for creating strings with an unknown length?

    Thanks,
    Sebastian

    #1139
    Torsten M.
    Member

    Line, as defined in textio package, is string access type:

    type line is access string;

    When I have strings of unknown length, I also use an access type. But maybe there is another option, which I don’t know…

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.