23
23
24
24
#include "php.h"
25
25
#include "php_ini.h"
26
+ #include "Zend/zend_interfaces.h"
26
27
#include "php_shmop.h"
27
28
#include "shmop_arginfo.h"
28
29
38
39
39
40
#include "ext/standard/info.h"
40
41
41
- #ifdef ZTS
42
- int shmop_globals_id ;
43
- #else
44
- php_shmop_globals shmop_globals ;
45
- #endif
46
-
47
- int shm_type ;
48
-
49
42
/* {{{ shmop_module_entry
50
43
*/
51
44
zend_module_entry shmop_module_entry = {
@@ -66,22 +59,70 @@ zend_module_entry shmop_module_entry = {
66
59
ZEND_GET_MODULE (shmop )
67
60
#endif
68
61
69
- /* {{{ rsclean
70
- */
71
- static void rsclean (zend_resource * rsrc )
62
+ typedef struct php_shmop
63
+ {
64
+ int shmid ;
65
+ key_t key ;
66
+ int shmflg ;
67
+ int shmatflg ;
68
+ char * addr ;
69
+ zend_long size ;
70
+ zend_object std ;
71
+ } php_shmop ;
72
+
73
+ zend_class_entry * shmop_ce ;
74
+ static zend_object_handlers shmop_object_handlers ;
75
+
76
+ static inline php_shmop * shmop_from_obj (zend_object * obj )
77
+ {
78
+ return (php_shmop * )((char * )(obj ) - XtOffsetOf (php_shmop , std ));
79
+ }
80
+
81
+ #define Z_SHMOP_P (zv ) shmop_from_obj(Z_OBJ_P(zv))
82
+
83
+ static zend_object * shmop_create_object (zend_class_entry * class_type )
84
+ {
85
+ php_shmop * intern = zend_object_alloc (sizeof (php_shmop ), class_type );
86
+
87
+ zend_object_std_init (& intern -> std , class_type );
88
+ object_properties_init (& intern -> std , class_type );
89
+ intern -> std .handlers = & shmop_object_handlers ;
90
+
91
+ return & intern -> std ;
92
+ }
93
+
94
+ static zend_function * shmop_get_constructor (zend_object * object )
95
+ {
96
+ zend_throw_error (NULL , "Cannot directly construct Shmop, use shmop_open() instead" );
97
+ return NULL ;
98
+ }
99
+
100
+ static void shmop_free_obj (zend_object * object )
72
101
{
73
- struct php_shmop * shmop = ( struct php_shmop * ) rsrc -> ptr ;
102
+ php_shmop * shmop = shmop_from_obj ( object ) ;
74
103
75
104
shmdt (shmop -> addr );
76
- efree (shmop );
105
+
106
+ zend_object_std_dtor (& shmop -> std );
77
107
}
78
- /* }}} */
79
108
80
109
/* {{{ PHP_MINIT_FUNCTION
81
110
*/
82
111
PHP_MINIT_FUNCTION (shmop )
83
112
{
84
- shm_type = zend_register_list_destructors_ex (rsclean , NULL , "shmop" , module_number );
113
+ zend_class_entry ce ;
114
+ INIT_CLASS_ENTRY (ce , "Shmop" , class_Shmop_methods );
115
+ shmop_ce = zend_register_internal_class (& ce );
116
+ shmop_ce -> ce_flags |= ZEND_ACC_FINAL ;
117
+ shmop_ce -> create_object = shmop_create_object ;
118
+ shmop_ce -> serialize = zend_class_serialize_deny ;
119
+ shmop_ce -> unserialize = zend_class_unserialize_deny ;
120
+
121
+ memcpy (& shmop_object_handlers , & std_object_handlers , sizeof (zend_object_handlers ));
122
+ shmop_object_handlers .offset = XtOffsetOf (php_shmop , std );
123
+ shmop_object_handlers .free_obj = shmop_free_obj ;
124
+ shmop_object_handlers .get_constructor = shmop_get_constructor ;
125
+ shmop_object_handlers .clone_obj = NULL ;
85
126
86
127
return SUCCESS ;
87
128
}
@@ -97,12 +138,12 @@ PHP_MINFO_FUNCTION(shmop)
97
138
}
98
139
/* }}} */
99
140
100
- /* {{{ proto resource shmop_open(int key, string flags, int mode, int size)
141
+ /* {{{ proto Shmop shmop_open(int key, string flags, int mode, int size)
101
142
gets and attaches a shared memory segment */
102
143
PHP_FUNCTION (shmop_open )
103
144
{
104
145
zend_long key , mode , size ;
105
- struct php_shmop * shmop ;
146
+ php_shmop * shmop ;
106
147
struct shmid_ds shm ;
107
148
char * flags ;
108
149
size_t flags_len ;
@@ -116,9 +157,8 @@ PHP_FUNCTION(shmop_open)
116
157
RETURN_FALSE ;
117
158
}
118
159
119
- shmop = emalloc (sizeof (struct php_shmop ));
120
- memset (shmop , 0 , sizeof (struct php_shmop ));
121
-
160
+ object_init_ex (return_value , shmop_ce );
161
+ shmop = Z_SHMOP_P (return_value );
122
162
shmop -> key = key ;
123
163
shmop -> shmflg |= mode ;
124
164
@@ -175,32 +215,30 @@ PHP_FUNCTION(shmop_open)
175
215
}
176
216
177
217
shmop -> size = shm .shm_segsz ;
218
+ return ;
178
219
179
- RETURN_RES (zend_register_resource (shmop , shm_type ));
180
220
err :
181
- efree ( shmop );
221
+ zend_object_release ( Z_OBJ_P ( return_value ) );
182
222
RETURN_FALSE ;
183
223
}
184
224
/* }}} */
185
225
186
- /* {{{ proto string shmop_read(resource shmid, int start, int count)
226
+ /* {{{ proto string shmop_read(Shmop shmid, int start, int count)
187
227
reads from a shm segment */
188
228
PHP_FUNCTION (shmop_read )
189
229
{
190
230
zval * shmid ;
191
231
zend_long start , count ;
192
- struct php_shmop * shmop ;
232
+ php_shmop * shmop ;
193
233
char * startaddr ;
194
234
int bytes ;
195
235
zend_string * return_string ;
196
236
197
- if (zend_parse_parameters (ZEND_NUM_ARGS (), "rll " , & shmid , & start , & count ) == FAILURE ) {
237
+ if (zend_parse_parameters (ZEND_NUM_ARGS (), "Oll " , & shmid , shmop_ce , & start , & count ) == FAILURE ) {
198
238
RETURN_THROWS ();
199
239
}
200
240
201
- if ((shmop = (struct php_shmop * )zend_fetch_resource (Z_RES_P (shmid ), "shmop" , shm_type )) == NULL ) {
202
- RETURN_THROWS ();
203
- }
241
+ shmop = Z_SHMOP_P (shmid );
204
242
205
243
if (start < 0 || start > shmop -> size ) {
206
244
php_error_docref (NULL , E_WARNING , "Start is out of range" );
@@ -221,62 +259,50 @@ PHP_FUNCTION(shmop_read)
221
259
}
222
260
/* }}} */
223
261
224
- /* {{{ proto void shmop_close(resource shmid)
225
- closes a shared memory segment */
262
+ /* {{{ proto void shmop_close(Shmop shmid)
263
+ used to close a shared memory segment; now a NOP */
226
264
PHP_FUNCTION (shmop_close )
227
265
{
228
266
zval * shmid ;
229
- struct php_shmop * shmop ;
230
267
231
- if (zend_parse_parameters (ZEND_NUM_ARGS (), "r " , & shmid ) == FAILURE ) {
268
+ if (zend_parse_parameters (ZEND_NUM_ARGS (), "O " , & shmid , shmop_ce ) == FAILURE ) {
232
269
RETURN_THROWS ();
233
270
}
234
-
235
-
236
- if ((shmop = (struct php_shmop * )zend_fetch_resource (Z_RES_P (shmid ), "shmop" , shm_type )) == NULL ) {
237
- RETURN_THROWS ();
238
- }
239
-
240
- zend_list_close (Z_RES_P (shmid ));
241
271
}
242
272
/* }}} */
243
273
244
- /* {{{ proto int shmop_size(resource shmid)
274
+ /* {{{ proto int shmop_size(Shmop shmid)
245
275
returns the shm size */
246
276
PHP_FUNCTION (shmop_size )
247
277
{
248
278
zval * shmid ;
249
- struct php_shmop * shmop ;
279
+ php_shmop * shmop ;
250
280
251
- if (zend_parse_parameters (ZEND_NUM_ARGS (), "r " , & shmid ) == FAILURE ) {
281
+ if (zend_parse_parameters (ZEND_NUM_ARGS (), "O " , & shmid , shmop_ce ) == FAILURE ) {
252
282
RETURN_THROWS ();
253
283
}
254
284
255
- if ((shmop = (struct php_shmop * )zend_fetch_resource (Z_RES_P (shmid ), "shmop" , shm_type )) == NULL ) {
256
- RETURN_THROWS ();
257
- }
285
+ shmop = Z_SHMOP_P (shmid );
258
286
259
287
RETURN_LONG (shmop -> size );
260
288
}
261
289
/* }}} */
262
290
263
- /* {{{ proto int shmop_write(resource shmid, string data, int offset)
291
+ /* {{{ proto int shmop_write(Shmop shmid, string data, int offset)
264
292
writes to a shared memory segment */
265
293
PHP_FUNCTION (shmop_write )
266
294
{
267
- struct php_shmop * shmop ;
295
+ php_shmop * shmop ;
268
296
zend_long writesize ;
269
297
zend_long offset ;
270
298
zend_string * data ;
271
299
zval * shmid ;
272
300
273
- if (zend_parse_parameters (ZEND_NUM_ARGS (), "rSl " , & shmid , & data , & offset ) == FAILURE ) {
301
+ if (zend_parse_parameters (ZEND_NUM_ARGS (), "OSl " , & shmid , shmop_ce , & data , & offset ) == FAILURE ) {
274
302
RETURN_THROWS ();
275
303
}
276
304
277
- if ((shmop = (struct php_shmop * )zend_fetch_resource (Z_RES_P (shmid ), "shmop" , shm_type )) == NULL ) {
278
- RETURN_THROWS ();
279
- }
305
+ shmop = Z_SHMOP_P (shmid );
280
306
281
307
if ((shmop -> shmatflg & SHM_RDONLY ) == SHM_RDONLY ) {
282
308
php_error_docref (NULL , E_WARNING , "Trying to write to a read only segment" );
@@ -295,20 +321,18 @@ PHP_FUNCTION(shmop_write)
295
321
}
296
322
/* }}} */
297
323
298
- /* {{{ proto bool shmop_delete(resource shmid)
324
+ /* {{{ proto bool shmop_delete(Shmop shmid)
299
325
mark segment for deletion */
300
326
PHP_FUNCTION (shmop_delete )
301
327
{
302
328
zval * shmid ;
303
- struct php_shmop * shmop ;
329
+ php_shmop * shmop ;
304
330
305
- if (zend_parse_parameters (ZEND_NUM_ARGS (), "r " , & shmid ) == FAILURE ) {
331
+ if (zend_parse_parameters (ZEND_NUM_ARGS (), "O " , & shmid , shmop_ce ) == FAILURE ) {
306
332
RETURN_THROWS ();
307
333
}
308
334
309
- if ((shmop = (struct php_shmop * )zend_fetch_resource (Z_RES_P (shmid ), "shmop" , shm_type )) == NULL ) {
310
- RETURN_THROWS ();
311
- }
335
+ shmop = Z_SHMOP_P (shmid );
312
336
313
337
if (shmctl (shmop -> shmid , IPC_RMID , NULL )) {
314
338
php_error_docref (NULL , E_WARNING , "Can't mark segment for deletion (are you the owner?)" );
0 commit comments