lx_nand_flash_block_allocate.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*************************************************************************** | ||
| 2 | * Copyright (c) 2024 Microsoft Corporation | ||
| 3 | * Copyright (c) 2026-present Eclipse ThreadX contributors | ||
| 4 | * | ||
| 5 | * This program and the accompanying materials are made available under the | ||
| 6 | * terms of the MIT License which is available at | ||
| 7 | * https://opensource.org/licenses/MIT. | ||
| 8 | * | ||
| 9 | * SPDX-License-Identifier: MIT | ||
| 10 | **************************************************************************/ | ||
| 11 | |||
| 12 | // Portions of this file were generated with AI assistance. | ||
| 13 | |||
| 14 | /**************************************************************************/ | ||
| 15 | /**************************************************************************/ | ||
| 16 | /** */ | ||
| 17 | /** LevelX Component */ | ||
| 18 | /** */ | ||
| 19 | /** NAND Flash */ | ||
| 20 | /** */ | ||
| 21 | /**************************************************************************/ | ||
| 22 | /**************************************************************************/ | ||
| 23 | |||
| 24 | #define LX_SOURCE_CODE | ||
| 25 | |||
| 26 | |||
| 27 | /* Disable ThreadX error checking. */ | ||
| 28 | |||
| 29 | #ifndef LX_DISABLE_ERROR_CHECKING | ||
| 30 | #define LX_DISABLE_ERROR_CHECKING | ||
| 31 | #endif | ||
| 32 | |||
| 33 | |||
| 34 | /* Include necessary system files. */ | ||
| 35 | |||
| 36 | #include "lx_api.h" | ||
| 37 | |||
| 38 | |||
| 39 | /**************************************************************************/ | ||
| 40 | /* */ | ||
| 41 | /* FUNCTION RELEASE */ | ||
| 42 | /* */ | ||
| 43 | /* _lx_nand_flash_block_allocate PORTABLE C */ | ||
| 44 | /* 6.2.1 */ | ||
| 45 | /* AUTHOR */ | ||
| 46 | /* */ | ||
| 47 | /* Xiuwen Cai, Microsoft Corporation */ | ||
| 48 | /* */ | ||
| 49 | /* DESCRIPTION */ | ||
| 50 | /* */ | ||
| 51 | /* This function attempts to get a block in the free block list. */ | ||
| 52 | /* */ | ||
| 53 | /* INPUT */ | ||
| 54 | /* */ | ||
| 55 | /* nand_flash NAND flash instance */ | ||
| 56 | /* block Return block number */ | ||
| 57 | /* */ | ||
| 58 | /* OUTPUT */ | ||
| 59 | /* */ | ||
| 60 | /* return status */ | ||
| 61 | /* */ | ||
| 62 | /* CALLS */ | ||
| 63 | /* */ | ||
| 64 | /* None */ | ||
| 65 | /* */ | ||
| 66 | /* CALLED BY */ | ||
| 67 | /* */ | ||
| 68 | /* Internal LevelX */ | ||
| 69 | /* */ | ||
| 70 | /**************************************************************************/ | ||
| 71 | 16110 | UINT _lx_nand_flash_block_allocate(LX_NAND_FLASH* nand_flash, ULONG* block) | |
| 72 | { | ||
| 73 | |||
| 74 | |||
| 75 | /* Check if the free block list is empty. */ | ||
| 76 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 16110 times.
|
16110 | if (nand_flash -> lx_nand_flash_free_block_list_tail == 0) |
| 77 | { | ||
| 78 | |||
| 79 | #ifdef LX_NAND_FLASH_ENABLE_LAZY_SECTOR_RELEASE | ||
| 80 | { | ||
| 81 | |||
| 82 | ULONG lg; | ||
| 83 | UINT compact_status; | ||
| 84 | |||
| 85 | /* Emergency compaction: find any pending compaction and execute it to | ||
| 86 | reclaim the old full block. */ | ||
| 87 | for (lg = 0; lg < nand_flash -> lx_nand_flash_total_blocks; lg++) | ||
| 88 | { | ||
| 89 | |||
| 90 | if (nand_flash -> lx_nand_flash_block_compaction_table[lg] != (USHORT)LX_NAND_BLOCK_UNMAPPED) | ||
| 91 | { | ||
| 92 | |||
| 93 | compact_status = _lx_nand_flash_logical_group_compact(nand_flash, lg); | ||
| 94 | |||
| 95 | if (compact_status == LX_SUCCESS) | ||
| 96 | { | ||
| 97 | |||
| 98 | /* Retry the allocation after compaction freed a block. */ | ||
| 99 | if (nand_flash -> lx_nand_flash_free_block_list_tail > 0) | ||
| 100 | { | ||
| 101 | |||
| 102 | nand_flash -> lx_nand_flash_free_block_list_tail--; | ||
| 103 | *block = nand_flash -> lx_nand_flash_block_list[nand_flash -> lx_nand_flash_free_block_list_tail]; | ||
| 104 | return(LX_SUCCESS); | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | /* Attempt only one compaction per allocate call. */ | ||
| 109 | break; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | } | ||
| 114 | #endif /* LX_NAND_FLASH_ENABLE_LAZY_SECTOR_RELEASE */ | ||
| 115 | |||
| 116 | /* Empty list, return error. */ | ||
| 117 | ✗ | return(LX_NO_BLOCKS); | |
| 118 | } | ||
| 119 | |||
| 120 | /* Remove one block from the list. */ | ||
| 121 | 16110 | nand_flash -> lx_nand_flash_free_block_list_tail--; | |
| 122 | |||
| 123 | /* Return the block number. */ | ||
| 124 | 16110 | *block = nand_flash -> lx_nand_flash_block_list[nand_flash -> lx_nand_flash_free_block_list_tail]; | |
| 125 | |||
| 126 | /* Return successful completion. */ | ||
| 127 | 16110 | return(LX_SUCCESS); | |
| 128 | } | ||
| 129 | |||
| 130 |