Michael G Schwern wrote:
> 
> On Wed, Oct 02, 2002 at 01:50:56PM +0200, H.Merijn Brand wrote:
> >  SYNOPSIS
> >         use Devel::Internals;
> 
> A little broad.  Perhaps Devel::Memory?
> 
> >         my $end = sbrk ();
> >
> >         my @array = (1..10000);
> >         print "The creation of this array used ",
> >             sbrk () - $end, "bytes\n";
> >
> >         # Easier
> >         use Devel::Internals;
> >
> >         my @array = (1..10000);
> >         MemUsed "creation of array";
> >
> >         my @dup = @array;
> >         MemUsed "duplication of array";
> >
> >  DESCRIPTION
> >       sbrk
> >           returns the current memory top. See sbrk(2) manual page
> 
> I realize that sbrk() is a familiar concept to deep C programmers on
> BSD-style systems, but in order for this to be useful to Perl
> programmers, or even users not familiar with BSD, you're going to have
> to name it something more descriptive than sbrk() and explain better
> what it does.

If "what it does" is merely return the value of the C sbrk() function,
then IMHO, sbrk() is a perfectly good name.

However, as to other possible names -- how about ProcessSize() ?  I'm
not sure if this is really a valid description of what sbrk() returns,
though.

> It can still do the same thing as sbrk(), and you might even want to
> leave in an alias to sbrk() so BSD core hackers can feel all warm and
> fuzzy.
> 
> >       MemUsed ($msg)
> >           Used in void context reports the number of bytes used
> >           since the previous call to MemUsed with the message passed
> 
> Global state is a Bad Thing.  Consider when you have two different,
> unrelated subroutines both calling MemUsed() in the same program. 
> foo() calls MemUsed() then bar() then foo() calls MemUsed() again
> expecting to get the memory used since it's last call but it's
> actually getting the memory used since bar() last called it.

Perhaps MemUsed should keep track of where it was called from?

Eg:
   sub MemUsed {
      my $sbrk = sbrk();
      my $prior = \ $sbrks{join "::", (caller(1))[3, 0] };
      if( defined $$prior ) {
         my $diff = $sbrk - $$prior;
         print ....;
      }
      $$prior = $sbrk;
   }

This works fine as long as there is no recursion.
If there *is* recursion, then the user need to stuff the return value of
sbrk() in a variable, or somesuch.

-- 
How many Monks would a Chipmonk chip,
if a Chipmonk could chip Monks?

Reply via email to