Memory allocation method and device in initialization stage of operating system and storage medium
By dividing the memory pool during the initialization phase of the operating system and allocating memory from the bottom and top of the pool, the problem that the existing technology cannot complete the memory allocation before initialization is solved, and simple and fast memory allocation and no additional fragmentation are achieved.
Patent Information
- Application Number
- CN202311753539.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-12-18
- Publication Date
- 2024-12-06
AI Technical Summary
The prior art cannot complete memory allocation before operating system initialization, resulting in memory fragmentation and algorithm complexity problems.
The memory address space is divided as a memory pool during the operating system initialization phase, allocating permanent memory from the bottom of the memory pool, allocating temporary memory from the top to the bottom, and freeing memory space except permanent memory at the end of the initialization.
It realizes memory allocation in the operating system initialization stage, simplifies the allocation process, and avoids the generation of additional memory fragmentation, which is convenient for practical application and promotion.
Smart Images

Figure CN119088722A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of memory allocation, and in particular relates to a method, device and storage medium for memory allocation in an initialization phase of an operating system. Background Art
[0002] In the field of memory allocation, there are three indicators to evaluate the quality of an allocation algorithm: allocation efficiency, memory fragmentation degree, and algorithm complexity. For memory fragmentation degree, the less memory fragmentation, the better. For algorithm complexity, the simpler the algorithm, the better. At present, there are relatively mature algorithms such as buddy algorithm and slab algorithm, which can better meet the above three indicators. However, these algorithms can only be used after the operating system is initialized, and cannot complete memory allocation before the operating system is initialized. Therefore, it is necessary to provide a solution that can complete memory allocation during the operating system initialization stage. Summary of the invention
[0003] The purpose of the present invention is to provide a method, device and storage medium for allocating memory in the initialization phase of an operating system, so as to solve the above-mentioned problems existing in the prior art.
[0004] In order to achieve the above object, the present invention adopts the following technical solutions:
[0005] In a first aspect, the present invention provides a method for allocating memory in an operating system initialization phase, comprising:
[0006] Allocate a section of memory address space as the memory pool in the initialization phase;
[0007] When permanent memory needs to be allocated, the permanent memory is allocated upward from the bottom of the memory pool;
[0008] When temporary memory needs to be allocated, the temporary memory is allocated downward from the top of the memory pool;
[0009] When the operating system initialization phase ends, the memory space in the memory pool except the permanent memory is released.
[0010] Based on the above disclosed content, the present invention divides a section of memory address space as a memory pool in the initialization phase; when permanent memory needs to be allocated, permanent memory is allocated from the bottom of the memory pool upward; when temporary memory needs to be allocated, temporary memory is allocated from the top of the memory pool downward; when the initialization phase of the operating system ends, the memory space in the memory pool except for the permanent memory is released. In this way, memory allocation can be completed in the initialization phase of the operating system, and the memory allocation process is simple and fast. At the same time, because permanent memory is allocated from the bottom of the memory pool upward, after the memory space in the memory pool except for the permanent memory is released, no additional memory fragments will be generated.
[0011] Through the above design, the present invention can complete memory allocation in the initialization stage of the operating system, and the memory allocation process is simple and fast. At the same time, since permanent memory is allocated from the bottom of the memory pool upward, after the memory space other than the permanent memory in the memory pool is released, no additional memory fragmentation will be generated, which is convenient for practical application and promotion.
[0012] In a possible design, releasing the memory space in the memory pool except the permanent memory includes:
[0013] Memory space is reclaimed starting from the top of the allocated permanent memory in the memory pool upwards.
[0014] In one possible design, the method further includes:
[0015] After allocating permanent memory from the bottom of the memory pool upward, updating the memory space address pointed to by the low pointer in the memory pool;
[0016] After allocating temporary memory downward from the top of the memory pool, the memory space address pointed to by the high pointer in the memory pool is updated.
[0017] In a possible design, dividing a section of memory address space as a memory pool in the initialization phase includes:
[0018] Find a continuous memory address space of a specified size starting from the bottom of the available memory address space;
[0019] The found memory address space is used as the memory pool in the initialization phase.
[0020] In a possible design, the size of the divided memory address space is determined by the permanent memory and temporary memory that need to be allocated.
[0021] In a second aspect, the present invention provides a memory allocation device in an operating system initialization phase, comprising:
[0022] A partitioning unit is used to partition a section of memory address space as a memory pool in the initialization phase;
[0023] an allocation unit, configured to allocate persistent memory from the bottom of the memory pool upwards when persistent memory needs to be allocated;
[0024] The allocation unit is further used to allocate temporary memory from the top of the memory pool downwards when temporary memory needs to be allocated;
[0025] The release unit is used to release the memory space in the memory pool except the permanent memory when the operating system initialization phase ends.
[0026] In a third aspect, the present invention provides another memory allocation device for the initialization phase of an operating system, comprising a memory, a processor and a transceiver that are communicatively connected in sequence, wherein the memory is used to store computer programs, the transceiver is used to send and receive messages, and the processor is used to read the computer program and execute the memory allocation method for the initialization phase of an operating system as described in the first aspect or any possible design of the first aspect.
[0027] In a fourth aspect, the present invention provides a computer-readable storage medium having instructions stored thereon, which, when executed on a computer, executes the memory allocation method for the operating system initialization phase as described in the first aspect or any possible design of the first aspect.
[0028] In a fifth aspect, the present invention provides a computer program product comprising instructions, which, when executed on a computer, causes the computer to execute the memory allocation method for the operating system initialization phase as described in the first aspect or any possible design of the first aspect.
[0029] Beneficial effects:
[0030] The memory allocation method, device and storage medium in the operating system initialization phase provided by the present invention can complete memory allocation in the operating system initialization phase, and the memory allocation process is simple and fast. At the same time, since permanent memory is allocated from the bottom of the memory pool upward, after the memory space other than the permanent memory in the memory pool is released, no additional memory fragmentation will be generated, which is convenient for practical application and promotion. BRIEF DESCRIPTION OF THE DRAWINGS
[0031] Figure 1 A flowchart of a method for allocating memory during the initialization phase of an operating system provided in an embodiment of the present application;
[0032] Figure 2 A schematic block diagram of a memory allocation device in the initialization phase of an operating system provided in an embodiment of the present application;
[0033] Figure 3 A block diagram of another memory allocation device during the initialization phase of an operating system provided in an embodiment of the present application. DETAILED DESCRIPTION
[0034] In order to more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the present invention will be briefly introduced below in combination with the drawings and the description of the embodiments or the prior art. Obviously, the following description of the structure of the drawings is only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work. It should be noted that the description of these embodiments is used to help understand the present invention, but does not constitute a limitation of the present invention.
[0035] It should be understood that although the terms first, second, etc. may be used herein to describe various units, these units should not be limited by these terms. These terms are only used to distinguish one unit from another unit. For example, a first unit can be referred to as a second unit, and similarly, a second unit can be referred to as a first unit without departing from the scope of the exemplary embodiments of the present invention.
[0036] It should be understood that the term "and / or" that may appear in this article is merely a description of the association relationship between associated objects, indicating that three relationships may exist. For example, A and / or B can represent three situations: A exists alone, B exists alone, and A and B exist at the same time. The term " / and" that may appear in this article describes another type of association object relationship, indicating that two relationships may exist. For example, A / and B can represent two situations: A exists alone, and A and B exist alone. In addition, the character " / " that may appear in this article generally indicates that the previous and next associated objects are in an "or" relationship.
[0037] In order to complete memory allocation during the operating system initialization phase, an embodiment of the present application provides a method, device and storage medium for memory allocation during the operating system initialization phase. The method, device and storage medium for memory allocation during the operating system initialization phase can complete memory allocation during the operating system initialization phase, and the memory allocation process is simple and fast, and no additional memory fragmentation is generated.
[0038] The memory allocation method in the operating system initialization phase provided in the embodiment of the present application can be applied to a user terminal device that needs to initialize the operating system. It can be understood that the execution subject does not constitute a limitation on the embodiment of the present application.
[0039] The memory allocation method during the initialization phase of the operating system provided in an embodiment of the present application will be described in detail below.
[0040] like Figure 1 As shown, it is a flowchart of the memory allocation method in the operating system initialization phase provided by the first aspect of the embodiment of the present application. The memory allocation method in the operating system initialization phase may include but is not limited to the following steps S101-S104.
[0041] Step S101: divide a section of memory address space as a memory pool in the initialization phase.
[0042] In the embodiment of the present application, when the operating system enters the initialization phase, a section of memory address space may be first divided out as a memory pool for the initialization phase.
[0043] Specifically, when dividing the memory pool, you can first find a continuous and specified size of memory address space from the bottom of the available memory address space, and then use the found memory address space as the memory pool in the initialization phase. Among them, starting from the bottom of the available memory address space can refer to searching in the available memory address space along the direction from small to large along the space address. The size of the divided memory space address can be determined according to the permanent memory and temporary memory to be allocated as needed, or it can be pre-set according to actual conditions, and is not specifically limited in the embodiments of the present application.
[0044] In one or more embodiments, if a continuous memory address space of a specified size cannot be found in the available memory address space, that is, a section of the memory address space cannot be divided as a memory pool in the initialization phase, the system may enter BUG mode and stop running.
[0045] Step S102: When permanent memory needs to be allocated, permanent memory is allocated from the bottom of the memory pool upwards.
[0046] If permanent memory needs to be allocated, it can be allocated from the bottom of the memory pool upward. Allocating permanent memory from the bottom of the memory pool upward means allocating permanent memory in the memory pool in ascending order along the spatial address. Persistent memory can be memory used to save the state of algorithms required by some drivers and system modules (such as file modules, memory modules, and operating system modules).
[0047] In the embodiment of the present application, after allocating permanent memory from the bottom of the memory pool upward, the memory space address pointed to by the low pointer in the memory pool can be updated. That is, after updating the memory space address pointed to by the low pointer, the low pointer points to the minimum space address of the currently unallocated memory.
[0048] Step S103: When temporary memory needs to be allocated, the temporary memory is allocated downward from the top of the memory pool.
[0049] If temporary memory needs to be allocated, it can be allocated from the top of the memory pool downwards. Allocating temporary memory from the top of the memory pool downwards means allocating temporary memory in the memory pool in descending order along the spatial address. Temporary memory can be memory used to store data structures temporarily used in the initialization phase. After the system initialization phase ends, this part of the data structure loses its value and waits to be recycled.
[0050] Accordingly, after allocating temporary memory from the top of the memory pool downward, the memory space address pointed to by the high pointer in the memory pool can be updated. That is, after updating the memory space address pointed to by the high pointer, the high pointer points to the maximum space address of the currently unallocated memory.
[0051] It can be understood that the order of step S102 and step S103 is not limited.
[0052] Step S104: When the operating system initialization phase ends, the memory space in the memory pool except for the permanent memory is released.
[0053] Specifically, when releasing memory space other than permanent memory in the memory pool, the memory space may be reclaimed upward starting from the top of the permanent memory allocated in the memory pool.
[0054] The present invention provides a memory allocation method in the initialization phase of an operating system, which divides a section of memory address space as a memory pool in the initialization phase; when permanent memory needs to be allocated, the permanent memory is allocated from the bottom of the memory pool upwards; when temporary memory needs to be allocated, the temporary memory is allocated from the top of the memory pool downwards; when the initialization phase of the operating system ends, the memory space in the memory pool except the permanent memory is released. In this way, memory allocation can be completed in the initialization phase of the operating system, and the memory allocation process is simple and fast. At the same time, because the permanent memory is allocated from the bottom of the memory pool upwards, after the memory space in the memory pool except the permanent memory is released, no additional memory fragments will be generated.
[0055] See also Figure 2 According to a second aspect of an embodiment of the present application, a memory allocation device for an operating system initialization phase is provided. The memory allocation device for an operating system initialization phase includes:
[0056] A partitioning unit is used to partition a section of memory address space as a memory pool in the initialization phase;
[0057] an allocation unit, configured to allocate persistent memory from the bottom of the memory pool upwards when persistent memory needs to be allocated;
[0058] The allocation unit is further used to allocate temporary memory from the top of the memory pool downwards when temporary memory needs to be allocated;
[0059] The release unit is used to release the memory space in the memory pool except the permanent memory when the operating system initialization phase ends.
[0060] The working process, working details and technical effects of the memory allocation device in the operating system initialization phase provided by the second aspect of this embodiment can be found in the first aspect of the embodiment and will not be described in detail here.
[0061] like Figure 3As shown, the third aspect of an embodiment of the present application provides another memory allocation device for the initialization phase of an operating system, comprising a memory, a processor and a transceiver that are communicatively connected in sequence, wherein the memory is used to store computer programs, the transceiver is used to send and receive messages, and the processor is used to read the computer program and execute the memory allocation method for the initialization phase of the operating system as described in the first aspect of the embodiment.
[0062] For specific examples, the memory may include but is not limited to random access memory (RAM), read-only memory (ROM), flash memory, first-in-first-out memory (FIFO) and / or first-in-last-out memory (FILO), etc.; the processor may be but is not limited to a microprocessor of the STM32F105 series, an ARM (Advanced RISC-Machines), an X86 or other architecture processor, or a processor with an integrated NPU (neural-network processing units); the transceiver may be but is not limited to a WiFi (Wireless Fidelity) wireless transceiver, a Bluetooth wireless transceiver, a General Packet Radio Service (GPRS) wireless transceiver, a ZigBee protocol (a low-power local area network protocol based on the IEEE802.15.4 standard, ZigBee) wireless transceiver, a 3G transceiver, a 4G transceiver and / or a 5G transceiver, etc.
[0063] The fourth aspect of this embodiment provides a computer-readable storage medium storing instructions including the memory allocation method for the operating system initialization phase described in the first aspect of the embodiment, that is, the computer-readable storage medium stores instructions, and when the instructions are run on a computer, the memory allocation method for the operating system initialization phase described in the first aspect is executed. The computer-readable storage medium refers to a carrier for storing data, which may include but is not limited to a floppy disk, an optical disk, a hard disk, a flash memory, a USB flash drive, and / or a memory stick, etc., and the computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable devices.
[0064] A fifth aspect of this embodiment provides a computer program product comprising instructions, which, when executed on a computer, causes the computer to execute the memory allocation method for the operating system initialization phase as described in the first aspect of the embodiment, wherein the computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable devices.
[0065] It should be understood that certain details are provided in the following description to facilitate a complete understanding of the example embodiments. However, it should be understood by those of ordinary skill in the art that the example embodiments can be implemented without these certain details. For example, the system can be shown in a block diagram to avoid obscuring the example with unnecessary details. In other examples, well-known processes, structures, and techniques may not be shown in unnecessary detail to avoid obscuring the example embodiments.
[0066] Finally, it should be noted that the above description is only a preferred embodiment of the present invention and is not intended to limit the protection scope of the present invention. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.
Claims
1. A memory allocation method in the initialization phase of an operating system, characterized in that: include: Allocate a section of memory address space as the memory pool in the initialization phase; When permanent memory needs to be allocated, the permanent memory is allocated upward from the bottom of the memory pool; When temporary memory needs to be allocated, the temporary memory is allocated downward from the top of the memory pool; When the operating system initialization phase ends, the memory space in the memory pool except the permanent memory is released.
2. The memory allocation method in the initialization phase of an operating system according to claim 1, characterized in that: The releasing of the memory space in the memory pool except the permanent memory includes: Memory space is reclaimed starting from the top of the allocated permanent memory in the memory pool upwards.
3. The memory allocation method in the initialization phase of an operating system according to claim 1, characterized in that: The method further comprises: After allocating permanent memory from the bottom of the memory pool upward, updating the memory space address pointed to by the low pointer in the memory pool; After allocating temporary memory downward from the top of the memory pool, the memory space address pointed to by the high pointer in the memory pool is updated.
4. The memory allocation method in the operating system initialization phase according to claim 1, characterized in that: The step of dividing a section of memory address space as a memory pool in the initialization phase includes: Find a continuous memory address space of a specified size starting from the bottom of the available memory address space; The found memory address space is used as the memory pool in the initialization phase.
5. The memory allocation method in the initialization phase of an operating system according to claim 1, characterized in that: The size of the divided memory address space is determined by the permanent memory and temporary memory that need to be allocated.
6. A memory allocation device for an operating system initialization phase, characterized in that: include: A partitioning unit is used to partition a section of memory address space as a memory pool in the initialization phase; an allocation unit, configured to allocate persistent memory from the bottom of the memory pool upwards when persistent memory needs to be allocated; The allocation unit is further used to allocate temporary memory from the top of the memory pool downwards when temporary memory needs to be allocated; The release unit is used to release the memory space in the memory pool except the permanent memory when the operating system initialization phase ends.
7. A memory allocation device for an operating system initialization phase, characterized in that: It comprises a memory, a processor and a transceiver which are sequentially communicatively connected, wherein the memory is used to store a computer program, the transceiver is used to send and receive messages, and the processor is used to read the computer program and execute the memory allocation method in the initialization phase of an operating system as described in any one of claims 1 to 5.
8. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores instructions, and when the instructions are executed on a computer, the memory allocation method in the initialization phase of an operating system as claimed in any one of claims 1 to 5 is executed.
