Skip to content

Commit 304aee0

Browse files
committed
fileinfo: Remove php_fileinfo struct
This is just a needless layer of indirection and requires an additional allocation.
1 parent b6bbd80 commit 304aee0

File tree

1 file changed

+22
-55
lines changed

1 file changed

+22
-55
lines changed

ext/fileinfo/fileinfo.c

Lines changed: 22 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,12 @@
3636
#include "fopen_wrappers.h" /* needed for is_url */
3737
#include "Zend/zend_exceptions.h"
3838

39-
/* {{{ macros and type definitions */
40-
typedef struct _php_fileinfo {
41-
zend_long options;
42-
struct magic_set *magic;
43-
} php_fileinfo;
44-
4539
static zend_object_handlers finfo_object_handlers;
4640
zend_class_entry *finfo_class_entry;
4741

4842
typedef struct _finfo_object {
49-
php_fileinfo *ptr;
43+
zend_long options;
44+
struct magic_set *magic;
5045
zend_object zo;
5146
} finfo_object;
5247

@@ -56,26 +51,12 @@ static inline finfo_object *php_finfo_fetch_object(zend_object *obj) {
5651

5752
#define Z_FINFO_P(zv) php_finfo_fetch_object(Z_OBJ_P((zv)))
5853

59-
#define FILEINFO_FROM_OBJECT(finfo, object) \
60-
{ \
61-
finfo_object *obj = Z_FINFO_P(object); \
62-
finfo = obj->ptr; \
63-
if (!finfo) { \
64-
zend_throw_error(NULL, "Invalid finfo object"); \
65-
RETURN_THROWS(); \
66-
} \
67-
}
68-
6954
/* {{{ finfo_objects_free */
7055
static void finfo_objects_free(zend_object *object)
7156
{
7257
finfo_object *intern = php_finfo_fetch_object(object);
7358

74-
if (intern->ptr) {
75-
magic_close(intern->ptr->magic);
76-
efree(intern->ptr);
77-
}
78-
59+
magic_close(intern->magic);
7960
zend_object_std_dtor(&intern->zo);
8061
}
8162
/* }}} */
@@ -153,7 +134,6 @@ PHP_FUNCTION(finfo_open)
153134
zend_long options = MAGIC_NONE;
154135
char *file = NULL;
155136
size_t file_len = 0;
156-
php_fileinfo *finfo;
157137
zval *object = getThis();
158138
char resolved_path[MAXPATHLEN];
159139
zend_error_handling zeh;
@@ -163,15 +143,10 @@ PHP_FUNCTION(finfo_open)
163143
}
164144

165145
if (object) {
166-
finfo_object *finfo_obj = Z_FINFO_P(object);
167-
168146
zend_replace_error_handling(EH_THROW, NULL, &zeh);
169147

170-
if (finfo_obj->ptr) {
171-
magic_close(finfo_obj->ptr->magic);
172-
efree(finfo_obj->ptr);
173-
finfo_obj->ptr = NULL;
174-
}
148+
magic_close(Z_FINFO_P(object)->magic);
149+
Z_FINFO_P(object)->magic = NULL;
175150
}
176151

177152
if (file_len == 0) {
@@ -199,13 +174,9 @@ PHP_FUNCTION(finfo_open)
199174
file = resolved_path;
200175
}
201176

202-
finfo = emalloc(sizeof(php_fileinfo));
203-
204-
finfo->options = options;
205-
finfo->magic = magic_open(options);
177+
struct magic_set *magic = magic_open(options);
206178

