## 2 Materials and Methods
### Memory Allocation for Real-Time Tasks  
The fixed-size allocation algorithm is a common algorithm for allocating memory space to real-time tasks. The fixed-size allocation algorithm divides the memory into several equally sized memory blocks and returns these blocks to fulfill requests when memory allocation is needed. This approach generally ensures timeliness but it has lower memory utilization. In situations with a high density of real-time tasks, this method may fail to allocate memory in a timely manner if there is insufficient memory space. To meet the demands of real-time tasks for timeliness and memory efficiency, we have made improvements to the fixed-size allocation algorithm.  
The improved fixed-size algorithm introduces a collection of free lists. The free lists contain small memory blocks organized in the form of linked lists, with different free lists responsible for different sizes of memory blocks. Memory blocks within the same free list always have the same size. When a real-time task generates a memory request, it is directly mapped to the most suitable free list according to size and the corresponding memory block is returned. The memory allocation method in the real-time zone is illustrated in Figure 3.  
Figure 2: Decision-making process of memory allocation.  
The improved fixed partitioning algorithm has the following characteristics:
* Memory blocks can be found through direct mapping. When the memory pool is initialized, a contiguous memory space is first divided into several regions of equal size. These regions are then allocated to the free lists in ascending order according to the size of the memory blocks they handle. After the regions are allocated, further division of small memory blocks occurs within the free lists. This organization ensures that memory blocks of the same size come from a fixed address range, which allows for efficient allocation and deallocation of small memory blocks through direct mapping.
* The range of the free lists is relatively small. The memory request sizes of real-time tasks in the edge PLCs are typically concentrated within a small range. Therefore, we limit the sizes of the small memory blocks maintained in the free lists to this range, reducing wastage of memory space due to excessively large or small blocks.
* The memory pool maintains an uninitialized region, referred to as the reserved region. When there are no available memory blocks in the free lists, conventional allocation methods can be used within this space to ensure timeliness during periods of high density real-time tasks.  
Algorithm 1 demonstrates the improved fixed-size allocation algorithm. In the algorithm, \(FreeList_{i}\) represents the \(i\) th free list.  
```
0: Memory request for real-time tasks
0: Allocated memory address
1: Map memory request to \(FreeList_{j}\)
2:for each\(i\geq j\) in set of free lists do
3: if \(FreeList_{i}\) contains memory blocks then
4: Extract a memory block from \(FreeList_{i}\)
5: Record the allocated memory address
6:break
7:endif
8:endfor
9:if Allocation fails in the free lists then
10: Perform conventional allocation in the reserved region
11: Record the allocated memory address
12:endif
13:return Allocated memory address
```  
**Algorithm 1** Improved Fixed-size Allocation Algorithm  
Figure 3: Memory allocation in the real-time zone.