1. lookaside Structure
Frequent memory application and recovery will result in a large amount of memory holes, which will lead to the failure to apply for memory. DDK provides programmers with a lookaside structure to solve this problem.
We can regard the lookaside object as a memory container. During initialization, it first applied for a large memory from windows. In the future, when programmers apply for memory, they do not apply for memory directly from windows, but want to apply for memory for lookaside objects. Looaside intelligently avoids memory holes ". If the internal memory of the lookaside object is insufficient, it will apply for more memory from the operating system.
Lookaside is generally used in the following scenarios:
1. Each time a programmer requests a fixed memory size.
2. Frequent application and recovery operations.
To use a looaside object, first initialize the lookaside object. You can use the following two functions:
(1) void
Exinitializenpagedlookasidelist (
In pnpaged_lookaside_listLookaside,
In pallocate_functionAllocateOptional,
In pfree_functionFreeOptional,
In ulongFlags,
In size_tSize,
In ulongTag,
In ushortDepth
);
(2) void
Exinitializepagedlookasidelist (
In ppaged_lookaside_listLookaside,
In pallocate_functionAllocateOptional,
In pfree_functionFreeOptional,
In ulongFlags,
In size_tSize,
In ulongTag,
In ushortDepth
);
After the lookaside object is initialized, you can apply for the memory:
(1) pvoid
Exallocatefromnpagedlookasidelist (
In pnpaged_lookaside_listLookaside
);
(2) pvoid
Exallocatefrompagedlookasidelist (
In ppaged_lookaside_listLookaside
);
Reclaim memory for lookaside objects:
(1) void
Exfreetonpagedlookasidelist (
In pnpaged_lookaside_listLookaside,
In pvoidEntry
);
(2) void
Exfreetopagedlookasidelist (
In ppaged_lookaside_listLookaside,
In pvoidEntry
);
After using the lookaside object, delete the lookaside object:
(1) void
Exdeletenpagedlookasidelist (
In pnpaged_lookaside_listLookaside
);
(2) void
Exdeletepagedlookasidelist (
In ppaged_lookaside_listLookaside
);
Test code:
# Pragma initcodevoid lookasidetets () {kdprint ("Enter the lookasidetest function! \ N "); paged_lookaside_list lookaside; Evaluate (& lookaside, null, null, 0, sizeof (mydatastruct), 'abcd', 0); pmydatastruct pmydata [50]; for (INT I = 0; I <50; I ++) {pmydata [I] = (pmydatastruct) exallocatefrompagedlookasidelist (& lookaside); If (I + 1) % 10 = 0) {kdprint ("% d data has been applied! \ N ", ++ I) ;}}for (INT I = 0; I <50; I ++) {exfreetopagedlookasidelist (& lookaside, pmydata [I]); pmydata [I] = NULL; If (I + 1) % 10 = 0) {kdprint ("the memory for % d of data is released! \ N ", ++ I) ;}} exdeletepagedlookasidelist (& lookaside );}
2. runtime functions
(1) Inter-memory replication (non-overlapping)
Void
Rtlcopymemory (
In void unaligned *Destination,
In const void unaligned *Source,
In size_tLength
);
(2) Inter-memory replication (OVERLAPPING)
Void
Rtlmovememory (
In void unaligned*Destination,
In const void unaligned *Source,
In size_tLength
);
(3) memory Filling
Void
Rtlfillmemory (
In void unaligned*Destination,
In size_tLength,
In ucharFill
);
Void
Rtlzeromemory (
In void unaligned*Destination,
In size_tLength
);
(4) memory comparison
Size_t
Rtlcomparememory (
In const void*Source1,
In const void*Source2,
In size_tLength
);
Ulong
Rtlequalmemory (
Const void *Source1,
Const void *Source2,
Size_tLength
);
# Define buffer_size 1024 # pragma initcodevoid rtltest () {kdprint ("Enter the rtltest function! \ N "); puchar pbuffer1 = (puchar) values (pagedpool, buffer_size); bytes (pbuffer1, buffer_size); puchar pbuffer2 = (puchar) exallocatepool (pagedpool, buffer_size ); rtlfillmemory (pbuffer2, buffer_size, 0xaa); rtlcopymemory (pbuffer1, pbuffer2, buffer_size); If (rtlequalmemory (pbuffer1, pbuffer2, buffer_size )) {kdprint ("the two memory blocks have the same data! \ N "); For (INT I = 0; I <buffer_size; I ++) {kdprint (" % 02x ", pbuffer1 [I]);} else {kdprint ("Two memory blocks have different data! \ N ");} kdprint (" Exit rtltest function! \ N "));}