In the block of shared memory, the information that comes frequently (15 - 25 times per second) must be constantly updated. The sizes of each “chunk” are different, and when reading it is desirable to have the exact size of the currently recorded data. With shmop_write , if the new data is shorter than the old, they will be written from zero offset, and the back of the old data remains.

Is it possible to somehow change the size of the memory block without permanently creating / deleting it with each data receipt? Like realloc from C, so that there are no "tails"?

  • one
    just precede the data with a number of their length or do padding - etki
  • 15-25 times a second, this is nonsense. Any memcached, redis handle. But it will be possible to read and write like a language from anywhere. Just a little clear task and the choice of tool. - E_p
  • Plus, PHP has a set of structures that are "closer to hardware" php.net/manual/en/spl.datastructures.php . - E_p
  • The task is to write data to the same memory address so that if the length of the new line is less than what has already been written to this address, the new line does not have a “tail” from the old one. And all) That is, update the size of the allocated memory. @etki, with padding did not understand - how will he reduce the buffer? - Iceman
  • one
    Yes, you do not pull out the magic user interface from local users if it is not in the documentation. If you have padding bytes that cannot be encountered in the string, then in this case you simply cut off all the excess to the right and do not even worry about what size there is. - etki

1 answer 1

Something like this:

 <?php class MemoryManager { public function __construct($maxSize, $maxClients) { // Validation $shm_key = ftok(__FILE__, 't'); $this->_memoryPointer = shmop_open($shm_key, "c", 0644, $maxSize * $maxClients); $this->_maxSize = $maxSize; $this->_maxClient = $maxSize; } public function writeClientData($clientId, $data) { $paddedData = str_pad($data, $this->_maxSize, $this->_padSymbol); shmop_write( $this->_memoryPointer, $paddedData, ($clientId - 1) * $this->_maxSize ); } public function readClientData($clientId) { $data = shmop_read( $this->_memoryPointer, ($clientId - 1) * $this->_maxSize, $this->_maxSize ); return trim($data, $this->_padSymbol); } public function readAll() { $data = shmop_read( $this->_memoryPointer, 0, $this->_maxSize*$this->_maxClient ); $string = preg_replace( sprintf("/(\%s+)/", $this->_padSymbol), $this->_padSymbol, trim($data, "\000" . $this->_padSymbol) ); return explode($this->_padSymbol, $string); } public function close() { shmop_delete($this->_memoryPointer); shmop_close($this->_memoryPointer); } private $_padSymbol = "|"; } $mem = new MemoryManager(100, 100); $mem->writeClientData(1, 'aaaaa'); $mem->writeClientData(2, 'bbbbbbbb'); var_dump($mem->readClientData(1)); var_dump($mem->readClientData(2)); $mem->writeClientData(2, 'cc'); var_dump($mem->readClientData(2)); var_dump($mem->readAll()); $mem->close(); 
  • There is validation missing everywhere. And maybe it makes sense to change | On the other symbol, well, not the essence. - E_p
  • And you suggested an idea! ) If you set a fixed buffer, for example, 20 kb, make str_pad to the newly arrived image to the required length (that is, fill the remaining buffer with spaces), and return the trim-string - also a class! And what will work faster, this option, or parsing of substrings for length values? It looks like the first one, huh? - Iceman
  • @Iceman Yes, vseravno, you can test. It seems to me there will be other problems than this pair of milliseconds. - E_p
  • Well, I have now tested with the parsing of substrings - it works as it should, though I have not checked it on the stream yet. But these pairs of milliseconds are very influential, since on the client it is necessary to generate this image, transfer it to the server, another client has to pick it up, which is also a delay, and the video should go smoothly). Anyway, thanks for the tip, +1! - Iceman
  • #Iceman Try. Unsubscribe. Even wondering what the result will be. - E_p