Memory page allocation method, readable storage medium and computer program product

CN121277843BActive Publication Date: 2026-10-30FREMONT MICRO DEVICES SHENZHEN LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202511278910.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-09-08
Publication Date
2026-10-30
Estimated Expiration
2045-09-08

AI Technical Summary

Technical Problem

[0003]分页机制的引入虽然可以增加RAM的内存空间,但是也带来了插入的切页指令会导致代码体积增大,占用MCU更多的只读存储器(Read-Only Memory,ROM)空间的同时产生了切页开销,程序效率降低的问题;因此,如何减少因为MCU的分页机制产生的切页开销,成了MCU编译器需要考虑的问题

Benefits of technology

[0019]根据上述实施例中的一个技术方案,具有以下有益效果或者优点:

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121277843B_ABST
    Figure CN121277843B_ABST
Patent Text Reader

Abstract

The disclosure provides a memory page allocation method, a readable storage medium and a computer program product; the allocation method is applied to a random access memory of a microcontroller, and includes: obtaining an object to be allocated; sequentially taking each of N pages as a candidate page, determining a page break overhead reduction amount between the object to be allocated and all adjacent objects when the object to be allocated is allocated to the candidate page according to the object to be allocated and adjacent relationship data; calculating a benefit density when the object to be allocated is allocated to the candidate page according to the page break overhead reduction amount, a size of the object to be allocated and a page size of the candidate page; wherein the adjacent object is an object having an adjacent access relationship with the object to be allocated; determining a target page from the N candidate pages according to the benefit density, and allocating the object to be allocated to the target page; the method can reduce the page break overhead of the RAM of the microcontroller, and optimize the ROM space of the microcontroller.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This disclosure relates to the field of microcontroller technology, and more particularly to a method for allocating memory pages, a readable storage medium, and a computer program product. Background Technology

[0002] Currently, memory paging is a common method in embedded architectures. Its advantage is that it can increase memory space without adding an address bus. For microcontroller unit (MCU) architectures, such as 8-bit microcontrollers, the data memory space, Random Access Memory (RAM), is divided into multiple pages, and the pages in RAM are called banks. The processor (CPU) in the MCU can only access one bank at a time. When accessing different banks, a page-splitting instruction (banksel) is needed to select the page.

[0003] While the introduction of paging mechanisms can increase RAM memory space, it also brings problems such as increased code size due to the inserted page-splitting instructions, increased ROM space usage in the MCU, page-splitting overhead, and reduced program efficiency. Therefore, how to reduce the page-splitting overhead caused by the MCU's paging mechanism has become a problem that MCU compilers need to consider. Summary of the Invention

[0004] In view of the above problems, this disclosure provides a memory page allocation method, a readable storage medium, and a computer program product, which can reduce the page switching overhead of the microcontroller's RAM, improve program efficiency, and optimize the microcontroller's ROM space.

[0005] Firstly, the following technical solution is provided through an embodiment:

[0006] A memory page allocation method is applied to the random access memory (RAM) of a microcontroller. The RAM is configured with N pages, where N ≥ 2 and is an integer. The method includes: obtaining objects to be allocated; sequentially selecting each of the N pages as candidate pages; determining, based on the objects to be allocated and adjacency relationship data, the reduction in page overhead between the objects to be allocated and all adjacent objects when allocating the objects to be allocated to the candidate pages; calculating the benefit density when allocating the objects to be allocated to the candidate pages based on the reduction in page overhead, the size of the objects to be allocated, and the size of the candidate pages; wherein, the adjacency relationship data includes adjacency access relationships between multiple objects, and adjacent objects are objects that have adjacency access relationships with the objects to be allocated; determining a target page from the N candidate pages based on the benefit density, and allocating the objects to be allocated to the target page.

[0007] In some embodiments, the adjacency relationship data also includes the page where each object is located and the number of adjacent accesses between any two adjacent objects; based on the object to be assigned and the adjacency relationship data, determining the reduction in page splitting overhead between the object to be assigned and all adjacent objects when assigning the object to be assigned to the candidate page includes: traversing each adjacent object of the object to be assigned in the adjacency relationship data, calculating the reduction in the number of page splitting instructions between the object to be assigned and adjacent objects when assigning the object to be assigned to the candidate page based on the page where the adjacent object is located and the candidate page; and obtaining the reduction in page splitting overhead based on the reduction in the number of page splitting instructions between the object to be assigned and each adjacent object and the number of adjacent accesses.

