Windows Memory Management (2)-lookaside structure and runtime functions

Source: Internet
Author: User

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_list
Lookaside,
In pallocate_function
AllocateOptional,
In pfree_function
FreeOptional,
In ulong
Flags,
In size_t
Size,
In ulong
Tag,
In ushort
Depth
);

(2) void
Exinitializepagedlookasidelist (
In ppaged_lookaside_list
Lookaside,
In pallocate_function
AllocateOptional,
In pfree_function
FreeOptional,
In ulong
Flags,
In size_t
Size,
In ulong
Tag,
In ushort
Depth
);

After the lookaside object is initialized, you can apply for the memory:

(1) pvoid

Exallocatefromnpagedlookasidelist (
In pnpaged_lookaside_list
Lookaside
);

(2) pvoid

Exallocatefrompagedlookasidelist (
In ppaged_lookaside_list
Lookaside
);

Reclaim memory for lookaside objects:

(1) void

Exfreetonpagedlookasidelist (
In pnpaged_lookaside_list
Lookaside,
In pvoid
Entry
);

(2) void

Exfreetopagedlookasidelist (
In ppaged_lookaside_list
Lookaside,
In pvoid
Entry
);

After using the lookaside object, delete the lookaside object:

(1) void

Exdeletenpagedlookasidelist (
In pnpaged_lookaside_list
Lookaside
);

(2) void
Exdeletepagedlookasidelist (
In ppaged_lookaside_list
Lookaside
);

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_t
Length
);

(2) Inter-memory replication (OVERLAPPING)

Void
Rtlmovememory (
In void unaligned
*Destination,
In const void unaligned *
Source,
In size_t
Length
);

(3) memory Filling

Void
Rtlfillmemory (
In void unaligned
*Destination,
In size_t
Length,
In uchar
Fill
);

Void
Rtlzeromemory (
In void unaligned
*Destination,
In size_t
Length
);

(4) memory comparison

Size_t
Rtlcomparememory (
In const void
*Source1,
In const void
*Source2,
In size_t
Length
);

Ulong
Rtlequalmemory (
Const void *
Source1,
Const void *
Source2,
Size_t
Length
);

# 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 "));}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.