From: Larry on
WAVEHDR allocationg several buffers

Hi,

I'm dealing with WAVEHDR from the waveForm API. I'd like to know which of
the following is the best approach to set up some buffers:


1)
// Define WAVEHDR Structure:

WAVEHDR *buff = new WAVEHDR[num_buffers];

for (int i = 0; i<num_buffers; i++)
{
ZeroMemory(&buff[i], sizeof(buff[i]));
buff[i].lpData = (LPSTR) malloc(system_buf_len);
buff[i].dwBufferLength = system_buf_len;
buff[i].dwBytesRecorded = 0;
buff[i].dwUser = 0;
buff[i].dwFlags = 0;
buff[i].dwLoops = 0;

if(waveInPrepareHeader(hwi, &buff[i], sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
printf("waveInPrepareHedare: ERROR!\n"); // return -1;

if(waveInAddBuffer(hwi, &buff[i], sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
printf("waveInAddBuffer: ERROR!\n"); // return -1;
}


2)
// Define WAVEHDR Structure:

WAVEHDR **buff = new WAVEHDR*[num_buffers];

for (int i = 0; i<num_buffers; i++)
{
buff[i] = new WAVEHDR;
ZeroMemory(buff[i],sizeof(WAVEHDR));
buff[i]->lpData = new char[system_buf_len;]
buff[i]->dwBufferLength = system_buf_len;
buff[i]->dwBytesRecorded = 0;
buff[i]->dwUser = 0;
buff[i]->dwFlags = 0;
buff[i]->dwLoops = 0;

if(waveInPrepareHeader(hwi, buff[i], sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
printf("waveInPrepareHedare: ERROR!\n"); // return -1;

if(waveInAddBuffer(hwi, buff[i], sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
printf("waveInAddBuffer: ERROR!\n"); // return -1;
}

thanks

NB: both of them seems to be working fine!

From: Giovanni Dicanio on
"Larry" <dontmewithme(a)got.it> ha scritto nel messaggio
news:4b508721$0$823$4fafbaef(a)reader5.news.tin.it...

> I'd like to know which of the following is the best approach to set up
> some buffers:

> 1)
> // Define WAVEHDR Structure:
>
> WAVEHDR *buff = new WAVEHDR[num_buffers];

> 2)
> // Define WAVEHDR Structure:
>
> WAVEHDR **buff = new WAVEHDR*[num_buffers];
>
> for (int i = 0; i<num_buffers; i++)
> {
> buff[i] = new WAVEHDR;

I'm not sure, but I think that option #1 would give you better locality,
because the WAVEHDR instances are allocated contiguously (instead, in
version #2, you allocate the pointers WAVEHDR* contiguously, but the
instances of WAVEHDR can be scattered in the heap).

Giovanni


From: Tim Roberts on
"Larry" <dontmewithme(a)got.it> wrote:
>
>WAVEHDR allocationg several buffers
>
> I'm dealing with WAVEHDR from the waveForm API. I'd like to know which of
>the following is the best approach to set up some buffers:

The "best" choice depends on your definition of the word "best".

>1)
>WAVEHDR *buff = new WAVEHDR[num_buffers];
>
>for (int i = 0; i<num_buffers; i++)
>{
> ZeroMemory(&buff[i], sizeof(buff[i]));
> buff[i].lpData = (LPSTR) malloc(system_buf_len);
>2)
>WAVEHDR **buff = new WAVEHDR*[num_buffers];
>
>for (int i = 0; i<num_buffers; i++)
>{
> buff[i] = new WAVEHDR;
> ZeroMemory(buff[i],sizeof(WAVEHDR));
> buff[i]->lpData = new char[system_buf_len;]

I see no justification for allocating the WAVEHDRs individually. On the
other hand, you could let the system help you by doing:

std::vector<WAVEHDR> buff;
buff.resize(num_buffers);

As for the individual buffers, that's really up to you. At the lowest
level, "malloc" and "new" both map to the same low-level APIs. In a C++
program, I usually prefer "new" to "malloc".
--
Tim Roberts, timr(a)probo.com
Providenza & Boekelheide, Inc.