3030
3131#define PS_MM_FILE "session_mm_"
3232
33+ #ifdef ZTS
34+ MUTEX_T session_mm_lock ;
35+ #endif
36+
3337/* This list holds all data associated with one session. */
3438
3539typedef struct ps_sd {
@@ -61,28 +65,14 @@ static ps_mm *ps_mm_instance = NULL;
6165# define ps_mm_debug (a )
6266#endif
6367
64- static inline uint32_t ps_sd_hash (const zend_string * data )
65- {
66- uint32_t h ;
67- const char * data_char = ZSTR_VAL (data );
68- const char * e = ZSTR_VAL (data ) + ZSTR_LEN (data );
69-
70- for (h = 2166136261U ; data_char < e ; ) {
71- h *= 16777619 ;
72- h ^= * data_char ++ ;
73- }
74-
75- return h ;
76- }
77-
7868static ps_sd * ps_sd_new (ps_mm * data , zend_string * key )
7969{
8070 ps_sd * sd ;
8171
82- sd = g_malloc (sizeof (ps_sd ) + ZSTR_LEN (key ));
72+ sd = g_try_malloc (sizeof (ps_sd ) + ZSTR_LEN (key ));
8373 if (!sd ) {
8474
85- // php_error_docref(NULL, E_WARNING, "g_malloc failed, avail %ld, err %s", mm_available(data->mm), mm_error() );
75+ php_error_docref (NULL , E_WARNING , "g_malloc failed" );
8676 return NULL ;
8777 }
8878
@@ -108,24 +98,6 @@ static void ps_sd_destroy(ps_mm *data, zend_string *key, ps_sd *sd)
10898 g_hash_table_remove (data -> hash , key );
10999}
110100
111- static ps_sd * ps_sd_lookup (ps_mm * data , const zend_string * key , bool _rw )
112- {
113- return (ps_sd * )g_hash_table_lookup (data -> hash , key );
114- }
115-
116- static zend_result ps_mm_key_exists (ps_mm * data , const zend_string * key )
117- {
118- ps_sd * sd ;
119- if (!key ) {
120- return FAILURE ;
121- }
122- sd = ps_sd_lookup (data , key , false);
123- if (sd ) {
124- return SUCCESS ;
125- }
126- return FAILURE ;
127- }
128-
129101const ps_module ps_mod_mm = {
130102 PS_MOD (mm )
131103};
@@ -242,13 +214,21 @@ PHP_MINIT_FUNCTION(ps_mm)
242214 return FAILURE ;
243215 }
244216
217+ #ifdef ZTS
218+ session_mm_lock = tsrm_mutex_alloc ();
219+ #endif
220+
245221 php_session_register_module (& ps_mod_mm );
246222 return SUCCESS ;
247223}
248224
249225PHP_MSHUTDOWN_FUNCTION (ps_mm )
250226{
251227 if (ps_mm_instance ) {
228+ #ifdef ZTS
229+ tsrm_mutex_free (session_mm_lock );
230+ session_mm_lock = NULL ;
231+ #endif
252232 ps_mm_destroy (ps_mm_instance );
253233 return SUCCESS ;
254234 }
@@ -282,7 +262,7 @@ PS_READ_FUNC(mm)
282262
283263 /* If there is an ID and strict mode, verify existence */
284264 if (PS (use_strict_mode )
285- && ps_mm_key_exists (data , key ) == FAILURE ) {
265+ && g_hash_table_contains (data -> hash , key ) == FAILURE ) {
286266 /* key points to PS(id), but cannot change here. */
287267 if (key ) {
288268 efree (PS (id ));
@@ -299,7 +279,7 @@ PS_READ_FUNC(mm)
299279 PS (session_status ) = php_session_active ;
300280 }
301281
302- sd = ps_sd_lookup (data , PS (id ), false );
282+ sd = g_hash_table_lookup (data -> hash , PS (id ));
303283 if (sd ) {
304284 * val = zend_string_init (sd -> data , sd -> datalen , false);
305285 ret = SUCCESS ;
@@ -313,9 +293,11 @@ PS_WRITE_FUNC(mm)
313293 PS_MM_DATA ;
314294 ps_sd * sd ;
315295
316- mm_lock (data -> mm , MM_LOCK_RW );
296+ #ifdef ZTS
297+ tsrm_mutex_lock (session_mm_lock );
298+ #endif
317299
318- sd = ps_sd_lookup (data , key , true );
300+ sd = g_hash_table_lookup (data -> hash , key );
319301 if (!sd ) {
320302 sd = ps_sd_new (data , key );
321303 ps_mm_debug (("new entry for %s\n" , ZSTR_VAL (key )));
@@ -339,6 +321,9 @@ PS_WRITE_FUNC(mm)
339321 }
340322 }
341323
324+ #ifdef ZTS
325+ tsrm_mutex_unlock (session_mm_lock );
326+ #endif
342327 return sd ? SUCCESS : FAILURE ;
343328}
344329
@@ -347,13 +332,18 @@ PS_DESTROY_FUNC(mm)
347332 PS_MM_DATA ;
348333 ps_sd * sd ;
349334
350- mm_lock (data -> mm , MM_LOCK_RW );
335+ #ifdef ZTS
336+ tsrm_mutex_lock (session_mm_lock );
337+ #endif
351338
352- sd = ps_sd_lookup (data , key , false );
339+ sd = g_hash_table_lookup (data -> hash , key );
353340 if (sd ) {
354341 ps_sd_destroy (data , key , sd );
355342 }
356343
344+ #ifdef ZTS
345+ tsrm_mutex_unlock (session_mm_lock );
346+ #endif
357347 return SUCCESS ;
358348}
359349
@@ -385,7 +375,7 @@ PS_CREATE_SID_FUNC(mm)
385375 do {
386376 sid = php_session_create_id ((void * * )& data );
387377 /* Check collision */
388- if (ps_mm_key_exists (data , sid ) == SUCCESS ) {
378+ if (g_hash_table_contains (data -> hash , sid ) == SUCCESS ) {
389379 if (sid ) {
390380 zend_string_release_ex (sid , false);
391381 sid = NULL ;
0 commit comments