1 /**************************************************************************/ 2 /* */ 3 /* Copyright (c) Microsoft Corporation. All rights reserved. */ 4 /* */ 5 /* This software is licensed under the Microsoft Software License */ 6 /* Terms for Microsoft Azure RTOS. Full text of the license can be */ 7 /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ 8 /* and in the root directory of this software. */ 9 /* */ 10 /**************************************************************************/ 11 12 13 /**************************************************************************/ 14 /**************************************************************************/ 15 /** */ 16 /** USBX Component */ 17 /** */ 18 /** USBX main stack */ 19 /** */ 20 /**************************************************************************/ 21 /**************************************************************************/ 22 23 24 /* Include necessary system files. */ 25 26 #define UX_SOURCE_CODE 27 28 #include "ux_api.h" 29 30 31 /**************************************************************************/ 32 /* */ 33 /* FUNCTION RELEASE */ 34 /* */ 35 /* _ux_utility_memory_free_block_best_get PORTABLE C */ 36 /* 6.1 */ 37 /* AUTHOR */ 38 /* */ 39 /* Chaoqiong Xiao, Microsoft Corporation */ 40 /* */ 41 /* DESCRIPTION */ 42 /* */ 43 /* This function returns the best free memory block. */ 44 /* */ 45 /* INPUT */ 46 /* */ 47 /* memory_cache_flag Memory pool source */ 48 /* memory_size_requested Size of memory requested */ 49 /* */ 50 /* OUTPUT */ 51 /* */ 52 /* Pointer to best free block */ 53 /* */ 54 /* CALLS */ 55 /* */ 56 /* None */ 57 /* */ 58 /* CALLED BY */ 59 /* */ 60 /* USBX Components */ 61 /* */ 62 /* RELEASE HISTORY */ 63 /* */ 64 /* DATE NAME DESCRIPTION */ 65 /* */ 66 /* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */ 67 /* 09-30-2020 Chaoqiong Xiao Modified comment(s), */ 68 /* resulting in version 6.1 */ 69 /* */ 70 /**************************************************************************/ _ux_utility_memory_free_block_best_get(ULONG memory_cache_flag,ULONG memory_size_requested)71UX_MEMORY_BLOCK *_ux_utility_memory_free_block_best_get(ULONG memory_cache_flag, 72 ULONG memory_size_requested) 73 { 74 75 UX_MEMORY_BLOCK *memory_block; 76 UX_MEMORY_BLOCK *best_memory_block; 77 78 79 /* Reset the free memory block. */ 80 best_memory_block = UX_NULL; 81 82 /* Check the type of memory we need. */ 83 switch (memory_cache_flag) 84 { 85 86 case UX_REGULAR_MEMORY : 87 88 /* Start at the beginning of the regular memory pool. */ 89 memory_block = _ux_system -> ux_system_regular_memory_pool_start; 90 break; 91 92 case UX_CACHE_SAFE_MEMORY : 93 94 /* Start at the beginning of the cache safe memory pool. */ 95 memory_block = _ux_system -> ux_system_cache_safe_memory_pool_start; 96 break; 97 98 default : 99 100 /* Wrong memory type. */ 101 return(UX_NULL); 102 103 } 104 105 /* Loop on all memory blocks from the beginning. */ 106 while (memory_block != UX_NULL) 107 { 108 109 /* Check the memory block status. */ 110 if (memory_block -> ux_memory_block_status == UX_MEMORY_UNUSED) 111 { 112 113 /* Check the size of this free block and see if it will 114 fit the memory requirement. */ 115 if (memory_block -> ux_memory_block_size > memory_size_requested) 116 { 117 118 /* This memory block will do. Now see if it is the best. 119 The best memory block is the one whose memory is closest 120 to the memory requested. */ 121 if (best_memory_block == UX_NULL) 122 123 /* Initialize the best block with the first free one. */ 124 best_memory_block = memory_block; 125 else 126 { 127 128 if (memory_block -> ux_memory_block_size < best_memory_block -> ux_memory_block_size) 129 130 /* We have discovered a better fit block. */ 131 best_memory_block = memory_block; 132 } 133 } 134 } 135 136 /* Search the next free block until the end. */ 137 memory_block = memory_block -> ux_memory_block_next; 138 } 139 140 /* If no free memory block was found, the return value will be NULL. */ 141 return(best_memory_block); 142 } 143 144