Skip to content

Commit

Permalink
Add Handle::rawStream()
Browse files Browse the repository at this point in the history
  • Loading branch information
jbboehr committed Apr 5, 2024
1 parent 6ff37cb commit ea70a49
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
7 changes: 7 additions & 0 deletions perfidious.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ final public function enable(): self {}
*/
final public function disable(): self {}

/**
* Get a raw byte stream from the handle's file descriptor
* @note closing this resource will cause subsequent calls to read to fail
* @return resource
*/
final public function rawStream() {}

/**
* @return array<string, int>
* @throws OverflowException|IOException
Expand Down
16 changes: 16 additions & 0 deletions src/handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,21 @@ static PHP_METHOD(PerfidousHandle, enable)
RETURN_ZVAL(ZEND_THIS, 1, 0);
}

ZEND_COLD
static PHP_METHOD(PerfidousHandle, rawStream)
{
ZEND_PARSE_PARAMETERS_NONE();

struct perfidious_handle_obj *obj = perfidious_fetch_handle_object(Z_OBJ_P(ZEND_THIS));

if (UNEXPECTED(FAILURE == perfidious_handle_marker_assert(obj->handle))) {
RETURN_NULL();
}

php_stream *stream = php_stream_fopen_from_fd(obj->handle->metrics[0].fd, "r", NULL);
php_stream_to_zval(stream, return_value);
}

ZEND_HOT
static PHP_METHOD(PerfidousHandle, read)
{
Expand Down Expand Up @@ -537,6 +552,7 @@ static PHP_METHOD(PerfidousHandle, reset)
static zend_function_entry perfidious_handle_methods[] = {
PHP_ME(PerfidousHandle, disable, perfidious_handle_disable_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
PHP_ME(PerfidousHandle, enable, perfidious_handle_enable_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
PHP_ME(PerfidousHandle, rawStream, perfidious_handle_raw_stream_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
PHP_ME(PerfidousHandle, read, perfidious_handle_read_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
PHP_ME(PerfidousHandle, readArray, perfidious_handle_read_array_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
PHP_ME(PerfidousHandle, reset, perfidious_handle_reset_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
Expand Down
3 changes: 3 additions & 0 deletions src/handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(perfidious_handle_enable_arginfo, 0, 0, Perfidious\\Handle, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO(perfidious_handle_raw_stream_arginfo, IS_RESOURCE, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(perfidious_handle_read_arginfo, 0, 0, Perfidious\\ReadResult, 0)
ZEND_END_ARG_INFO()

Expand Down
17 changes: 17 additions & 0 deletions tests/handle/dirty.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Perfidious\Handle::rawStream() - do dirty things
--EXTENSIONS--
perfidious
--FILE--
<?php
$handle = Perfidious\open([
"perf::PERF_COUNT_SW_CPU_CLOCK:u",
]);
$stream = $handle->rawStream();
var_dump(strlen(fread($stream, 32)));
fclose($stream);
var_dump($handle->read());
--EXPECTF--
int(32)
%A Uncaught Perfidious\IOException: failed to read: Bad file descriptor %A
%A Uncaught Perfidious\IOException: close failed: Bad file descriptor %A
29 changes: 29 additions & 0 deletions tests/handle/raw-stream.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
--TEST--
Perfidious\Handle
--EXTENSIONS--
perfidious
--FILE--
<?php
(function () {
$handle = Perfidious\open([
"perf::PERF_COUNT_SW_CPU_CLOCK:u",
]);
$handle->enable();
$stream = $handle->rawStream();
var_dump(strlen(fread($stream, 32)));
var_dump($handle->read());
})();
gc_collect_cycles();
--EXPECTF--
int(32)
object(Perfidious\ReadResult)#%d (3) {
["timeEnabled"]=>
int(%d)
["timeRunning"]=>
int(%d)
["values"]=>
array(1) {
["perf::PERF_COUNT_SW_CPU_CLOCK:u"]=>
int(%d)
}
}

0 comments on commit ea70a49

Please sign in to comment.