I / O Port Replenishment and OVERLAPPED Management

How win32 manages instances of the OVERLAPPED structure in the context of two functions:

GetQueuedCompletionStatus PostQueuedCompletionStatus 
  • When I call GetQueuedCompletionStatus does win32 a free instance of the OVERLAPPED structure or should I do it myself?

  • When I send data using PostQueuedCompletionStatus, does win32 copy it to internal structures? When should I free the memory of sent data?

  • Where could I find an OVERLAPPED data processing image between GetQueuedCompletionStatus, PostQueuedCompletionStatus and IOCP queue?

+4
source share
2 answers

OVERLAPPED structure must exist when a successful I / O operation (or manual PostQueuedCompletionStatus () ) is performed until OVERLAPPED arises from a call to GetQueuedCompletionStatus () .

You are responsible for the lifetime of the structure.

In MSDN docs, you'll see that GetQueuedCompletionStatus() actually takes a "pointer to a variable that receives the address of the OVERLAPPED structure that was specified when the completed I / O operation started." What you actually get from this call is a pointer to the original OVERLAPPED that you passed when you made the call to PostQueuedCompletionStatus() (or initiated the I / O overlap operation).

This is all very useful, since the “normal” way to use the OVERLAPPED structure is to put it in a large structure that contains all the information about “every operation” that you may need - so this is an ideal way to navigate directly from a limited the information you give when you call GetQueuedCompletionStatus() , for example, in the data buffer that you used in your overlapping read call ...

I believe that the best way to deal with OVERLAPPED structures is to: a) embed them in the buffer that you use for reading / writing; b) refer to them and c) return them to the pool for reuse when ref count drops to 0.

I have the source code that you can download ( here ), which can make it a little easier to understand (this is a complete IOCP server example so it is a bit complicated, but it works and shows how these things can be used).

+4
source
  • You must pass the address OVERLAPPED * to GetQueuedCompletionStatus . This is populated with the value passed to the PostQueuedCompletionStatus .
  • You should not release this data in the context of PostQueuedCompletionStatus . This needs to be done using context using GetQueuedCompletionStatus . (Assuming that it was distributed dynamically in the first place - there is no requirement that it be a dynamically distributed structure, it could be pushed out of a fixed pool or a function that cannot be returned on the stack until it is informed that the operation is completed) .
  • I'm not sure there is such a picture.
+1
source

Source: https://habr.com/ru/post/1497020/


All Articles