Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Thanks xiper++ once again for the excellent feedback! :-D

I have made several improvements after reading your comments...

1) no explicit Init statement required
The default MMF namespace (shared memory) will be created if the user has not explicitly provided namespace parameters. Once initialized, all subsequent tie() will use the default namespace, unless otherwise specified in the parameter list to tie().
use strict; use Win32::MMF::Shareable; use Data::Dumper; my $ns = tie my $ref, 'Win32::MMF::Shareable', '$ref'; tie my $alias, 'Win32::MMF::Shareable', '$ref'; $ref = { a => 1, b => 2, c => 3 }; $alias->{d} = 4; print Dumper($ref); $ref = [ qw( a b c d e f g h ) ]; print Dumper($alias); # $ns->debug();
__OUTPUT__ $VAR1 = { 'c' => '3', 'a' => '1', 'b' => '2', 'd' => 4 }; $VAR1 = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' ];

2) Namespace consideration
Win32::MMF has a slightly different design to IPC::Shareable in shared memory management. The following diagram is a view of a MMF namespace:
+-----------------+ | Var1 definition | +-----------------+ | Var2 definition | +-----------------+<- Heap Top | | | | | | | | | | | | | | | | |-----------------|<- Watermark | | | | | | | | +-----------------+<- Heap Bottom | MMF Descriptor | +-----------------+
Every MMF created carries a MMF descriptor table that contains information about the size of the MMF, number of variables declared inside the namespace and heap information. Variable definition starts from the top of MMF and grows downward. The variable definition carries a pointer if there is associated data for the variable. Memory is allocated from the heap bottom and grows upward to hold the actual data. When a variable is deleted, the MMF manager deletes the variable definition and frees the memory allocated to the variable data.

A MMF contains everything a process need to know about, because all the memory management information is carried inside the MMF itself. So that when one process adds variable, another process immediately sees it, and can use it without any additional initialization steps.

The tie() wrapper does not directly control how a variable is stored inside the MMF. It doesn't care how much memory is required to store a particular tied variable, the MMF manager automatically shrinks or grows the memory allocated to the variable, as long as there is still enough memory left in the heap.

# key ĘC same as your 'varid', suggest you use this, or at least accept it as an alias

The Win32::MMF::Shareable module is designed as a drop-in replacement to IPC::Shareable under Windows, you can use it the same way as IPC::Shareable. Options like mode, destroy, are ignored. I have created the copied the following demo from then IPC::Shareable demo in the POD, replaced use IPC::Shareable to automatically switch between Win32::MMF::Shareable and IPC::Shareable on different platform, leaving the rest of the code untouched.
use strict; # ---------------------------------------------------------------- # code to switch between Win32::MMF::Shareable and IPC::Shareable # automatically on different platforms # ---------------------------------------------------------------- use vars qw/ $Shareable /; BEGIN { $Shareable = ($^O eq 'MSWin32') ? 'Win32::MMF::Shareable' : 'IPC::Shareable'; eval "require $Shareable"; $Shareable->import(); } # ---------------------------------------------------------------- my $glue = 'data'; my %options = ( create => 0, exclusive => 0, mode => 0644, destroy => 0, ); my %colours; tie %colours, $Shareable, $glue, { %options } or die "client: tie failed\n"; foreach my $c (keys %colours) { print "client: these are $c: ", join(', ', @{$colours{$c}}), "\n"; } delete $colours{'red'}; exit;

# The two modules Win32::MMF and Win32::MMF::Shareable, while implemented in a similar way, behave quiet differently to the user. It may be a good idea to document each separately to avoid confusion.

Will do.

# In the pod: "Because memory and variable management are managed internally by the Win32::MMF module, you do not need to specify how much memory is required by the variable." Is this correct? You don't have to specify a size for a tied variable? Will it automatically expand if the data grows? If so, why not have this for non-tied variables as well?

That's correct, the MMF manager decides the correct amount of memory required inside the MMF to hold the object, so it can be correctly recreated from another process. This applies to untied variables too.

# How does your module handle the issue of when to clear the data in shared memory? I assume that it's when the last 'connected' process exits or tied variable falls out of scope, correct? What about if the process is killed (via a SIGTERM)? Any possible memory leaks? (I assume you've already covered these issues, just interested to know).

Currently it's up to Windows own memory management system to handle a killed process. Windows will destroy the MMF if all processes accessing it are killed. (A least I hope so.)

In reply to Re: Re: Re: Re: Win32::MMF - Memory Mapped File Support for Perl by Roger
in thread (Updated Again) Win32::MMF - Memory Mapped File Support for Perl by Roger

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2023-06-04 10:30 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (20 votes). Check out past polls.