# Registering constants to internal classes (ZE2)

- Thread: https://externals.io/message/3089
- Original archive: https://news-web.php.net/php.internals/3089
- Started by Moriyoshi Koizumi on 2003-07-01T17:33:08+00:00
- Messages: 1
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #3089 — Moriyoshi Koizumi — 2003-07-01T17:33:08+00:00

URL: https://externals.io/message/3089#3089

Hi,

Perhaps I'm missing something, but I noticed the current ZE2 
implementation doesn't seem to correctly handle constants registered to 
internal classes. I tracked down the cause of this problem, and finally 
figured out that those constants are destroyed by the ordinary 
zval_ptr_dtor_wrapper() at the destruction of the constant hash after the 
deactivation of the memory manager. Then I'm putting forward the following 
ideas to avoid this.

1. Unify the calls of pefree() / efree() by a function pointer followed by 
the actual memory block which directs to the actual free() implementation 
the memory block should be freed by. Here's the example stuff I'm 
referring to: 

typedef struct _memblk_t {
	void (*free_fn)(void *ptr);
	byte blk[];
} memblk_t;

void efree_impl(void *ptr)
{
	...
}

void pefree_impl(void *ptr)
{
	free(ptr);
}

void *pemalloc_impl(size_t sz)
{
	return malloc(sz);
}

void *pemalloc(size_t sz, int persistent)
{
	void *ptr;

	if (persistent) {
		ptr = emalloc_impl(sz + sizeof(memblk_t));
		ptr->free_fn = efree_impl;
	} else {
		ptr = pemalloc_impl(sz + sizeof(memblk_t));
		ptr->free_fn = pefree_impl;
	}
	return (void *)ptr->blk;
}

void efree(void *ptr)
{
	memblk_t *actual_head;
	actual_head = (memblk_t *)((byte *)ptr - &((memblk_t *)0)->blk);
	actual_head->free_fn((void *)actual_head);
}


2. Add some new constructors / destructors dedicated to the persistent 
zvals, like ALLOC_INIT_PERSISTENT_ZVAL(), persistent_zval_dtor(), or 
persistent_zval_ptr_dtor(). I don't like this idea though.

Any opinions?

Moriyoshi
