A multi-segment memory allocation method based on distance constraint under a real-time system

By employing a multi-segment memory allocation method in real-time systems, calculating gaps and releasing gap memory, the problem of memory waste is solved, memory utilization and device operating efficiency are improved, and it is applicable to operating systems and real-time systems.

CN121029424BActive Publication Date: 2026-02-13WEAPON EQUIP RES INST OF CHINA NAT WEAPON EQUIP GRP
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202511543796.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-10-28
Publication Date
2026-02-13
Estimated Expiration
2045-10-28

AI Technical Summary

Technical Problem

Existing technologies directly allocate large blocks of memory in real-time systems, leading to wasted intermediate memory, especially on devices with limited memory. This results in low memory utilization and frequent memory requests during dynamic library updates, impacting device operating efficiency.

Method used

A distance-constrained multi-segment memory allocation method is adopted. Contiguous memory is allocated through the operating system interface, the gap size is calculated and the gap memory is released, a gap release interface is added, the memory management interface is optimized, and dynamic library updates are adapted.

Benefits of technology

It effectively saves memory, improves memory utilization, simplifies the dynamic library update process, and enhances device operating efficiency. It is suitable for memory-constrained devices in real-time systems and operating systems.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121029424B_ABST
    Figure CN121029424B_ABST
Patent Text Reader

Abstract

The application provides a multi-section memory allocation method based on distance constraint under a real-time system and relates to the technical field of memory resource management, which comprises the following steps: using a memory opening interface of an operating system to open a continuous memory capable of covering multi-section memory demand, and performing alignment operation on the multi-section memory; calculating the gap size and gap address between the last section memory and the previous section memory after the alignment operation; and adding a gap memory release interface to release the memory of the gap. The method saves a large amount of memory and effectively improves the memory use efficiency.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of memory resource management, and particularly relates to a multi-segment memory allocation method based on distance constraints in a real-time system. BACKGROUND

[0002] In the current operating system, a large memory is directly allocated to directly meet the memory demand of a service. The whole memory is allocated to the service, and the middle memory is also allocated to the service, and it does not care whether each memory is used or not, so that most of the middle memory is wasted. When the memory of a device itself is small, such as 256 KB, a dynamic library occupies more than 64 KB of memory, which uses 25% of the system memory, and at least 56 KB of the 64 KB of memory is not used, so that at least 21% of the memory is wasted. The waste is huge. SUMMARY

[0003] Therefore, the present application provides a multi-segment memory allocation method based on distance constraints in a real-time system, comprising the following steps.

[0004] Step 1, using a memory opening interface of an operating system, opening a continuous memory that can cover multi-segment memory demand, and performing an alignment operation on the multi-segment memory.

[0005] Step 2, calculating the gap size and gap address between the rear memory and the front memory after the alignment operation.

[0006] Step 3, adding a gap memory release interface to release the memory of the gap.

[0007] Further, in step 1, the alignment operation on the multi-segment memory is specifically: the size of the multi-segment memory is the starting address of the first segment to the tail address of the last segment, and the tail address of the last segment is aligned in a predetermined memory segment size.

[0008] Further, in step 1, the predetermined memory segment size is 4 KB or 8 KB.

[0009] Further, in step 2, the gap size and gap address are calculated in the following manner:

[0010] The gap size = the front alignment address of the rear segment - the rear alignment address of the front segment.

[0011] The gap starting address is the tail address of the front segment plus 1.

[0012] The gap tail address is the starting address of the rear segment minus 1.

[0013] Further, in step 3, the added gap memory release interface call variable includes the previous segment start physical address, the gap start physical address and the gap size, and the return value is the gap tail address plus 1.

[0014] Further, step 3 specifically includes the following sub-steps:

[0015] Step 31, according to the previous segment start physical address, find the first memory control node, record the first memory control node size, modify the size of the first memory control node to the start physical address of the gap minus the start physical address of the previous segment;

