@@ -45,7 +45,7 @@ typedef struct ps_sd {
45
45
void * data ;
46
46
size_t datalen ; /* amount of valid data */
47
47
size_t alloclen ; /* amount of allocated memory for data */
48
- char key [ 1 ]; /* inline key */
48
+ zend_string * key ;
49
49
} ps_sd ;
50
50
51
51
typedef struct {
@@ -64,14 +64,15 @@ static ps_mm *ps_mm_instance = NULL;
64
64
# define ps_mm_debug (a )
65
65
#endif
66
66
67
- static inline uint32_t ps_sd_hash (const char * data , size_t len )
67
+ static inline uint32_t ps_sd_hash (const zend_string * data )
68
68
{
69
69
uint32_t h ;
70
- const char * e = data + len ;
70
+ const char * data_char = ZSTR_VAL (data );
71
+ const char * e = ZSTR_VAL (data ) + ZSTR_LEN (data );
71
72
72
- for (h = 2166136261U ; data < e ; ) {
73
+ for (h = 2166136261U ; data_char < e ; ) {
73
74
h *= 16777619 ;
74
- h ^= * data ++ ;
75
+ h ^= * data_char ++ ;
75
76
}
76
77
77
78
return h ;
@@ -106,30 +107,27 @@ static void hash_split(ps_mm *data)
106
107
data -> hash_max = nmax ;
107
108
}
108
109
109
- static ps_sd * ps_sd_new (ps_mm * data , const char * key )
110
+ static ps_sd * ps_sd_new (ps_mm * data , zend_string * key )
110
111
{
111
112
uint32_t hv , slot ;
112
113
ps_sd * sd ;
113
- size_t keylen ;
114
114
115
- keylen = strlen (key );
116
-
117
- sd = mm_malloc (data -> mm , sizeof (ps_sd ) + keylen );
115
+ sd = mm_malloc (data -> mm , sizeof (ps_sd ) + ZSTR_LEN (key ));
118
116
if (!sd ) {
119
117
120
118
php_error_docref (NULL , E_WARNING , "mm_malloc failed, avail %ld, err %s" , mm_available (data -> mm ), mm_error ());
121
119
return NULL ;
122
120
}
123
121
124
- hv = ps_sd_hash (key , keylen );
122
+ hv = ps_sd_hash (key );
125
123
slot = hv & data -> hash_max ;
126
124
127
125
sd -> ctime = 0 ;
128
126
sd -> hv = hv ;
129
127
sd -> data = NULL ;
130
128
sd -> alloclen = sd -> datalen = 0 ;
131
129
132
- memcpy ( sd -> key , key , keylen + 1 );
130
+ sd -> key = zend_string_copy ( key );
133
131
134
132
sd -> next = data -> hash [slot ];
135
133
data -> hash [slot ] = sd ;
@@ -142,7 +140,7 @@ static ps_sd *ps_sd_new(ps_mm *data, const char *key)
142
140
}
143
141
}
144
142
145
- ps_mm_debug (("inserting %s(%p) into slot %d\n" , key , sd , slot ));
143
+ ps_mm_debug (("inserting %s(%p) into slot %d\n" , ZSTR_VAL ( key ) , sd , slot ));
146
144
147
145
return sd ;
148
146
}
@@ -151,7 +149,7 @@ static void ps_sd_destroy(ps_mm *data, ps_sd *sd)
151
149
{
152
150
uint32_t slot ;
153
151
154
- slot = ps_sd_hash (sd -> key , strlen ( sd -> key ) ) & data -> hash_max ;
152
+ slot = ps_sd_hash (sd -> key ) & data -> hash_max ;
155
153
156
154
if (data -> hash [slot ] == sd ) {
157
155
data -> hash [slot ] = sd -> next ;
@@ -168,20 +166,21 @@ static void ps_sd_destroy(ps_mm *data, ps_sd *sd)
168
166
if (sd -> data ) {
169
167
mm_free (data -> mm , sd -> data );
170
168
}
169
+ zend_string_release (sd -> key );
171
170
172
171
mm_free (data -> mm , sd );
173
172
}
174
173
175
- static ps_sd * ps_sd_lookup (ps_mm * data , const char * key , bool rw )
174
+ static ps_sd * ps_sd_lookup (ps_mm * data , const zend_string * key , bool rw )
176
175
{
177
176
uint32_t hv , slot ;
178
177
ps_sd * ret , * prev ;
179
178
180
- hv = ps_sd_hash (key , strlen ( key ) );
179
+ hv = ps_sd_hash (key );
181
180
slot = hv & data -> hash_max ;
182
181
183
182
for (prev = NULL , ret = data -> hash [slot ]; ret ; prev = ret , ret = ret -> next ) {
184
- if (ret -> hv == hv && ! strcmp (ret -> key , key )) {
183
+ if (ret -> hv == hv && zend_string_equals (ret -> key , key )) {
185
184
break ;
186
185
}
187
186
}
@@ -196,12 +195,12 @@ static ps_sd *ps_sd_lookup(ps_mm *data, const char *key, bool rw)
196
195
data -> hash [slot ] = ret ;
197
196
}
198
197
199
- ps_mm_debug (("lookup(%s): ret=%p,hv=%u,slot=%d\n" , key , ret , hv , slot ));
198
+ ps_mm_debug (("lookup(%s): ret=%p,hv=%u,slot=%d\n" , ZSTR_VAL ( key ) , ret , hv , slot ));
200
199
201
200
return ret ;
202
201
}
203
202
204
- static zend_result ps_mm_key_exists (ps_mm * data , const char * key )
203
+ static zend_result ps_mm_key_exists (ps_mm * data , const zend_string * key )
205
204
{
206
205
ps_sd * sd ;
207
206
@@ -349,7 +348,7 @@ PS_READ_FUNC(mm)
349
348
350
349
/* If there is an ID and strict mode, verify existence */
351
350
if (PS (use_strict_mode )
352
- && ps_mm_key_exists (data , key -> val ) == FAILURE ) {
351
+ && ps_mm_key_exists (data , key ) == FAILURE ) {
353
352
/* key points to PS(id), but cannot change here. */
354
353
if (key ) {
355
354
efree (PS (id ));
@@ -366,7 +365,7 @@ PS_READ_FUNC(mm)
366
365
PS (session_status ) = php_session_active ;
367
366
}
368
367
369
- sd = ps_sd_lookup (data , PS (id )-> val , 0 );
368
+ sd = ps_sd_lookup (data , PS (id ), 0 );
370
369
if (sd ) {
371
370
* val = zend_string_init (sd -> data , sd -> datalen , 0 );
372
371
ret = SUCCESS ;
@@ -384,10 +383,10 @@ PS_WRITE_FUNC(mm)
384
383
385
384
mm_lock (data -> mm , MM_LOCK_RW );
386
385
387
- sd = ps_sd_lookup (data , key -> val , 1 );
386
+ sd = ps_sd_lookup (data , key , 1 );
388
387
if (!sd ) {
389
- sd = ps_sd_new (data , key -> val );
390
- ps_mm_debug (("new entry for %s\n" , key -> val ));
388
+ sd = ps_sd_new (data , key );
389
+ ps_mm_debug (("new entry for %s\n" , ZSTR_VAL ( key ) ));
391
390
}
392
391
393
392
if (sd ) {
@@ -423,7 +422,7 @@ PS_DESTROY_FUNC(mm)
423
422
424
423
mm_lock (data -> mm , MM_LOCK_RW );
425
424
426
- sd = ps_sd_lookup (data , key -> val , 0 );
425
+ sd = ps_sd_lookup (data , key , 0 );
427
426
if (sd ) {
428
427
ps_sd_destroy (data , sd );
429
428
}
@@ -454,7 +453,7 @@ PS_GC_FUNC(mm)
454
453
for (sd = * ohash ; sd ; sd = next ) {
455
454
next = sd -> next ;
456
455
if (sd -> ctime < limit ) {
457
- ps_mm_debug (("purging %s\n" , sd -> key ));
456
+ ps_mm_debug (("purging %s\n" , ZSTR_VAL ( sd -> key ) ));
458
457
ps_sd_destroy (data , sd );
459
458
(* nrdels )++ ;
460
459
}
@@ -475,7 +474,7 @@ PS_CREATE_SID_FUNC(mm)
475
474
do {
476
475
sid = php_session_create_id ((void * * )& data );
477
476
/* Check collision */
478
- if (ps_mm_key_exists (data , sid -> val ) == SUCCESS ) {
477
+ if (ps_mm_key_exists (data , sid ) == SUCCESS ) {
479
478
if (sid ) {
480
479
zend_string_release_ex (sid , 0 );
481
480
sid = NULL ;
0 commit comments