[0008] In some embodiments, the reduction in the number of page-slicing instructions between the object to be assigned and its neighboring objects is calculated when the object to be assigned is assigned to the page to be selected, based on the page number of the page to be selected and the page number of the page where the neighboring object is located. This includes: determining the Hamming distance between the page to be selected and the page where the neighboring object is located based on the page number of the page to be selected and the page number of the page where the neighboring object is located; and obtaining the reduction in the number of page-slicing instructions based on the difference between log2N and the Hamming distance.

[0009] In some embodiments, the reduction in the number of page-slicing instructions between the object to be assigned and its neighboring objects is calculated when the object to be assigned is assigned to the page to be selected, based on the page number of the first page where the object to be assigned is located before assignment and the page number of the second page where the neighboring object is located; the second Hamming distance between the page to be assigned and the second page is located, based on the page number of the first page where the object to be assigned is located and the page number of the second page where the neighboring object is located; the second Hamming distance between the page to be assigned and the second page, based on the page number of the page to be assigned and the page number of the second page; and the reduction in the number of page-slicing instructions is obtained based on the difference between the first Hamming distance and the second Hamming distance.

[0010] In some embodiments, the reduction in page truncation overhead is obtained based on the reduction in the number of page truncation instructions between the object to be assigned and each adjacent object and the number of adjacent accesses, including: obtaining the page truncation benefit based on the product of the reduction in the number of page truncation instructions between the object to be assigned and an adjacent object and the number of adjacent accesses; and obtaining the reduction in page truncation overhead based on the sum of the page truncation benefits between the object to be assigned and all adjacent objects.

[0011] In some embodiments, the revenue density of allocating the object to be allocated to the selected page is calculated based on the reduction in page splitting overhead, the size of the object to be allocated, and the page size of the selected page, including: obtaining the capacity ratio by dividing the page size of the selected page by the size of the object to be allocated; and obtaining the revenue density by multiplying the reduction in page splitting overhead by the capacity ratio.

[0012] In some embodiments, determining the target page from N candidate pages based on the benefit density includes: selecting the candidate page with the highest benefit density as the target page; or, obtaining the remaining space of each candidate page, and selecting the candidate page with the highest benefit density from the candidate pages whose remaining space is greater than a set value or greater than the size of the object to be allocated as the target page.

[0013] In some embodiments, after assigning the object to be assigned to the target page, the method further includes updating the adjacency relationship data based on the page number of the object to be assigned and the page number of the target page.

[0014] In some embodiments, the objects to be allocated include function stacks and / or global variables.

[0015] Secondly, based on the same inventive concept, the following technical solution is provided through an embodiment:

[0016] A computer-readable storage medium having a computer program stored thereon, wherein the computer program, when executed by a processor, implements any of the allocation methods provided in the first aspect embodiment.

[0017] Thirdly, based on the same inventive concept, the following technical solution is provided through an embodiment:

[0018] A computer program product includes a computer program and / or instructions that, when executed by a processor, implement any of the allocation methods provided in the first aspect embodiment.

[0019] According to one of the technical solutions in the above embodiments, the following beneficial effects or advantages are achieved:

[0020] This disclosure provides a memory page allocation method for RAM in a microcontroller (MCU). Based on the object to be allocated and its adjacency data, the method calculates the reduction in page overhead between the object and all adjacent objects when allocating the object to be allocated to any candidate page. The method then comprehensively determines the benefit density of allocating the object to be allocated to a candidate page based on the reduction in page overhead, the size of the object to be allocated, and the page size of the candidate pages. Finally, the method determines the target page to which the object to be allocated is based on this benefit density. The method fully considers the MCU's paging mechanism when determining the benefit density, taking into account the influence of multiple factors such as the reduction in page overhead, the size of the object to be allocated, and the page size. The resulting benefit density parameter more accurately reflects the program efficiency when the object to be allocated is allocated to different pages. Therefore, determining the target page of the object to be allocated based on this benefit density can reduce the page overhead of the microcontroller's RAM, improve program efficiency, and optimize the microcontroller's ROM space.

[0021] The above description is merely an overview of the technical solution disclosed herein. In order to better understand the technical means of this disclosure and to implement it in accordance with the contents of the specification, and to make the above and other objects, features and advantages of this disclosure more apparent and understandable, specific embodiments of this disclosure are described below. Attached Figure Description

[0022] Various other advantages and benefits will become apparent to those skilled in the art upon reading the following detailed description of preferred embodiments. The accompanying drawings are for illustrative purposes only and are not intended to limit the scope of this disclosure. Furthermore, the same reference numerals denote the same parts throughout the drawings. In the drawings:

[0023] Figure 1 A flowchart illustrating a memory page allocation method according to an embodiment of the present disclosure is shown;

[0024] Figure 2 An example diagram illustrating the adjacency relationship of multiple objects in an initial state according to an embodiment of the present disclosure is shown;