[0016] Step 32, create a second memory control node, set the address of the second memory control node to the start physical address of the gap plus the gap size, and the size to the start physical address of the previous segment plus the first memory control node size minus the sum of the start physical address of the gap and the gap size, and hang the second memory control node behind the first memory control node and put it on the allocated memory control node chain table;

[0017] Step 33, create a third memory control node, set the address of the third memory control node to the start physical address of the gap, and the size to the gap size, and hang the third memory control node to the free partition control node chain table.

[0018] The application also provides the application of the method in dynamic library updating, and after the dynamic library is updated, the free memory margin before and after each segment in the allocated memory is directly used.

[0019] The application has the following beneficial technical effects:

[0020] 1. In addition to meeting the memory demand of the service, the memory is saved, and for a device with tight memory, the released memory is considerable, for example, a device with a memory size of 256KB can release 60KB of space, so that the memory utilization of the device is greatly improved.

[0021] 2. The gap calculation method is used, a memory margin is left before and after each segment of memory, and when the memory is updated, memory application is not necessary again, the service update process is simplified, and the device running efficiency is improved.

[0022] 3. The interface for releasing a block of memory on the allocated memory is added, the memory management interface of the operating system is complete and rich.

[0023] 4. The dynamic library is introduced into the real-time system, the algorithm is used, the memory constraint of the real-time system is no longer limited, and the dynamic library feature can be used on a smaller memory system.

[0024] 5. This algorithm can be used not only in real-time systems but also in operating systems. In systems with memory allocation constraints, using this algorithm can save a significant amount of memory and effectively improve memory utilization efficiency. Attached Figure Description

[0025] To more clearly illustrate the technical solutions of the specific embodiments of the present invention, the drawings used in the description of the specific embodiments will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present invention. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.

[0026] Figure 1 This is a flowchart of the distance-constrained multi-segment memory allocation method of the present invention;

[0027] Figure 2 This is a schematic diagram illustrating the memory requirements of a program segment in a dynamic library of the present invention.

[0028] Figure 3 This is a flowchart of the distance-constrained multi-segment memory gap release method of the present invention;

[0029] Figure 4 This is a schematic diagram of the memory gap release interface in an embodiment of the present invention. Detailed Implementation

[0030] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.

[0031] like Figure 1 As shown, the distance-constrained multi-segment memory allocation method of the present invention includes:

[0032] 1. Allocate a large, contiguous block of memory to fully cover the needs of multiple memory segments.

[0033] Memory opening, using the memory opening interface of the operating system, the memory allocation interface is not modified. The size of the multi-segment memory is the starting position of the first segment to the tail address of the last segment. In order to make the memory management of the operating system convenient, the address of the last segment is aligned, such as 4KB alignment or 8KB alignment. For example: the relative starting address of the first segment is 0, and the relative address of the tail of the last segment is 0x10898, 4KB alignment, the address is 0x10FFF, and the size of the multi-segment memory is 0x11000, that is, 68KB.

[0034] 2. Calculate the size of the gap

[0035] Since it is multi-segment memory, there is a gap between segments, and each segment needs to be aligned, so the size of the gap is calculated as:

[0036] The address of the front alignment of the rear segment - the address of the rear alignment of the front segment (1)

[0037] For example:

[0038] The relative address of the tail of the front segment is: 0x00774, and the 4KB aligned address is: 0x00FFF

[0039] The starting address of the rear segment is: 0x10774, and the 4KB front alignment address is: 0x10FFF

[0040] The size of the gap is: 0x10FFF-0x01FFF= 0xF000, that is, 60KB

[0041] The starting relative address of the gap is: 0x01000

[0042] As can be seen from the above example, the gap between the two segments of memory is 60KB, and the gap space is relatively large. This block is released and can be allocated to other modules, so that the memory utilization of the device is improved.

