Data read-write device and method for dynamic efficient buffer, electronic equipment and medium
By dynamically managing virtual address and block attributes, the problems of improper space allocation and low efficiency of traditional ring cache areas are solved, efficient memory utilization and read and write operations are achieved, and program logic is simplified.
Patent Information
- Application Number
- CN202411155575.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-21
- Publication Date
- 2024-12-17
AI Technical Summary
Traditional ring cache areas need to allocate fixed-sized storage space in advance, resulting in wasted or insufficient space. It also requires frequent judgment on whether it reaches the tail when reading and writing data, which affects efficiency.
Dynamic efficient buffers are adopted, and the mapping mechanism of virtual addresses and block addresses is used to flexibly manage block attributes according to data status, avoid the limitation of static allocation, and realize the allocation of memory on demand, reduce bit operations, and improve efficiency.
Effectively utilize memory space, avoid space waste and overflow problems, improve data reading and writing efficiency and system performance, and simplify the reading and writing logic management of the main program.
Smart Images

Figure CN119149451A_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the technical field of data reading and writing, and in particular to a data reading and writing device, method, electronic equipment and medium for a dynamic and efficient buffer. Background Art
[0002] The ring buffer area is a very important data structure in embedded systems. It is a buffer with a fixed size. The storage area of the ring buffer area is a linear memory. The software processes this memory into a logically connected ring storage area. When data needs to be stored or read, it is necessary to determine whether the data has reached the end of the linear memory through software every time. When it reaches the end of the linear memory, the data is rolled back to the head of the linear memory.
[0003] In the related art, a fixed-size storage space needs to be allocated in advance for the circular cache area. However, if too large a storage space is requested, it will waste storage space, while if too small a storage space is requested, the cache area will not be sufficient and may cause data overflow and program errors. Allocating storage space is entirely based on experience and there is no fixed method. Summary of the invention
[0004] In order to overcome the problems existing in the related art, the present disclosure provides a data reading and writing device, method, electronic device and medium of a dynamic and efficient buffer. The technical solution of the present disclosure is as follows:
[0005] According to a first aspect of an embodiment of the present disclosure, a data reading and writing device of a dynamic and efficient buffer is provided, comprising:
[0006] A ring buffer area, including a virtual address composed of multiple block addresses;
[0007] a memory file, associated with the ring buffer area, for storing data written to the block address;
[0008] An exception handling function, used to set block attributes of the plurality of block addresses, the block attributes including: a write mode, a read mode, and a non-readable and non-writable mode;
[0009] When data is read or written, a data read operation is performed according to the block attribute of the block address; or a page file mapped by the block address is determined from the memory file, and a data write operation is performed.
[0010] Optionally include:
[0011] The exception handling function is further used to determine the page file mapped by the next block address from the memory file when the write address of the data write operation exceeds the block address, modify the block attribute of the block address to a read mode; and modify the block attribute of the next block address to a write mode;
[0012] The exception handling function is further used to determine the read length information of the data read operation and determine whether to change the block attribute of the block address when the data read operation reads a block address whose block attribute is a write mode.
[0013] Optionally, it also includes:
[0014] A read pointer, used to point to a readable data position in a virtual address corresponding to the ring buffer area;
[0015] A write pointer, used to point to a writable data location in a virtual address corresponding to the ring buffer area;
[0016] The data is stored in the virtual address corresponding to the annular buffer area by moving the write pointer and waits to be read; the data is read out of the virtual address corresponding to the annular buffer area by moving the read pointer and the annular buffer area is released.
[0017] Optionally include:
[0018] The exception handling function is also used to roll back the corresponding pointer to the beginning of the virtual address when the read pointer is at the very end of the virtual address and / or when the write pointer is at the very end of the virtual address; the beginning of the virtual address is represented by: the beginning of the first block address that constitutes the virtual address.
[0019] According to a second aspect of an embodiment of the present disclosure, a method for reading and writing data in a dynamic and efficient buffer is provided, comprising:
[0020] Acquiring data status, the data status including: reading data and writing data;
[0021] When the data state is data reading, performing a data reading operation according to a block attribute of the block address;
[0022] When the data state is data writing, a page file of block address mapping is determined from the memory file, and a data writing operation is performed.
[0023] Optionally, the block attribute includes a write mode, a read mode and a non-readable and non-writable mode, and when the data state is read data, performing a data read operation according to the block attribute of the block address includes:
[0024] In the case where the data reading operation reads a block address whose block attribute is a read mode, directly reading the data in the block address;
[0025] In the case where the data read operation reads a block address whose block attribute is a write mode, read length information of the data read operation is determined; and according to the read length information, an execution action of the data read operation is determined.
[0026] Optionally, determining an execution action of the data reading operation according to the read length information includes:
[0027] In the case where the read length information of the data read operation is less than the data length in the block address, modifying the block attribute of the block address to a read mode; reading data from the block address; and after the data read operation is completed, modifying the block attribute of the block address to a write-back mode;
[0028] In a case where the read length of the data read operation is greater than the data length in the block address, the data read operation on the block address is canceled.
[0029] Optionally, in a case where the read length of the data read operation is greater than the data length in the block address, the method further includes:
[0030] Putting the data reading operation into a waiting state;
[0031] In the case where the data length in the block address is greater than the read length of the data read operation, the data read operation is awakened and the block attribute of the block address is modified to a read mode, and the data read operation reads data from the block address; after the data read is completed, the block attribute of the block address is modified back to a write mode.
[0032] Optionally, reading the data in the block address further includes:
[0033] When the data reading operation reads to the very end of the block address, the page file mapped by the block address is released.
[0034] Optionally, the data writing operation is performed sequentially according to the order of the block addresses, and when the data state is writing data, a page file mapped by the block address is determined from the memory file, including:
[0035] The block address currently performing the data write operation is determined as the first block address, and when the first block address is not mapped to a page file, the page file mapped to the first block address is determined from the memory file; the block attribute of the first block address is determined as a write mode; the write mode indicates that: the block address only allows data to be written, and the block address is not fully written;
[0036] Performing a data write operation on the first block address;
[0037] In the case where the write address of the data write operation exceeds the first block address, the next block address adjacent to the first block address is determined as the second block address, and the page file mapped by the second block address is determined from the memory file; the block attribute of the second block address is determined as a write mode, and the block attribute of the first block address is determined as a read mode; the read mode indicates that: the block address is full and the data read operation is allowed;
[0038] A data write operation is performed on the second block address.
[0039] Optionally, each block address forms a virtual address in address order, and also includes:
[0040] When the data read operation reads to the very end of the virtual address, or when the data write operation writes to the very end of the virtual address, the address is rolled back to the beginning of the virtual address, and the data write operation or data read operation is performed starting from the beginning of the first block address in the virtual address.
[0041] According to a third aspect of an embodiment of the present disclosure, there is provided an electronic device, comprising: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the computer program is executed by the processor, the steps of the data reading and writing method of the dynamic and efficient buffer as described in the second aspect are implemented.
[0042] According to a fourth aspect of an embodiment of the present disclosure, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the steps of the data reading and writing method of the dynamic and efficient buffer as described in the second aspect are implemented.
[0043] According to a fifth aspect of an embodiment of the present disclosure, a computer program product is provided, comprising a computer program, which, when executed by a processor, implements the steps of the method for reading and writing data in a dynamic and efficient buffer as described in the second aspect.
[0044] The present invention uses a virtual address composed of multiple block addresses as a circular buffer area, and flexibly processes data read and write operations according to actual data needs and states by setting block attributes of the block address. According to the block attributes of the block address, corresponding data read operations or data write operations are performed, and there is no need to judge whether the data reaches the end of the circular buffer area every time; by determining the circular buffer area as a virtual address and associating the virtual address with a memory file, when executing a data write operation, a page file is determined from the memory file to map the block address, so that the memory space can be effectively used; the block attribute of the block address is changed through an exception handling function, so as to realize the management of the block address read state, without considering the read and write rollback problem, so that the main program does not need to process additional read and write logic. BRIEF DESCRIPTION OF THE DRAWINGS
[0045] In order to more clearly illustrate the technical solutions of the embodiments of the present disclosure, the drawings required for use in the description of the embodiments of the present disclosure will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present disclosure. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0046] Figure 1 is a schematic diagram of a traditional ring buffer area shown in an embodiment of the present disclosure;
[0047] Figure 2 is a schematic diagram of a data reading and writing device for a dynamic and efficient buffer area shown in an embodiment of the present disclosure;
[0048] Figure 3 It is a schematic diagram of the steps of a method for reading and writing data in a dynamic and efficient buffer area shown in an embodiment of the present disclosure;
[0049] Figure 4 is a schematic diagram of system initialization steps shown in an embodiment of the present disclosure;
[0050] Figure 5 is a schematic diagram of a flow chart of a data read buffer area shown in an embodiment of the present disclosure;
[0051] Figure 6 is a schematic diagram of a process of writing data into a buffer zone shown in an embodiment of the present disclosure;
[0052] Figure 7 is a schematic diagram of a data rollback process shown in an embodiment of the present disclosure;
[0053] Figure 8 It is a schematic diagram of an electronic device shown in an embodiment of the present disclosure. DETAILED DESCRIPTION
[0054] The following will be combined with the drawings in the embodiments of the present disclosure to clearly and completely describe the technical solutions in the embodiments of the present disclosure. Obviously, the described embodiments are part of the embodiments of the present disclosure, rather than all of the embodiments. Based on the embodiments in the present disclosure, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present disclosure.
[0055] The terms "first", "second", etc. in the specification and claims of the present disclosure are used to distinguish similar objects, and are not used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable when appropriate, so that the embodiments of the present disclosure can be implemented in an order other than those illustrated or described herein, and the objects distinguished by "first", "second", etc. are generally of the same type, and the number of objects is not limited. For example, the first object can be one or more. In addition, "and / or" in the specification and claims represents at least one of the connected objects, and the character " / " generally indicates that the objects associated with each other are in an "or" relationship.
[0056] The circular cache area is a very important data structure in embedded systems. It is a buffer with a fixed size. The circular cache area is often used in scenarios where continuous reading and writing of data is required. It has the following important features: 1 Buffering data: The circular cache area can temporarily store data, waiting for further processing or transmission. 2 Implementing a pipeline mechanism: In some scenarios, data needs to go through multiple processing stages, and each stage requires a certain amount of time to process the data. 3 Smoothing data flow: The circular cache area can smooth data flow and avoid sudden data blockage or overflow caused by inconsistent reading and writing speeds. 4 Relieve system load: The circular cache area can balance the system load and disperse instantaneous peak data processing work into different time periods.
[0057] The circular buffer area is actually an application of a queue, that is, a continuous memory. The software processes this memory into a logically connected circular storage area. The circular buffer has two pointers, one pointing to the queue head and the other pointing to the queue tail. The pointer pointing to the queue head is the readable data location of the circular buffer area, and the pointer pointing to the queue tail is the writable data location of the circular buffer area. By moving the write pointer, the data is temporarily stored in the circular buffer area waiting to be read. By moving the read pointer, the data is read out of the circular buffer area and the buffer space is released. In addition, by comparing the relationship between the head pointer and the tail pointer, it is determined whether there is data to read in the circular buffer area or whether there is space to write in the circular buffer area. This design method is used to achieve the reuse of the circular buffer area.
[0058] Figure 1 FIG. 1 is a schematic diagram of a traditional ring buffer area shown in an embodiment of the present disclosure. Figure 1As shown, in the related art, the ring buffer area is a pure software structure, which is a linear memory with no connection between the head and the tail. When data needs to be stored, it is necessary to determine whether the data has reached the tail of the linear memory by software. When the data reaches the tail of the linear memory, the data is rolled back to the head of the linear memory; when data needs to be read, it is also necessary to determine whether the tail of the linear memory has been read. When the tail is reached, the software is also required to roll the data back to the head of the linear memory, so as to realize the logical ring area by software. However, there are some disadvantages in the traditional ring buffer area. The first point is that the ring buffer area needs to allocate a fixed size of storage space in advance. If too much storage space is applied, it will cause a waste of storage space. If too little storage space is applied, it will cause insufficient storage space and cause data overflow program errors. The allocation of storage space in the ring buffer area is completely determined based on experience. The second point is that the traditional ring buffer area will determine whether it exceeds the maximum address of the ring buffer area every time the read pointer or write pointer is moved. If it reaches the tail, it will roll back to the head of the ring buffer area. This will perform a large number of comparison operations and bit operations, which is not suitable for scenarios requiring high speed and efficiency. The third point is that when writing data to the ring buffer area, it is necessary to confirm each time whether there is enough space to accommodate the data.
[0059] In order to solve the above problems, the present disclosure proposes a data reading and writing device and method for a dynamic and efficient buffer, wherein the device can dynamically manage the consumption of physical memory without performing excessive bit operations to affect efficiency.
[0060] Figure 2 Schematic diagram of a data reading and writing device for a dynamic and efficient buffer zone shown in an embodiment of the present disclosure. Figure 2 As shown, the device comprises:
[0061] The ring buffer area 210 includes a virtual address composed of a plurality of block addresses;
[0062] Each block is the basic unit in the ring cache area for storing data. The size of the block can be adjusted according to the application scenario and performance requirements. The ring cache area manages blocks through a virtual address system. These virtual addresses are logically consecutive numbers and are associated with actual storage locations in physical memory or on disk through a mapping mechanism.
[0063] A memory file 220, associated with the ring buffer area, for storing data written to the block address;
[0064] Set the ring cache area to a virtual address composed of multiple block addresses, and apply for a memory file to associate with the virtual address corresponding to the ring cache area. The size of the memory file is not necessarily consistent with the size of the virtual address, and the size of the memory file can be smaller than the size of the virtual address. The memory file is used to actually store data. When data is stored in the ring cache area, it is actually stored in the memory file corresponding to the ring cache area. When data is read from the ring cache area, please rest assured that the corresponding data is actually read from the memory file corresponding to the ring cache area. The memory file can be further divided into multiple pages, and the pages can be mapped to the block addresses in the ring cache area.
[0065] The exception handling function 230 is used to set the block attributes of the plurality of block addresses, wherein the block attributes include: a write mode, a read mode, and a non-readable and non-writable mode;
[0066] The ring buffer area is divided into multiple block addresses, each of which has the same size. The corresponding block attributes are set through the exception handling function. The block attributes include write mode, read mode, and non-readable and non-writable mode. When the block attribute is in write mode, the block address only allows data write operations. When the block attribute is in read mode, the block address only allows data read operations. When the block attribute is in non-readable and non-writable mode, the block address does not allow data read operations or data write operations. When the block attribute of the block address needs to be changed, the exception handling function will be triggered, and the block attribute of the block address will be changed through the exception handling function.
[0067] When data is read or written, a data read operation is performed according to the block attribute of the block address; or a page file mapped by the block address is determined from the memory file, and a data write operation is performed.
[0068] When performing data reading and writing, the corresponding operation is performed according to the block attribute of the block address. When the block address is in read mode, data reading operations are allowed to be performed on the block address; when the block address is in write mode, data writing operations are allowed to be performed on the block address, and the data is written to the page file mapped by the block address. Block addresses are not always mapped to page files. Page files are part of memory files. When a block address needs to store data, a page address is determined from the memory file. The size of the page file corresponds to the size of the block address. This page address is mapped to the block address. When reading and writing operations are performed on the block address, read and write operations are actually performed on the page file mapped by the block address. The block address is a virtual address.
[0069] By adopting the embodiments of the present disclosure, the present disclosure dynamically manages memory usage through virtual addresses and block addresses, avoids the limitation of static allocation, allocates on demand, does not waste, and will not cause program errors due to insufficient fixed allocation. It applies for a larger virtual address but only consumes a small amount of actual physical memory; the exception handling function can set and adjust the read and write permissions of the block according to the attributes of the block. During the data reading and writing process, there is no need to consider the read and write rollback problem, so that the main program does not need to process additional read and write logic.
[0070] Among them, in an optional embodiment, the exception handling function is also used to determine the page file mapped by the next block address from the memory file when the write address of the data write operation exceeds the block address, and modify the block attribute of the block address to read mode; and modify the block attribute of the next block address to write mode; the exception handling function is also used to determine the read length information of the data read operation and determine whether to change the block attribute of the block address when the data read operation reads a block address with a block attribute in write mode.
[0071] The data write operation in the ring buffer area is executed in sequence according to the order of the block addresses. The block addresses are arranged in sequence in the ring buffer area. The corresponding data write operation will be executed on the next block address only after the storage space corresponding to the previous block address is exhausted.
[0072] Initialize the ring buffer area. During initialization, determine the first block address of the virtual area corresponding to the ring buffer area as the target block address, determine a page file from the memory file, map the target block address to the page file, and determine the block attribute of the target block address as write mode. Perform a data write operation on the block address in write mode. When the write address of the data write operation exceeds the capacity of the current block address, trigger the exception handling function. The exception handling function determines the page file from the memory file for the next block address, maps the next block address to the new page file, modifies the block attribute of the next block address to write mode, and modifies the block attribute of the current block address, that is, the block address whose write address is out of range, to read mode. This allows the data write operation to be performed normally even when the capacity of a block address is insufficient.
[0073] It is allowed to perform data read operations on block addresses with the block attribute in read mode. When performing data read operations on block addresses with the block attribute in read mode, the exception handling function will not be triggered. When the data read operation continues to read data and reads to the block address with the block attribute in write mode, the exception handling function will be triggered. The exception handling function will determine the request length of the data read operation, that is, how much data is expected to be read; based on the read length and the current content status of the write mode block address, the exception handling function will decide whether the block attributes need to be changed.
[0074] By adopting the embodiments of the present disclosure, by dynamically adjusting block attributes in write and read operations, it is possible to ensure that data is not accidentally modified during the write process, while ensuring that the read operation obtains the latest and consistent data. Through block attributes, the efficiency of data read and write operations can be improved, and by quickly locating the next block address, the overall performance of the file system can be improved. In a concurrent environment, this mechanism can ensure that multiple write and read operations can share resources safely and effectively, reducing conflicts and waiting time.
[0075] Among them, in an optional embodiment, a read pointer is used to point to a readable data position in a virtual address corresponding to the annular cache area; a write pointer is used to point to a writable data position in a virtual address corresponding to the annular cache area; by moving the write pointer, data is stored in the virtual address corresponding to the annular cache area and waits to be read; by moving the read pointer, data is read out of the virtual address corresponding to the annular cache area and the annular cache area is released.
[0076] Also includes read pointer and write pointer.
[0077] The read pointer is an index or pointer that identifies the location of the next data to be read in the ring buffer. It points to the first byte of data in the cache that is currently readable (that is, has been written but not yet read). When data is read from the cache, the read pointer moves forward by the number of bytes read. Once the data is read, the location is considered free and can be used for subsequent write operations.
[0078] The write pointer is an index or pointer that identifies the location in the ring buffer where data will be written next. It points to the first byte of data in the cache that is currently writable (i.e., has not yet been written). As data is written to the cache, the write pointer moves forward by the number of bytes written. Once data is written, the location is considered occupied until it is read and released by the read pointer.
[0079] By adopting the embodiments of the present disclosure, through the read pointer and the write pointer, data read and write operations can be performed quickly without conflict between the read and write pointers, so that the circular buffer area meets the application scenarios of fast response and high throughput, such as real-time data processing and network communication. By controlling the access rights and movement timing of the read and write pointers, the consistency and integrity of the data can be ensured, and the concurrency performance can be improved. By moving the read pointer, the data is read and the space in the circular buffer area is released, avoiding the problem of having to clean up or reallocate memory after the data is read, thereby improving the efficiency of memory use.
[0080] In one optional embodiment, the exception handling function is also used to roll back the corresponding pointer to the beginning of the virtual address when the read pointer is at the very end of the virtual address and / or when the write pointer is at the very end of the virtual address; the starting representation of the virtual address is: the beginning of the first block address that constitutes the virtual address.
[0081] When the read pointer reaches the end of the virtual address, the exception handling function will be triggered, and the exception handling function will roll back the read pointer to the beginning of the virtual address, forming a ring structure; when the write pointer reaches the end of the cache, the exception handling function will also be triggered, and the exception handling function will roll back the write pointer to the beginning of the virtual address; among them, a certain safety distance must be maintained between the write pointer and the read pointer to prevent the write operation from overwriting the data that has not been read. The beginning of the virtual address is also the beginning of the first block address that constitutes the virtual address.
[0082] By adopting the embodiments of the present disclosure, the system crash or data corruption caused by the pointer crossing the boundary is avoided by automatically rolling back when the pointer reaches the end. Allowing the pointer to be recycled can more effectively utilize the virtual address space and avoid frequent reallocation and release of memory resources. In some high-performance application scenarios, by optimizing the pointer management and exception handling process, unnecessary context switching and interrupt processing time can be reduced, thereby improving the overall performance of the system.
[0083] The present disclosure provides a data reading and writing method for a dynamic and efficient buffer zone, which is applied to a data reading and writing device for the dynamic and efficient buffer zone. Figure 3 1 is a schematic diagram of the steps of a method for reading and writing data in a dynamic and efficient buffer according to an embodiment of the present disclosure. Figure 3 As shown, the data reading and writing method of the dynamic and efficient buffer may specifically include the following steps:
[0084] Step S31: Acquire data status, where the data status includes: read data and write data.
[0085] A data processing request is received, where the data processing request indicates whether to read data or write data; and the data processing request is parsed to obtain a data status.
[0086] Before executing a corresponding data writing operation or data reading operation according to a data processing request, it may be first checked whether the requested block address is valid.
[0087] Step S32: When the data state is data reading, a data reading operation is performed according to the block attribute of the block address.
[0088] According to the block attribute of the block address, the data read operation is performed; according to the block attribute of the block address, there are three cases. In the first case, the data read operation is performed normally; in the second case, the data read operation returns; in the third case, the data read operation waits and performs the data read operation after waking up.
[0089] Step S33: when the data state is data writing, determine the page file mapped by the block address from the memory file, and perform a data writing operation.
[0090] When performing a data write operation on the virtual address corresponding to the ring buffer area, data is written in units of block addresses. The block address needs to have its own mapped page file, that is, the actual storage address. The page file is determined from the memory file and is part of the memory file. When the block address is mapped to the corresponding page file, the data write operation is performed.
[0091] By using the embodiments of the present disclosure, the efficiency of data processing can be significantly improved by distinguishing read and write operations and optimizing them separately. Through sophisticated block management and page mapping strategies, the space of storage devices can be used more efficiently and space waste can be reduced. Separating read and write operations and designing them modularly makes it easier to expand and modify the system according to needs. At the same time, clear process definitions also help reduce system complexity and maintenance costs.
[0092] Figure 4 Schematic diagram of system initialization steps shown in the embodiment of the present disclosure. Figure 4As shown, in order to implement the steps of the data reading and writing method of the above-mentioned dynamic and efficient buffer, the circular cache area can be initialized first, and the exception handling function is first set, which is used to manage the read and write operations; then a memory file is created, and the memory file is used to store data, and the size of the memory file is a multiple of the entire page; apply for a larger virtual address, determine this virtual address as the circular cache area, and associate the virtual address with the memory file; set the read pointer and write pointer, the read pointer is used to indicate the readable position of the data, and the write pointer is used to indicate the writable position of the data, and the read and write pointers are respectively pointed to the starting position of the virtual address; divide the virtual address into N block addresses of the same size, set the block attribute of the first block address to write mode, and the block attributes of other block addresses to non-readable and non-writable modes. Through Figure 4 The schematic diagram of the initialization steps shown in the figure completes the configuration of the ring buffer area.
[0093] Among them, in an optional embodiment, the block attributes include a write mode, a read mode and a non-readable and non-writable mode. When the data state is to read data, a data read operation is performed according to the block attributes of the block address, including: when the data read operation reads a block address whose block attribute is a read mode, directly reading the data in the block address; when the data read operation reads a block address whose block attribute is a write mode, determining the read length information of the data read operation; and determining the execution action of the data read operation based on the read length information.
[0094] Each block address has its own corresponding block mode, including write mode, read mode and non-readable and non-writable mode. When the block address is in write mode, it means that the storage space corresponding to the current block address still has room, and data write operations can be performed. When the block address is in write mode, data read operations are not allowed, and only data write operations are allowed. When the block address is in read mode, it means that the storage space corresponding to the current block address is full of data, and data write operations cannot be performed, and only data read operations can be performed. When the block address is in non-readable and non-writable mode, the block address has not yet mapped the corresponding page file, and the block address of the virtual address cannot perform data write operations or data read and write operations.
[0095] The data read operation is performed in sequence according to the block address in the circular buffer area. When reading, the readable position in the circular buffer area is marked by the read pointer, and the read pointer moves forward according to the number of bytes read.
[0096] For the block address in read mode, the data read operation is directly performed on the block address without additional inspection or processing. For the block address in write mode, the read length information of the data read operation needs to be determined. The read length information is the size of the data that needs to be read by the data read operation. According to the read length information, different operations will be performed to process the data read operation.
[0097] In some cases, the data to be acquired by a data read operation may be stored in multiple block addresses, and the data read operation can be segmented and the corresponding data read operation can be performed according to the attributes of the corresponding block addresses. For example, when the data to be read by a data read operation is located in block address A and block address B, block address A is in read mode, block address B is in write mode, and block address A is read first, then after the data in block address A is read normally, the data read operation for the data in block address B is determined according to the remaining data length information to be read by the data read operation.
[0098] By using the embodiments of the present disclosure, by distinguishing different modes of blocks, the system can flexibly process read requests according to actual conditions, thereby improving the flexibility and efficiency of data reading. For block addresses in write mode, by determining read length information and performing corresponding read actions, it can be ensured that the data read during the data writing process is accurate and consistent. By considering different modes and states of blocks during the reading process, the system can handle various abnormal situations more robustly and improve the stability and reliability of the system. By clarifying the reading strategies under different modes, the error handling process can be simplified, reducing the risk of system crashes or data corruption caused by read errors.
[0099] Among them, in an optional embodiment, determining the execution action of the data read operation based on the read length information includes: when the read length information of the data read operation is less than the data length in the block address, modifying the block attribute of the block address to a read mode; reading data from the block address; after the data read operation is completed, modifying the block attribute of the block address to a write mode; when the read length of the data read operation is greater than the data length in the block address, canceling the data read operation on the block address.
[0100] For a block address in write mode, when performing a data read operation on the block address, it is necessary to determine the read length information of the data read operation through an exception handling function, that is, the size of data that the data read operation needs to read at the block address.
[0101] Determine the length of the data stored in the block address. Compare the read length with the length of the data in the block to determine the subsequent operation.
[0102] If the read length is less than the data length in the block, that is, the data stored in the block address is sufficient for the data read operation, then the exception handling function modifies the block attribute of the block address, changes the original write mode to the read mode, so that the block address allows data read operations and ensures that the data will not be accidentally modified during the reading process. After the block attribute of the block address is changed to the read mode, the corresponding data read operation is performed on the block address. After the data reading is completed, the system changes the block attribute of the block address back to the original write mode for subsequent possible write operations.
[0103] If the read length is greater than the data length in the block, that is, the block address does not store enough data for the data read operation to be performed, in this case, the data read operation for the block address is canceled.
[0104] By adopting the embodiments of the present disclosure, unnecessary data reading is avoided by dynamically adjusting the reading behavior, and the precise control of the reading operation can reduce the occupation of system resources. Modifying the block attribute to the read mode before reading the data can prevent the data from being accidentally modified during the reading process, thereby enhancing the security of the data. By handling the situation where the read length is greater than the data length, system errors or crashes caused by attempting to read non-existent data are avoided, thereby enhancing the stability and robustness of the system.
[0105] Among them, in an optional embodiment, when the read length of the data read operation is greater than the data length in the block address, it also includes: putting the data read operation in a waiting state; when the data length in the block address is greater than the read length of the data read operation, waking up the data read operation and modifying the block attribute of the block address to a read mode, and the data read operation reads data from the block address; after the data reading is completed, modifying the block attribute of the block address to a write-back mode.
[0106] In the case where the read length of the data read operation is greater than the data length in the block address, another processing method for the data read operation may be selected.
[0107] When the read length of the data read operation is greater than the data length in the block address, the data processing operation is put into a waiting state and temporarily not executed. Specifically, the request corresponding to the data read operation can be put into a waiting queue or the related process / thread can be suspended.
[0108] The data length information in the block address can be continuously monitored, which can be achieved through polling, event notification or interruption.
[0109] When it is monitored that the data length in the block address is greater than the read length of the data read operation, that is, when the data in the block address satisfies the data to be read by the data read operation, the data read operation in the waiting state is awakened, and before or after the awakening of the data read operation, the exception handling function modifies the block attribute of the block address from the write mode to the read mode to ensure that the data will not be modified during the reading process. After the block attribute of the block address is modified to the read mode, the data read operation is performed on the block address.
[0110] After the data is read, the system changes the block attribute of the block address back to the original write mode to facilitate subsequent possible write operations.
[0111] By adopting the embodiments of the present disclosure, the system ensures the consistency and integrity of the data by waking up the read operation only when the data is long enough and setting the block attributes to read mode during the reading process. Invalid read attempts for non-existent or unready data are avoided, thereby reducing unnecessary resource consumption. When requesting data, there is no need to wait for the entire block to be fully written, but the required part can be obtained immediately when the data is ready, thereby improving the response speed and user experience. By managing the waiting and waking up of the read operation, concurrent requests and data conflicts can be better handled, and the stability and reliability of the system are enhanced. This mechanism allows the system to dynamically adjust behavior based on the actual availability of data and reading requirements, providing higher flexibility and adaptability.
[0112] In the case where the read length of the data read operation is greater than the data length in the block address, whether the data read operation is in a waiting mode or directly canceled is pre-set. If the data read operation is canceled when the read length is greater than the data length, then all data read operations of the current device will be canceled in this case. As long as the pre-set is not modified, then in the case where the read length is greater than the data length, the processing of the data read operation will not be modified.
[0113] Wherein, in an optional embodiment, reading the data in the block address further includes: when the data reading operation reads to the very end of the block address, releasing the page file mapped by the block address.
[0114] Perform a data read operation on the block address. When the data in the ring buffer area is read, there is a read pointer in the ring buffer area that moves forward according to the number of bytes read. When the data read operation reads to the end of the block address, that is, the read pointer is at the end of the block address, the resources of the block address are released. This includes the page file mapped by the block address, and the page file is also released. The page file is in the initialized state in the memory file. When needed, the page file is mapped to the block address. The block address mapped by the page file can be different.
[0115] By adopting the embodiments of the present disclosure, by timely releasing the page file mapping that is no longer needed, the waste of memory or disk space can be reduced, and the overall utilization of system resources can be improved. The performance bottleneck caused by invalid or outdated page files occupying resources can be reduced. Through a sophisticated resource management strategy, the robustness and reliability of the system can be improved, and the clear resource release logic helps to simplify the maintenance and management of the system.
[0116] Figure 5 FIG. 1 is a flow chart of a data read buffer according to an embodiment of the present disclosure. Figure 5 As shown, data is read from the circular buffer area; it is determined whether a full block address is read; in the case of reading a full block address, a data read operation is directly performed on the block address; during the reading process, it is determined whether the size exceeds a block address; in the case of not exceeding the size of a block address, the data read operation is completed; in the case of exceeding the size of a block address, the resources of the current block address, including the mapped page file, etc., are released; the next block address is read; in the case of performing a data read operation on the next block address, it is determined again whether a full block address is read; in the case of not reading a full block address, an exception handling function is triggered to determine whether there is enough data to read in the block address; in the case of having enough data to read In the case of a read operation, the block attribute of the block address is modified to the read mode, and the corresponding data is read from the block address. After the data is read, the block attribute of the block address is modified back to the original block mode; find the return point of the read function and put the read data into the stack; clear the exception flag, balance the data stack, and jump directly to the read process read function return point, and the read function returns; in the case of insufficient data to be read, determine whether the data read operation is to wait; in the case of no waiting, find the return point of the read function, put the exception status into the corresponding stack, and the read function returns; clear the exception flag, balance the data stack, and jump directly to the read process read function return point, and the read function returns; in the case of waiting, block and wait for the data read operation to wake up, and the read function returns.
[0117] Among them, in an optional embodiment, the data write operation is performed in sequence according to the order of the block addresses, and when the data state is writing data, the page file mapped by the block address is determined from the memory file, including: determining the block address of the current data write operation as the first block address, and when the first block address is not mapped to a page file, determining the page file mapped by the first block address from the memory file; determining the block attribute of the first block address as a write mode; the write mode indicates that: the block address only allows data writing, and the block address is not full; performing a data write operation on the first block address; when the write address of the data write operation exceeds the first block address, determining the next block address adjacent to the first block address as the second block address, and determining the page file mapped by the second block address from the memory file; determining the block attribute of the second block address as a write mode, and determining the block attribute of the first block address as a read mode; the read mode indicates that: the block address is full and data read operations are allowed; performing a data write operation on the second block address.
[0118] Data write operations can be performed on the ring buffer area in units of block addresses.
[0119] When initializing the circular buffer area, you can first determine a page file from the memory file, and map the first block of the circular buffer area to the page file so that the block address has actual physical storage space. And the block attribute of the block address is determined to be write mode. When the block attribute of the block address is write mode, it means that the block address has a mapped page file, that is, actual physical storage space, data writing operations are allowed, and the block address is not full. The block address is not full means that the write pointer of the circular buffer area is not located at the end of the block address.
[0120] Perform a data write operation on the block address; if the block address is not mapped to a page file, determine the mapped page file for the block address, and determine the block attribute of the block address as write mode; if the block address is already mapped to a page file, it means that the block attribute of the current block address is already in write mode; directly perform a data write operation on the block address in write mode. Determine the block address as the first block address, that is, the first block address to perform a data write operation. As data is written, the write pointer moves forward according to the number of bytes written. When the write pointer is at the end of the first block address, it means that the current first block address has been filled. If the data write operation has not ended, it means that there is still data to be written, that is, the write address of the data write operation exceeds the first block address, triggering the exception handling function.
[0121] The next block address adjacent to the first block address is determined as the second block address, that is, the block address where the data write operation is performed subsequently. The exception handling function determines the page file mapped by the second block address from the memory file. The page file mapped by the first block address is different from the page file mapped by the second block address. The block attribute of the first block address is changed from write mode to read mode, and the block attribute of the second block address is changed to write mode. The read mode indicates that the storage space corresponding to the block address has been filled, and the read pointer has moved from the beginning of the block address to the end of the block address. The block address can perform data read operations. Continue to perform data write operations on the second block address.
[0122] By adopting the embodiments of the present disclosure, page file mapping is dynamically managed to ensure that page files are allocated or loaded only when needed, thereby reducing the waste of memory and disk space. By strictly managing the write mode and read mode of the block address, the consistency and integrity of the data writing process are ensured, avoiding the risk of data overwriting or loss. Clear block address management and page file mapping strategies simplify the complexity of data management and maintenance, making data read and write operations more orderly and controllable. It is easy to expand to meet the needs of larger-scale data storage.
[0123] Figure 6 FIG. 1 is a flow chart of writing data into a buffer according to an embodiment of the present disclosure. Figure 6 As shown, data is written into the circular buffer area; it is determined whether to perform a data write operation on the first block address; in the case of performing a data write operation on the first block address, the data write operation is directly performed; in the process of performing the data write operation, it is determined whether it exceeds the size of a block address; in the case of not exceeding the size of a block address, the data write operation is completed; in the case of exceeding the size of a block address, an exception handling function is triggered; a page file is determined from the memory file; the next block address is mapped to the page file; the block attribute of the block address is modified to a read mode; the block attribute of the next block address is modified to a write mode; an exception is returned; a data write operation is performed on the next block address; in the process of performing a data write operation on the next block address, it is continued to be determined whether it exceeds the size of a block address; in the case of not performing a data write operation on the first block address, an exception handling function is triggered; a page file is determined from the memory file; the next block address is mapped to the page file; the block attribute of the block address is modified to a read mode; the block attribute of the next block address is modified to a write mode; an exception is returned; a data write operation is performed on the next block address; in the process of performing a data write operation on the next block address, it is determined whether it exceeds the size of a block address.
[0124] Among them, in an optional embodiment, each block address forms a virtual address in address order, and also includes: when the data read operation reads to the very end of the virtual address, or when the data write operation writes to the very end of the virtual address, the address is rolled back to the beginning of the virtual address, and the data write operation or the data read operation is performed from the beginning of the first block address in the virtual address.
[0125] When the data read operation is executed to the end of the virtual address, that is, when the corresponding read pointer moves to the end of the virtual address, the exception handling function is triggered, and the read address is rolled back to the beginning of the virtual address through the exception handling function; the end of the virtual address is the end of the last block address constituting the virtual address, and the beginning of the virtual address is the beginning of the first block address constituting the virtual address. Then the data read operation is started or continued from the beginning of the first block address.
[0126] When the data write operation is executed to the end of the virtual address, the exception handling function is also triggered, and the write address is rolled back to the beginning of the virtual address through the exception handling function; then the data write operation is started or continued from the beginning of the first block address.
[0127] By adopting the embodiments of the present disclosure, the system can effectively prevent the buffer boundary from being exceeded during data writing or reading by rolling back the address to the starting position of the virtual address. The core function of the circular buffer is implemented, so that the memory area forms a loop logically, allowing the buffer to automatically reuse space after reaching the maximum capacity, thereby improving memory utilization. When the data is written or read to the very end of the virtual address, it automatically rolls back to the starting address, ensuring the continuity and consistency of the data. The automatic rollback mechanism implemented by the exception handling function simplifies the complexity of memory management. For application scenarios that require continuous processing of data streams, such as real-time data processing, streaming media processing, etc., automatically rolling back to the starting position of the buffer can ensure the continuity and real-time nature of data processing.
[0128] Figure 7 FIG. 1 is a flow chart of a data rollback process shown in an embodiment of the present disclosure. Figure 7 As shown, when the read address or the write address exceeds the maximum address of the virtual address; the exception handling function is triggered; the exception state is cleared; the read pointer or the write pointer is rolled back to the beginning of the virtual address; and the exception is returned.
[0129] The present disclosure also provides an electronic device, referring to Figure 8 , Figure 8 Schematic diagram of an electronic device shown in an embodiment of the present disclosure. Figure 8As shown, the electronic device 800 includes: a memory 810 and a processor 820. The memory 810 and the processor 820 are connected via a bus communication. A computer program is stored in the memory 810. The computer program can be run on the processor 820 to implement the steps in the data reading and writing method of the dynamic and efficient buffer in the embodiment of the present disclosure.
[0130] The embodiment of the present disclosure further provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps in the data reading and writing method of the dynamic and efficient buffer as disclosed in the embodiment of the present disclosure are implemented.
[0131] The embodiment of the present disclosure further provides a computer program product, including a computer program, which, when executed by a processor, implements the steps in the data reading and writing method of the dynamic and efficient buffer as disclosed in the embodiment of the present disclosure.
[0132] The various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the various embodiments can be referenced to each other.
[0133] Those skilled in the art will appreciate that the embodiments of the present disclosure may be provided as methods, devices or computer program products. Therefore, the embodiments of the present disclosure may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the embodiments of the present disclosure may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0134] The embodiments of the present disclosure are described with reference to the flowcharts and / or block diagrams of the methods, apparatuses, electronic devices, and computer program products according to the embodiments of the present disclosure. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of the processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing terminal device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing terminal device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0135] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing terminal device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce a manufactured product including an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0136] These computer program instructions can also be loaded onto a computer or other programmable data processing terminal device so that a series of operating steps are executed on the computer or other programmable terminal device to produce a computer-implemented process, thereby providing instructions for executing on the computer or other programmable terminal device to implement the process. Figure 1 A process or multiple processes and / or boxes Figure 1 A step that specifies a function in one or more boxes.
[0137] Although some embodiments of the present disclosure have been described, those skilled in the art may make additional changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiment and all changes and modifications that fall within the scope of the present disclosure.
[0138] The above is a detailed introduction to a data reading and writing device, method, electronic device and medium of a dynamic and efficient buffer provided by the present invention. Specific examples are used in this article to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only used to help understand the method and core idea of the present invention. At the same time, for a person skilled in the art, according to the idea of the present invention, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as a limitation on the present invention.
Claims
1. A data reading and writing device for a dynamic and efficient buffer, characterized in that: include: A ring buffer area, including a virtual address composed of multiple block addresses; a memory file, associated with the ring buffer area, for storing data written to the block address; An exception handling function, used to set block attributes of the plurality of block addresses, the block attributes including: a write mode, a read mode, and a non-readable and non-writable mode; When data is read or written, a data read operation is performed according to the block attribute of the block address; or a page file mapped by the block address is determined from the memory file, and a data write operation is performed.
2. The device according to claim 1, characterized in that include: The exception handling function is further used to determine the page file mapped by the next block address from the memory file when the write address of the data write operation exceeds the block address, modify the block attribute of the block address to a read mode; and modify the block attribute of the next block address to a write mode; The exception handling function is further used to determine the read length information of the data read operation and determine whether to change the block attribute of the block address when the data read operation reads a block address whose block attribute is a write mode.
3. The device according to claim 1, characterized in that Also includes: A read pointer, used to point to a readable data position in a virtual address corresponding to the ring buffer area; A write pointer, used to point to a writable data location in a virtual address corresponding to the ring buffer area; The data is stored in the virtual address corresponding to the annular buffer area by moving the write pointer and waits to be read; the data is read out of the virtual address corresponding to the annular buffer area by moving the read pointer and the annular buffer area is released.
4. The device according to claim 3, characterized in that include: The exception handling function is further used to roll back the corresponding pointer to the beginning of the virtual address when the read pointer is at the very end of the virtual address and / or when the write pointer is at the very end of the virtual address; The starting representation of the virtual address is: the starting point of the first block address constituting the virtual address.
5. A method for reading and writing data in a dynamic and efficient buffer, characterized in that: include: Acquiring data status, the data status including: reading data and writing data; When the data state is data reading, performing a data reading operation according to a block attribute of the block address; When the data state is data writing, a page file of block address mapping is determined from the memory file, and a data writing operation is performed.
6. The method according to claim 5, characterized in that The block attributes include a write mode, a read mode, and a non-readable and non-writable mode. When the data state is read data, a data read operation is performed according to the block attribute of the block address, including: In the case where the data reading operation reads a block address whose block attribute is a read mode, directly reading the data in the block address; In the case where the data read operation reads a block address whose block attribute is a write mode, read length information of the data read operation is determined; and according to the read length information, an execution action of the data read operation is determined.
7. The method according to claim 6, characterized in that The step of determining the execution action of the data reading operation according to the read length information includes: In the case where the read length information of the data read operation is less than the data length in the block address, modifying the block attribute of the block address to a read mode; reading data from the block address; and after the data read operation is completed, modifying the block attribute of the block address to a write-back mode; In a case where the read length of the data read operation is greater than the data length in the block address, the data read operation on the block address is canceled.
8. The method according to claim 7, characterized in that In the case where the read length of the data read operation is greater than the data length in the block address, the method further includes: Putting the data reading operation into a waiting state; In the case where the data length in the block address is greater than the read length of the data read operation, the data read operation is awakened and the block attribute of the block address is modified to a read mode, and the data read operation reads data from the block address; after the data read is completed, the block attribute of the block address is modified back to a write mode.
9. The method according to claim 6, characterized in that Reading the data in the block address also includes: When the data reading operation reads to the very end of the block address, the page file mapped by the block address is released.
10. The method according to claim 5, characterized in that The data writing operation is performed sequentially according to the order of the block addresses, and when the data state is writing data, a page file mapped by the block address is determined from the memory file, including: The block address currently performing the data write operation is determined as the first block address, and when the first block address is not mapped to a page file, the page file mapped to the first block address is determined from the memory file; the block attribute of the first block address is determined as a write mode; the write mode indicates that: the block address only allows data to be written, and the block address is not fully written; Performing a data write operation on the first block address; In the case where the write address of the data write operation exceeds the first block address, the next block address adjacent to the first block address is determined as the second block address, and the page file mapped by the second block address is determined from the memory file; the block attribute of the second block address is determined as a write mode, and the block attribute of the first block address is determined as a read mode; the read mode indicates that: the block address is full and the data read operation is allowed; A data write operation is performed on the second block address.
11. The method according to any one of claims 5 to 10, characterized in that: Each block address forms a virtual address in address order, and also includes: When the data read operation reads to the very end of the virtual address, or when the data write operation writes to the very end of the virtual address, the address is rolled back to the beginning of the virtual address, and the data write operation or data read operation is performed starting from the beginning of the first block address in the virtual address.
12. An electronic device, characterized in that: include: A memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the computer program is executed by the processor, the steps of the data reading and writing method of the dynamic and efficient buffer as described in any one of claims 5 to 11 are implemented.
13. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by the processor, the steps of the data reading and writing method of the dynamic and efficient buffer as described in any one of claims 5-11 are implemented.
Citation Information
Cited By
Storage system capable of reducing file fragments, read-write method and controller
CN119828966A
Intelligent file uploading method, device and system
CN119966979A
An intelligent file uploading method, device and system
CN119966979B
