原文
#include <windows.h>
#include <malloc.h>
#include <stdio.h>//用于`列表项`的结构;第一个成员是`SLIST_ENTRY`结构,其他成员是数据.在此,数据只是测试
typedef struct _PROGRAM_ITEM {SLIST_ENTRY ItemEntry;ULONG Signature;
} PROGRAM_ITEM, *PPROGRAM_ITEM;
int main( )
{ULONG Count;PSLIST_ENTRY pFirstEntry, pListEntry;PSLIST_HEADER pListHead;PPROGRAM_ITEM pProgramItem;//初化`列表头`为`MEMORY_ALLOCATION_ALIGNMENT`边界.pListHead = (PSLIST_HEADER)_aligned_malloc(sizeof(SLIST_HEADER),MEMORY_ALLOCATION_ALIGNMENT);if( NULL == pListHead ){printf("分配失败\n");return -1;}InitializeSListHead(pListHead);//在列表中插入`10`个项目.for( Count = 1; Count <= 10; Count += 1 ){pProgramItem = (PPROGRAM_ITEM)_aligned_malloc(sizeof(PROGRAM_ITEM),MEMORY_ALLOCATION_ALIGNMENT);if( NULL == pProgramItem ){printf("分配失败");return -1;}pProgramItem->Signature = Count;pFirstEntry = InterlockedPushEntrySList(pListHead, &(pProgramItem->ItemEntry)); }//从列表中删除`10`项,并显示签名.for( Count = 10; Count >= 1; Count -= 1 ){pListEntry = InterlockedPopEntrySList(pListHead);if( NULL == pListEntry ){printf("List is empty.\n");return -1;}pProgramItem = (PPROGRAM_ITEM)pListEntry;printf("Signature is %d\n", pProgramItem->Signature);//此例假定`SLIST_ENTRY`结构,是该结构的第一个成员.如果结构不遵循此约定,则必须在调用`free`函数前,计算结构的`起始地址`._aligned_free(pListEntry);}//刷新列表并验证项是否已消失.pListEntry = InterlockedFlushSList(pListHead);pFirstEntry = InterlockedPopEntrySList(pListHead);if (pFirstEntry != NULL){printf("Error: List is not empty.\n");return -1;}_aligned_free(pListHead);return 1;
}