[0043] The gap calculation provides a change margin for using multi-segment memory through the rear alignment of the front end and the front alignment of the rear end.

[0044] For example, a dynamic library, the program segment memory requirement is as shown in Figure 2

[0045] The above dynamic library has two program segments, and the alignment mode uses 64KB. The first segment generally places program logic, and the second segment generally places variables. In the program instructions of the first segment, the relative address of the second segment will be referenced, so the distance between the two blocks cannot be changed.

[0046] ​When the dynamic library is updated, the allocation of memory is not required, and the allocated memory is directly used, because there is free memory margin before and after each segment, and slight size changes of a segment can be tolerated, so that the dynamic library can be quickly updated, and the operation efficiency of the business is improved.

[0047] 3. Add a release interface to release the gap memory

[0048] For the gap memory, a release interface is added:

[0049] Void *free_gap(void *pre_startaddr, void *gap_startaddr, int gap_size)

[0050] Wherein, pre_startaddr represents the starting physical address of the front segment, through which the memory control block can be quickly located; gap_startaddr represents the starting physical address of the gap, which is the address of the front segment after alignment + 1; gap_size represents the size of the gap, which is calculated according to formula (1); the return value is the tail address of the gap + 1. It should be noted that Void in Void *free_gap indicates that the function does not need to return a value, and void *pre_startaddr and void *gap_startaddrint indicate that Void can accept any type of pointer assignment, and int in int gap_size indicates an integer variable. Void and int are both programming terms known in C language.

[0051] The release process is shown in Figure 3 .

[0052] According to pre_startaddr, find the memory control node 1, record the size of the memory control node 1 as per_size, modify the size of the memory control node 1 as gap_startaddr - pre_startaddr;

[0053] Create a memory control node 2, set the address of the memory control node 2 as gap_startaddr+gap_size, and the size as pre_startaddr+per_size- (gap_startaddr+gap_size), and hang the memory control node 2 behind the memory control node 1, that is, put it on the allocated memory control node chain table;

[0054] Create a memory control node 3, set the address of the memory control node 3 as gap_startaddr, and the size as gap_size, and hang the memory control node 3 to the free partition control node chain table.

[0055] For example, as shown in Figure 4

[0056] A service requires 2 segments of memory from the operating system, the first segment is 774 bytes in size, and the second segment is 224 bytes in size, and there is a requirement of a distance of 64 KB between the first address of the second segment and the tail address of the first segment;

[0057] Using the memory allocation interface of the operating system, a space with a starting address of 0x18000 and a size of 68 KB is allocated;

[0058] Using the gap calculation method, assuming 4 KB alignment;

[0059] The first segment of memory is aligned to 4 KB, and the starting address is 0x18000. After 4 KB alignment, the tail address is 0x18FFF.

[0060] The starting address of the second segment of memory is 0x18774 (the tail address of the first segment + 1) + 0x10000 = 0x28774, and the first 4 KB is aligned, and the address is 0x28000.

[0061] The starting address of the gap is 0x19000, and the size is 0x28000-0x19000=0xF000, that is, 60 KB.

[0062] Using the allocation gap release algorithm, void *free_gap(0x18000, 0x19000, 0xF000), returns 0x28000, and the changes of the memory control node and the memory partition are as shown in Figure 4

[0063] It should be noted that the starting address of the gap and the calculation method can have different calculation formulas in combination with the memory algorithm. Some systems, in a single memory partition for memory management, can calculate according to the manner provided by the present application, but are not limited to this management manner. Some memory management algorithms, memory management and memory partitioning are on one memory, and the memory partition has a head-tail magic number and other control fields. Still, the steps of the present algorithm can be used to calculate the gap memory. The principle and steps of calculating the starting address and size of the gap memory can be changed and applied to different memory management algorithms. The steps of releasing a block of memory on the already opened memory are applicable to different memory management algorithms and do not constrain the memory management algorithm. The dynamic library memory requirement used in the example of the present application can still use a block of memory by modifying the comparison method of the compiler to shorten the distance between the two ends of the memory, but the present application is not limited to solving the memory requirement of the dynamic library.