[0025] Figure 3 It shows Figure 2 Example diagram showing the adjacency relationship of multiple objects after page allocation;

[0026] Figure 4 A schematic diagram of a computer-readable storage medium according to an embodiment of the present disclosure is shown. Detailed Implementation

[0027] Hereinafter, embodiments of the present disclosure will be described with reference to the accompanying drawings; however, it should be understood that these descriptions are merely exemplary and not intended to limit the scope of the present disclosure; furthermore, descriptions of well-known structures and technologies are omitted in the following description to avoid unnecessarily obscuring the concepts of the present disclosure.

[0028] The accompanying drawings illustrate various structural schematics according to embodiments of the present disclosure; these drawings are not to scale, and some details have been enlarged and may have been omitted for clarity. The various regions, shapes, and their relative sizes and positional relationships shown in the drawings are merely exemplary and may deviate from reality due to manufacturing tolerances or technical limitations. Furthermore, those skilled in the art can design regions / layers with different shapes, sizes, and relative positions as needed.

[0029] Unless otherwise defined, the technical or scientific terms used in this disclosure shall have the ordinary meaning understood by one of ordinary skill in the art to which this disclosure pertains. The terms “first,” “second,” and similar terms used in this disclosure do not indicate any order, quantity, or importance, but are merely used to distinguish different components. Similarly, the terms “an,” “a,” or “the,” and similar terms do not indicate a quantity limitation, but rather indicate the presence of at least one. The terms “comprising,” “including,” or “including,” and similar terms mean that the element or object preceding the word encompasses the elements or objects listed following the word and their equivalents, without excluding other elements or objects.

[0030] To address the issues of increased code size and reduced program efficiency caused by RAM paging mechanisms in MCU architectures, many current studies have transformed the RAM paging optimization problem into a variable classification problem. Variable classification is usually regarded as a k-balanced partitioning problem, which belongs to NP-complete problems (i.e., nondeterministic problems with polynomial complexity).

[0031] For example, one possible approach is to provide a low-complexity heuristic algorithm based on the DataFlow Graph (DFG). This algorithm analyzes the DFG and selects the edge with the highest weight (weight representing the number of adjacent visits between two variables) each time. It then assigns the variable corresponding to that edge to the same page (bank), merges the nodes after assignment, and updates the DFG. However, this method only uses weight as the basis for assignment and does not fully consider the characteristics of the MCU's paging mechanism, such as the impact of variable size and page size on the assignment. For the edge with the highest weight, if the two variables corresponding to that edge are very large, and the total size of the variables is close to or exceeds the page size of a bank, selecting multiple edges with smaller variables and assigning them to a bank might result in an even larger sum of weights. This could increase the number of page-splitting instructions in the program or fail to achieve the maximum reduction. Therefore, considering only the edge with the highest weight is not a superior choice.

[0032] To address this issue, in one optional embodiment, this disclosure provides a method for allocating memory pages, applied to the random access memory (RAM) of a microcontroller. The RAM is configured with N pages, where N ≥ 2 and is an integer. The specific number of N is determined according to actual needs and is usually a power of 2.

[0033] Please see Figure 1 The allocation method includes steps S101 to S103, as follows:

[0034] S101: Get the object to be allocated.

[0035] The object to be allocated refers to a function or variable that needs to be specified to which page (bank) it will be allocated to. The object to be allocated can include the function stack and / or global variables. The function stack can be not split and includes function parameters, return values, local variables and compiler temporary variables, etc. It can be allocated as a whole variable, which can improve the allocation efficiency of page memory.

[0036] In implementation, a list can be created to store all objects to be allocated. Then, steps S102 to S103 are executed sequentially for each object to be allocated. After the page allocation of an object to be allocated is completed, it is removed from the list until the page allocation of all objects to be allocated is completed.

[0037] S102: Sequentially select each of the N pages as a candidate page. Based on the object to be assigned and the adjacency relationship data, determine the reduction in page splitting overhead between the object to be assigned and all adjacent objects when assigning the object to be assigned to a candidate page. Calculate the revenue density when assigning the object to be assigned to a candidate page based on the reduction in page splitting overhead, the size of the object to be assigned, and the page size of the candidate page. The adjacency relationship data includes the adjacency access relationships between multiple objects, where adjacent objects are objects that have an adjacency access relationship with the object to be assigned.

[0038] In simple terms, step S102 is to calculate the reduction in page slicing overhead when an object to be assigned is assigned to any candidate page, and then, by combining the size of the object to be assigned and the page size of the candidate page, calculate the revenue density when the object to be assigned is assigned to each candidate page.

[0039] The size of the object to be allocated can be the size of the RAM occupied by the object, and the page size is the capacity of the page bank in RAM. The unit of both is the same, which can be bytes. Under normal circumstances, the page size of all pages is the same, so the page size of any page can also be used as the page size of the page to be selected for calculation.

