line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::MemoryMap; |
2
|
2
|
|
|
2
|
|
441940
|
use Mojo::Base -base; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
12
|
|
3
|
|
|
|
|
|
|
|
4
|
2
|
|
|
2
|
|
1273
|
use File::Map qw(map_anonymous); |
|
2
|
|
|
|
|
12326
|
|
|
2
|
|
|
|
|
8
|
|
5
|
2
|
|
|
2
|
|
197
|
use Mojo::File qw(tempfile); |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
83
|
|
6
|
2
|
|
|
2
|
|
998
|
use Mojo::MemoryMap::Writer; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
27
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub new { |
9
|
4
|
|
|
4
|
1
|
8315
|
my ($class, $size) = @_; |
10
|
4
|
|
100
|
|
|
39
|
my $self = $class->SUPER::new(size => $size // 52428800, usage => 0); |
11
|
|
|
|
|
|
|
|
12
|
4
|
|
|
|
|
49
|
$self->{tempfile} = tempfile->touch; |
13
|
4
|
|
|
|
|
2561
|
map_anonymous my $map, $self->{size}, 'shared'; |
14
|
4
|
|
|
|
|
182
|
$self->{map} = \$map; |
15
|
4
|
|
|
|
|
27
|
$self->writer->store({}); |
16
|
|
|
|
|
|
|
|
17
|
4
|
|
|
|
|
18
|
return $self; |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
5
|
|
|
5
|
1
|
48
|
sub size { shift->{size} } |
21
|
|
|
|
|
|
|
|
22
|
4
|
|
|
4
|
1
|
25
|
sub usage { shift->{usage} } |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub writer { |
25
|
288
|
|
|
288
|
1
|
3027784
|
my $self = shift; |
26
|
|
|
|
|
|
|
|
27
|
288
|
|
66
|
|
|
1109
|
my $fh = $self->{fh}{$$} ||= $self->{tempfile}->open('>'); |
28
|
288
|
|
|
|
|
1842
|
return Mojo::MemoryMap::Writer->new(fh => $fh, map => $self->{map}, usage => \$self->{usage}); |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
1; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=encoding utf8 |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head1 NAME |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
Mojo::MemoryMap - Safely use anonymous memory mapped segments |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=head1 SYNOPSIS |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
use Mojo::MemoryMap; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
my $map = Mojo::MemoryMap->new(4096); |
44
|
|
|
|
|
|
|
say $map->usage; |
45
|
|
|
|
|
|
|
$map->writer->store({foo => 123}); |
46
|
|
|
|
|
|
|
say $map->writer->fetch->{foo}; |
47
|
|
|
|
|
|
|
say $map->writer->change(sub { delete $_->{foo} }); |
48
|
|
|
|
|
|
|
say $map->usage; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=head1 DESCRIPTION |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
L uses L to allow you to safely cache mutable data structures in anonymous mapped memory |
53
|
|
|
|
|
|
|
segments, and share it between multiple processes. |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=head1 METHODS |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
L inherits all methods from L and implements the following new ones. |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=head2 new |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
my $map = Mojo::MemoryMap->new; |
62
|
|
|
|
|
|
|
my $map = Mojo::MemoryMap->new(4096); |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
Construct a new L object, defaults to a L"size"> of C<52428800> bytes (50 MiB). |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head2 size |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
my $size = $map->size; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
Size of anonymous memory segment in bytes. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head2 usage |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
my $usage = $map->usage; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
Current usage of anonymous memory segment in bytes. |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head2 writer |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
my $writer = $map->writer; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Acquire exclusive lock and return L object. Allowing the shared data structure to be retrieved |
83
|
|
|
|
|
|
|
and modified safely. The lock is released when the writer object is destroyed. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# Retrieve data |
86
|
|
|
|
|
|
|
my $data = $map->writer->fetch; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# Modify data safely |
89
|
|
|
|
|
|
|
my $writer = $map->writer; |
90
|
|
|
|
|
|
|
my $data = $writer->fetch; |
91
|
|
|
|
|
|
|
$data->{foo} += 23; |
92
|
|
|
|
|
|
|
$writer->store($data); |
93
|
|
|
|
|
|
|
undef $writer; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# Modify data safely (with less code) |
96
|
|
|
|
|
|
|
$map->writer->change(sub { $_->{foo} += 23 }); |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head1 SEE ALSO |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
L, L, L. |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=cut |