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,59 @@ 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
+ zend_class_entry * shmop_ce ;
63
+ static zend_object_handlers shmop_object_handlers ;
64
+
65
+ static inline php_shmop * shmop_from_obj (zend_object * obj )
66
+ {
67
+ return (php_shmop * )((char * )(obj ) - XtOffsetOf (php_shmop , std ));
68
+ }
69
+
70
+ #define Z_SHMOP_P (zv ) shmop_from_obj(Z_OBJ_P(zv))
71
+
72
+ static zend_object * shmop_create_object (zend_class_entry * class_type )
73
+ {
74
+ php_shmop * intern = zend_object_alloc (sizeof (php_shmop ), class_type );
75
+
76
+ zend_object_std_init (& intern -> std , class_type );
77
+ object_properties_init (& intern -> std , class_type );
78
+ intern -> std .handlers = & shmop_object_handlers ;
79
+
80
+ return & intern -> std ;
81
+ }
82
+
83
+ static zend_function * shmop_get_constructor (zend_object * object )
84
+ {
85
+ zend_throw_error (NULL , "Cannot directly construct Shmop, use shmop_open() instead" );
86
+ return NULL ;
87
+ }
88
+
89
+ static void shmop_free_obj (zend_object * object )
72
90
{
73
- struct php_shmop * shmop = ( struct php_shmop * ) rsrc -> ptr ;
91
+ php_shmop * shmop = shmop_from_obj ( object ) ;
74
92
75
93
shmdt (shmop -> addr );
76
- efree (shmop );
94
+
95
+ zend_object_std_dtor (& shmop -> std );
77
96
}
78
- /* }}} */
79
97
80
98
/* {{{ PHP_MINIT_FUNCTION
81
99
*/
82
100
PHP_MINIT_FUNCTION (shmop )
83
101
{
84
- shm_type = zend_register_list_destructors_ex (rsclean , NULL , "shmop" , module_number );
102
+ zend_class_entry ce ;
103
+ INIT_CLASS_ENTRY (ce , "Shmop" , class_Shmop_methods );
104
+ shmop_ce = zend_register_internal_class (& ce );
105
+ shmop_ce -> ce_flags |= ZEND_ACC_FINAL ;
106
+ shmop_ce -> create_object = shmop_create_object ;
107
+ shmop_ce -> serialize = zend_class_serialize_deny ;
108
+ shmop_ce -> unserialize = zend_class_unserialize_deny ;
109
+
110
+ memcpy (& shmop_object_handlers , & std_object_handlers , sizeof (zend_object_handlers ));
111
+ shmop_object_handlers .offset = XtOffsetOf (php_shmop , std );
112
+ shmop_object_handlers .free_obj = shmop_free_obj ;
113
+ shmop_object_handlers .get_constructor = shmop_get_constructor ;
114
+ shmop_object_handlers .clone_obj = NULL ;
85
115
86
116
return SUCCESS ;
87
117
}
@@ -97,12 +127,12 @@ PHP_MINFO_FUNCTION(shmop)
97
127
}
98
128
/* }}} */
99
129
100
- /* {{{ proto resource shmop_open(int key, string flags, int mode, int size)
130
+ /* {{{ proto Shmop shmop_open(int key, string flags, int mode, int size)
101
131
gets and attaches a shared memory segment */
102
132
PHP_FUNCTION (shmop_open )
103
133
{
104
134
zend_long key , mode , size ;
105
- struct php_shmop * shmop ;
135
+ php_shmop * shmop ;
106
136
struct shmid_ds shm ;
107
137
char * flags ;
108
138
size_t flags_len ;
@@ -116,9 +146,8 @@ PHP_FUNCTION(shmop_open)
116
146
RETURN_FALSE ;
117
147
}
118
148
119
- shmop = emalloc (sizeof (struct php_shmop ));
120
- memset (shmop , 0 , sizeof (struct php_shmop ));
121
-
149
+ object_init_ex (return_value , shmop_ce );
150
+ shmop = Z_SHMOP_P (return_value );
122
151
shmop -> key = key ;
123
152
shmop -> shmflg |= mode ;
124
153
@@ -175,32 +204,30 @@ PHP_FUNCTION(shmop_open)
175
204
}
176
205
177
206
shmop -> size = shm .shm_segsz ;
207
+ return ;
178
208
179
- RETURN_RES (zend_register_resource (shmop , shm_type ));
180
209
err :
181
- efree ( shmop );
210
+ zend_object_release ( Z_OBJ_P ( return_value ) );
182
211
RETURN_FALSE ;
183
212
}
184
213
/* }}} */
185
214
186
- /* {{{ proto string shmop_read(resource shmid, int start, int count)
215
+ /* {{{ proto string shmop_read(Shmop shmid, int start, int count)
187
216
reads from a shm segment */
188
217
PHP_FUNCTION (shmop_read )
189
218
{
190
219
zval * shmid ;
191
220
zend_long start , count ;
192
- struct php_shmop * shmop ;
221
+ php_shmop * shmop ;
193
222
char * startaddr ;
194
223
int bytes ;
195
224
zend_string * return_string ;
196
225
197
- if (zend_parse_parameters (ZEND_NUM_ARGS (), "rll " , & shmid , & start , & count ) == FAILURE ) {
226
+ if (zend_parse_parameters (ZEND_NUM_ARGS (), "Oll " , & shmid , shmop_ce , & start , & count ) == FAILURE ) {
198
227
RETURN_THROWS ();
199
228
}
200
229
201
- if ((shmop = (struct php_shmop * )zend_fetch_resource (Z_RES_P (shmid ), "shmop" , shm_type )) == NULL ) {
202
- RETURN_THROWS ();
203
- }
230
+ shmop = Z_SHMOP_P (shmid );
204
231
205
232
if (start < 0 || start > shmop -> size ) {
206
233
php_error_docref (NULL , E_WARNING , "Start is out of range" );
@@ -221,62 +248,50 @@ PHP_FUNCTION(shmop_read)
221
248
}
222
249
/* }}} */
223
250
224
- /* {{{ proto void shmop_close(resource shmid)
225
- closes a shared memory segment */
251
+ /* {{{ proto void shmop_close(Shmop shmid)
252
+ used to close a shared memory segment; now a NOP */
226
253
PHP_FUNCTION (shmop_close )
227
254
{
228
255
zval * shmid ;
229
- struct php_shmop * shmop ;
230
256
231
- if (zend_parse_parameters (ZEND_NUM_ARGS (), "r " , & shmid ) == FAILURE ) {
257
+ if (zend_parse_parameters (ZEND_NUM_ARGS (), "O " , & shmid , shmop_ce ) == FAILURE ) {
232
258
RETURN_THROWS ();
233
259
}
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
260
}
242
261
/* }}} */
243
262
244
- /* {{{ proto int shmop_size(resource shmid)
263
+ /* {{{ proto int shmop_size(Shmop shmid)
245
264
returns the shm size */
246
265
PHP_FUNCTION (shmop_size )
247
266
{
248
267
zval * shmid ;
249
- struct php_shmop * shmop ;
268
+ php_shmop * shmop ;
250
269
251
- if (zend_parse_parameters (ZEND_NUM_ARGS (), "r " , & shmid ) == FAILURE ) {
270
+ if (zend_parse_parameters (ZEND_NUM_ARGS (), "O " , & shmid , shmop_ce ) == FAILURE ) {
252
271
RETURN_THROWS ();
253
272
}
254
273
255
- if ((shmop = (struct php_shmop * )zend_fetch_resource (Z_RES_P (shmid ), "shmop" , shm_type )) == NULL ) {
256
- RETURN_THROWS ();
257
- }
274
+ shmop = Z_SHMOP_P (shmid );
258
275
259
276
RETURN_LONG (shmop -> size );
260
277
}
261
278
/* }}} */
262
279
263
- /* {{{ proto int shmop_write(resource shmid, string data, int offset)
280
+ /* {{{ proto int shmop_write(Shmop shmid, string data, int offset)
264
281
writes to a shared memory segment */
265
282
PHP_FUNCTION (shmop_write )
266
283
{
267
- struct php_shmop * shmop ;
284
+ php_shmop * shmop ;
268
285
zend_long writesize ;
269
286
zend_long offset ;
270
287
zend_string * data ;
271
288
zval * shmid ;
272
289
273
- if (zend_parse_parameters (ZEND_NUM_ARGS (), "rSl " , & shmid , & data , & offset ) == FAILURE ) {
290
+ if (zend_parse_parameters (ZEND_NUM_ARGS (), "OSl " , & shmid , shmop_ce , & data , & offset ) == FAILURE ) {
274
291
RETURN_THROWS ();
275
292
}
276
293
277
- if ((shmop = (struct php_shmop * )zend_fetch_resource (Z_RES_P (shmid ), "shmop" , shm_type )) == NULL ) {
278
- RETURN_THROWS ();
279
- }
294
+ shmop = Z_SHMOP_P (shmid );
280
295
281
296
if ((shmop -> shmatflg & SHM_RDONLY ) == SHM_RDONLY ) {
282
297
php_error_docref (NULL , E_WARNING , "Trying to write to a read only segment" );
@@ -295,20 +310,18 @@ PHP_FUNCTION(shmop_write)
295
310
}
296
311
/* }}} */
297
312
298
- /* {{{ proto bool shmop_delete(resource shmid)
313
+ /* {{{ proto bool shmop_delete(Shmop shmid)
299
314
mark segment for deletion */
300
315
PHP_FUNCTION (shmop_delete )
301
316
{
302
317
zval * shmid ;
303
- struct php_shmop * shmop ;
318
+ php_shmop * shmop ;
304
319
305
- if (zend_parse_parameters (ZEND_NUM_ARGS (), "r " , & shmid ) == FAILURE ) {
320
+ if (zend_parse_parameters (ZEND_NUM_ARGS (), "O " , & shmid , shmop_ce ) == FAILURE ) {
306
321
RETURN_THROWS ();
307
322
}
308
323
309
- if ((shmop = (struct php_shmop * )zend_fetch_resource (Z_RES_P (shmid ), "shmop" , shm_type )) == NULL ) {
310
- RETURN_THROWS ();
311
- }
324
+ shmop = Z_SHMOP_P (shmid );
312
325
313
326
if (shmctl (shmop -> shmid , IPC_RMID , NULL )) {
314
327
php_error_docref (NULL , E_WARNING , "Can't mark segment for deletion (are you the owner?)" );
0 commit comments