[0040] Adjacency data includes the adjacency access relationships between multiple objects, that is, which objects have adjacent access; adjacent access means that after the program accesses one object, it will then access another object, so there is an adjacent access relationship between the two objects; an object can have adjacent access relationships with one or more objects, and multiple objects with adjacent access relationships are each other's adjacent objects.

[0041] For two adjacent objects, such as v1 and v2, if they are located on different pages, the program needs to insert a page-splitting instruction after accessing v1 and before accessing v2, thus incurring page-splitting overhead. Page-splitting overhead is the time or resource cost incurred by the processor (CPU) during the process of switching from accessing the first object to accessing the second object when two objects or variables are located in different banks. Therefore, the reduction in page-splitting overhead refers to the reduction in page-splitting overhead between the object to be allocated and all adjacent objects after the object is allocated to a certain page, compared to its state before allocation. Page-splitting overhead can be measured by the time cost of completing the page switch or by the number of page-splitting instructions, and can be configured according to actual needs. When using time cost, the reduction in page-splitting overhead is the reduction in page-splitting time after allocation; when using the number of page-splitting instructions, the reduction in page-splitting overhead is the reduction in the number of page-splitting instructions after allocation.

[0042] It should be noted that the reduction in page splitting overhead can be positive or negative. A positive value indicates that after the object to be allocated is assigned to the page, the page splitting overhead between it and all adjacent objects is reduced, and the corresponding benefit density is positive. A negative value indicates that after the object to be allocated is assigned to the page, the page splitting overhead between it and all adjacent objects is increased, and the corresponding benefit density is negative. If the object to be allocated is in an initialized state before allocation, i.e., not assigned to a page, then the reduction in page splitting overhead after allocation will definitely be positive. If the object to be allocated is already on a page before allocation, the value of the reduction in page splitting overhead can be positive or negative, and the corresponding benefit density after allocation can be positive or negative.

[0043] S103: Determine the target page from N candidate pages based on the revenue density, and assign the objects to be allocated to the target page.

[0044] After obtaining the revenue density corresponding to the object to be assigned to each candidate page, the target page to be assigned can be determined from the N candidate pages based on the revenue density. It is clear that the target page must be determined from the candidate pages with positive revenue density, and the higher the revenue density, the greater the reduction in page slicing overhead, and the more significant the improvement in program efficiency. In some embodiments, the candidate page with the highest revenue density can be determined as the target page. In other embodiments, the revenue density and the remaining space of the candidate pages can be considered together, and the candidate page with a relatively high revenue density and sufficient remaining space can be determined as the target page.

[0045] This disclosure provides a memory page allocation method for RAM of a microcontroller (MCU). Based on the object to be allocated and its adjacency data, the method calculates the reduction in page overhead between the object and all adjacent objects when allocating the object to be allocated to any candidate page. The method then comprehensively determines the benefit density of allocating the object to be allocated to a candidate page based on the reduction in page overhead, the size of the object to be allocated, and the page size of the candidate pages. Finally, the method determines the target page to which the object to be allocated is based on this benefit density. The method fully considers the MCU's paging mechanism when determining the benefit density, taking into account the influence of multiple factors such as the reduction in page overhead, the size of the object to be allocated, and the page size. The resulting benefit density parameter more accurately reflects the program (code) efficiency when the object to be allocated is allocated to different pages. Therefore, determining the target page of the object to be allocated based on this benefit density can reduce the page overhead of the microcontroller's RAM, improve program efficiency, and optimize the microcontroller's ROM space.

[0046] The principle of the above scheme can be summarized as follows: During program execution, if the page where the currently accessed RAM object (or variable) is located is denoted as bank_cur, and the page where the next RAM access object is located is denoted as bank_x, if bank_x equals bank_cur, that is, the two adjacent objects are located on the same page, then no page-splitting instruction needs to be inserted before executing the next access instruction, thus reducing page-splitting overhead. Based on this concept, through steps S101 to S103, the benefit density is obtained by comprehensively considering the reduction in page-splitting overhead between the object to be allocated and all adjacent objects, the size of the object to be allocated, and the size of the page to be selected. The target page is determined according to the benefit density, which can allocate these objects with adjacent access relationships to the same page or pages that are close to each other as much as possible. In this way, the number of page-splitting instructions inserted in the program code can be reduced to the greatest extent, thereby reducing page-splitting overhead and improving program efficiency.

