560 likes | 706 Views
Chapter 8 Real-Time Embedded Multithreading. Memory Management: Byte Pools And Block Pools. OUTLINE (cont.). Introduction Summary of Memory Byte Pools Memory Byte Pool Control Block Pitfalls of Memory Byte Pools Summary of Memory Byte Pool Services Memory Byte Pool Example
E N D
Chapter 8 Real-Time Embedded Multithreading Memory Management: Byte Pools And Block Pools
OUTLINE (cont.) • Introduction • Summary of Memory Byte Pools • Memory Byte Pool Control Block • Pitfalls of Memory Byte Pools • Summary of Memory Byte Pool Services • Memory Byte Pool Example • Memory Byte Pool Internals
OUTLINE • Summary of Memory Block Pools • Memory Block Pool Control Block • Summary of Memory Block Pool Services • Memory Block Pool Example • Memory Block Pool Internals
Introduction (cont.) • A memory byte pool is a contiguous block of bytes. • A memory block pool is also a contiguous block of bytes, but it is organized into a collection of fixed-size memory blocks.
Introduction • There is no fragmentation problem, and allocation and releasing memory blocks is fast. • In general, the use of memory block pools is preferred over memory byte pools.
Summary of Memory Byte Pools (cont.) • ThreadX allocates memory from the memory byte pool in a first-fit manner. • ThreadX converts excess memory from this block into a new free memory, it merges adjacent free memory blocks together. This process is called defragmentation.
Summary of Memory Byte Pools (cont.) • The number of allocatable bytes in a memory byte pool is slightly less than what was specified during creation • Each free memory block in the pool requires the equivalent of two C pointers of overhead.
Summary of Memory Byte Pools (cont.) • ThreadX automatically divides it into two blocks , a large free block and a small permanently. • Application thread can suspend while waiting for memory bytes form a pool.
Summary of Memory Byte Pools • tx_byte_pool_prioritize places the highest priority thread at front of the suspension list, while leaving all other suspended threads in the same FIFO order.
Memory Byte Pool Control Block (cont.) • The characteristics of each memory byte pool are found in its Control Block. • Memory Byte Pool Control Blocks can be located anywhere in memory, but it is most common to make the Control Block a global structure by defining it outside the scope of any function.
Pitfalls of Memory Byte Pools (cont.) • A memory byte pool may have 2000 bytes of memory available but not be able to satisfy an allocation request of even 1000 bytes. • There is no guarantee on how many of the free bytes are contiguous.
Pitfalls of Memory Byte Pools • There is no guarantee on how long it might take to find the block. • Many such applications pre-allocate their required memory during initialization or run-time configuration. • Users of byte pool allocated memory must not write outside its boundaries.
Summary of Memory Byte Pool Services (cont.) • Creating a Memory Byte Pool • A memory byte pool is declared with the TX_BYTE_POOL data type and is defined with the tx_byte_pool_create
Summary of Memory Byte Pool Services (cont.) • Allocating from a Memory Byte Pool • The tx_byte_allocate service is the method by witch bytes of memory are allocated from the memory byte pool. • If the allocation succeeds, the pointer memory_ptr contains the starting location of the allocated bytes.
Summary of Memory Byte Pool Services (cont.) • Deleting a Memory Byte Pool • A memory pool can be deleted with the tx_byte_pool_delete service. • All threads that are suspended because they are waiting for memory from this byte pool are resumed and receive a TX_DELETED return status.
Summary of Memory Byte Pool Services (cont.) • Retrieving Memory byte Pool Information • The tx_byte_pool_info_get service retrieves a variety of information about a memory byte pool.
Summary of Memory Byte Pool Services (cont.) • Prioritizing a Memory Byte Pool Suspension List • The tx_byte_pool_prioritize service places the highest-priority thread suspended for ownership of a specific memory byte pool at the front of the suspension list. • All other thread remain in the same FIFO order in which they were suspended.
Summary of Memory Byte Pool Services (cont.) • Releasing Memory to a Byte Pool • The tx_byte_release service releases a previously allocated memory area block to its associated pool. • If one or more thread are suspended on this pool, each suspended thread receives the memory it requested and is resumed.
Memory Byte Pool Internals (cont.) • When the TX_BYTE_POOL data type is used to declare a byte pool, a Byte Pool Control Block is created, and that Control Block is added to a doubly linked circular list. • The pointer named tx_byte_pool_created_ptr points to the first Control Block in the list.
Memory Byte Pool Internals (cont.) • ThreadX allocates from the pool in a first-fit manner, converts excess memory from this block into a new block, and places it back in the free memory list. This process is called fragmentation.
Memory Byte Pool Internals (cont.) • This process is called fragmentation • ThreadX merges free memory blocks together during a subsequent allocation search for a large enough free memory block • This process is called defragmentation.
Summary of Memory Block Pools (cont.) • Memory block pools consist of fixed-size blocks, using them involves no fragmentation problem. • The allocation service does not have to search through a list of block. • Lack of flexibility is the main drawback of fixed-size memory pools.
Summary of Memory Block Pools (cont.) • Memory block pools contain a number of fixed-size blocks. • The block size, in bytes is specified during creation of the pool. • Each memory block in the pool imposes a small amount of overhead – the size of a C pointer
Summary of Memory Block Pools (cont.) • The number of memory blocks in a pool depends on the block size and the total number of bytes in the memory area supplied during creation. • The memory area for the block pool is specifies during creation , and can be located anywhere in the target’s address space.
Summary of Memory Block Pools (cont.) • tx_block_pool_prioritize places the highest priority thread at the front of the suspension list, while leaving all other suspended threads in the same FIFO order.
Memory Block Pool Control Block (cont.) • The characteristics of each memory block pool are found in its Control Block . • The user of an allocated memory block must not write outside its boundaries.
Summary of Memory Block Pool Services (cont.) • Creating a Memory Block Pool • A memory block pool is declared with the TX_BLOCK_POOL data type and is defined with the tx_block_pool_create service.
Summary of Memory Block Pool Services (cont.) • Allocating a Memory Block Pool • The tx_block_allocate service is the method that allocates a fixed size block of memory form the memory block pool
Summary of Memory Block Pool Services (cont.) • Deleting a Memory Block Pool • A mymory block pool can be deleted with the tx_block_pool_delete service. • All threads that are suspended because they are waiting for memory from this block pool are resumed and receive a TX_DELETED return status.
Summary of Memory Block Pool Services (cont.) • Retrieving Memory Block Pool Information • The tx_block_pool_info_get service retrieves a variety of information about a memory block pool.
Summary of Memory Block Pool Services (cont.) • Prioritizing a Memory Block Pool Suspension List • The tx_block_pool_prioritize service place the highest-priority thread suspended for ownership of a specific memory block pool at the front of the suspension list. • All other threads remain in the same FIFO order in which they were suspended.
Summary of Memory Block Pool Services (cont.) • Releasing a Memory Block • The tx_block_release service releases one previously allocated memory block back to its associated block pool. • If one or more threads are suspended on this pool, each suspended thread receives a memory block and is resumed until the pool runs out of blocks or until there are no more suspended threads.