Swoole Buffer

Memory management module enable developers managing memory like C language without worrying about memory allocation and release.

The memory allocated by swoole_buffer is not shared memory and can not be accessed by multiple processes.

Example

<?php
$buffer = new swoole_buffer();
$buffer->append(str_repeat("A", 10));
$buffer->append(str_repeat("B", 20));
$buffer->append(str_repeat("C", 30));
var_dump($buffer);

Swoole\Buffer::\_\_construct(int $size = 128)

Alias: swoole_buffer->__construct(int $size = 128)

Fixed size memory blocks allocation.

Swoole\Buffer::append(string $data)

Alias: swoole_buffer->append(string $data)

Append the string or binary data at the end of the memory buffer and return the new size of memory allocated.

Swoole\Buffer::substr(int $offset, int $length = -1, bool $remove = false)

Alias: swoole_buffer->substr(int $offset, int $length = -1, bool $remove = false)

Read data from the memory buffer based on offset and length. Or remove data from the memory buffer.

If $remove is set to be true and $offset is set to be 0, the data will be removed from the buffer. The memory for storing the data will be released when the buffer object is deconstructed.

Swoole\Buffer::clear()

Alias: swoole_buffer->clear()

The memory buffer will be reset.

Swoole\Buffer::expand(int $new_size)

Alias: swoole_buffer->expand(int $new_size)

Expand the size of memory buffer.

Swoole\Buffer::write(int $offset, string $data)

Alias: swoole_buffer->write(int $offset, string $data)

Write data to the memory buffer. The memory allocated for the buffer will not be changed.

Swoole\Buffer::read(int $offset, int $length)

Alias: swoole_buffer->read(int $offset, int $length)

Read data from the memory buffer based on offset and length.

Swoole\Buffer::recycle()

Alias: swoole_buffer->recycle()

Release the memory to OS which is not used by the memory buffer.