colder Mon Jan 26 11:38:03 2009 UTC
Modified files: (Branch: PHP_5_3)
/php-src/ext/spl php_spl.c php_spl.h
Log:
MFH: Improve spl_object_hash()
http://cvs.php.net/viewvc.cgi/php-src/ext/spl/php_spl.c?r1=1.52.2.28.2.17.2.34&r2=1.52.2.28.2.17.2.35&diff_format=u
Index: php-src/ext/spl/php_spl.c
diff -u php-src/ext/spl/php_spl.c:1.52.2.28.2.17.2.34
php-src/ext/spl/php_spl.c:1.52.2.28.2.17.2.35
--- php-src/ext/spl/php_spl.c:1.52.2.28.2.17.2.34 Wed Dec 31 11:15:43 2008
+++ php-src/ext/spl/php_spl.c Mon Jan 26 11:38:03 2009
@@ -16,7 +16,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: php_spl.c,v 1.52.2.28.2.17.2.34 2008/12/31 11:15:43 sebastian Exp $ */
+/* $Id: php_spl.c,v 1.52.2.28.2.17.2.35 2009/01/26 11:38:03 colder Exp $ */
#ifdef HAVE_CONFIG_H
#include "config.h"
@@ -39,7 +39,9 @@
#include "spl_heap.h"
#include "zend_exceptions.h"
#include "zend_interfaces.h"
-#include "ext/standard/md5.h"
+#include "ext/standard/php_rand.h"
+#include "ext/standard/php_lcg.h"
+#include "main/snprintf.h"
#ifdef COMPILE_DL_SPL
ZEND_GET_MODULE(spl)
@@ -669,34 +671,41 @@
PHP_FUNCTION(spl_object_hash)
{
zval *obj;
- char* md5str;
+ char* hash;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o", &obj) ==
FAILURE) {
return;
}
- md5str = emalloc(33);
- php_spl_object_hash(obj, md5str TSRMLS_CC);
+ hash = emalloc(33);
+ php_spl_object_hash(obj, hash TSRMLS_CC);
- RETVAL_STRING(md5str, 0);
+ RETVAL_STRING(hash, 0);
}
/* }}} */
-PHPAPI void php_spl_object_hash(zval *obj, char *md5str TSRMLS_DC) /* {{{*/
+PHPAPI void php_spl_object_hash(zval *obj, char *result TSRMLS_DC) /* {{{*/
{
- int len;
- char *hash;
- PHP_MD5_CTX context;
- unsigned char digest[16];
-
- len = spprintf(&hash, 0, "%p:%d", Z_OBJ_HT_P(obj), Z_OBJ_HANDLE_P(obj));
-
- md5str[0] = '\0';
- PHP_MD5Init(&context);
- PHP_MD5Update(&context, (unsigned char*)hash, len);
- PHP_MD5Final(digest, &context);
- make_digest(md5str, digest);
- efree(hash);
+ intptr_t hash_handle, hash_handlers;
+ char *hex;
+
+ if (!SPL_G(hash_mask_init)) {
+ if (!BG(mt_rand_is_seeded)) {
+ php_mt_srand(GENERATE_SEED() TSRMLS_CC);
+ }
+
+ SPL_G(hash_mask_handle) = (intptr_t)(php_mt_rand(TSRMLS_C) >>
1);
+ SPL_G(hash_mask_handlers) = (intptr_t)(php_mt_rand(TSRMLS_C) >>
1);
+ SPL_G(hash_mask_init) = 1;
+ }
+
+ hash_handle = SPL_G(hash_mask_handle)^(intptr_t)Z_OBJ_HANDLE_P(obj);
+ hash_handlers = SPL_G(hash_mask_handlers)^(intptr_t)Z_OBJ_HT_P(obj);
+
+ spprintf(&hex, 32, "%016x%016x", hash_handle, hash_handlers);
+
+ strlcpy(result, hex, 33);
+ efree(hex);
}
/* }}} */
@@ -844,6 +853,7 @@
SPL_G(autoload_extensions) = NULL;
SPL_G(autoload_extensions_len) = 0;
SPL_G(autoload_functions) = NULL;
+ SPL_G(hash_mask_init) = 0;
return SUCCESS;
} /* }}} */
@@ -859,6 +869,9 @@
FREE_HASHTABLE(SPL_G(autoload_functions));
SPL_G(autoload_functions) = NULL;
}
+ if (SPL_G(hash_mask_init)) {
+ SPL_G(hash_mask_init) = 0;
+ }
return SUCCESS;
} /* }}} */
http://cvs.php.net/viewvc.cgi/php-src/ext/spl/php_spl.h?r1=1.17.2.1.2.3.2.6&r2=1.17.2.1.2.3.2.7&diff_format=u
Index: php-src/ext/spl/php_spl.h
diff -u php-src/ext/spl/php_spl.h:1.17.2.1.2.3.2.6
php-src/ext/spl/php_spl.h:1.17.2.1.2.3.2.7
--- php-src/ext/spl/php_spl.h:1.17.2.1.2.3.2.6 Wed Dec 31 11:15:43 2008
+++ php-src/ext/spl/php_spl.h Mon Jan 26 11:38:03 2009
@@ -62,6 +62,9 @@
HashTable * autoload_functions;
int autoload_running;
int autoload_extensions_len;
+ intptr_t hash_mask_handle;
+ intptr_t hash_mask_handlers;
+ int hash_mask_init;
ZEND_END_MODULE_GLOBALS(spl)
#ifdef ZTS
--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php