casting issue with linked list item

2019-06-14 10:09发布

This is my PER_IO_CONTEXT structure (i stored them in singly linked list):

 typedef struct _PER_IO_CONTEXT 
 {

    SLIST_ENTRY       ItemEntry;
    WSAOVERLAPPED     Overlapped;
    WSABUF                 wsabuf; 
    /* some other data*/

 } PER_IO_CONTEXT, *PPER_IO_CONTEXT;

and below is WSAsend , that use the list for getting WSAOVERLAPPED structure:

...
PSLIST_HEADER pListHead;
...

PSLIST_ENTRY  pListEntry = InterlockedPopEntrySList(pListHead);
PPER_IO_CONTEXT ovl = (PPER_IO_CONTEXT)pListEntry;
WSASend(pTmp1->Socket,..., &(ovl->Overlapped), NULL);

and the last part when GQCS gets notification:

 LPWSAOVERLAPPED lpOverlapped = NULL;
 PPER_IO_CONTEXT lpIOContext = NULL; 
 ....
 GetQueuedCompletionStatus(..... (LPOVERLAPPED *)&lpOverlapped,  INFINITE);
 lpIOContext = (PPER_IO_CONTEXT)lpOverlapped;
 lpIOContext->wsabuf       // this fail

As you can see following cast lpIOContext =(PPER_IO_CONTEXT)lpOverlapped doesn't work because WSAsend was provided with wsaoverlapped - the second member of PER_IO_CONTEXT structure, so dereferences such as lpIOContext-> can't be used in this case.

There is a way to deal with this situation?

2条回答
倾城 Initia
2楼-- · 2019-06-14 10:46

To get the address of the corresponding PER_IO_CONTEXT struct you can use this:

lpIOContext = CONTAINING_RECORD(lpOverlapped, PER_IO_CONTEXT, Overlapped);

CONTAINING_RECORD is a macro defined in VC\crt\src\collections.h in such a way:

#define CONTAINING_RECORD(address, type, field) \
    ((type *)((char *)(address) - (ULONG_PTR)(&((type *)0)->field)))

More information: http://msdn.microsoft.com/en-us/library/windows/hardware/ff542043%28v=vs.85%29.aspx

查看更多
ら.Afraid
3楼-- · 2019-06-14 10:52

I'm not sure whether there's a supported mechanism to convert a pointer to a member of a struct to a pointer to the struct. You could cast everything to BYTE * and do the arithmetic, which would work in practice, but depending on your needs it might be cleaner to reorganize to avoid the necessity:

typedef struct _PER_IO_CONTEXT 
{
   WSAOVERLAPPED     Overlapped;
   WSABUF                 wsabuf; 
   /* some other data*/
} PER_IO_CONTEXT, *PPER_IO_CONTEXT;

typedef struct _PER_IO_CONTEXT_LIST_ITEM
{
   SLIST_ENTRY       ItemEntry;
   PER_IO_CONTEXT    Item;
} PER_IO_CONTEXT_LIST_ITEM, *PPER_IO_CONTEXT_LIST_ITEM;
查看更多
登录 后发表回答