Skip to content

Commit f14e31a

Browse files
committed
Simplify
1 parent f9c4311 commit f14e31a

File tree

2 files changed

+10
-27
lines changed

2 files changed

+10
-27
lines changed

src/FFI.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,8 @@ private static function init(): void
539539
typedef void (*FreeFn)(void* a);
540540
void vips_value_set_blob (GValue* value,
541541
FreeFn free_fn, void* data, size_t length);
542+
void* vips_blob_copy (const void *data, size_t length);
543+
void vips_area_unref (void *area);
542544
543545
const char* vips_value_get_ref_string (const GValue* value,
544546
size_t* length);
@@ -760,8 +762,7 @@ private static function init(): void
760762
761763
VipsSource* vips_source_new_from_descriptor (int descriptor);
762764
VipsSource* vips_source_new_from_file (const char* filename);
763-
VipsSource* vips_source_new_from_memory (const void* data,
764-
size_t size);
765+
VipsSource* vips_source_new_from_blob (void* blob);
765766
766767
typedef struct _VipsSourceCustom {
767768
VipsSource parent_object;

src/Source.php

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,6 @@ class Source extends Connection
1212
*/
1313
public \FFI\CData $pointer;
1414

15-
/**
16-
* Pointer to the underlying memory buffer when using
17-
* @see Source::newFromMemory()
18-
*
19-
* Must be freed when no longer needed.
20-
*
21-
* @internal
22-
*/
23-
public ?\FFI\CData $memory = null;
24-
2515
public function __construct(\FFI\CData $pointer)
2616
{
2717
$this->pointer = FFI::vips()->cast(FFI::ctypes('VipsSource'), $pointer);
@@ -74,27 +64,19 @@ public static function newFromFile(string $filename): self
7464
*/
7565
public static function newFromMemory(string $data): self
7666
{
77-
# we need to set the memory to a copy of the data
78-
$n = strlen($data);
79-
$memory = FFI::vips()->new("char[$n]", false, true);
80-
\FFI::memcpy($memory, $data, $n);
81-
$pointer = FFI::vips()->vips_source_new_from_memory($memory, $n);
67+
$blob = FFI::vips()->vips_blob_copy($data, strlen($data));
68+
if ($blob === null) {
69+
throw new Exception("can't create source from memory");
70+
}
8271

72+
$pointer = FFI::vips()->vips_source_new_from_blob($blob);
8373
if ($pointer === null) {
84-
\FFI::free($memory);
74+
FFI::vips()->vips_area_unref($blob);
8575
throw new Exception("can't create source from memory");
8676
}
8777

8878
$source = new self($pointer);
89-
$source->memory = $memory;
79+
FFI::vips()->vips_area_unref($blob);
9080
return $source;
9181
}
92-
93-
public function __destruct()
94-
{
95-
if ($this->memory !== null) {
96-
\FFI::free($this->memory);
97-
}
98-
parent::__destruct();
99-
}
10082
}

0 commit comments

Comments
 (0)