# Using do_alloca() instead of emalloc()

- Thread: https://externals.io/message/5608
- Original archive: https://news-web.php.net/php.internals/5608
- Started by Andi Gutmans on 2003-11-20T09:48:57+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #5608 — Andi Gutmans — 2003-11-20T09:48:57+00:00

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

Hey,

I've been taking a look at the __autoload problem. During that time I saw 
that lots of places in the code (especially reflection API) use 
zend_str_tolower_dup().
In most of these cases it would be much more efficient to use 
do_alloca()/free_alloca() because that usually uses alloca() which 
allocates on the stack.
Simplified example from reflection API code:

lcname = zend_str_tolower_dup(name_str, name_len);

if (zend_hash_find(&ce->properties_info, lcname, name_len + 1, (void **) 
&property_info) == FAILURE) {
	efree(lcname);
	/* Error message */
	return;
}

efree(lcname);

This should really be:

lcname = do_alloca(name_len+1);
zend_str_tolower_copy(lcname, name_str, name_len+1);

if (zend_hash_find(&ce->properties_info, lcname, name_len + 1, (void **) 
&property_info) == FAILURE) {
	free_alloca(lcname);
	/* Error message */
	return;
}

free_alloca(lcname);

There are two times where you really shouldn't use do_alloca/free_alloca:
a) If you're in code which requires a regular malloc() don't use them (the 
fallback is emalloc()).
b) Only use them in functions which are about to return. They shouldn't be 
used in places like the main zend_execute loop because it would just make 
the stack grow more and more (in almost all cases where 
zend_str_tolower_dup() is being used, do_alloca/free_alloca are applicable).

I need to go away for the weekend now but it be cool if people here would 
at least take care of this in their code (such as reflection API code).

Thanks,

Andi

---

## #5623 — Timm Friebe — 2003-11-20T22:09:10+00:00

URL: https://externals.io/message/5608#5623 · In reply to: #5608

> Hey,
> 
> I've been taking a look at the __autoload problem. During that time I saw 
> that lots of places in the code (especially reflection API) use 
> zend_str_tolower_dup().
[...]

Here's a patch for zend_reflection_api.c which uses do_alloca /
free_alloca in all relevant places.

- Timm

---

## #5669 — Marcus Boerger — 2003-11-22T19:45:50+00:00

URL: https://externals.io/message/5608#5669 · In reply to: #5608

Hello Andi,

Thursday, November 20, 2003, 10:48:57 AM, you wrote:

> Hey,

> I've been taking a look at the __autoload problem. During that time I saw 
> that lots of places in the code (especially reflection API) use 
> zend_str_tolower_dup().
> In most of these cases it would be much more efficient to use 
> do_alloca()/free_alloca() because that usually uses alloca() which 
> allocates on the stack.

Well Zeev and  you  told us to never it :-)

> Simplified example from reflection API code:

> lcname = zend_str_tolower_dup(name_str, name_len);

> if (zend_hash_find(&ce->properties_info, lcname, name_len + 1, (void **) 
> &property_info) == FAILURE) {
> 	efree(lcname);
> 	/* Error message */
> 	return;
> }

> efree(lcname);

> This should really be:

> lcname = do_alloca(name_len+1);
> zend_str_tolower_copy(lcname, name_str, name_len+1);

> if (zend_hash_find(&ce->properties_info, lcname, name_len + 1, (void **) 
> &property_info) == FAILURE) {
> 	free_alloca(lcname);
> 	/* Error message */
> 	return;
> }

> free_alloca(lcname);

> There are two times where you really shouldn't use do_alloca/free_alloca:
> a) If you're in code which requires a regular malloc() don't use them (the
> fallback is emalloc()).
> b) Only use them in functions which are about to return. They shouldn't be
> used in places like the main zend_execute loop because it would just make 
> the stack grow more and more (in almost all cases where 
> zend_str_tolower_dup() is being used, do_alloca/free_alloca are applicable).

> I need to go away for the weekend now but it be cool if people here would 
> at least take care of this in their code (such as reflection API code).

> Thanks,

> Andi




-- 
Best regards,
 Marcus                            mailto:helly@php.net

---

## #5678 — Andi Gutmans — 2003-11-23T12:56:20+00:00

URL: https://externals.io/message/5608#5678 · In reply to: #5608

Commited thanks.
Marcus, what we meant was that you shouldn't use do_alloca() in the engine 
except for in the cases I mentioned, and possibly cases where the 
allocations would be quite large. In the case of reflection API the 
allocations are small *and* the function returns right away, thus not 
keeping stuff on the stack for too long.

Andi

At 11:09 PM 11/20/2003 +0100, Timm Friebe wrote:
>
> > […]
>[...]
>
>Here's a patch for zend_reflection_api.c which uses do_alloca /
>free_alloca in all relevant places.
>
>- Timm

---

## #5682 — Marcus Boerger — 2003-11-23T16:13:29+00:00

URL: https://externals.io/message/5608#5682 · In reply to: #5678

Hello Andi,

Sunday, November 23, 2003, 1:56:20 PM, you wrote:

> Commited thanks.
> Marcus, what we meant was that you shouldn't use do_alloca() in the engine
> except for in the cases I mentioned, and possibly cases where the 
> allocations would be quite large. In the case of reflection API the 
> allocations are small *and* the function returns right away, thus not 
> keeping stuff on the stack for too long.

Me has a big 'aha' effect :-)

-- 
Best regards,
 Marcus                            mailto:helly@php.net