[0064] ​​According to the above introduction, the multi-segment memory allocation algorithm of the application first applies a whole large memory, and then releases the idle memory in the middle, so as to meet the memory requirement of the application and save a large amount of memory; the gap starting address and size calculation method uses the method of large block alignment before (except the first segment) and after the segment, to calculate the gap address and size, so as to put down the business data and provide a margin for the memory change of the business data update; the gap release algorithm adds a gap release function to the operating system, and uses the method of modifying a memory control node, adding a memory control node and adding a memory idle node to complete the segmentation of the memory; the memory allocation solution for the dynamic library of the algorithm can effectively optimize the memory occupancy rate of the dynamic library, save a large amount of memory, and solve the memory limit of the multiple dynamic libraries used by the operating system.

[0065] Please note that the technical features of the above embodiments can be combined in any way, and in order to make the description simple, not all possible combinations of the technical features in the above embodiments are described, however, as long as the combination of the technical features does not exist contradictory, it should be considered as the scope of the description. The above embodiments only express several implementation manners of the application, and the description is more specific and detailed, but it should not be understood as the limitation of the patent scope of the application. It should be pointed out that for those skilled in the art, without departing from the concept of the application, a number of modifications and improvements can be made, which all belong to the protection scope of the application. Therefore, the patent protection scope of the application should be subject to the appended claims.

Claims

1. A method for multi-segment memory allocation based on distance constraint in real-time systems, characterized in that, The method comprises: Step 1, using a memory opening interface of an operating system, opening a continuous memory which can cover the requirements of multiple memory segments, and performing alignment operation on each memory segment in the multiple memory segments; Step 2, calculating the gap size and gap address between the rear memory segment and the front memory segment after the alignment operation; Step 3, adding a gap memory release interface to release the memory of the gap; In step 2, the gap size and gap address are calculated in the following manner: Gap size = address of the rear memory segment after alignment - address of the front memory segment after alignment; Gap start address = tail address of the front memory segment + 1; Gap tail address = start address of the rear memory segment - 1; In step 3, the variables called by the added gap memory release interface include the start physical address of the front memory segment, the start physical address of the gap and the gap size, and the return value is the tail address of the gap + 1; Step 3 specifically comprises the following sub-steps: Step 31, finding a first memory control node according to the start physical address of the front memory segment, recording the size of the first memory control node, and modifying the size of the first memory control node to the start physical address of the gap - the start physical address of the front memory segment; Step 32, creating a second memory control node, setting the address of the second memory control node to the start physical address of the gap + the gap size, and setting the size of the second memory control node to the start physical address of the front memory segment + the size of the first memory control node - the sum of the start physical address of the gap and the gap size, and hanging the second memory control node behind the first memory control node and on the allocated memory control node chain table; Step 33, creating a third memory control node, setting the address of the third memory control node to the start physical address of the gap, and setting the size of the third memory control node to the gap size, and hanging the third memory control node on the free partition control node chain table.

2. The method as described in claim 1, characterized in that, In step 1, the alignment operation on the multiple memory segments is specifically: the size of the multiple memory segments is from the start address of the first segment to the tail address of the last segment, and the address of the last segment is aligned according to a predetermined memory segment size.

3. The method of claim 2, wherein, In step 1, the predetermined memory segment size is 4KB or 8KB.

4. Use of the method according to any one of claims 1 to 3 in dynamic library updating, characterized in that, After the dynamic library is updated, the free memory reserves before and after each segment in the allocated memory are directly used.

Citation Information

Patent Citations

  • Multi-partition dynamic memory allocation method for embedded partition operating system

    CN118193194A

  • Memory release method and device, computer equipment and storage medium

    CN119537026A