IOBuffer allocation strategy...

Whilst being wrong this week I reviewed some of the other socket server entries on this site and was reminded of an item that’s on a todo list somewhere by a comment on this posting from Matt: “What do you think about using VirtualAlloc to allocate page-aligned memory for the receive buffers? Would it reduce the number of locked pages? I mean if you allocate a buffer of ‘page size’ using ’new[]’ it is very likely that the buffer uses 2 pages instead of 1.”

This sounds sensible and the change is easy to make, in Buffer.cpp

void *CBuffer::operator new(
   size_t objectSize, size_t bufferSize)
{
   void *pMem = VirtualAlloc(0, objectSize + bufferSize, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
   //void *pMem = new char[objectSize + bufferSize];

   return pMem;
}

void CBuffer::operator delete(
   void *pObject, size_t /* bufferSize*/)
{
   VirtualFree(pObject, 0, MEM_RELEASE);
   //delete [] pObject;
}

Obviously things will work better is you set your buffer size of be a multiple of the page size less ‘objectSize’ and to that end the next release of CBufferAllocator will have some allocation flags that you can pass to the constructor which make it easier to allocate buffers in page sizes…