Skip to contentSkip to conversation

Scope of snd_pcm_hw_params_alloca() ?

DISCUSSION Alan Horstmann 2 participants 5 messages Mar 13, 2008 – Mar 14, 2008 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

I've spent a couple of days pining this down.

snd_pcm_hw_params_alloca() is part of the user API -right? But accomplished as a #define in pcm.h.

However if it is called inside a function, and the pointer stored for future use, after leaving the function the allocation seems to have gone, and attempting thereafter to use the pointer to access the hwparams space in another API function results in seg-fault.

This seems rather counter-intuitive (ie unexpected) to someone who has no knowledge of the inner workings, but simply uses the API; the allocation disappears without having been specifically free'd or released in any way. I know that calling it globally gets round the problem, but certainly I could do without all the debug needed to discover that.

Comments?

Alan

#1 lore raw

At Thu, 13 Mar 2008 16:21:51 +0000,

Quoted 5 lines · Alan Horstmann

I've spent a couple of days pining this down.

snd_pcm_hw_params_alloca() is part of the user API -right? But accomplished as a #define in pcm.h.

Because alloca can be used only with a macro due to its nature.

Quoted 10 lines

However if it is called inside a function, and the pointer stored for future use, after leaving the function the allocation seems to have gone, and attempting thereafter to use the pointer to access the hwparams space in another API function results in seg-fault.

This seems rather counter-intuitive (ie unexpected) to someone who has no knowledge of the inner workings, but simply uses the API; the allocation disappears without having been specifically free'd or released in any way. I know that calling it globally gets round the problem, but certainly I could do without all the debug needed to discover that.

Well, alloca() is an (almost) standard stuff. You had a good lecture for a bit more deep studying C :)

Takashi

A message that is not in the archive (sent off-list or not yet received): [email protected]

#3 lore raw

At Thu, 13 Mar 2008 15:41:02 -0300,

Quoted 27 lines · Aldrin Martoq

Hello,

Quoted 10 lines · Takashi Iwai

At Thu, 13 Mar 2008 16:21:51 +0000,

Quoted 3 lines · Alan Horstmann

I've spent a couple of days pining this down. snd_pcm_hw_params_alloca() is part of the user API -right? But accomplished as a #define in pcm.h.

Because alloca can be used only with a macro due to its nature.

Quoted 4 lines

However if it is called inside a function, and the pointer stored for future use, after leaving the function the allocation seems to have gone, and attempting thereafter to use the pointer to access the hwparams space in another API function results in seg-fault.

You should use snd_pcm_hw_params_malloc() instead. *_alloca() from asoundlib are based in alloca(). From my system man:

$ man alloca
NAME
       alloca - memory allocator

SYNOPSIS

       #include <alloca.h>

       void *alloca(size_t size);
Quoted 8 lines

DESCRIPTION
The alloca() function allocates size bytes of space in the stack frame of the caller. This temporary space is automatically freed when the function that called alloca() returns to its caller.

[..]

Yup, this is actually very useful in many many many places!

Especially in C, that isn't easy to have implicit destructors.

BTW, a pitfall once I fell into is that the alloca frees the data at the function exit, not at the block exit. So, the following code eats stack up and shall evetually dies:

{
	...
	for (;;) {
		char *tmp = alloca(4096);
		...
	}
	...
}

Takashi

#4 lore raw
In reply to Takashi Iwai
Quoted 12 lines · you

At Thu, 13 Mar 2008 16:21:51 +0000,

Quoted 2 lines · Alan Horstmann

snd_pcm_hw_params_alloca() is part of the user API -right? But accomplished as a #define in pcm.h.

Because alloca can be used only with a macro due to its nature.

Quoted 4 lines

However if it is called inside a function, and the pointer stored for future use, after leaving the function the allocation seems to have gone, and attempting thereafter to use the pointer to access the hwparams space in another API function results in seg-fault.

So would it be better in this case to use the malloc /free functions if possible? Am I right that they are not provided in salsa, so alloca would be necessary to use that lib?

Alan

#5 lore raw

At Fri, 14 Mar 2008 12:19:57 +0000,

Quoted 10 lines · Alan Horstmann
Quoted 8 lines · you

At Thu, 13 Mar 2008 16:21:51 +0000,

Quoted 2 lines · Alan Horstmann

snd_pcm_hw_params_alloca() is part of the user API -right? But accomplished as a #define in pcm.h.

Because alloca can be used only with a macro due to its nature.

Quoted 7 lines
Quoted 4 lines
Quoted 4 lines

However if it is called inside a function, and the pointer stored for future use, after leaving the function the allocation seems to have gone, and attempting thereafter to use the pointer to access the hwparams space in another API function results in seg-fault.

So would it be better in this case to use the malloc /free functions if possible?

No, it depends on your purpose.

The merit of alloca is that it's really a temporary variable that requires no explicit destructor, and is faster and lighter than malloc/free. If you don't need to reuse it, you can safely use alloca. But if you need to reused the allocated record later, malloc is the only choice.

Quoted 2 lines

Am I right that they are not provided in salsa, so alloca would be
necessary to use that lib?

Hm? It's provided in SALSA, too.

Takashi