Index: /branches/1.3/utils.c
===================================================================
--- /branches/1.3/utils.c	(revision 636)
+++ /branches/1.3/utils.c	(revision 641)
@@ -554,12 +554,61 @@
 #endif
 
-static void xc_copy_zend_constant(zend_constant *c) /* {{{ */
-{
+void xc_hash_copy_if(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, void *tmp, uint size, xc_if_func_t checker) /* {{{ */
+{
+	Bucket *p;
+	void *new_entry;
+	zend_bool setTargetPointer;
+
+	setTargetPointer = !target->pInternalPointer;
+	p = source->pListHead;
+	while (p) {
+		if (checker(p->pData)) {
+			if (setTargetPointer && source->pInternalPointer == p) {
+				target->pInternalPointer = NULL;
+			}
+			if (p->nKeyLength) {
+				zend_hash_quick_update(target, p->arKey, p->nKeyLength, p->h, p->pData, size, &new_entry);
+			} else {
+				zend_hash_index_update(target, p->h, p->pData, size, &new_entry);
+			}
+			if (pCopyConstructor) {
+				pCopyConstructor(new_entry);
+			}
+		}
+		p = p->pListNext;
+	}
+	if (!target->pInternalPointer) {
+		target->pInternalPointer = target->pListHead;
+	}
+}
+/* }}} */
+#ifdef HAVE_XCACHE_CONSTANT
+static zend_bool xc_is_internal_zend_constant(zend_constant *c) /* {{{ */
+{
+	return (c->flags & CONST_PERSISTENT) ? 1 : 0;
+}
+/* }}} */
+void xc_zend_constant_ctor(zend_constant *c) /* {{{ */
+{
+	assert((c->flags & CONST_PERSISTENT));
+#ifdef IS_UNICODE
+	c->name.u = zend_ustrndup(c->name.u, c->name_len - 1);
+#else
 	c->name = zend_strndup(c->name, c->name_len - 1);
-	if (!(c->flags & CONST_PERSISTENT)) {
-		zval_copy_ctor(&c->value);
-	}
-}
-/* }}} */
+#endif
+}
+/* }}} */
+void xc_zend_constant_dtor(zend_constant *c) /* {{{ */
+{
+	free(ZSTR_U(c->name));
+}
+/* }}} */
+void xc_copy_internal_zend_constants(HashTable *target, HashTable *source) /* {{{ */
+{
+	zend_constant tmp_const;
+	xc_hash_copy_if(target, source, (copy_ctor_func_t) xc_zend_constant_ctor, (void *) &tmp_const, sizeof(zend_constant), (xc_if_func_t) xc_is_internal_zend_constant);
+}
+/* }}} */
+#endif
 xc_sandbox_t *xc_sandbox_init(xc_sandbox_t *sandbox, char *filename TSRMLS_DC) /* {{{ */
 {
@@ -599,7 +648,8 @@
 	h = OG(zend_constants);
 	zend_hash_init_ex(&TG(zend_constants),  20, NULL, h->pDestructor, h->persistent, h->bApplyProtection);
+	xc_copy_internal_zend_constants(&TG(zend_constants), &XG(internal_constant_table));
 	{
 		zend_constant tmp_const;
-		zend_hash_copy(&TG(zend_constants), &XG(internal_constant_table), (copy_ctor_func_t) xc_copy_zend_constant, (void *) &tmp_const, sizeof(tmp_const));
+		zend_hash_copy(&TG(zend_constants), &XG(internal_constant_table), (copy_ctor_func_t) xc_zend_constant_ctor, (void *) &tmp_const, sizeof(tmp_const));
 	}
 #endif
Index: /branches/1.3/utils.h
===================================================================
--- /branches/1.3/utils.h	(revision 627)
+++ /branches/1.3/utils.h	(revision 641)
@@ -119,2 +119,11 @@
 xc_sandbox_t *xc_sandbox_init(xc_sandbox_t *sandbox, char *filename TSRMLS_DC);
 void xc_sandbox_free(xc_sandbox_t *sandbox, xc_install_action_t install TSRMLS_DC);
+
+typedef zend_bool (*xc_if_func_t)(void *data);
+
+void xc_hash_copy_if(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, void *tmp, uint size, xc_if_func_t checker);
+#ifdef HAVE_XCACHE_CONSTANT
+void xc_zend_constant_ctor(zend_constant *c);
+void xc_zend_constant_dtor(zend_constant *c);
+void xc_copy_internal_zend_constants(HashTable *target, HashTable *source);
+#endif
Index: /branches/1.3/xcache.c
===================================================================
--- /branches/1.3/xcache.c	(revision 639)
+++ /branches/1.3/xcache.c	(revision 641)
@@ -1585,12 +1585,4 @@
 }
 /* }}} */
-static void xc_copy_zend_constant(zend_constant *c) /* {{{ */
-{
-	c->name = zend_strndup(c->name, c->name_len - 1);
-	if (!(c->flags & CONST_PERSISTENT)) {
-		zval_copy_ctor(&c->value);
-	}
-}
-/* }}} */
 static void xc_request_init(TSRMLS_D) /* {{{ */
 {
@@ -1598,7 +1590,4 @@
 
 	if (!XG(internal_table_copied)) {
-#ifdef HAVE_XCACHE_CONSTANT
-		zend_constant tmp_const;
-#endif
 		zend_function tmp_func;
 		xc_cest_t tmp_cest;
@@ -1611,5 +1600,5 @@
 
 #ifdef HAVE_XCACHE_CONSTANT
-		zend_hash_init_ex(&XG(internal_constant_table), 20,  NULL, NULL, 1, 0);
+		zend_hash_init_ex(&XG(internal_constant_table), 20,  NULL, (dtor_func_t) xc_zend_constant_dtor, 1, 0);
 #endif
 		zend_hash_init_ex(&XG(internal_function_table), 100, NULL, NULL, 1, 0);
@@ -1617,5 +1606,5 @@
 
 #ifdef HAVE_XCACHE_CONSTANT
-		zend_hash_copy(&XG(internal_constant_table), EG(zend_constants), (copy_ctor_func_t) xc_copy_zend_constant, &tmp_const, sizeof(tmp_const));
+		xc_copy_internal_zend_constants(&XG(internal_constant_table), EG(zend_constants));
 #endif
 		zend_hash_copy(&XG(internal_function_table), CG(function_table), NULL, &tmp_func, sizeof(tmp_func));
@@ -1678,5 +1667,5 @@
 
 #ifdef HAVE_XCACHE_CONSTANT
-	zend_hash_init_ex(&xcache_globals->internal_constant_table, 1, NULL, NULL, 1, 0);
+	zend_hash_init_ex(&xcache_globals->internal_constant_table, 1, NULL, (dtor_func_t) xc_zend_constant_dtor, 1, 0);
 #endif
 	zend_hash_init_ex(&xcache_globals->internal_function_table, 1, NULL, NULL, 1, 0);
@@ -1958,5 +1947,5 @@
 		default:
 #ifdef IS_UNICODE
-			convert_to_text(name);
+			convert_to_unicode(name);
 #else
 			convert_to_string(name);