[0047] The solution provided in this disclosure is preferably applicable to the currently common 8-bit MCU architecture, but can also be applied to 16-bit and 32-bit MCU architectures. The reason it is better suited for use in MCUs is that MCUs have both RAM and ROM. In the MCU field, pages in RAM are called banks. By optimizing the RAM page allocation, program efficiency can be improved and its own ROM space can be optimized at the same time. However, common CPUs do not have a paging mechanism and cannot optimize their own ROM space by adjusting the RAM page allocation. If ROM space optimization is not required, the above solution can also be used in other types of controllers.

[0048] In some embodiments, the adjacency data also includes the page where each object is located and the number of adjacent visits between any two adjacent objects; for clarity, an adjacency graph can be constructed based on the adjacency data. Figure 2 An example of an adjacency graph is provided, which includes multiple nodes {v1, v2, v3, v4, v5, v6}, each node v i Represents an object (or variable); each node is connected to other nodes by one or more edges, indicating that the connected objects have an adjacency relationship; for example, object v1 has an adjacency relationship with objects v2, v4, and v5, and they are adjacent objects to each other; object v3 has an adjacency relationship with objects v2, v4, and v6, and they are adjacent objects to each other; the number of adjacency visits between two adjacent objects reflects the frequency of adjacency visits between them, which can be represented by the weight w(e), representing the total number of consecutive visits to two adjacent objects in a visit sequence; for example, please refer to Figure 2 The weight w(e(v1,v2)) of the edge between adjacent objects v1 and v2 is 77, which means that v1 and v2 are visited 77 times.

[0049] It should be noted that the weight of each edge in the adjacency graph is initialized to 0. Based on the data flow analysis framework within the function, all instructions in the program are scanned. Each instruction that accesses RAM is associated with an independent allocation object. The page state of each allocation object or program point is calculated. For each adjacent access between two allocation objects, the weight of the corresponding edge, i.e., the number of adjacent accesses, is incremented by 1.

[0050] Please see Figure 2 Each node also includes object size information and bank information. The size information indicates the size of the object in the node, in bytes; the bank information indicates the page where the object is located. Figure 2 -1 in the value indicates that the object is in the initialization state and no pages have been allocated.

[0051] Based on the aforementioned adjacency relationship data, in some embodiments, the reduction in page-splitting overhead between the object to be assigned and all adjacent objects when assigning the object to be assigned to a candidate page is determined according to the object to be assigned and the adjacency relationship data. This includes: traversing each adjacent object of the object to be assigned in the adjacency relationship data; calculating the reduction in the number of page-splitting instructions between the object to be assigned and its adjacent objects when assigning the object to be assigned to a candidate page based on the page where the adjacent objects are located and the candidate page; and obtaining the reduction in page-splitting overhead based on the reduction in the number of page-splitting instructions between the object to be assigned and each adjacent object and the number of adjacent accesses.

[0052] Specifically, RAM page splitting is implemented by setting or unsetting a flag bit with multiple page splitting instructions (set / unset). Therefore, using the reduction in the number of page splitting instructions as a measure of the reduction in page splitting overhead has the advantages of being simple, fast, and accurate.

[0053] When traversing adjacent objects in the adjacent relationship data, the object to be assigned is first assigned to a candidate page. Then, based on the adjacent relationship data, the reduction in the number of page-splitting instructions between the object to be assigned and each adjacent object in one adjacent access process is calculated after the assignment compared to before the assignment. Then, the page-splitting overhead between the object to be assigned and each adjacent object is determined sequentially using the number of adjacent accesses between the object to be assigned and each adjacent object as the weight. Finally, the reduction in page-splitting overhead is obtained by summing the page-splitting overhead of all adjacent objects.

[0054] Taking object v1 as an example, if the page to be allocated is b1, then calculate the reduction ΔH in the number of page splitting instructions between object v1 and object v2 after object v1 is allocated to b1. 12 The reduction in the number of page-splitting instructions between object v1 and object v4, ΔH 14 And the reduction in the number of page-splitting instructions between object v1 and object v5, ΔH 15 Then, according to ΔH 12 The corresponding adjacent visit count w(e(v1,v2)) = 77, ΔH 14 The corresponding number of adjacent visits w(e(v1,v4)) = 126, ΔH 15 The benefit is obtained by taking the corresponding number of adjacent accesses w(e(v1,v5)) = 69 and calculating the reduction in page overhead between object v1 and all adjacent objects.

[0055] In some embodiments, the change in page-splitting time can also be used to measure page-splitting overhead. The corresponding approach is to traverse each neighboring object of the object to be assigned in the adjacency relationship data, calculate the change in page-splitting time between the object to be assigned and its neighboring objects when assigning the object to be assigned to the page ...

[0056] In the following embodiments, unless otherwise specified, the method for determining the page splitting overhead between the object to be allocated and any adjacent object will be described using the reduction in the number of page splitting instructions between adjacent objects as an example.