207-
if (finfo->magic == NULL) {
208-
efree(finfo);
179+
if (magic == NULL) {
209180
php_error_docref(NULL, E_WARNING, "Invalid mode '" ZEND_LONG_FMT "'.", options);
210181
if (object) {
211182
zend_restore_error_handling(&zeh);
@@ -216,10 +187,9 @@ PHP_FUNCTION(finfo_open)
216187
RETURN_FALSE;
217188
}
218189

219-
if (magic_load(finfo->magic, file) == -1) {
190+
if (magic_load(magic, file) == -1) {
220191
php_error_docref(NULL, E_WARNING, "Failed to load magic database at \"%s\"", file);
221-
magic_close(finfo->magic);
222-
efree(finfo);
192+
magic_close(magic);
223193
if (object) {
224194
zend_restore_error_handling(&zeh);
225195
if (!EG(exception)) {
@@ -230,14 +200,15 @@ PHP_FUNCTION(finfo_open)
230200
}
231201

232202
if (object) {
233-
finfo_object *obj;
234203
zend_restore_error_handling(&zeh);
235-
obj = Z_FINFO_P(object);
236-
obj->ptr = finfo;
204+
finfo_object *obj = Z_FINFO_P(object);
205+
obj->magic = magic;
206+
obj->options = options;
237207
} else {
238208
zend_object *zobj = finfo_objects_new(finfo_class_entry);
239209
finfo_object *obj = php_finfo_fetch_object(zobj);
240-
obj->ptr = finfo;
210+
obj->magic = magic;
211+
obj->options = options;
241212
RETURN_OBJ(zobj);
242213
}
243214
}
@@ -260,18 +231,16 @@ PHP_FUNCTION(finfo_close)
260231
PHP_FUNCTION(finfo_set_flags)
261232
{
262233
zend_long options;
263-
php_fileinfo *finfo;
264234
zval *self;
265235

266236
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &self, finfo_class_entry, &options) == FAILURE) {
267237
RETURN_THROWS();
268238
}
269-
FILEINFO_FROM_OBJECT(finfo, self);
270239

271240
/* We do not check the return value as it can only ever fail if options contains MAGIC_PRESERVE_ATIME
272241
* and the system neither has utime(3) nor utimes(2). Something incredibly unlikely. */
273-
magic_setflags(finfo->magic, options);
274-
finfo->options = options;
242+
magic_setflags(Z_FINFO_P(self)->magic, options);
243+
Z_FINFO_P(self)->options = options;
275244

276245
RETURN_TRUE;
277246
}
@@ -331,13 +300,12 @@ PHP_FUNCTION(finfo_file)
331300
zend_string *path = NULL;
332301
zend_long options = 0;
333302
zval *zcontext = NULL;
334-
php_fileinfo *finfo = NULL;
335303

336304
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OP|lr!", &self, finfo_class_entry, &path, &options, &zcontext) == FAILURE) {
337305
RETURN_THROWS();
338306
}
339-
FILEINFO_FROM_OBJECT(finfo, self);
340-
struct magic_set *magic = finfo->magic;
307+
308+
struct magic_set *magic = Z_FINFO_P(self)->magic;
341309

342310
if (UNEXPECTED(ZSTR_LEN(path) == 0)) {
343311
zend_argument_must_not_be_empty_error(2);
@@ -358,7 +326,7 @@ PHP_FUNCTION(finfo_file)
358326
const char *ret_val = php_fileinfo_from_path(magic, path, context);
359327
/* Restore options */
360328
if (options) {
361-
magic_setflags(magic, finfo->options);
329+
magic_setflags(magic, Z_FINFO_P(self)->options);
362330
}
363331

364332
if (UNEXPECTED(ret_val == NULL)) {
@@ -375,13 +343,12 @@ PHP_FUNCTION(finfo_buffer)
375343
zend_string *buffer = NULL;
376344
zend_long options = 0;
377345
zval *dummy_context = NULL;
378-
php_fileinfo *finfo = NULL;
379346

380347
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OS|lr!", &self, finfo_class_entry, &buffer, &options, &dummy_context) == FAILURE) {
381348
RETURN_THROWS();
382349
}
383-
FILEINFO_FROM_OBJECT(finfo, self);
384-
struct magic_set *magic = finfo->magic;
350+
351+
struct magic_set *magic = Z_FINFO_P(self)->magic;
385352

386353
/* Set options for the current file/buffer. */
387354
if (options) {
@@ -392,7 +359,7 @@ PHP_FUNCTION(finfo_buffer)
392359

393360
/* Restore options */
394361
if (options) {
395-
magic_setflags(magic, finfo->options);
362+
magic_setflags(magic, Z_FINFO_P(self)->options);
396363
}
397364

398365
if (UNEXPECTED(ret_val == NULL)) {

0 commit comments

Comments
 (0)