Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 7ba2de07b56071e1c85841840992a27ffc6f8dfe 0 parents
@klaussilveira authored
Showing with 237 additions and 0 deletions.
  1. +9 −0 LICENSE.txt
  2. +46 −0 README.markdown
  3. +159 −0 SimpleSHM.class.php
  4. +23 −0 demo.php
9 LICENSE.txt
@@ -0,0 +1,9 @@
+Copyright (c) 2011, Klaus Silveira and contributors
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+Neither the name of SimpleSHM nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46 README.markdown
@@ -0,0 +1,46 @@
+# SimpleSHM
+SimpleSHM is a simple and small abstraction layer for shared memory manipulation using PHP. It makes use of the SHMOP functions, built into most PHP packages.
+
+## Authors and contributors
+* [Klaus Silveira](http://www.klaussilveira.com) (Creator, developer, support)
+
+## License
+[New BSD license](http://www.opensource.org/licenses/bsd-license.php)
+
+## Todo
+* add support for arrays, by automatically serializing or converting to JSON
+* add support for objects, by using JSON
+* create a better documentation
+* error handling can, and should, be improved
+* test, test, test
+
+## About Shared Memory
+Shared Memory is an efficient mean of exchanging data between applications in the same machine. One application will create a memory portion which other processes can access, as long as they have the proper permissions. You can read more about it here: http://stereochro.me/assets/uploads/notes/dcom3/shmem.pdf
+
+## Using SimpleSHM
+The idea behind SimpleSHM is to keep things very easy to use. If you want better control and flexibility, you can always modify the class or extend it.
+
+```php
+<?php
+
+/**
+ * Creating new block, with a random ID
+ */
+$memory = new SimpleSHM;
+$memory->write('Sample');
+echo $memory->read();
+
+/**
+ * Creating new block, with an specified ID
+ */
+$new = new SimpleSHM(897);
+$new->write('Sample');
+echo $new->read();
+
+/**
+ * Reading an existing block, with the ID of 42
+ */
+$existing = new SimpleSHM(42);
+echo $existing->read();
+
+```
159 SimpleSHM.class.php
@@ -0,0 +1,159 @@
+<?php
+/**
+ * SimpleSHM
+ *
+ * A simple and small abstraction layer for Shared Memory manipulation using PHP
+ *
+ * @author Klaus Silveira <contact@klaussilveira.com>
+ * @package simpleshm
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+ * @version 0.1
+ */
+class SimpleSHM {
+
+ /**
+ * Holds the system id for the shared memory block
+ *
+ * @var int
+ * @access private
+ */
+ private $id;
+
+ /**
+ * Holds the shared memory block id returned by shmop_open
+ *
+ * @var int
+ * @access private
+ */
+ private $shmid;
+
+ /**
+ * Holds the default permission (octal) that will be used in created memory blocks
+ *
+ * @var int
+ * @access private
+ */
+ private $perms = 0644;
+
+
+ /**
+ * Shared memory block instantiation
+ *
+ * In the constructor we'll check if the block we're going to manipulate
+ * already exists or needs to be created. If it exists, let's open it.
+ *
+ * @access public
+ * @param string $id (optional) ID of the shared memory block you want to manipulate
+ */
+ public function __construct($id = null) {
+ if($id === null) {
+ $this->id = $this->generateID();
+ } else {
+ $this->id = $id;
+
+ if($this->exists($this->id)) {
+ $this->shmid = shmop_open($this->id, "w", 0, 0);
+ }
+ }
+ }
+
+ /**
+ * Generates a random ID for a shared memory block
+ *
+ * @access private
+ * @return int Randomly generated ID, between 1 and 65535
+ */
+ private function generateID() {
+ $id = mt_rand(1, 65535);
+ return $id;
+ }
+
+ /**
+ * Checks if a shared memory block with the provided exists or not
+ *
+ * In order to check for shared memory existance, we have to open it with
+ * reading access. If it doesn't exist, warnings will be cast, therefore we
+ * suppress those with the @ operator.
+ *
+ * @access private
+ * @param string $id ID of the shared memory block you want to check
+ * @return boolean True if the block exists, false if it doesn't
+ */
+ private function exists($id) {
+ $status = @shmop_open($id, "a", 0, 0);
+
+ if($status) {
+ $status = true;
+ } else {
+ $status = false;
+ }
+
+ return $status;
+ }
+
+ /**
+ * Sets the default permission (octal) that will be used in created memory blocks
+ *
+ * @access public
+ * @param string $perms Permission, in octal form, that will be used in created memory blocks
+ */
+ public function setPermissions($perms) {
+ $this->perms = $perms;
+ }
+
+ /**
+ * Writes on a shared memory block
+ *
+ * First we check for the block existance, and if it doesn't, we'll create it. Now, if the
+ * block already exists, we need to delete it and create it again with a new byte allocation that
+ * matches the size of the data that we want to write there. We mark for deletion, close the semaphore
+ * and create it again.
+ *
+ * @access public
+ * @param string $data The data that you wan't to write into the shared memory block
+ */
+ public function write($data) {
+ $size = mb_strlen($data, 'UTF-8');
+
+ if($this->exists($this->id)) {
+ shmop_delete($this->shmid);
+ shmop_close($this->shmid);
+ $this->shmid = shmop_open($this->id, "c", $this->perms, $size);
+ shmop_write($this->shmid, $data, 0);
+ } else {
+ $this->shmid = shmop_open($this->id, "c", $this->perms, $size);
+ shmop_write($this->shmid, $data, 0);
+ }
+ }
+
+ /**
+ * Reads from a shared memory block
+ *
+ * @access public
+ * @return string The data read from the shared memory block
+ */
+ public function read() {
+ $size = shmop_size($this->shmid);
+ $data = shmop_read($this->shmid, 0, $size);
+
+ return $data;
+ }
+
+ /**
+ * Mark a shared memory block for deletion
+ *
+ * @access public
+ */
+ public function delete() {
+ shmop_delete($this->shmid);
+ }
+
+ /**
+ * Closes the shared memory block and stops manipulation
+ *
+ * @access public
+ */
+ public function __destruct() {
+ shmop_close($this->shmid);
+ }
+}
23 demo.php
@@ -0,0 +1,23 @@
+<?php
+
+require('SimpleSHM.class.php');
+
+/**
+ * Creating new block, with a random ID
+ */
+$memory = new SimpleSHM;
+$memory->write('Sample');
+echo $memory->read();
+
+/**
+ * Creating new block, with an specified ID
+ */
+$new = new SimpleSHM(897);
+$new->write('Sample');
+echo $new->read();
+
+/**
+ * Reading an existing block, with the ID of 42
+ */
+$existing = new SimpleSHM(42);
+echo $existing->read();
Please sign in to comment.
Something went wrong with that request. Please try again.