Windows core programming learning notes-Chapter 15th

Source: Internet
Author: User

Chapter 1 use virtual memory in applications


Windows provides three mechanisms to manipulate the memory.

Ø virtual memory: It is ideal for managing large object arrays or large Structured arrays. ---- This chapter discusses

Ø memory ing file: It is most suitable for managing large data streams (usually files) and sharing data among multiple processes running on the same machine. ---- Chapter 16

Ø heap: Suitable for managing a large number of small objects. ---- Chapter 17

Once again, the virtual memory is most suitable for managing large arrays, including object data and Structured arrays.

15.1 reserved address space area

Pvoid virtualalloc (

Pvoid pvaddress, // null.

Size_t dwsize, // the size of the expected region, in bytes. Because the system always predefines the area based on an integer multiple of the CPU page size, If you reserve a area of 62kb on a machine with a page size of 4 kb, the resulting area size is 64 KB (an integer multiple of 4 ).

DWORD fdwallocationtype, // specifies whether to reserve a region or allocate physical storage. Mem_reserve is used to reserve the address space area.

DWORD fdwprotect); // specifies the protection attribute for the region, which is generally page_readwrite. The protection attribute of the region is not related to the protection attribute of the physical storage. When no physical memory is allocated to the region, any memory address in the attempt to access the region will cause access violations.

Return Value: The base address of the specified region is returned successfully. If the return value is null.

15.2 allocating physical storage to the region

After a region is reserved, You need to allocate physical memory to the region to access the memory address. When transferring physical memory, the starting address is always an integer multiple of the page size, and the entire size is also an integer multiple of the page size.

Using virtualalloc, the third parameter uses mem_commit. For a reserved region, you must tell virtualalloc how much physical storage is allocated. Pvaddress (memory address to which the physical memory is allocated) and dwsize (number of physical memory, in bytes) are used. 8-bit (BIT) = 1 byte, 1024 bytes = 1 kb) to specify. This method is used to operate large arrays. You do not need to specify the memory address when transferring resources directly instead of in the reserved area. Of course, it is not necessary to reserve virtual memory first.

15.3 simultaneously reserve and allocate physical storage

The third parameter needs to be passed into mem_reserve | mem_commit.

15.4 when to transfer physical storage

For a workbook, if it supports 200 rows and 256 columns. For each cell, we need a celldata structure to describe the content of the cell. The simplest operation method for two-dimensional array cells is to declare the following variable: celldata
Celldata [200] [256]; if the celldata structure is 128 bytes, the two-dimensional array requires 200*256*128 bytes of physical storage. For a workbook, most users only use a few cells. Is it a waste?

If the linked list is used, the celldata structure corresponding to a cell in the workbook must be created only when data is indeed stored in a cell. It saves a lot of physical memory, but increases the difficulty of reading the content of cells and needs to be traversed each time.

The virtual memory technology provides a compromise:

1)
Reserve a large enough area to accommodate the entire array of the celldata structure. Physical storage is not consumed only for the specified region.

2)
When you input data in a cell, first determine the memory address of the celldata structure in the region. Because the address has not been mapped to the physical memory, access to the memory address will cause access violations.

3)
Allocate physical memory to the memory address in step 1.

4)
Set the celldata structure member.

There is a problem: You must determine when physical storage needs to be transferred. If you only edit or modify data in cells, you do not have to allocate physical storage. There are two ways to determine whether to allocate physical storage to a part of the region:

1)
Physical storage is always transferred. If the instance has been transferred, the system will not transfer the instance and return null.

2)
Use structured exception handling (seh) ------ the best solution. Chapter 23-25.

15.5 cancel physical storage allocation and release area

Bool virtaulfree (

Lpvoid pvaddress,

Size_t dwsize,

DWORD fdwfreetype );

If dwsize is 0 and pvaddress is the base address of the Region (that is, the address returned by virtualalloc when the region is reserved), virtaulfree will release the entire region and allocate it to the physical memory of the region. The third parameter is mem_release, which tells the system to release all address spaces reserved for this region.

If you want to cancel a part of the physical memory allocated to the region, but do not want to release the entire region, You need to specify the memory address to pvaddress to tell the system to cancel the address of the first page; specify the size of the physical memory to be released in dwsize, and pass mem_decommit to the third parameter.

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.