I noticed that Valgrind is not happy about what's going on inside aio_read(). Take a look at this program:
Code:
#include <sys/types.h>
#include <aio.h>
#include <fcntl.h>
#include <iostream>
#include <vector>
using namespace std;
const int SIZE_TO_READ = 100;
vector<aiocb*> cbs;
void fillaiocbs(int count, int size, int offset, int file)
{
for(int i = 0; i < count; i++)
{
aiocb* cb = new aiocb;
memset(cb, 0, sizeof(aiocb));
cb->aio_nbytes = SIZE_TO_READ;
cb->aio_fildes = file;
cb->aio_offset = 0;
cb->aio_buf = new char[size];
cbs.push_back(cb);
}
}
void deletecbs()
{
for (int i = 0; i < cbs.size(); i++)
{
delete[] (char*)cbs[i]->aio_buf;
delete cbs[i];
}
}
int main()
{
// open the file
int file = open("something or other", O_RDONLY, 0);
if (file == -1)
{
cout << "Unable to open file!" << endl;
return 1;
}
// create the buffer
//char* buffer = new char[SIZE_TO_READ];
// create the control block structure
fillaiocbs(1000, SIZE_TO_READ, 0, file);
// read!
for (int i = 0; i < cbs.size(); i++)
{
if (aio_read(cbs[i]) == -1)
{
cout << "Unable to create request!" << endl;
close(file);
}
}
cout << "Request enqueued!" << endl;
// wait until the request has finished
for (int i = 0; i < cbs.size(); i++)
{
while(aio_error(cbs[i]) == EINPROGRESS)
{
cout << "Working..." << endl;
}
}
// success?
/*int numBytes = aio_return(cb);
if (numBytes != -1)
cout << "Success!" << endl;
else
cout << "Error!" << endl;
*/
// now clean up
//delete cb;
//delete[] buffer;
deletecbs();
close(file);
return 0;
}
The code creates 1000 read requests from a certain file and waits for them to complete. Valgrind reports that almost 30KB is leaked inside aio_read().
I'm using Debian Testing with the 2.6.8-2-386 kernel. Is this a known issue? Does anyone else get similar results? Am I doing something wrong?