[0057] Option 1: In some embodiments, the Hamming distance between the page to be selected and the page containing the adjacent object is determined based on the page number of the page to be selected and the page number of the adjacent object; the reduction in the number of page cutting instructions is obtained based on the difference between log2N and the Hamming distance.

[0058] Specifically, the number of page splitting instructions (page splitting overhead) between two different pages can be represented by Hamming distance. In the initial state, i.e., the state where no pages are allocated, the number of page splitting instructions between any two adjacent objects is log2N, where N is the number of pages. After pages are allocated, the number of page splitting instructions is the Hamming distance between the banks where the adjacent objects are located, and the value of this Hamming distance is usually less than log2N.

[0059] For example, please see Figure 3 If object v1 is allocated to bank: b1 and object v2 is allocated to bank: b2, then the Hamming distance between the pages containing object v1 and object v2 is H(b1,b2). This Hamming distance is the number of page splitting instructions between object v1 and object v2. log2N-H(b1,b2) is the amount by which the number of page splitting instructions between object v1 and object v2 is reduced after the page is allocated, and it is also the amount by which the number of page splitting instructions is reduced.

[0060] The Hamming distance between different pages can be determined by the page number, which is the binary code of the page bank number. For example, for N pages, their numbers are 0, 1, 2, ..., N-1. The corresponding equal-length binary codes of these numbers are the page numbers. The Hamming distance is the number of corresponding bits that differ between two equal-length binary codes. Therefore, the Hamming distance between two pages can be quickly determined by the page number, thereby quickly determining the reduction in the number of page cutting instructions between object v1 and object v2.

[0061] For example, page number b1 is 10101 and page number b2 is 00110, with a corresponding N of 32. It can be seen that the first, fourth, and fifth bits of the two page number codes are different. Therefore, H(b1,b2) = 3 and log2N - H(b1,b2) = 2, indicating that the number of page switching instructions between object v1 and object v2 is reduced by 2 compared to the initial state.

[0062] Scheme 2, in some embodiments, determines the first Hamming distance between the first page and the second page based on the page number of the first page where the candidate object is located before allocation and the page number of the second page where the adjacent object is located; determines the second Hamming distance between the candidate page and the second page based on the page number of the candidate page and the page number of the second page; and obtains the reduction in the number of page cutting instructions based on the difference between the first Hamming distance and the second Hamming distance.

[0063] The difference between Scheme 2 and Scheme 1 is that before the page is allocated, the object to be allocated is not in an initialized state; it is already associated with an old page and needs to be allocated to a new page. For example, let the first page where object v1 is located before allocation be b11, the candidate page to be allocated be b12, and the second page where its adjacent object v2 is located be b2. Then, the number of page splitting instructions between object v1 and object v2 before allocation is the first Hamming distance H(b11,b2), and the number of page splitting instructions between object v1 and object v2 after allocation is the second Hamming distance H(b12,b2). The reduction in the number of page splitting instructions between object v1 and object v2 after page allocation is H(b11,b2) - H(b12,b2).

[0064] Both Scheme 1 and Scheme 2 can determine the reduction in the number of page-splitting instructions after the object to be allocated is assigned to the page. However, Scheme 1 does not need to consider the page the object to be allocated was on before allocation, and the reduction in the number of page-splitting instructions is always positive. Therefore, Scheme 1 is relatively simpler and more efficient.

[0065] In some embodiments, after obtaining the reduction in the number of page-splitting instructions between the object to be assigned and each adjacent object through Scheme 1 or Scheme 2, the total reduction in page-splitting overhead can be calculated through the following scheme: the page-splitting benefit is obtained by multiplying the reduction in the number of page-splitting instructions between the object to be assigned and an adjacent object by the number of adjacent accesses; the reduction in page-splitting overhead is obtained by summing the page-splitting benefits between the object to be assigned and all adjacent objects.

[0066] Taking Option 1 as an example, the reduction in page splitting overhead is the sum of the benefits of all page splitting, and its mathematical expression is as follows:

[0067] benefit(v,b)=∑f(v,u)=∑[w(e(v,u))×(log2(N)-H(b,u.bank))] (1)

[0068] In the above formula, benefit(v,b) represents the reduction in page splitting overhead by assigning the sub-object v to page b, f(v,u) is the page splitting benefit between object v and object u, u∈v.neghbors, v.neghbors represents the set of neighboring objects (nodes) of object (node) v, i.e., u is a neighboring object of v; w(e(v,u)) is the number of adjacent visits (weight) between object v and object u, and H(b,u.bank) is the Hamming distance between page b and page u.bank where object u is located.

