Discussion:
WAVEHDR allocationg several buffers
(too old to reply)
Larry
2010-01-15 15:17:53 UTC
Permalink
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!
Giovanni Dicanio
2010-01-15 17:29:03 UTC
Permalink
I'd like to know which of the following is the best approach to set up
1)
WAVEHDR *buff = new WAVEHDR[num_buffers];
2)
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
Tim Roberts
2010-01-17 02:11:08 UTC
Permalink
Post by Larry
WAVEHDR allocationg several buffers
I'm dealing with WAVEHDR from the waveForm API. I'd like to know which of
The "best" choice depends on your definition of the word "best".
Post by Larry
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, ***@probo.com
Providenza & Boekelheide, Inc.
Loading...