Skip to content

Commit c3a3171

Browse files
committed
Add helper function for updating bucket contents
1 parent 994fcfc commit c3a3171

File tree

1 file changed

+32
-40
lines changed

1 file changed

+32
-40
lines changed

Zend/zend_hash.c

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,21 @@ ZEND_API ulong zend_hash_func(const char *arKey, uint nKeyLength)
141141

142142
static const Bucket *uninitialized_bucket = NULL;
143143

144+
static inline void zend_hash_bucket_update(
145+
const HashTable *ht, Bucket *p, void *pData, uint nDataSize, void **pDest ZEND_FILE_LINE_DC
146+
) {
147+
ZEND_ASSERT(p->pData != pData);
148+
HANDLE_BLOCK_INTERRUPTIONS();
149+
if (ht->pDestructor) {
150+
ht->pDestructor(p->pData);
151+
}
152+
UPDATE_DATA(ht, p, pData, nDataSize);
153+
HANDLE_UNBLOCK_INTERRUPTIONS();
154+
if (pDest) {
155+
*pDest = p->pData;
156+
}
157+
}
158+
144159
ZEND_API int _zend_hash_init(HashTable *ht, uint nSize, dtor_func_t pDestructor, zend_bool persistent ZEND_FILE_LINE_DC)
145160
{
146161
uint i = 3;
@@ -209,21 +224,14 @@ ZEND_API int _zend_hash_add_or_update(HashTable *ht, const char *arKey, uint nKe
209224
p = ht->arBuckets[nIndex];
210225
while (p != NULL) {
211226
if (p->arKey == arKey ||
212-
((p->h == h) && (p->nKeyLength == nKeyLength) && !memcmp(p->arKey, arKey, nKeyLength))) {
213-
if (flag & HASH_ADD) {
214-
return FAILURE;
215-
}
216-
ZEND_ASSERT(p->pData != pData);
217-
HANDLE_BLOCK_INTERRUPTIONS();
218-
if (ht->pDestructor) {
219-
ht->pDestructor(p->pData);
220-
}
221-
UPDATE_DATA(ht, p, pData, nDataSize);
222-
if (pDest) {
223-
*pDest = p->pData;
224-
}
225-
HANDLE_UNBLOCK_INTERRUPTIONS();
226-
return SUCCESS;
227+
((p->h == h) && (p->nKeyLength == nKeyLength)
228+
&& !memcmp(p->arKey, arKey, nKeyLength))
229+
) {
230+
if (flag & HASH_ADD) {
231+
return FAILURE;
232+
}
233+
zend_hash_bucket_update(ht, p, pData, nDataSize, pDest ZEND_FILE_LINE_RELAY_CC);
234+
return SUCCESS;
227235
}
228236
p = p->pNext;
229237
}
@@ -272,21 +280,14 @@ ZEND_API int _zend_hash_quick_add_or_update(HashTable *ht, const char *arKey, ui
272280
p = ht->arBuckets[nIndex];
273281
while (p != NULL) {
274282
if (p->arKey == arKey ||
275-
((p->h == h) && (p->nKeyLength == nKeyLength) && !memcmp(p->arKey, arKey, nKeyLength))) {
276-
if (flag & HASH_ADD) {
277-
return FAILURE;
278-
}
279-
ZEND_ASSERT(p->pData != pData);
280-
HANDLE_BLOCK_INTERRUPTIONS();
281-
if (ht->pDestructor) {
282-
ht->pDestructor(p->pData);
283-
}
284-
UPDATE_DATA(ht, p, pData, nDataSize);
285-
if (pDest) {
286-
*pDest = p->pData;
287-
}
288-
HANDLE_UNBLOCK_INTERRUPTIONS();
289-
return SUCCESS;
283+
((p->h == h) && (p->nKeyLength == nKeyLength)
284+
&& !memcmp(p->arKey, arKey, nKeyLength))
285+
) {
286+
if (flag & HASH_ADD) {
287+
return FAILURE;
288+
}
289+
zend_hash_bucket_update(ht, p, pData, nDataSize, pDest ZEND_FILE_LINE_RELAY_CC);
290+
return SUCCESS;
290291
}
291292
p = p->pNext;
292293
}
@@ -351,16 +352,7 @@ ZEND_API int _zend_hash_index_update_or_next_insert(HashTable *ht, ulong h, void
351352
if (flag & HASH_NEXT_INSERT || flag & HASH_ADD) {
352353
return FAILURE;
353354
}
354-
ZEND_ASSERT(p->pData != pData);
355-
HANDLE_BLOCK_INTERRUPTIONS();
356-
if (ht->pDestructor) {
357-
ht->pDestructor(p->pData);
358-
}
359-
UPDATE_DATA(ht, p, pData, nDataSize);
360-
HANDLE_UNBLOCK_INTERRUPTIONS();
361-
if (pDest) {
362-
*pDest = p->pData;
363-
}
355+
zend_hash_bucket_update(ht, p, pData, nDataSize, pDest ZEND_FILE_LINE_RELAY_CC);
364356
return SUCCESS;
365357
}
366358
p = p->pNext;

0 commit comments

Comments
 (0)