[0069] by Figure 3 For example, if object v1 is the object to be assigned, and b1 is the page to be selected for object v1, then the page splitting benefit between v1 and v2 is:

[0070] f(v1,v2)= w(e(v1,v2)) × [log2(N)-H(b1,b2)] (2) The page splitting revenue between v1 and v2 is:

[0071] f(v1,v4)= w(e(v1,v4)) × [log2(N)-H(b1,b4)] (3) The page splitting revenue between v1 and v2 is:

[0072] f(v1,v5)= w(e(v1,v5)) × [log2(N)-H(b1,b5)] (4)

[0073] The total reduction in page splitting overhead is:

[0074] benefit(v1,b1)= f(v1,v2)+ f(v1,v4)+ f(v1,v5) (5)

[0075] In the above formula, w(e(v1,v2)) is the number of adjacent visits between objects v1 and v2, w(e(v1,v4)) is the number of adjacent visits between objects v1 and v4, w(e(v1,v5)) is the number of adjacent visits between objects v1 and v5, H(b1,b2) is the Hamming distance between page b1 and page b2, H(b1,b4) is the Hamming distance between page b1 and page b4, and H(b1,b5) is the Hamming distance between page b1 and page b5.

[0076] In some embodiments, after obtaining the page overhead reduction benefit(v,b), the benefit density can be calculated as follows: the capacity ratio is obtained by dividing the page size of the candidate page by the size of the object to be allocated; the benefit density is obtained by multiplying the page overhead reduction by the capacity ratio.

[0077] In some embodiments, the revenue density can be obtained by multiplying the reduction in page slicing overhead by the page size and then dividing by the size of the object to be allocated.

[0078] The corresponding mathematical expression is as follows:

[0079] density(v,b) = [benefit(v,b) × banksize] / v.size (6)

[0080] In the above formula, density(v,b) is the revenue density when the object to be assigned v is assigned to page b; banksize is the page size. In MCU, the page size of each page is usually fixed and can be regarded as a constant; v.size is the size of the object to be assigned v.

[0081] In some embodiments, a correction parameter k can be introduced to modify formula (6) to obtain the corrected return density for subsequent judgment.

[0082] After calculating the revenue density, in some embodiments, the method for determining the target page based on the revenue density may include: selecting the candidate page with the highest revenue density as the target page; this scheme adopts a greedy strategy to select the page with the highest current revenue density for allocation; by maximizing the revenue density, that is, maximizing the reduction of the number of page-splitting instructions inserted in the program code, the page-splitting overhead of RAM can be significantly reduced, and the program efficiency can be improved.

[0083] In some embodiments, when determining the target page, the remaining space of each candidate page can also be obtained, and the candidate page with the highest benefit density can be selected as the target page from the candidate pages whose remaining space is greater than a set value or greater than the size of the object to be allocated.

[0084] Specifically, when determining the target page for an object to be assigned, it is not only necessary to consider the revenue density, but also the impact of the remaining space on the page. If there is enough remaining space, the candidate page with the highest revenue density is selected as the target page. The setting value can be set according to actual needs.

[0085] In some embodiments, after assigning the object to be assigned to the target page, the method further includes updating the adjacency relationship data based on the page numbers of the object to be assigned and the target page. By updating the adjacency relationship data (graph) after the page assignment is completed, the complexity of the program or algorithm can be reduced compared to updating it at the same time as the assignment.

[0086] In practical implementation, based on the above embodiments, an optional pseudocode for the page allocation process for variable v is as follows:

[0087]

[0088] In summary, the above embodiments provide a method for allocating RAM memory pages, proposing a new method for determining the benefit density as the selection criterion. This method fully considers the influence of multiple factors, such as the MCU paging mechanism, the size of the object to be allocated, and the page splitting overhead (Hamming distance between pages) between different objects. By using a greedy algorithm strategy based on the benefit density to determine the target page to be allocated, the number of page splitting instructions in the program is significantly reduced, resulting in a more significant improvement in program efficiency while optimizing ROM space. Furthermore, no update of the adjacency relationship data (graph) is required during the allocation process, reducing the algorithm complexity.

[0089] Those skilled in the art will understand that all or part of the steps in the methods of the above embodiments can be implemented by a program instructing related hardware, and the program can be stored in a computer-readable storage medium. Therefore, based on the same inventive concept as the foregoing embodiments, in a second aspect, in an optional embodiment, as... Figure 4 As shown, a computer-readable storage medium 400 is provided, on which a computer program 411 is stored. When the computer program 411 is executed by a processor, it executes the steps of the control method in the aforementioned embodiments. The storage medium may be ROM / RAM, magnetic disk, optical disk, hard disk, etc.

