Memory allocation method, device and storage medium
By allocating a preset memory size and configuring a bitmap, the problem of low object allocation performance is solved, efficient memory management and pointer operations are achieved, and program performance is improved.
Patent Information
- Application Number
- CN202311085520.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-08-25
- Publication Date
- 2025-09-23
- Estimated Expiration
- 2043-08-25
AI Technical Summary
The object allocation performance in the existing technology is not high, resulting in low memory management efficiency and inability to support pointer operations.
By allocating a preset size of memory space to the thread to be allocated memory, configuring a corresponding bitmap, and using the mapping relationship to identify the memory start address of the object in the bitmap, continuous allocation of objects is achieved without the need for additional metadata storage.
It improves object allocation performance, supports continuous access to objects, saves memory overhead, improves program memory access performance, and supports pointer operations.
Smart Images

Figure CN117112215B_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present disclosure relate to the field of computer and network communication technology, and in particular to a memory allocation method, device, and storage medium. Background Art
[0002] Memory management is a crucial component of a program's runtime system, primarily responsible for memory allocation and deallocation. In a program, the vast majority of memory allocation requests come from object allocation, specifically allocating memory space to objects. Because object allocation is a very high-frequency operation, its performance significantly impacts program performance.
[0003] The form of memory management determines the form and efficiency of object allocation. However, the performance of object allocation in the prior art is not high. Summary of the Invention
[0004] Embodiments of the present disclosure provide a memory allocation method, device, and storage medium to improve object allocation performance.
[0005] In a first aspect, an embodiment of the present disclosure provides a memory allocation method, comprising:
[0006] Allocating a first memory space of a preset size to a first thread to be allocated memory, and configuring a first bitmap having a mapping relationship with the first memory space;
[0007] Memory is continuously allocated in the first memory space according to the size of each object to be allocated memory in the first thread, and a memory start address of each object is marked in the first bitmap based on the mapping relationship.
[0008] In a second aspect, an embodiment of the present disclosure provides a memory allocation device, including:
[0009] an allocating unit, configured to allocate a first memory space of a preset size to a first thread to which memory is to be allocated, and to configure a first bitmap having a mapping relationship with the first memory space; and to continuously allocate memory in the first memory space according to the size of each object to be allocated memory in the first thread;
[0010] A processing unit is configured to mark a memory start address of each of the objects in the first bitmap based on the mapping relationship.
[0011] In a third aspect, an embodiment of the present disclosure provides an electronic device, including: at least one processor and a memory;
[0012] The memory stores computer-executable instructions;
[0013] The at least one processor executes the computer-executable instructions stored in the memory, so that the at least one processor executes the memory allocation method described in the first aspect and various possible designs of the first aspect.
[0014] In a fourth aspect, an embodiment of the present disclosure provides a computer-readable storage medium, in which computer-executable instructions are stored. When a processor executes the computer-executable instructions, the memory allocation method described in the first aspect and various possible designs of the first aspect is implemented.
[0015] In a fifth aspect, an embodiment of the present disclosure provides a computer program product, comprising computer-executable instructions. When a processor executes the computer-executable instructions, the memory allocation method as described in the first aspect and various possible designs of the first aspect is implemented.
[0016] The memory allocation method, device and storage medium provided by the embodiments of the present disclosure allocate a first memory space of a preset size to a first thread to be allocated memory, and configure a first bitmap having a mapping relationship with the first memory space; continuously allocate memory in the first memory space according to the size of each object to be allocated memory in the first thread, and identify the memory start address of each object in the first bitmap based on the mapping relationship. By continuously allocating memory to each object to be allocated memory in the first memory space, the object allocation performance is improved, which is conducive to continuous access to the object, and there is no need to allocate additional memory at the starting position of the object to store the metadata of the object, which saves memory overhead and improves the performance of program memory access. By using a more compact bitmap to store the memory start address information of the object, the metadata of the object can be quickly determined, and the pointer can point to any location in the object memory, thereby supporting pointer operations and ensuring the efficiency and simplicity of the code. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] In order to more clearly illustrate the embodiments of the present disclosure or the technical solutions in the prior art, a brief introduction will be given below to the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present disclosure. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.
[0018] Figure 1a This is an example diagram of a memory allocation method in the prior art;
[0019] Figure 1b This is an example diagram of another memory allocation method in the prior art;
[0020] Figure 2 A schematic diagram of a memory allocation method provided in one embodiment of the present disclosure;
[0021] Figure 3 A flowchart of a memory allocation method provided in one embodiment of the present disclosure;
[0022] Figure 4 A schematic diagram of a memory allocation method provided by another embodiment of the present disclosure;
[0023] Figure 5 A schematic diagram of a memory allocation method provided by another embodiment of the present disclosure;
[0024] Figure 6a -d is a schematic diagram of determining metadata of a target object provided by an embodiment of the present disclosure;
[0025] Figure 7 A structural block diagram of a memory allocation device provided in one embodiment of the present disclosure;
[0026] Figure 8 A schematic diagram of the hardware structure of an electronic device provided in one embodiment of the present disclosure. DETAILED DESCRIPTION
[0027] To make the objectives, technical solutions, and advantages of the embodiments of the present disclosure more clear, the technical solutions in the embodiments of the present disclosure will be clearly and completely described below in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present disclosure, not all of the embodiments. All other embodiments obtained by ordinary technicians in this field based on the embodiments of the present disclosure without making any creative efforts shall fall within the scope of protection of the present disclosure.
[0028] The form of memory management determines the form and efficiency of object allocation.
[0029] For example, a block memory allocator allocates a large block of memory to the operating system, which is then divided into different groups. Different groups are used to allocate objects of different sizes, and the memory within a group is divided into blocks of equal size. At the same time, the memory management system records metadata for each memory group, such as the start and end addresses of the memory group and the size of the allocated objects.
[0030] like Figure 1a As shown in the figure, the memory obtained from the operating system is divided into three groups, used to allocate 8B, 16B, and 24B objects, respectively. Each group's memory is divided into equal-sized blocks, with gray indicating allocated blocks and white indicating free blocks. When a program needs to allocate an object, it first obtains the object's size. Based on the size, it finds the corresponding memory group and obtains a free block from it. The starting address of the free block is returned, completing the object allocation.
[0031] The advantage of this allocation method is that by combining the pointer to the object and the metadata of the memory group, the object metadata, such as the object's start and end addresses and size, can be obtained, facilitating memory management. Furthermore, since the object metadata is determined at allocation time, there's no need to determine the object type through the pointer type. This form of memory management also provides language flexibility, allowing the language to support pointer arithmetic, such as converting a pointer to an object into a pointer to a field within it, leading to more efficient programs. However, this allocation method also has significant disadvantages. Because object allocation involves numerous operations, it requires finding the corresponding memory group based on the object's size and then finding a free block within that memory group, resulting in inefficient object allocation.
[0032] In addition, another memory allocator focuses on more efficient object allocation. The allocator allocates a large object allocation buffer to each thread and allocates objects continuously in it. Figure 1b As shown, base and end represent the starting and ending addresses of the object allocation buffer respectively; top represents the currently used position, the left side of top is the allocated memory, the right side of top is the memory to be allocated, the dotted line represents the boundary of the object, and ptr is the pointer to the object. Allocating objects in such an object allocation buffer is very efficient. According to the object size s, move the top pointer to the position of top+s, and return the address of top before the move, and the allocation is completed. Therefore, this memory allocator will continuously allocate objects of different sizes in an object allocation buffer. In this scenario, additional memory will be allocated at the starting position of the object to save the metadata of the object. Figure 1b In the example, the gray area at the object's starting location represents the object's metadata. Furthermore, pointers to objects can only point to specific locations within the object, accurately retrieving the object's metadata. The disadvantages of this approach are that it requires additional space to store object metadata, requires pointers to point to specific locations within the object (the object's starting memory address), and does not support pointer arithmetic.
[0033] In order to solve the above technical problems, the present disclosure provides a memory allocation method, which allocates a first memory space of a preset size to a first thread to be allocated memory, and configures a first bitmap having a mapping relationship with the first memory space, such as Figure 2As shown; memory is continuously allocated in the first memory space according to the size of each object to be allocated memory in the first thread, and the memory start address of each object is identified in the first bitmap based on the mapping relationship. By continuously allocating memory for each object to be allocated memory in the first memory space, the object allocation performance is improved, which is conducive to continuous access to the object, and there is no need to allocate additional memory at the starting position of the object to save the metadata of the object, which saves memory overhead and improves the performance of program memory access. By using a more compact bitmap to store the memory start address information of the object, the metadata of the object can be quickly determined. Since the object memory is continuous and there is no object metadata in the memory, the pointer can point to any position in the object memory, thereby supporting pointer operations and ensuring the efficiency and simplicity of the code.
[0034] The memory allocation method disclosed herein will be described in detail below with reference to specific embodiments.
[0035] refer to Figure 3 , Figure 3 This is a flow chart of a memory allocation method provided in one embodiment of the present disclosure. The method of this embodiment can be applied in a terminal device or a server, and the memory allocation method includes:
[0036] S201: Allocate a first memory space of a preset size to a first thread to be allocated memory, and configure a first bitmap having a mapping relationship with the first memory space.
[0037] In this embodiment, for the first thread to which program objects are continuously allocated memory, when memory for each object in the first thread needs to be allocated, a first memory space (or buffer) of a preset size can be allocated to the first thread. The first memory space can be a larger memory space, for example, its preset size can be 1KB, and the objects of the first thread will be continuously allocated memory in the first memory space.
[0038] In order to facilitate the determination of the metadata of each object, that is, the object memory-related information, including the memory start address, memory end address, and memory size, etc., and without the need to store the metadata in the memory together with the object, and to support pointer operations, this embodiment further configures a bitmap (Bitmap) that has a mapping relationship with the first memory space, denoted as the first bitmap, wherein the smallest unit of the bitmap is a bit, which can have two values of 1 or 0, wherein the initial value of the first bitmap is 0, wherein the mapping relationship is to map the first memory space to the first bitmap, especially to map the space of a specific size of the first memory space to the first bitmap, so as to reflect the storage status of the first memory space with the first bitmap. The mapping relationship can be any mapping relationship, as long as the space of a specific size of the first memory space can be mapped to the first bitmap, for example, it can correspond sequentially, or in a certain specified order.
[0039] Optionally, the mapping relationship is: each bit in the first bitmap corresponds to a preset number n bytes of memory space in turn. For example, every 8 bytes of the first memory space corresponds to 1 bit in the first bitmap. If the first memory space is 1KB, the size of the first bitmap is 1KB / 8=128bit=16B, that is, the first bitmap has a total of 128 bits and occupies 16 bytes of space.
[0040] In a first optional embodiment, the corresponding relationship between the first memory space and the first bitmap can be as follows: Figure 2 As shown, the first n bytes of the first memory space are aligned with the last bit of the first bitmap, the second n bytes of the first memory space are aligned with the second to last bit of the first bitmap, and so on, the last n bytes of the first memory space are aligned with the first bit of the first bitmap. Figure 2 In the figure, the head (starting position base) of the first memory space is on the left and the tail (ending position end) is on the right, while the head of the first bitmap is on the right and the tail is on the left.
[0041] In a second optional embodiment, the corresponding relationship between the first memory space and the first bitmap can be as follows: Figure 4 As shown, the first n bytes of the first memory space are aligned with the first bit of the first bitmap, the second n bytes of the first memory space are aligned with the second bit of the first bitmap, and so on, the last n bytes of the first memory space are aligned with the last bit of the first bitmap. Figure 4 In the figure, the head (starting position base) of the first memory space is on the left and the tail (ending position end) is on the right, while the head of the first bitmap is on the left and the tail is on the right.
[0042] The following embodiments are described using the first optional embodiment as an example. The second optional embodiment is similar to the first optional embodiment and can be adaptively modified. Detailed description is omitted here. Of course, the mapping relationship in this embodiment is not limited to the above example, and other mapping relationships are also possible.
[0043] S202: Allocate memory continuously in the first memory space according to the size of each object to be allocated memory in the first thread, and mark the memory start address of each object in the first bitmap based on the mapping relationship.
[0044] In this embodiment, memory is continuously allocated in the first memory space according to the size of each object to be allocated memory in the first thread, thereby achieving continuous memory allocation, and the size of memory allocated to each object is the same as the size of the object or larger than the size of the object.
[0045] In this embodiment, it is not necessary to store the metadata of the object (including the memory start address, memory end address, memory size, etc.) in the memory of the object. Instead, the mapping relationship between the first memory space and the first bitmap is used to identify the memory start address of each object in the first bitmap.
[0046] In particular, based on the mapping relationship, the first memory space is aligned with the bits in the first bit map, and then the bit corresponding to the memory starting address of each object in the first bit map can be determined based on the mapping relationship. The bit can be set to 1, and then the mapping relationship can be used to reversely infer the memory starting address of each object based on the bit with a value of 1. Since the memory of the object is continuously allocated, the memory ending address of each object can also be reversed, and the object size can also be determined.
[0047] The memory allocation method provided in this embodiment allocates a first memory space of a preset size to the first thread to be allocated memory, and configures a first bitmap that has a mapping relationship with the first memory space; continuously allocates memory in the first memory space according to the size of each object to be allocated memory in the first thread, and identifies the memory start address of each object in the first bitmap based on the mapping relationship. By continuously allocating memory to each object to be allocated memory in the first memory space, the object allocation performance is improved, which is conducive to continuous access to the object, and there is no need to allocate additional memory at the starting position of the object to store the metadata of the object, which saves memory overhead and improves the performance of program memory access. By using a more compact bitmap to store the memory start address information of the object, the metadata of the object can be quickly determined, and the pointer can point to any location in the object memory, thereby supporting pointer operations and ensuring the efficiency and simplicity of the code.
[0048] Based on the above embodiment, if the mapping relationship between the first memory space and the first bitmap is that each bit in the first bitmap corresponds to a preset number of bytes in the first memory space in sequence, then S202 may specifically include:
[0049] Clearing all bits of the first bitmap to zero;
[0050] Continuously allocating memory in the first memory space according to the size of each object to be allocated memory in the first thread, and the memory size of each object is a multiple of the preset number;
[0051] The bit corresponding to the memory start address of each object in the first bitmap is set to 1.
[0052] In this embodiment, before allocating memory for the first thread's object in the first memory space for the first time, or when configuring the first bitmap that has a mapping relationship with the first memory space, all bits of the first bitmap can be cleared to avoid affecting the subsequent identification process.
[0053] When allocating memory to any object, the memory allocated to each object is a multiple of a preset number n (including 1 times, 2 times, etc.). The starting address of the object in the first memory space can be determined. The memory starting addresses of all objects are multiples of the preset number n. For example, if n is 8, the memory starting addresses of all objects are multiples of 8. In this way, the corresponding bit in the first bitmap can be determined based on the memory starting address of the object, and the bit is set to 1, which means that this bit corresponds to the memory starting address of the object.
[0054] Among them, when determining the corresponding bit in the first bitmap based on the memory starting address of the object, the byte where the bit corresponding to the memory starting address of the object is located in the first bitmap can be determined. Specifically, the memory starting address of the object can be subtracted from the starting address of the first memory space, and then divided by n to determine that the memory starting address corresponds to the mth bit in the first bitmap (that is, the offset in the first bitmap). The byte where the bit is located in the first bitmap is the m / 8th byte (where 8bit=1Byte). Adding m / 8 to the starting address of the first bitmap can locate the address of the byte where the bit corresponding to the memory starting address of the object is located, and the bit corresponding to the memory starting address of the object is set to 1 in the byte through bit operation.
[0055] Of course, the method of marking the memory start address of each object in the first bitmap is not limited to the above example, and other methods may also be used.
[0056] Based on any of the foregoing embodiments, continuously allocating memory in the first memory space according to the size of each object to be allocated memory in the first thread may specifically include:
[0057] Before allocating memory to any object, determining whether the unused memory in the first memory space satisfies the size of the object;
[0058] If the unused memory in the first memory space satisfies the size of the object, memory is allocated starting from the starting address of the unused memory according to the size of the object.
[0059] In this embodiment, since a first memory space of a preset size has been allocated to the first thread, and the first memory space is limited, there are multiple objects to be allocated memory in the first thread, so the first memory space may be full. Before allocating memory to any object to be allocated memory, it is first determined whether the unused memory of the first memory space meets the size of the object. If it meets the size of the object, it means that the unused memory of the first memory space is relatively sufficient, and memory can continue to be allocated to the object in the first memory space, that is, memory is allocated starting from the starting address of the unused memory according to the size of the object.
[0060] To determine whether the unused memory of the first memory space meets the size of the object, the starting address of the unused memory of the first memory space is added to the size of the object, and whether the addition result exceeds the end address of the first memory space. If it exceeds, it means that the unused memory of the first memory space does not meet the size of the object.
[0061] like Figure 5 As shown, if the unused memory in the first memory space does not meet the size of the object, the corresponding bit of the starting address of the unused memory in the first bitmap is set to 1, that is, the unused memory is regarded as an empty object, and the corresponding bit of the memory starting address of the empty object in the first bitmap is set to 1 (to serve as the boundary with the previous object in the first bitmap), and the unused memory is discarded, and a second memory space of a preset size is reallocated (wherein the size of the second memory space can be equal to the size of the first memory space, or can be larger or smaller than the size, and optionally can be increased according to a certain rule), and a second bitmap having a mapping relationship with the second memory space is configured (the mapping relationship between the second memory space and the second bitmap can be the same as the mapping relationship in the above embodiment, or different); memory is continuously allocated in the second memory space according to the size of each object to be allocated memory in the first thread, and the memory starting address of each object is marked in the second bitmap based on the mapping relationship. The process is the same as the above embodiment and will not be repeated here. Similarly, for the second memory space and the second bitmap, the other processes described below are also the same as those for the first memory space and the first bitmap. The other processes described below are only illustrated by taking the first memory space and the first bitmap as examples.
[0062] Based on any of the above embodiments, the above memory allocation method supports pointer operations, including but not limited to pointer arithmetic operations (such as addition and subtraction) and relational operations (such as comparing the sizes of two pointers and whether they are equal). A pointer can point to any location in the memory of an object. Therefore, it is necessary to determine the metadata of the object pointed to based on the pointer, including but not limited to at least one of the object's memory start address, memory end address, and memory size. The specific process is as follows:
[0063] Determining an address of a pointer within a memory range pointing to any target object in the first memory space;
[0064] The metadata of the target object is determined according to the address of the pointer and the first bitmap.
[0065] In this embodiment, if a pointer points to a memory range of any target object in the first memory space, including the target object's memory start address, memory end address, or any address between the memory start address and memory end address, the address pointed to by the pointer, i.e., the address of the pointer, can be determined first. Because the first bitmap contains the start address information of each object, the target object can be specified based on the pointer address. The target object's metadata, including but not limited to at least one of the target object's memory start address, memory end address, and memory size, can then be inferred in conjunction with the first bitmap.
[0066] Specifically, determining the metadata of the target object according to the address of the pointer and the first bitmap may include:
[0067] First, the first bit corresponding to the address of the pointer in the first bitmap is determined.
[0068] In this embodiment, since each preset number n bytes of the first memory space corresponds to a bit in the first bitmap, when determining the bit corresponding to the address of the pointer in the first bitmap (recorded as the first bit), it is sufficient to determine the bit corresponding to the first byte in the n bytes where the pointer is located in the first bitmap. The address of the first byte in the n bytes where the pointer is located can be obtained by clearing the lower log2n bits of the address of the pointer. Assuming that the preset number n is 8, the lower 3 bits of the address of the pointer are cleared to obtain the address of the first byte in the n bytes where the pointer is located, and then the bit corresponding to the first byte in the n bytes where the pointer is located in the first bitmap is determined as the first bit corresponding to the address of the pointer in the first bitmap. Specifically, the position of the first bit in the first bitmap = (address of the first byte in the n bytes where the pointer is located - starting address of the first memory space) / n, or (address of the pointer - starting address of the first memory space) / n, and rounded up.
[0069] Furthermore, the process of determining the memory start address, memory end address, and memory size of the target object is as follows.
[0070] The process of determining the target object's memory starting address is as follows:
[0071] Search the first bit in the first bitmap toward the head direction for the second bit whose value is 1 and which is closest to the first bit, determine a first offset between the first bit and the second bit, and determine a first memory offset between the memory starting address of the target object and the address of the pointer based on the first offset and the preset number; determine the memory starting address of the target object based on the address of the pointer and the first memory offset.
[0072] In this embodiment, after determining the first bit corresponding to the address of the pointer in the first bitmap, the second bit with a value of 1 closest to the first bit is searched in the first bitmap in the head direction. The second bit corresponds to the memory starting address of the target object. Figure 2 , the head direction is the right direction, that is, in the first bit map, the second bit with the nearest value of 1 is searched to the right of the first bit; Figure 4 , the head direction is the left direction, that is, in the first bit map, the second bit with the nearest value of 1 is searched on the left side of the first bit.
[0073] Obtain the first offset between the first bit and the second bit, that is, the number of bits between the first bit and the second bit. Correspondingly, it is possible to determine the number of bytes between the memory starting address of the target object and the address of the pointer (the address of the first byte in the n bytes where the pointer is located) in the first memory space = the first offset × the preset number n, that is, the first memory offset. According to the address of the pointer and the first memory offset, the memory starting address of the target object can be determined. Specifically, the memory starting address of the target object = the address of the first byte in the n bytes where the pointer is located - the first memory offset.
[0074] The process of determining the target object's memory end address is as follows:
[0075] Search the first bit map toward the tail for the third bit whose value is 1 and which is closest to the first bit, determine a second offset between the first bit and the third bit, and determine a second memory offset between the end start address of the target object and the address of the pointer based on the second offset and the preset number; determine the memory end address of the target object based on the address of the pointer and the second memory offset.
[0076] In this embodiment, similar to the above process of determining the memory start address of the target object, after determining the first bit corresponding to the pointer address in the first bitmap, the first bitmap is searched for the third bit with a value of 1 closest to the first bit in the tail direction. The third bit corresponds to the memory start address of the next object adjacent to the target object, and is also the memory end address of the target object. Figure 2 , the tail direction is the left direction, that is, in the first bit map, the nearest third bit with a value of 1 is searched on the left side of the first bit; for Figure 4 , the tail direction is the right direction, that is, in the first bit map, the third bit with the nearest value of 1 is searched to the right of the first bit.
[0077] Obtain the second offset between the first bit and the third bit, that is, the number of bits between the first bit and the third bit. Correspondingly, it is possible to determine the number of bytes between the end address of the target object and the address of the pointer (the address of the first byte in the n bytes where the pointer is located) in the first memory space = the second offset × the preset number n, that is, the second memory offset. According to the address of the pointer and the second memory offset, the memory end address of the target object can be determined. Specifically, the memory end address of the target object = the address of the first byte in the n bytes where the pointer is located + the second memory offset.
[0078] The process of determining the memory size of the target object is as follows:
[0079] The memory size of the target object is determined according to the memory start address of the target object and the memory end address of the target object.
[0080] Based on the above embodiment, determining the first bit corresponding to the address of the pointer in the first bitmap includes:
[0081] The byte where the first bit is located in the first bitmap is determined, and a first partial bitmap corresponding to the byte is read, and the first bit is determined in the first partial bitmap.
[0082] In this embodiment, since the first bitmap may be relatively large, when determining the metadata of the target object based on the first bitmap, it is not necessary to read the entire first bitmap. Instead, it is only necessary to read the byte where the first bit in the first bitmap is located, which is recorded as the first partial bitmap. The first partial bitmap includes 8 bits (1Byte=8bit). The first bit is determined in the first partial bitmap, and the subsequent second and third bits are searched based on the first bit.
[0083] To determine the byte where the first bit is located in the first bitmap, it is possible to first determine the bit position of the first bit in the entire first bitmap. The specific process is as described in the above embodiment, and the bit position corresponding to the first byte of the n bytes where the pointer is located in the first bitmap is determined. The position of the first bit in the first bitmap = (the address of the first byte in the n bytes where the pointer is located - the starting address of the first memory space) / n. Furthermore, since every 8 bits of the first bitmap is a byte, it is possible to determine the address of the byte where the first bit is located in the first bitmap = the starting address of the first bitmap + (the position of the first bit in the first bitmap / 8), and then read the byte from the first bitmap according to the address of the byte where the first bit is located in the first bitmap to obtain the first partial bitmap.
[0084] The position of the first bit in the first partial bitmap is the position of the first bit in the first bitmap%8 (ie, modulo 8).
[0085] Furthermore, searching in the first bitmap in a head direction for a second bit whose value is 1 and is closest to the first bit, and determining a first offset between the first bit and the second bit, may specifically include:
[0086] determining a third offset of the first bit relative to the end of the first partial bitmap;
[0087] Determine whether the second bit exists in the first partial bitmap;
[0088] If it is determined that the second bit exists in the first partial bitmap, determining a fourth offset of the second bit relative to the end of the first partial bitmap; and determining a first offset between the first bit and the second bit based on the third offset and the fourth offset; or
[0089] If it is determined that the second bit does not exist in the first partial bitmap, read the partial bitmap corresponding to the adjacent bytes along the head direction until the second partial bitmap with the second bit is found, and determine the fifth offset of the second bit relative to the end of the second partial bitmap; determine the sixth offset of the second bit relative to the end of the first partial bitmap based on the fifth offset; and determine the first offset between the first bit and the second bit based on the third offset and the sixth offset.
[0090] In this embodiment, after determining the first bit in the first partial bitmap, a third offset of the first bit relative to the end of the first partial bitmap can be determined, that is, the number of bits between the first bit and the end of the first partial bitmap. The concepts of the head and the tail of the first partial bitmap are the same as those in the above embodiment.
[0091] In addition, considering that the memory of the object may be large, resulting in multiple bytes spanning in the first bitmap, and since the first partial bitmap is only one byte of the first partial bitmap, the second bit of the first bit in the first partial bitmap toward the head direction may be in the first partial bitmap, or may not be in the first partial bitmap. Therefore, it is also necessary to determine whether the second bit exists in the first partial bitmap. If it is determined that the second bit exists in the first partial bitmap, the fourth offset of the second bit relative to the end of the first partial bitmap is determined, that is, how many bits exist between the second bit and the end of the first partial bitmap. The first offset between the first bit and the second bit is obtained by subtracting the third offset from the fourth offset. If it is determined that the second bit does not exist in the first partial bitmap, the partial bits corresponding to the adjacent bytes are read in sequence along the head direction. Figure, it is also necessary to determine in sequence whether there is a second bit in the partial bitmap corresponding to the adjacent bytes. If the second bit does not exist in the partial bitmap corresponding to the first adjacent byte, continue to read the partial bitmap corresponding to the next adjacent byte, and so on, until a partial bitmap with a second bit is found, recorded as the second partial bitmap, and further determine the fifth offset of the second bit relative to the end of the second partial bitmap. Since the third offset is based on the end of the first partial bitmap and the fifth offset is based on the end of the second partial bitmap, it is necessary to convert the fifth offset to the end of the first partial bitmap as the reference to obtain the sixth offset of the second bit relative to the end of the first partial bitmap. In this way, the sixth offset is subtracted from the third offset to obtain the first offset between the first bit and the second bit. Specifically, when determining the sixth offset, the number of bits between the end of the second partial bitmap and the end of the first partial bitmap can be obtained, and the fifth offset can be added to the number of bits to obtain the sixth offset.
[0092] Based on the above embodiment, the above process of determining the offset can be implemented by bit operation. The specific process is as follows:
[0093] The first bit is determined in the first partial bitmap, and a first mask can be constructed according to the position of the first bit in the first partial bitmap. The length of the first mask is the same as that of the first partial bitmap, and the position corresponding to the first bit is 1, and the remaining positions are 0.
[0094] For example, for Figure 6a , the preset number n = 8, the pointer points to the 30th byte in the first memory space (starting address base, ending address end, assuming base = 0), that is, the address of the pointer is ptr = (30) 10 =(11110)2, the address of the first byte in the 8 bytes where the pointer is located p=(24) 10=(11000)2 (the low log28 bits of ptr are cleared), the position of the first bit in the first bitmap is index = (ptr-base) / n = 3 (rounded), assuming that the address of the first bitmap is from x to y, where the address of the first partial bitmap is bytep = x + (index / 8) = x, and the position of the first bit in the first partial bitmap is index%8 = 3%8 = 3, constructing the first mask mask1 = 1<<(index%8) = 1<<(3%8) = 1<<3 = 00001000.
[0095] Furthermore, the number of zeros in the first mask from 1 toward the end of the first mask, i.e., the number of zeros to the left of 1, is determined as a third offset of the first bit relative to the end of the first partial bitmap. This can be implemented using a pre-set instruction leadingZeros(a) in the CPU, where leadingZeros(a) counts the number of consecutive zeros at the beginning of a. Therefore, the third offset = leadingZeros(mask1) = 4.
[0096] When determining whether the second bit exists in the first partial bitmap, the method may specifically include:
[0097] Constructing a second mask, where the length of the second mask is the same as that of the first partial bitmap, and the positions of the preset bits at the end of the second mask are 0, and the remaining positions are 1, and the preset number is the third offset;
[0098] In the above example, the second mask mask2=00001111 can be obtained as follows: mask2=0xFF>>third offset=00001111 (ie, 11111111 is shifted right by the third offset).
[0099] Furthermore, a bitwise AND operation is performed on the second mask and the first partial bitmap, and the operation result bt = *bytep&mask2 = 01100011&00001111 = 00000011. If the operation result bt is not 0, it means that the second bit exists in the first partial bitmap, and the first 1 at the end of the operation result bt corresponds to the position of the second bit; if the operation result bt is 0, it means that the second bit does not exist in the first partial bitmap.
[0100] If the calculation result bt is not 0, the number of trailing zeros in the calculation result bt is determined, that is, the number of zeros in the second bit toward the end of the calculation result, and this is determined as the fourth offset: fourth offset = leadingZeros(bt) = 6. Furthermore, the first offset between the first and second bits = fourth offset - third offset = 6 - 4 = 2.
[0101] If the operation result bt is 0, then Figure 6b As shown, the partial bitmap corresponding to the adjacent bytes is read along the head direction (to the right), and it is determined whether the partial bitmap is 0. If it is determined that the partial bitmap is not 0, it is recorded as the second partial bitmap, and the presence of the second bit in the second partial bitmap is determined; then the number of 0s at the end of the second partial bitmap is determined, which is determined as the fifth offset of the second bit relative to the end of the second partial bitmap; the number of bits between the end of the second partial bitmap and the end of the first partial bitmap is 8, and the fifth offset is added to the number of bits (i.e., the fifth offset + 8) to obtain the sixth offset of the second bit relative to the end of the first partial bitmap. Then, the first offset between the first bit and the second bit = the sixth offset - the third offset.
[0102] If it is determined that the partial bitmap corresponding to the adjacent byte is 0, continue to read the partial bitmap corresponding to the next adjacent byte along the head direction (to the right). If it is determined that the partial bitmap is not 0, then the partial bitmap is recorded as the second partial bitmap. Similarly, the number of 0s at the end of the second partial bitmap is determined, which is determined as the fifth offset of the second bit relative to the end of the second partial bitmap. The number of bits between the end of the second partial bitmap and the end of the first partial bitmap is 8*2 (that is, 2 bytes). The fifth offset is added to the number of bits (that is, the fifth offset + 8*2) to obtain the offset of the second bit relative to the end of the first partial bitmap, and the third offset is subtracted to obtain the first offset between the first bit and the second bit. And so on, no further details are given here.
[0103] Based on the above embodiment, searching in the first bitmap in a tail direction for a third bit whose value is 1 and is closest to the first bit, and determining a second offset between the first bit and the third bit includes:
[0104] Determining a seventh offset of the first bit relative to the first partial bitmap header;
[0105] Determine whether the third bit exists in the first partial bitmap;
[0106] If it is determined that the third bit exists in the first partial bitmap, determining an eighth offset of the third bit relative to the header of the first partial bitmap; and determining a second offset between the first bit and the third bit based on the seventh offset and the eighth offset; or
[0107] If it is determined that the third bit does not exist in the first partial bitmap, the partial bitmaps corresponding to adjacent bytes are read sequentially in the tail direction until the third partial bitmap containing the third bit is found, and a ninth offset of the third bit relative to the header of the third partial bitmap is determined; a tenth offset of the third bit relative to the header of the first partial bitmap is determined based on the ninth offset; and a second offset between the first bit and the third bit is determined based on the seventh offset and the tenth offset.
[0108] In this embodiment, after determining the first bit in the first partial bitmap, the seventh offset of the first bit relative to the first partial bitmap header can be determined, that is, the number of bits between the first bit and the first partial bitmap header. The concepts of the first partial bit header and the tail are the same as those in the above embodiment.
[0109] Similarly, since the memory of the object may be large, resulting in multiple bytes being spanned in the first bitmap, the third bit from the first bit in the first bitmap to the tail may be in the first partial bitmap or may not be in the first partial bitmap. Therefore, it is also necessary to determine whether there is a third bit in the first partial bitmap. If it is determined that the third bit exists in the first partial bitmap, the eighth offset of the third bit relative to the head of the first partial bitmap is determined, that is, how many bits are between the third bit and the head of the first partial bitmap. The second offset between the first bit and the third bit is obtained by subtracting the seventh offset from the eighth offset. If it is determined that there is no third bit in the first partial bitmap, the partial bitmaps corresponding to the adjacent bytes are read in sequence along the tail direction, and the corresponding bits are also determined in sequence. Whether there is a third bit in the partial bitmap corresponding to the adjacent byte. If the third bit does not exist in the partial bitmap corresponding to the first adjacent byte, continue to read the partial bitmap corresponding to the next adjacent byte, and so on, until a partial bitmap with a third bit is found, recorded as the third partial bitmap, and further determine the ninth offset of the third bit relative to the header of the third partial bitmap. Since the seventh offset is based on the header of the first partial bitmap and the ninth offset is based on the header of the third partial bitmap, it is necessary to convert the ninth offset to the header of the first partial bitmap as the reference to obtain the tenth offset of the third bit relative to the header of the first partial bitmap. In this way, the tenth offset is subtracted from the seventh offset to obtain the second offset between the first bit and the third bit. Specifically, when determining the tenth offset, the number of bits between the header of the third partial bitmap and the header of the first partial bitmap can be obtained, and the ninth offset is added to the number of bits to obtain the tenth offset.
[0110] Based on the above embodiment, the above process of determining the offset can be implemented by bit operation. The specific process is as follows:
[0111] like Figure 6c In the above example, a first mask mask1 = 00001000 is constructed (if the first mask has been constructed previously, it does not need to be constructed again here). The number of zeros in the first mask, i.e., the number of zeros to the right of 1, is determined as the seventh offset of the first bit relative to the head of the first partial bitmap. This can be implemented using the CPU's preset instruction trailingZeros(a), where trailingZeros(a) counts the number of consecutive zeros at the end of a. Therefore, the seventh offset = trailingZeros(mask1) = 3.
[0112] When determining whether the third bit exists in the first partial bitmap, the method may specifically include:
[0113] Constructing a third mask, wherein the length of the third mask is the same as that of the first partial bitmap, and the positions of the preset bits in the third mask are 0, and the remaining positions are 1, and the preset number is the seventh offset plus 1;
[0114] In the above example, the third mask mask3=11110000 can be obtained as follows: mask2=0xFF<<(seventh offset+1)=11110000 (that is, 11111111 is shifted left by the seventh offset+1).
[0115] Furthermore, a bitwise AND operation is performed on the third mask and the first partial bitmap, and the operation result bt = *bytep&mask3 = 01100011&11110000 = 01100000. If the operation result bt is not 0, it means that the third bit exists in the first partial bitmap, and the first 1 at the head of the operation result bt corresponds to the position of the third bit; if the operation result bt is 0, it means that the third bit does not exist in the first partial bitmap.
[0116] If the calculation result bt is not 0, the number of zeros at the beginning of the calculation result bt is determined, that is, the number of zeros at the third bit position toward the beginning of the calculation result, which is determined as the eighth offset: eighth offset = trailingZeros(bt) = 5. Furthermore, the second offset between the first bit position and the third bit position = eighth offset - seventh offset = 5 - 3 = 2.
[0117] If the operation result bt is 0, then Figure 6dAs shown, the partial bitmap corresponding to the adjacent bytes is read in the tail direction (to the left), and it is determined whether the partial bitmap is 0. If it is determined that the partial bitmap is not 0, it is recorded as the third partial bitmap, and the presence of the third bit in the third partial bitmap is determined. Then, the number of 0s in the header of the third partial bitmap is determined, which is determined as the ninth offset of the third bit relative to the header of the third partial bitmap. The number of bits between the header of the third partial bitmap and the header of the first partial bitmap is 8. The ninth offset is added to the number of bits (i.e., the ninth offset + 8) to obtain the tenth offset of the third bit relative to the header of the first partial bitmap. Then, the second offset between the first bit and the third bit = the tenth offset - the seventh offset.
[0118] If it is determined that the partial bitmap corresponding to the adjacent byte is 0, then continue to read the partial bitmap corresponding to the next adjacent byte in the tail direction (to the left). If it is determined that the partial bitmap is not 0, then the partial bitmap is recorded as the third partial bitmap. Similarly, the number of 0s in the header of the third partial bitmap is determined, which is determined as the ninth offset of the third bit relative to the header of the third partial bitmap. The number of bits between the header of the third partial bitmap and the header of the first partial bitmap is 8*2 (that is, 2 bytes). The ninth offset is added to the number of bits (that is, the ninth offset + 8*2) to obtain the offset of the third bit relative to the header of the first partial bitmap. Subtracting the seventh offset from the offset yields the second offset between the first bit and the third bit. And so on, no further details will be given here.
[0119] Corresponding to the memory allocation method of the above embodiment, Figure 7 This is a structural block diagram of a memory allocation device provided by an embodiment of the present disclosure. For ease of explanation, only the parts related to the embodiment of the present disclosure are shown. Figure 7 The memory allocation device 700 includes: an allocation unit 701 and a processing unit 702.
[0120] The allocating unit 701 is configured to allocate a first memory space of a preset size to a first thread to which memory is to be allocated, and to configure a first bitmap having a mapping relationship with the first memory space; and to continuously allocate memory in the first memory space according to the size of each object to be allocated memory in the first thread;
[0121] The processing unit 702 is configured to mark the memory start address of each of the objects in the first bitmap based on the mapping relationship.
[0122] In one or more embodiments of the present disclosure, the mapping relationship is: each bit in the first bitmap corresponds in sequence to a preset number of bytes in the first memory space;
[0123] The allocating unit 701 is configured to clear all bits of the first bitmap to zero when continuously allocating memory in the first memory space according to the size of each object to be allocated memory in the first thread; continuously allocating memory in the first memory space according to the size of each object to be allocated memory in the first thread, and the memory size of each object is a multiple of the preset number;
[0124] The processing unit 702 is configured to set a bit corresponding to the memory start address of each object in the first bitmap to 1 when marking the memory start address of each object in the first bitmap based on the mapping relationship.
[0125] In one or more embodiments of the present disclosure, when continuously allocating memory in the first memory space according to the size of each object to be allocated memory in the first thread, the allocating unit 701 is configured to:
[0126] Before allocating memory to any object, determining whether the unused memory in the first memory space satisfies the size of the object;
[0127] If the unused memory in the first memory space satisfies the size of the object, memory is allocated starting from the starting address of the unused memory according to the size of the object.
[0128] In one or more embodiments of the present disclosure, the allocation unit 701 is further configured to, if the unused memory in the first memory space does not meet the size of the object, set the corresponding bit of the start address of the unused memory in the first bitmap to 1, reallocate a second memory space of a preset size, and identify the memory start address of each of the objects in the second bitmap based on the mapping relationship; and continuously allocate memory in the second memory space according to the size of each object to be allocated memory in the first thread;
[0129] The processing unit 702 is further configured to mark the memory start address of each object in the second bitmap based on the mapping relationship.
[0130] In one or more embodiments of the present disclosure, the processing unit 702 is further configured to:
[0131] Determining an address of a pointer within a memory range pointing to any target object in the first memory space;
[0132] The metadata of the target object is determined according to the address of the pointer and the first bitmap, wherein the metadata includes at least one of a memory start address, a memory end address, and a memory size.
[0133] In one or more embodiments of the present disclosure, when determining the metadata of the target object according to the address of the pointer and the first bitmap, the processing unit 702 is configured to:
[0134] Determine the first bit corresponding to the address of the pointer in the first bitmap;
[0135] Searching the first bitmap toward the head for a second bit whose value is 1 and is closest to the first bit, determining a first offset between the first bit and the second bit, and determining a first memory offset between the memory start address of the target object and the address of the pointer based on the first offset and the preset number; determining the memory start address of the target object based on the address of the pointer and the first memory offset; and / or
[0136] Searching the first bitmap in the tail direction for a third bit whose value is 1 and is closest to the first bit, determining a second offset between the first bit and the third bit, and determining a second memory offset between the end start address of the target object and the address of the pointer based on the second offset and the preset number; determining the memory end address of the target object based on the address of the pointer and the second memory offset; and / or
[0137] The memory size of the target object is determined according to the memory start address of the target object and the memory end address of the target object.
[0138] In one or more embodiments of the present disclosure, when determining the first bit corresponding to the address of the pointer in the first bitmap, the processing unit 702 is configured to:
[0139] The byte where the first bit is located in the first bitmap is determined, and a first partial bitmap corresponding to the byte is read, and the first bit is determined in the first partial bitmap.
[0140] In one or more embodiments of the present disclosure, the processing unit 702 searches for a second bit whose value is 1 and is closest to the first bit in the head direction in the first bitmap, and determines a first offset between the first bit and the second bit, for:
[0141] determining a third offset of the first bit relative to the end of the first partial bitmap;
[0142] Determine whether the second bit exists in the first partial bitmap;
[0143] If it is determined that the second bit exists in the first partial bitmap, determining a fourth offset of the second bit relative to the end of the first partial bitmap; and determining a first offset between the first bit and the second bit based on the third offset and the fourth offset; or
[0144] If it is determined that the second bit does not exist in the first partial bitmap, the partial bitmaps corresponding to adjacent bytes are read sequentially along the head direction until the second partial bitmap containing the second bit is found, and the fifth offset of the second bit relative to the end of the second partial bitmap is determined; the sixth offset of the second bit relative to the end of the first partial bitmap is determined based on the fifth offset; and the first offset between the first bit and the second bit is determined based on the third offset and the sixth offset.
[0145] In one or more embodiments of the present disclosure, the processing unit 702 searches for a third bit whose value is 1 and is closest to the first bit in the first bitmap in a tail direction, and determines the second offset between the first bit and the third bit, and is configured to:
[0146] Determining a seventh offset of the first bit relative to the first partial bitmap header;
[0147] Determine whether the third bit exists in the first partial bitmap;
[0148] If it is determined that the third bit exists in the first partial bitmap, determining an eighth offset of the third bit relative to the header of the first partial bitmap; and determining a second offset between the first bit and the third bit based on the seventh offset and the eighth offset; or
[0149] If it is determined that the third bit does not exist in the first partial bitmap, the partial bitmaps corresponding to adjacent bytes are read sequentially in the tail direction until the third partial bitmap containing the third bit is found, and a ninth offset of the third bit relative to the header of the third partial bitmap is determined; a tenth offset of the third bit relative to the header of the first partial bitmap is determined based on the ninth offset; and a second offset between the first bit and the third bit is determined based on the seventh offset and the tenth offset.
[0150] In one or more embodiments of the present disclosure, after determining the first bit in the first partial bitmap, the processing unit 702 is further configured to:
[0151] A first mask is constructed according to the position of the first bit in the first partial bitmap, the length of the first mask is the same as that of the first partial bitmap, the position corresponding to the first bit is 1, and the remaining positions are 0.
[0152] In one or more embodiments of the present disclosure, when determining the third offset of the first bit relative to the end of the first partial bitmap, the processing unit 702 is configured to:
[0153] The number of 0s in the first mask in a direction from 1 to the tail of the first mask is determined as the third offset.
[0154] In one or more embodiments of the present disclosure, when determining whether the second bit exists in the first partial bitmap, the processing unit 702 is configured to:
[0155] Constructing a second mask, where the length of the second mask is the same as that of the first partial bitmap, and the positions of the preset bits at the end of the second mask are 0, and the remaining positions are 1, and the preset number is the third offset;
[0156] Performing a bitwise AND operation on the second mask and the first partial bitmap, and if the operation result is not 0, determining that the second bit exists in the first partial bitmap; or if the operation result is 0, determining that the second bit does not exist in the first partial bitmap;
[0157] Accordingly, when determining the fourth offset of the second bit relative to the end of the first partial bitmap, the processing unit 702 is configured to:
[0158] The number of 0s at the end of the operation result is determined as the fourth offset.
[0159] In one or more embodiments of the present disclosure, when determining that the second bit exists in the second partial bitmap, the processing unit 702 is configured to:
[0160] If it is determined that the second partial bitmap is not 0, determining that the second bit exists in the second partial bitmap;
[0161] Determine the number of zeros at the end of the second partial bitmap, and determine the number as the fifth offset;
[0162] Accordingly, when determining the sixth offset of the second bit relative to the end of the first partial bitmap according to the fifth offset, the processing unit 702 is configured to:
[0163] Obtaining the number of bits between the tail of the second partial bitmap and the tail of the first partial bitmap;
[0164] The fifth offset is added to the number of bits to obtain the sixth offset.
[0165] In one or more embodiments of the present disclosure, when determining the seventh offset of the first bit relative to the first partial bitmap header, the processing unit 702 is configured to:
[0166] In the first mask, the number of 0s existing in the direction from 1 to the bottom of the first mask is determined as the seventh offset.
[0167] In one or more embodiments of the present disclosure, when determining whether the third bit exists in the first partial bitmap, the processing unit 702 is configured to:
[0168] Constructing a third mask, wherein the length of the third mask is the same as that of the first partial bitmap, and the positions of the preset bits in the third mask are 0, and the remaining positions are 1, and the preset number is the seventh offset plus 1;
[0169] Performing a bitwise AND operation on the third mask and the first partial bitmap, and if the operation result is not 0, determining that the third bit exists in the first partial bitmap; or if the operation result is 0, determining that the third bit does not exist in the first partial bitmap;
[0170] Accordingly, when determining the eighth offset of the third bit relative to the first partial bitmap header, the processing unit 702 is configured to:
[0171] The number of 0s in the header of the operation result is determined as the eighth offset.
[0172] In one or more embodiments of the present disclosure, when determining that the third bit exists in the third partial bitmap, the processing unit 702 is configured to:
[0173] If it is determined that the third partial bitmap is not 0, then determining that the third bit exists in the third partial bitmap;
[0174] Determine the number of zeros in the header of the third partial bitmap, and determine the number as the ninth offset;
[0175] Accordingly, when determining the tenth offset of the third bit relative to the first partial bitmap header according to the ninth offset, the processing unit 702 is configured to:
[0176] Obtaining the number of bits between the third partial bitmap header and the first partial bitmap header;
[0177] The ninth offset is added to the number of bits to obtain the tenth offset.
[0178] The device provided in this embodiment can be used to execute the technical solution of the above method embodiment. Its implementation principle and technical effects are similar and will not be described in detail in this embodiment.
[0179] refer to Figure 8 , which shows a schematic structural diagram of an electronic device 800 suitable for implementing an embodiment of the present disclosure. The electronic device 800 may be a terminal device or a server. The terminal device may include, but is not limited to, mobile terminals such as mobile phones, laptop computers, digital broadcast receivers, personal digital assistants (PDAs), tablet computers (Portable Android Devices, PADs), portable multimedia players (PMPs), in-vehicle terminals (e.g., in-vehicle navigation terminals), and fixed terminals such as digital TVs and desktop computers. Figure 8 The electronic device shown is only an example and should not limit the functions and scope of use of the embodiments of the present disclosure.
[0180] like Figure 8 As shown, the electronic device 800 may include a processing device (e.g., a central processing unit, a graphics processing unit, etc.) 801, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 802 or a program loaded from a storage device 808 into a random access memory (RAM) 803. Various programs and data required for the operation of the electronic device 800 are also stored in the RAM 803. The processing device 801, the ROM 802, and the RAM 803 are connected to each other via a bus 804. An input / output (I / O) interface 805 is also connected to the bus 804.
[0181] Typically, the following devices may be connected to the I / O interface 805: an input device 806 including, for example, a touch screen, a touchpad, a keyboard, a mouse, a camera, a microphone, an accelerometer, a gyroscope, etc.; an output device 807 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; a storage device 808 including, for example, a magnetic tape, a hard disk, etc.; and a communication device 809. The communication device 809 may allow the electronic device 800 to communicate with other devices wirelessly or by wire to exchange data. Although Figure 8The electronic device 800 is shown with various devices, but it should be understood that it is not required to implement or possess all of the devices shown. More or fewer devices may be implemented or possessed instead.
[0182] In particular, according to an embodiment of the present disclosure, the process described above with reference to the flowchart can be implemented as a computer software program. For example, an embodiment of the present disclosure includes a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network via the communication device 809, or installed from the storage device 808, or installed from the ROM 802. When the computer program is executed by the processing device 801, the above-mentioned functions defined in the method of the embodiment of the present disclosure are performed.
[0183] It should be noted that the computer-readable medium mentioned above in the present disclosure may be a computer-readable signal medium or a computer-readable storage medium, or any combination of the two. A computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device, or component, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present disclosure, a computer-readable storage medium may be any tangible medium that contains or stores a program that can be used by or in conjunction with an instruction execution system, device, or component. In the present disclosure, a computer-readable signal medium may include a data signal propagated in baseband or as part of a carrier wave, which carries computer-readable program code. Such a propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. A computer-readable signal medium may also be any computer-readable medium other than a computer-readable storage medium that can transmit, propagate, or transport a program for use by or in conjunction with an instruction execution system, apparatus, or device. The program code contained on the computer-readable medium may be transmitted using any suitable medium, including but not limited to wires, optical cables, RF (radio frequency), etc., or any suitable combination thereof.
[0184] The computer-readable medium may be included in the electronic device, or may exist independently without being incorporated into the electronic device.
[0185] The computer-readable medium carries one or more programs. When the one or more programs are executed by the electronic device, the electronic device executes the method shown in the above embodiment.
[0186] Computer program code for performing the operations of the present disclosure may be written in one or more programming languages or a combination thereof, including object-oriented programming languages such as Java, Smalltalk, C++, and conventional procedural programming languages such as "C" or similar programming languages. The program code may be executed entirely on the user's computer, partially on the user's computer, as a separate software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In cases involving a remote computer, the remote computer may be connected to the user's computer via any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (e.g., via the Internet using an Internet service provider).
[0187] The flowcharts and block diagrams in the accompanying drawings illustrate the possible implementation architecture, functions and operations of the systems, methods and computer program products according to various embodiments of the present disclosure. In this regard, each box in the flowchart or block diagram can represent a module, program segment, or a part of code, and the module, program segment, or a part of code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order than that marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram and / or flowchart, and the combination of the boxes in the block diagram and / or flowchart, can be implemented with a dedicated hardware-based system that performs the specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0188] The units involved in the embodiments described in this disclosure may be implemented in software or hardware. In some cases, the name of a unit does not limit the unit itself. For example, the first acquisition unit may also be described as a "unit for acquiring at least two Internet Protocol addresses."
[0189] The functions described above herein may be performed, at least in part, by one or more hardware logic components. For example, and without limitation, exemplary types of hardware logic components that may be used include: field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chip (SOCs), complex programmable logic devices (CPLDs), and the like.
[0190] In the context of the present disclosure, a machine-readable medium can be a tangible medium that can contain or store a program for use by or in conjunction with an instruction execution system, device or equipment. A machine-readable medium can be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium can include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or equipment, or any suitable combination of the foregoing. A more specific example of a machine-readable storage medium can include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
[0191] In a first aspect, according to one or more embodiments of the present disclosure, a memory allocation method is provided, comprising:
[0192] Allocating a first memory space of a preset size to a first thread to be allocated memory, and configuring a first bitmap having a mapping relationship with the first memory space;
[0193] Memory is continuously allocated in the first memory space according to the size of each object to be allocated memory in the first thread, and a memory start address of each object is marked in the first bitmap based on the mapping relationship.
[0194] According to one or more embodiments of the present disclosure, the mapping relationship is: each bit in the first bitmap corresponds in sequence to a preset number of bytes in the first memory space;
[0195] Accordingly, continuously allocating memory in the first memory space according to the size of each object to be allocated memory in the first thread, and marking the memory start address of each object in the first bitmap based on the mapping relationship, includes:
[0196] Clearing all bits of the first bitmap to zero;
[0197] Continuously allocating memory in the first memory space according to the size of each object to be allocated memory in the first thread, and the memory size of each object is a multiple of the preset number;
[0198] The bit corresponding to the memory start address of each object in the first bitmap is set to 1.
[0199] According to one or more embodiments of the present disclosure, continuously allocating memory in the first memory space according to the size of each object to be allocated memory in the first thread includes:
[0200] Before allocating memory to any object, determining whether the unused memory in the first memory space satisfies the size of the object;
[0201] If the unused memory in the first memory space satisfies the size of the object, memory is allocated starting from the starting address of the unused memory according to the size of the object.
[0202] According to one or more embodiments of the present disclosure, the method further includes:
[0203] If the unused memory in the first memory space does not meet the size of the object, the corresponding bit of the starting address of the unused memory in the first bitmap is set to 1, and a second memory space of a preset size is reallocated, and the memory starting address of each of the objects is marked in the second bitmap based on the mapping relationship;
[0204] Memory is continuously allocated in the second memory space according to the size of each object to be allocated memory in the first thread, and a memory start address of each object is marked in the second bitmap based on a mapping relationship.
[0205] According to one or more embodiments of the present disclosure, the method further includes:
[0206] Determining an address of a pointer within a memory range pointing to any target object in the first memory space;
[0207] The metadata of the target object is determined according to the address of the pointer and the first bitmap, wherein the metadata includes at least one of a memory start address, a memory end address, and a memory size.
[0208] According to one or more embodiments of the present disclosure, determining metadata of the target object according to the address of the pointer and the first bitmap includes:
[0209] Determine the first bit corresponding to the address of the pointer in the first bitmap;
[0210] Searching the first bitmap for a second bit whose value is 1 and is closest to the first bit in the head direction, determining a first offset between the first bit and the second bit, and determining a first memory offset between the memory start address of the target object and the address of the pointer based on the first offset and the preset number; determining the memory start address of the target object based on the address of the pointer and the first memory offset; and / or
[0211] Searching the first bitmap in the tail direction for a third bit whose value is 1 and is closest to the first bit, determining a second offset between the first bit and the third bit, and determining a second memory offset between the end start address of the target object and the address of the pointer based on the second offset and the preset number; determining the memory end address of the target object based on the address of the pointer and the second memory offset; and / or
[0212] The memory size of the target object is determined according to the memory start address of the target object and the memory end address of the target object.
[0213] According to one or more embodiments of the present disclosure, determining the first bit corresponding to the address of the pointer in the first bitmap includes:
[0214] The byte where the first bit is located in the first bitmap is determined, and a first partial bitmap corresponding to the byte is read, and the first bit is determined in the first partial bitmap.
[0215] According to one or more embodiments of the present disclosure, searching in the first bitmap in a head direction for a second bit whose value is 1 and is closest to the first bit, and determining a first offset between the first bit and the second bit, includes:
[0216] determining a third offset of the first bit relative to the end of the first partial bitmap;
[0217] Determine whether the second bit exists in the first partial bitmap;
[0218] If it is determined that the second bit exists in the first partial bitmap, determining a fourth offset of the second bit relative to the end of the first partial bitmap; and determining a first offset between the first bit and the second bit based on the third offset and the fourth offset; or
[0219] If it is determined that the second bit does not exist in the first partial bitmap, the partial bitmaps corresponding to adjacent bytes are read sequentially along the head direction until the second partial bitmap containing the second bit is found, and the fifth offset of the second bit relative to the end of the second partial bitmap is determined; the sixth offset of the second bit relative to the end of the first partial bitmap is determined based on the fifth offset; and the first offset between the first bit and the second bit is determined based on the third offset and the sixth offset.
[0220] According to one or more embodiments of the present disclosure, searching in the first bitmap in a tail direction for a third bit whose value is 1 and is closest to the first bit, and determining a second offset between the first bit and the third bit includes:
[0221] Determining a seventh offset of the first bit relative to the first partial bitmap header;
[0222] Determine whether the third bit exists in the first partial bitmap;
[0223] If it is determined that the third bit exists in the first partial bitmap, determining an eighth offset of the third bit relative to the header of the first partial bitmap; and determining a second offset between the first bit and the third bit based on the seventh offset and the eighth offset; or
[0224] If it is determined that the third bit does not exist in the first partial bitmap, the partial bitmaps corresponding to adjacent bytes are read in sequence until the third partial bitmap containing the third bit is found, and a ninth offset of the third bit relative to the header of the third partial bitmap is determined; a tenth offset of the third bit relative to the header of the first partial bitmap is determined based on the ninth offset; and a second offset between the first bit and the third bit is determined based on the seventh offset and the tenth offset.
[0225] According to one or more embodiments of the present disclosure, after determining the first bit in the first partial bitmap, the method further includes:
[0226] A first mask is constructed according to the position of the first bit in the first partial bitmap, the length of the first mask is the same as that of the first partial bitmap, the position corresponding to the first bit is 1, and the remaining positions are 0.
[0227] According to one or more embodiments of the present disclosure, determining a third offset of the first bit relative to the end of the first partial bitmap includes:
[0228] The number of 0s in the first mask in a direction from 1 to the tail of the first mask is determined as the third offset.
[0229] According to one or more embodiments of the present disclosure, determining whether the second bit exists in the first partial bitmap includes:
[0230] Constructing a second mask, where the length of the second mask is the same as that of the first partial bitmap, and the positions of the preset bits at the end of the second mask are 0, and the remaining positions are 1, and the preset number is the third offset;
[0231] Performing a bitwise AND operation on the second mask and the first partial bitmap, and if the operation result is not 0, determining that the second bit exists in the first partial bitmap; or if the operation result is 0, determining that the second bit does not exist in the first partial bitmap;
[0232] Accordingly, determining a fourth offset of the second bit relative to the end of the first partial bitmap includes:
[0233] The number of 0s at the end of the operation result is determined as the fourth offset.
[0234] According to one or more embodiments of the present disclosure, determining that the second bit exists in the second partial bitmap and then determining a fifth offset of the second bit relative to the end of the second partial bitmap includes:
[0235] If it is determined that the second partial bitmap is not 0, determining that the second bit exists in the second partial bitmap;
[0236] Determine the number of zeros at the end of the second partial bitmap, and determine the number as the fifth offset;
[0237] Correspondingly, determining a sixth offset of the second bit relative to the end of the first partial bitmap according to the fifth offset includes:
[0238] Obtaining the number of bits between the tail of the second partial bitmap and the tail of the first partial bitmap;
[0239] The fifth offset is added to the number of bits to obtain the sixth offset.
[0240] According to one or more embodiments of the present disclosure, determining a seventh offset of the first bit relative to the first partial bitmap header includes:
[0241] In the first mask, the number of 0s existing in the direction from 1 to the bottom of the first mask is determined as the seventh offset.
[0242] According to one or more embodiments of the present disclosure, determining whether the third bit exists in the first partial bitmap includes:
[0243] Constructing a third mask, wherein the length of the third mask is the same as that of the first partial bitmap, and the positions of the preset bits in the third mask are 0, and the remaining positions are 1, and the preset number is the seventh offset plus 1;
[0244] Performing a bitwise AND operation on the third mask and the first partial bitmap, and if the operation result is not 0, determining that the third bit exists in the first partial bitmap; or if the operation result is 0, determining that the third bit does not exist in the first partial bitmap;
[0245] Accordingly, determining an eighth offset of the third bit relative to the first partial bitmap header includes:
[0246] The number of 0s in the header of the operation result is determined as the eighth offset.
[0247] According to one or more embodiments of the present disclosure, determining that the third bit exists in the third partial bitmap, and then determining a ninth offset of the third bit relative to the header of the third partial bitmap, includes:
[0248] If it is determined that the third partial bitmap is not 0, then determining that the third bit exists in the third partial bitmap;
[0249] Determine the number of zeros in the header of the third partial bitmap, and determine the number as the ninth offset;
[0250] Accordingly, determining a tenth offset of the third bit relative to the first partial bitmap header according to the ninth offset includes:
[0251] Obtaining the number of bits between the third partial bitmap header and the first partial bitmap header;
[0252] The ninth offset is added to the number of bits to obtain the tenth offset.
[0253] In a second aspect, according to one or more embodiments of the present disclosure, a memory allocation device is provided, comprising:
[0254] an allocating unit, configured to allocate a first memory space of a preset size to a first thread to which memory is to be allocated, and to configure a first bitmap having a mapping relationship with the first memory space; and to continuously allocate memory in the first memory space according to the size of each object to be allocated memory in the first thread;
[0255] A processing unit is configured to mark a memory start address of each of the objects in the first bitmap based on the mapping relationship.
[0256] According to one or more embodiments of the present disclosure, the mapping relationship is: each bit in the first bitmap corresponds in sequence to a preset number of bytes in the first memory space;
[0257] The allocating unit is configured to clear all bits of the first bitmap to zero when continuously allocating memory in the first memory space according to the size of each object to be allocated memory in the first thread; continuously allocating memory in the first memory space according to the size of each object to be allocated memory in the first thread, and the memory size of each object is a multiple of the preset number;
[0258] The processing unit is configured to set a bit corresponding to the memory start address of each object in the first bitmap to 1 when marking the memory start address of each object in the first bitmap based on the mapping relationship.
[0259] According to one or more embodiments of the present disclosure, when continuously allocating memory in the first memory space according to the size of each object to be allocated memory in the first thread, the allocation unit is configured to:
[0260] Before allocating memory to any object, determining whether the unused memory in the first memory space satisfies the size of the object;
[0261] If the unused memory in the first memory space satisfies the size of the object, memory is allocated starting from the starting address of the unused memory according to the size of the object.
[0262] According to one or more embodiments of the present disclosure, the allocation unit is further configured to, if the unused memory in the first memory space does not satisfy the size of the object, set the corresponding bit of the starting address of the unused memory in the first bitmap to 1, reallocate a second memory space of a preset size, and identify the memory starting address of each of the objects in the second bitmap based on a mapping relationship; and continuously allocate memory in the second memory space according to the size of each object to be allocated memory in the first thread;
[0263] The processing unit is further configured to mark the memory start address of each of the objects in the second bitmap based on the mapping relationship.
[0264] According to one or more embodiments of the present disclosure, the processing unit is further configured to:
[0265] Determining an address of a pointer within a memory range pointing to any target object in the first memory space;
[0266] The metadata of the target object is determined according to the address of the pointer and the first bitmap, wherein the metadata includes at least one of a memory start address, a memory end address, and a memory size.
[0267] According to one or more embodiments of the present disclosure, when determining the metadata of the target object according to the address of the pointer and the first bitmap, the processing unit is configured to:
[0268] Determine the first bit corresponding to the address of the pointer in the first bitmap;
[0269] Searching the first bitmap toward the head for a second bit whose value is 1 and is closest to the first bit, determining a first offset between the first bit and the second bit, and determining a first memory offset between the memory start address of the target object and the address of the pointer based on the first offset and the preset number; determining the memory start address of the target object based on the address of the pointer and the first memory offset; and / or
[0270] Searching the first bitmap in the tail direction for a third bit whose value is 1 and is closest to the first bit, determining a second offset between the first bit and the third bit, and determining a second memory offset between the end start address of the target object and the address of the pointer based on the second offset and the preset number; determining the memory end address of the target object based on the address of the pointer and the second memory offset; and / or
[0271] The memory size of the target object is determined according to the memory start address of the target object and the memory end address of the target object.
[0272] According to one or more embodiments of the present disclosure, when determining the first bit corresponding to the address of the pointer in the first bitmap, the processing unit is configured to:
[0273] The byte where the first bit is located in the first bitmap is determined, and a first partial bitmap corresponding to the byte is read, and the first bit is determined in the first partial bitmap.
[0274] According to one or more embodiments of the present disclosure, the processing unit searches for a second bit whose value is 1 and is closest to the first bit in the first bitmap in a head direction, and determines a first offset between the first bit and the second bit, for:
[0275] determining a third offset of the first bit relative to the end of the first partial bitmap;
[0276] Determine whether the second bit exists in the first partial bitmap;
[0277] If it is determined that the second bit exists in the first partial bitmap, determining a fourth offset of the second bit relative to the end of the first partial bitmap; and determining a first offset between the first bit and the second bit based on the third offset and the fourth offset; or
[0278] If it is determined that the second bit does not exist in the first partial bitmap, the partial bitmaps corresponding to adjacent bytes are read in sequence until the second partial bitmap containing the second bit is found, and the fifth offset of the second bit relative to the end of the second partial bitmap is determined; the sixth offset of the second bit relative to the end of the first partial bitmap is determined based on the fifth offset; and the first offset between the first bit and the second bit is determined based on the third offset and the sixth offset.
[0279] According to one or more embodiments of the present disclosure, the processing unit searches for a third bit whose value is 1 and is closest to the first bit in the first bitmap in a tail direction, and determines a second offset between the first bit and the third bit, for:
[0280] Determining a seventh offset of the first bit relative to the first partial bitmap header;
[0281] Determine whether the third bit exists in the first partial bitmap;
[0282] If it is determined that the third bit exists in the first partial bitmap, determining an eighth offset of the third bit relative to the header of the first partial bitmap; and determining a second offset between the first bit and the third bit based on the seventh offset and the eighth offset; or
[0283] If it is determined that the third bit does not exist in the first partial bitmap, the partial bitmaps corresponding to adjacent bytes are read sequentially in the tail direction until the third partial bitmap containing the third bit is found, and a ninth offset of the third bit relative to the header of the third partial bitmap is determined; a tenth offset of the third bit relative to the header of the first partial bitmap is determined based on the ninth offset; and a second offset between the first bit and the third bit is determined based on the seventh offset and the tenth offset.
[0284] According to one or more embodiments of the present disclosure, after determining the first bit in the first partial bitmap, the processing unit is further configured to:
[0285] A first mask is constructed according to the position of the first bit in the first partial bitmap, the length of the first mask is the same as that of the first partial bitmap, the position corresponding to the first bit is 1, and the remaining positions are 0.
[0286] According to one or more embodiments of the present disclosure, when determining the third offset of the first bit relative to the end of the first partial bitmap, the processing unit is configured to:
[0287] The number of 0s in the first mask in a direction from 1 to the tail of the first mask is determined as the third offset.
[0288] According to one or more embodiments of the present disclosure, when determining whether the second bit exists in the first partial bitmap, the processing unit is configured to:
[0289] Constructing a second mask, where the length of the second mask is the same as that of the first partial bitmap, and the positions of the preset bits at the end of the second mask are 0, and the remaining positions are 1, and the preset number is the third offset;
[0290] Performing a bitwise AND operation on the second mask and the first partial bitmap, and if the operation result is not 0, determining that the second bit exists in the first partial bitmap; or if the operation result is 0, determining that the second bit does not exist in the first partial bitmap;
[0291] Accordingly, when determining the fourth offset of the second bit relative to the end of the first partial bitmap, the processing unit is configured to:
[0292] The number of 0s at the end of the operation result is determined as the fourth offset.
[0293] According to one or more embodiments of the present disclosure, when determining that the second bit exists in the second partial bitmap, the processing unit is configured to:
[0294] If it is determined that the second partial bitmap is not 0, determining that the second bit exists in the second partial bitmap;
[0295] Determine the number of zeros at the end of the second partial bitmap, and determine the number as the fifth offset;
[0296] Accordingly, when determining the sixth offset of the second bit relative to the end of the first partial bitmap according to the fifth offset, the processing unit is configured to:
[0297] Obtaining the number of bits between the tail of the second partial bitmap and the tail of the first partial bitmap;
[0298] The fifth offset is added to the number of bits to obtain the sixth offset.
[0299] According to one or more embodiments of the present disclosure, when determining the seventh offset of the first bit relative to the first partial bitmap header, the processing unit is configured to:
[0300] In the first mask, the number of 0s existing in the direction from 1 to the bottom of the first mask is determined as the seventh offset.
[0301] According to one or more embodiments of the present disclosure, when determining whether the third bit exists in the first partial bitmap, the processing unit is configured to:
[0302] Constructing a third mask, wherein the length of the third mask is the same as that of the first partial bitmap, and the positions of the preset bits in the third mask are 0, and the remaining positions are 1, and the preset number is the seventh offset plus 1;
[0303] Performing a bitwise AND operation on the third mask and the first partial bitmap, and if the operation result is not 0, determining that the third bit exists in the first partial bitmap; or if the operation result is 0, determining that the third bit does not exist in the first partial bitmap;
[0304] Accordingly, when determining the eighth offset of the third bit relative to the first partial bitmap header, the processing unit is configured to:
[0305] The number of 0s in the header of the operation result is determined as the eighth offset.
[0306] According to one or more embodiments of the present disclosure, when determining that the third bit exists in the third partial bitmap, the processing unit is configured to:
[0307] If it is determined that the third partial bitmap is not 0, then determining that the third bit exists in the third partial bitmap;
[0308] Determine the number of zeros in the header of the third partial bitmap, and determine the number as the ninth offset;
[0309] Accordingly, when determining the tenth offset of the third bit relative to the first partial bitmap header according to the ninth offset, the processing unit is configured to:
[0310] Obtaining the number of bits between the third partial bitmap header and the first partial bitmap header;
[0311] The ninth offset is added to the number of bits to obtain the tenth offset.
[0312] In a third aspect, according to one or more embodiments of the present disclosure, there is provided an electronic device, comprising: at least one processor and a memory;
[0313] The memory stores computer-executable instructions;
[0314] The at least one processor executes the computer-executable instructions stored in the memory, so that the at least one processor executes the memory allocation method described in the first aspect and various possible designs of the first aspect.
[0315] In a fourth aspect, according to one or more embodiments of the present disclosure, a computer-readable storage medium is provided, in which computer-executable instructions are stored. When a processor executes the computer-executable instructions, the memory allocation method described in the first aspect and various possible designs of the first aspect is implemented.
[0316] In a fifth aspect, according to one or more embodiments of the present disclosure, a computer program product is provided, comprising computer-executable instructions. When a processor executes the computer-executable instructions, the memory allocation method as described in the first aspect and various possible designs of the first aspect is implemented.
[0317] The above description is merely a preferred embodiment of the present disclosure and an illustration of the technical principles employed. Those skilled in the art should understand that the scope of disclosure involved in the present disclosure is not limited to the technical solutions formed by the specific combination of the above-mentioned technical features, but also encompasses other technical solutions formed by any combination of the above-mentioned technical features or their equivalents without departing from the above-mentioned disclosed concepts. For example, a technical solution formed by replacing the above-mentioned features with (but not limited to) technical features with similar functions disclosed in the present disclosure.
[0318] In addition, although each operation is described in a specific order, this should not be understood as requiring these operations to be performed in the specific order shown or in a sequential order. Under certain circumstances, multitasking and parallel processing may be advantageous. Similarly, although some specific implementation details have been included in the above discussion, these should not be interpreted as limiting the scope of the present disclosure. Some features described in the context of a separate embodiment can also be implemented in a single embodiment in combination. On the contrary, the various features described in the context of a single embodiment can also be implemented in multiple embodiments individually or in any suitable sub-combination mode.
[0319] Although the subject matter has been described in language specific to structural features and / or methodological logical acts, it should be understood that the subject matter defined in the appended claims is not necessarily limited to the specific features or acts described above. Rather, the specific features and acts described above are merely example forms of implementing the claims.
Claims
1. A memory allocation method, characterized in that: include: Allocating a first memory space of a preset size to a first thread to be allocated memory, and configuring a first bitmap having a mapping relationship with the first memory space; Allocate memory continuously in the first memory space according to the size of each object to be allocated memory in the first thread, and mark the memory start address of each object in the first bitmap based on the mapping relationship; The continuously allocating memory in the first memory space according to the size of each object to be allocated memory in the first thread includes: Determining whether the unused memory in the first memory space satisfies the size of the object; If so, allocating memory starting from the starting address of the unused memory according to the size of the object; The method further comprises: If not, setting the corresponding bit of the starting address of the unused memory in the first bitmap to 1, reallocating a second memory space of a preset size, and configuring a second bitmap that has a mapping relationship with the second memory space; and Memory is continuously allocated in the second memory space according to the size of each object to be allocated memory in the first thread, and the memory start address of each object is marked in the second bitmap based on the mapping relationship between the second memory space and the second bitmap.
2. The method according to claim 1, characterized in that The mapping relationship between the first memory space and the first bitmap is: each bit in the first bitmap corresponds to a preset number of bytes in the first memory space in sequence; Accordingly, continuously allocating memory in the first memory space according to the size of each object to be allocated memory in the first thread, and marking the memory start address of each object in the first bitmap based on the mapping relationship, includes: Clearing all bits of the first bitmap to zero; Continuously allocating memory in the first memory space according to the size of each object to be allocated memory in the first thread, and the memory size of each object is a multiple of the preset number; The bit corresponding to the memory start address of each object in the first bitmap is set to 1.
3. The method according to claim 2, characterized in that The method further comprises: Determining an address of a pointer within a memory range pointing to any target object in the first memory space; The metadata of the target object is determined according to the address of the pointer and the first bitmap, wherein the metadata includes at least one of a memory start address, a memory end address, and a memory size.
4. The method according to claim 3, characterized in that Determining metadata of the target object according to the address of the pointer and the first bitmap includes: Determine the first bit corresponding to the address of the pointer in the first bitmap; Searching the first bitmap for a second bit whose value is 1 and is closest to the first bit in the head direction, determining a first offset between the first bit and the second bit, and determining a first memory offset between the memory start address of the target object and the address of the pointer based on the first offset and the preset number; determining the memory start address of the target object based on the address of the pointer and the first memory offset; and / or Searching the first bitmap in the tail direction for a third bit whose value is 1 and is closest to the first bit, determining a second offset between the first bit and the third bit, and determining a second memory offset between the end start address of the target object and the address of the pointer based on the second offset and the preset number; determining the memory end address of the target object based on the address of the pointer and the second memory offset; and / or The memory size of the target object is determined according to the memory start address of the target object and the memory end address of the target object.
5. The method according to claim 4, characterized in that Determining the first bit corresponding to the address of the pointer in the first bitmap includes: The byte where the first bit is located in the first bitmap is determined, and a first partial bitmap corresponding to the byte is read, and the first bit is determined in the first partial bitmap.
6. The method according to claim 5, characterized in that The step of searching in the first bitmap in a head direction for a second bit whose value is 1 and which is closest to the first bit, and determining a first offset between the first bit and the second bit includes: determining a third offset of the first bit relative to the end of the first partial bitmap; Determine whether the second bit exists in the first partial bitmap; If it is determined that the second bit exists in the first partial bitmap, determining a fourth offset of the second bit relative to the end of the first partial bitmap; and determining a first offset between the first bit and the second bit based on the third offset and the fourth offset; or If it is determined that the second bit does not exist in the first partial bitmap, the partial bitmaps corresponding to adjacent bytes are read sequentially along the head direction until the second partial bitmap containing the second bit is found, and the fifth offset of the second bit relative to the end of the second partial bitmap is determined; the sixth offset of the second bit relative to the end of the first partial bitmap is determined based on the fifth offset; and the first offset between the first bit and the second bit is determined based on the third offset and the sixth offset.
7. The method according to claim 5, characterized in that Searching in the first bitmap in a tail direction for a third bit whose value is 1 and which is closest to the first bit, and determining a second offset between the first bit and the third bit, includes: Determining a seventh offset of the first bit relative to the first partial bitmap header; Determine whether the third bit exists in the first partial bitmap; If it is determined that the third bit exists in the first partial bitmap, determining an eighth offset of the third bit relative to the header of the first partial bitmap; and determining a second offset between the first bit and the third bit based on the seventh offset and the eighth offset; or If it is determined that the third bit does not exist in the first partial bitmap, the partial bitmaps corresponding to adjacent bytes are read sequentially in the tail direction until the third partial bitmap containing the third bit is found, and a ninth offset of the third bit relative to the header of the third partial bitmap is determined; a tenth offset of the third bit relative to the header of the first partial bitmap is determined based on the ninth offset; and a second offset between the first bit and the third bit is determined based on the seventh offset and the tenth offset.
8. The method according to claim 6, characterized in that After determining the first bit in the first partial bitmap, the method further includes: Constructing a first mask according to the position of the first bit in the first partial bitmap, where the length of the first mask is the same as that of the first partial bitmap, and the position corresponding to the first bit is 1, and the remaining positions are 0; Determining a third offset of the first bit relative to the end of the first partial bitmap includes: The number of 0s in the first mask in a direction from 1 to the tail of the first mask is determined as the third offset.
9. The method according to claim 8, characterized in that The determining whether the second bit exists in the first partial bitmap includes: Constructing a second mask, where the length of the second mask is the same as that of the first partial bitmap, and the positions of the preset bits at the end of the second mask are 0, and the remaining positions are 1, and the preset number is the third offset; Performing a bitwise AND operation on the second mask and the first partial bitmap, and if the operation result is not 0, determining that the second bit exists in the first partial bitmap; or if the operation result is 0, determining that the second bit does not exist in the first partial bitmap; Accordingly, determining a fourth offset of the second bit relative to the end of the first partial bitmap includes: The number of 0s at the end of the operation result is determined as the fourth offset.
10. The method according to claim 9, characterized in that Determining that the second bit exists in the second partial bitmap, and then determining a fifth offset of the second bit relative to the end of the second partial bitmap, includes: If it is determined that the second partial bitmap is not 0, determining that the second bit exists in the second partial bitmap; Determine the number of zeros at the end of the second partial bitmap, and determine the number as the fifth offset; Correspondingly, determining a sixth offset of the second bit relative to the end of the first partial bitmap according to the fifth offset includes: Obtaining the number of bits between the tail of the second partial bitmap and the tail of the first partial bitmap; The fifth offset is added to the number of bits to obtain the sixth offset.
11. The method according to claim 7, characterized in that After determining the first bit in the first partial bitmap, the method further includes: Constructing a first mask according to the position of the first bit in the first partial bitmap, where the length of the first mask is the same as that of the first partial bitmap, and the position corresponding to the first bit is 1, and the remaining positions are 0; Determining a seventh offset of the first bit relative to the first partial bitmap header includes: In the first mask, the number of 0s existing in the direction from 1 to the bottom of the first mask is determined as the seventh offset.
12. The method according to claim 11, characterized in that The determining whether the third bit exists in the first partial bitmap includes: Constructing a third mask, wherein the length of the third mask is the same as that of the first partial bitmap, and the positions of the preset bits in the third mask are 0, and the remaining positions are 1, and the preset number is the seventh offset plus 1; Performing a bitwise AND operation on the third mask and the first partial bitmap, and if the operation result is not 0, determining that the third bit exists in the first partial bitmap; or if the operation result is 0, determining that the third bit does not exist in the first partial bitmap; Accordingly, determining an eighth offset of the third bit relative to the first partial bitmap header includes: The number of 0s in the header of the operation result is determined as the eighth offset.
13. The method according to claim 12, characterized in that Determining that the third bit exists in the third partial bitmap, and then determining a ninth offset of the third bit relative to the header of the third partial bitmap, includes: If it is determined that the third partial bitmap is not 0, then determining that the third bit exists in the third partial bitmap; Determine the number of zeros in the header of the third partial bitmap, and determine the number as the ninth offset; Accordingly, determining a tenth offset of the third bit relative to the first partial bitmap header according to the ninth offset includes: Obtaining the number of bits between the third partial bitmap header and the first partial bitmap header; The ninth offset is added to the number of bits to obtain the tenth offset.
14. A memory allocation device, characterized in that: include: an allocating unit, allocating a first memory space of a preset size to a first thread to be allocated memory, and configuring a first bitmap having a mapping relationship with the first memory space; Allocating memory continuously in the first memory space according to the size of each object to be allocated memory in the first thread; A processing unit, configured to mark the memory start address of each of the objects in the first bitmap based on the mapping relationship; The allocating unit is further configured to determine whether the unused memory in the first memory space satisfies the size of the object; if so, allocate memory starting from the starting address of the unused memory according to the size of the object; in, The allocating unit is further configured to, if not, set the corresponding bit in the first bitmap of the starting address of the unused memory to 1, reallocate a second memory space of a preset size, and configure a second bitmap that has a mapping relationship with the second memory space; and continuously allocate memory in the second memory space according to the size of each object to be allocated memory in the first thread; The processing unit is further configured to mark the memory start address of each of the objects in the second bitmap based on a mapping relationship between the second memory space and the second bitmap.
15. An electronic device, characterized in that: include: at least one processor and memory; The memory stores computer-executable instructions; The at least one processor executes the computer-executable instructions stored in the memory, so that the at least one processor performs the method according to any one of claims 1 to 13.
16. A computer-readable storage medium, characterized in that The computer-readable storage medium stores computer-executable instructions, and when a processor executes the computer-executable instructions, the method according to any one of claims 1 to 13 is implemented.
17. A computer program product, characterized in that The method comprises computer-executable instructions, and when a processor executes the computer-executable instructions, the method according to any one of claims 1 to 13 is implemented.
Citation Information
Patent Citations
Heap memory management method and device, readable storage medium and electronic equipment
CN112667152A