├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── examples └── map.php ├── lib ├── MMap │ └── StreamWrapper.php └── registration.php └── subprocess └── mmap-proxy.py /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/* 2 | 3 | composer.phar 4 | composer.lock 5 | 6 | /vendor/ 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Michael Calcinai 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # php-mmap 2 | This library is a lightweight implementation of c's mmap. In previous versions of PHP, `fopen` used mmap where possible, but it looks like 3 | this was dropped in 5.3. The actual mapping is delegated to a python subprocess for compatibility and compilability . 4 | 5 | This started as a component of another project, but it made more sense for it to be stand alone. It is synchronous in operation, but if 6 | you're reading large amounts of data it can be done piece by piece. 7 | 8 | I have tried to make it functionally equivalent to c's implementation, but it's not quite complete due to some limitations. 9 | 10 | I have also written [a compatible extension](https://github.com/calcinai/php-ext-mmap) that you can compile and install as a faster drop-in replacement 11 | 12 | ## Setup 13 | 14 | Using composer: 15 | 16 | ```json 17 | "require": { 18 | "calcinai/php-mmap": "^0.1" 19 | } 20 | ``` 21 | 22 | ## Usage 23 | 24 | Via a URI (of sorts) 25 | ```php 26 | $mmap = fopen('mmap:///dev/mem:1024?offset=0', 'rw'); 27 | ``` 28 | 29 | Via wrapper method 30 | ```php 31 | $mmap = mmap_open('/dev/mem', 1024, 0); 32 | ``` 33 | 34 | You can use `fseek()`, `fread()` and `fwrite()` on the stream. If you have a use for others, let me know. 35 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "calcinai/php-mmap", 3 | "description": "mmap for PHP", 4 | "type": "library", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Michael Calcinai", 9 | "email": "michael@calcin.ai" 10 | } 11 | ], 12 | "require": { 13 | "php": ">=5.4.0" 14 | }, 15 | "suggest": { 16 | "ext-mmap": "Much more efficient native extension" 17 | }, 18 | "autoload": { 19 | "psr-4": { 20 | "Calcinai\\MMap\\": "lib/MMap" 21 | }, 22 | "files": ["lib/registration.php"] 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /examples/map.php: -------------------------------------------------------------------------------- 1 | 5 | */ 6 | 7 | namespace Calcinai\MMap; 8 | 9 | class StreamWrapper { 10 | 11 | private $process; 12 | private $pipes; 13 | 14 | private $size; 15 | private $position; 16 | 17 | const COMMAND_SEEK = 's'; 18 | const COMMAND_TELL = 't'; 19 | const COMMAND_READ = 'r'; 20 | const COMMAND_WRITE = 'w'; 21 | const COMMAND_EXIT = 'e'; 22 | 23 | /** 24 | * @param $path 25 | * @param $mode 26 | * @return bool 27 | */ 28 | public function stream_open($path, $mode){ 29 | 30 | //Yuck. 31 | $subprocess_path = __DIR__ .'/../../subprocess/mmap-proxy.py'; 32 | 33 | $components = self::parseURI($path); 34 | $offset = isset($components['options']['offset']) ? $components['options']['offset'] : 0; 35 | 36 | //Test it can be opened in the correct mode before passing to child 37 | $test = @fopen($components['file_name'], $mode); 38 | if($test === false){ 39 | return false; 40 | } 41 | fclose($test); 42 | 43 | $subprocess_cmd = sprintf('python -u %s %s %d %d', $subprocess_path, $components['file_name'], $components['block_size'], $offset); 44 | 45 | $descriptorspec = [ 46 | 0 => ['pipe', 'r'], 47 | 1 => ['pipe', 'w'], 48 | //2 => ['pipe', 'w'] //Show it in the console / don't hijack stderr 49 | ]; 50 | 51 | 52 | $this->process = proc_open($subprocess_cmd, $descriptorspec, $this->pipes); 53 | $this->size = $components['block_size']; 54 | $this->position = 0; 55 | 56 | return is_resource($this->process); 57 | } 58 | 59 | /** 60 | * 61 | */ 62 | public function stream_close(){ 63 | 64 | if(is_resource($this->process)){ 65 | $this->subprocess_write(self::COMMAND_EXIT); 66 | proc_close($this->process); 67 | } 68 | } 69 | 70 | /** 71 | * TODO send whence 72 | * 73 | * @param $address 74 | * @param int $whence 75 | * @return bool 76 | */ 77 | public function stream_seek($address, $whence = SEEK_SET){ 78 | $this->subprocess_write(self::COMMAND_SEEK, pack('v', $address)); 79 | 80 | //This is an assumption that the stream will always seek where its sold - can get some info back if this fails. 81 | $this->position = $address; 82 | return true; 83 | } 84 | 85 | /** 86 | * @return mixed 87 | */ 88 | public function stream_tell(){ 89 | return $this->position; 90 | } 91 | 92 | /** 93 | * @param $length 94 | * @return string 95 | */ 96 | public function stream_read($length){ 97 | $this->subprocess_write(self::COMMAND_READ, pack('v', $length)); 98 | 99 | //Assume success 100 | $this->position += $length; 101 | return $this->subprocess_read($length); 102 | } 103 | 104 | /** 105 | * @param $data 106 | * @return int 107 | */ 108 | public function stream_write($data){ 109 | 110 | $length = strlen($data); 111 | $this->subprocess_write(self::COMMAND_WRITE, pack('v', $length).$data); 112 | 113 | //Assume success for now 114 | $this->position += $length; 115 | return $length; 116 | } 117 | 118 | /** 119 | * @return mixed 120 | */ 121 | public function stream_eof(){ 122 | return $this->position >= $this->size; 123 | } 124 | 125 | 126 | /** 127 | * @param $command 128 | * @param $data 129 | */ 130 | private function subprocess_write($command, $data = ''){ 131 | fwrite($this->pipes[0], $command.$data); 132 | } 133 | 134 | /** 135 | * @param $length 136 | * @return string 137 | */ 138 | private function subprocess_read($length){ 139 | $data = fread($this->pipes[1], $length); 140 | return $data; 141 | } 142 | 143 | 144 | /** 145 | * Can't parse_url as it's too malformed 146 | * 147 | * @param $uri 148 | * @return array 149 | * @throws \Exception 150 | */ 151 | private static function parseURI($uri){ 152 | //Remove protocol (for clarity). 153 | $uri = substr($uri, strlen('mmap://')); 154 | $parts = explode('?', $uri); 155 | 156 | $file_name_block_size = explode(':', $parts[0]); 157 | 158 | if(!isset($file_name_block_size[1])){ 159 | throw new \Exception(sprintf('%s is not a valid uri', $uri)); 160 | } 161 | 162 | $parsed = []; 163 | 164 | list($parsed['file_name'], $parsed['block_size']) = $file_name_block_size; 165 | 166 | if(isset($parts[1])){ 167 | //Extra params 168 | parse_str($parts[1], $parsed['options']); 169 | } else { 170 | $parsed['options'] = []; 171 | } 172 | 173 | return $parsed; 174 | } 175 | 176 | public function __destruct() { 177 | $this->stream_close(); 178 | } 179 | 180 | public static function register(){ 181 | stream_register_wrapper('mmap', __CLASS__); 182 | } 183 | } 184 | -------------------------------------------------------------------------------- /lib/registration.php: -------------------------------------------------------------------------------- 1 | 5 | */ 6 | 7 | 8 | use Calcinai\MMap\StreamWrapper; 9 | 10 | if(!function_exists('mmap_open')){ 11 | 12 | StreamWrapper::register(); 13 | 14 | //Kindof a backward way go get it as a resource. 15 | function mmap_open($file_name, $block_size, $offset = 0){ 16 | //TODO - finish these 17 | return fopen(sprintf('mmap://%s:%s?offset=%s', $file_name, $block_size, $offset), 'rw'); 18 | } 19 | } -------------------------------------------------------------------------------- /subprocess/mmap-proxy.py: -------------------------------------------------------------------------------- 1 | import os 2 | import sys 3 | import mmap 4 | import struct 5 | 6 | filename = sys.argv[1] 7 | block_size = int(sys.argv[2], 0) 8 | offset = int(sys.argv[3], 0) 9 | 10 | fd = os.open(filename, os.O_RDWR | os.O_SYNC) 11 | mem = mmap.mmap(fd, block_size, offset=offset) 12 | os.close(fd) 13 | 14 | try: 15 | while True: 16 | command = sys.stdin.read(1) 17 | 18 | if(command == 's'): 19 | address = struct.unpack('