[0090] Based on the same inventive concept, in a third aspect, in some optional embodiments, a computer program product is provided, including a computer program and / or instructions, which, when executed by a processor, implement any of the memory page allocation methods provided in the first aspect embodiments.

[0091] Although preferred embodiments of the present disclosure have been described, those skilled in the art, upon learning the basic inventive concept, can make other changes and modifications to these embodiments. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments as well as all changes and modifications falling within the scope of this disclosure.

[0092] Obviously, those skilled in the art can make various modifications and variations to this disclosure without departing from its spirit and scope. Therefore, if such modifications and variations fall within the scope of the claims of this disclosure and their equivalents, this disclosure is also intended to include such modifications and variations.

Claims

1. A method for allocating memory pages, applied to the random access memory of a microcontroller, characterized in that, The random access memory is configured with N pages, where N ≥ 2 and is an integer. The method includes: Get the object to be allocated; Each of the N pages is selected as a candidate page in turn. Based on the candidate object and the adjacent relationship data, the amount of reduction in page switching overhead between the candidate object and all adjacent objects is determined when the candidate object is assigned to the candidate page. The revenue density of allocating the object to be assigned to the page to be selected is calculated based on the reduction in page slicing overhead, the size of the object to be assigned, and the page size of the page to be selected. This includes: obtaining the capacity ratio by dividing the page size of the page to be selected by the size of the object to be assigned; and obtaining the revenue density by multiplying the reduction in page slicing overhead by the capacity ratio. Based on the revenue density, a target page is determined from N candidate pages, and the object to be assigned is assigned to the target page; The adjacent relationship data includes the adjacent access relationship between multiple objects, the page where each object is located, and the number of adjacent accesses between any two adjacent objects. The adjacent object is an object that has an adjacent access relationship with the object to be assigned. The step of determining the reduction in page-splitting overhead between the object to be assigned and all adjacent objects when assigning the object to be assigned to the candidate page, based on the object to be assigned and the adjacency relationship data, includes: Traverse each neighboring object of the object to be assigned in the adjacent relationship data, and calculate the reduction in the number of page-slicing instructions between the object to be assigned and the neighboring object when assigning the object to be assigned to the page to the page to the page to the page to the page to the page. The reduction in page slicing overhead is obtained based on the reduction in the number of page slicing instructions between the object to be allocated and each of the adjacent objects and the number of adjacent accesses, including: obtaining the page slicing benefit by multiplying the reduction in the number of page slicing instructions between the object to be allocated and one of the adjacent objects by the number of adjacent accesses; and obtaining the reduction in page slicing overhead based on the sum of the page slicing benefits between the object to be allocated and all the adjacent objects.

2. The method as described in claim 1, characterized in that, The step of calculating the reduction in the number of page-slicing instructions between the object to be assigned and the adjacent objects when assigning the object to be assigned to the page to the candidate page, based on the page where the adjacent objects are located and the candidate page, includes: The Hamming distance between the candidate page and the page containing the adjacent object is determined based on the page number of the candidate page and the page number of the adjacent object. The reduction in the number of page-slicing instructions is obtained based on the difference between log2N and the Hamming distance.

3. The method as described in claim 1, characterized in that, The step of calculating the reduction in the number of page-slicing instructions between the object to be assigned and the adjacent objects when assigning the object to be assigned to the page to the candidate page, based on the page where the adjacent objects are located and the candidate page, includes: The first Hamming distance between the first page and the second page is determined based on the page number of the first page where the candidate page was located before allocation and the page number of the second page where the adjacent object is located. Based on the page number of the candidate page and the page number of the second page, determine the second Hamming distance between the candidate page and the second page; The reduction in the number of page-slicing instructions is obtained based on the difference between the first Hamming distance and the second Hamming distance.

4. The method as described in claim 1, characterized in that, The step of determining the target page from N candidate pages based on the revenue density includes: The candidate page with the highest revenue density is selected as the target page; or... Obtain the remaining space of each candidate page, and from the candidate pages whose remaining space is greater than a set value or greater than the size of the object to be allocated, select the candidate page with the highest revenue density as the target page.

5. The method as described in claim 1, characterized in that, After assigning the object to be assigned to the target page, the method further includes: Update the adjacency relationship data based on the page number of the object to be assigned and the page number of the target page.

6. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the method of any one of claims 1 to 5.

7. A computer program product, comprising a computer program and / or instructions, characterized in that, When the computer program and / or instructions are executed by the processor, they implement the method as described in any one of claims 1 to 5.

Citation Information

Patent Citations

  • Method for dynamically allocating on-chip heterogeneous memory resources by utilizing virtual memory mechanism

    CN101739358A

  • Transparent large page distribution method and device, electronic equipment and readable medium

    CN116263703A