int main(int argc, char *argv[]) { FILE *f; struct superblock sb; if (argc < 3) { printf("Usage: %s <image file> <block #>\n", argv[0]); return 1; } f = fopen(argv[1], "r"); if (f == NULL) { perror("fopen"); return 1; } if (get_superblock(f, &sb)) return 1; // Print the allocation status of the given block. switch (is_block_free(&sb, strtoul(argv[2], NULL, 0))) { case 0: printf("allocated\n"); break; case 1: printf("free\n"); break; default: printf("error\n"); return 1; } fclose(f); return 0; }
int main(int argc, char *argv[]) { FILE *f; struct superblock sb; int i, len; char *block; if (argc < 3) { printf("Usage: %s <image file> <search string>\n", argv[0]); return 1; } f = fopen(argv[1], "r"); if (f == NULL) { perror("fopen"); return 1; } if (get_superblock(f, &sb)) return 1; block = malloc(blocksize(&sb)); if (block == NULL) return 1; // Search free blocks len = strlen(argv[2]); for (i = 0; i < sb.s_blocks_count; i++) { if (!is_block_free(&sb, i)) continue; if (get_block_data(&sb, i, block)) return 1; if (!strncmp(argv[2], block, len)) printf("%d\n", i); } free(block); fclose(f); return 0; }
/**@brief Function to format and print information with respect to each block. * * @details Internal function that formats and prints information related to the block category * identified by 'block_cat'. This function also appends the number of bytes in use to * p_mem_in_use based on current count of block in the category. * * @param[in] block_cat Identifies the category of block. * @param[out] p_mem_in_use Updates the memory in use based on count in use. */ void print_block_info(uint32_t block_cat, uint32_t * p_mem_in_use) { #define PRINT_COLUMN_WIDTH 13 #define PRINT_BUFFER_SIZE 80 #define ASCII_VALUE_FOR_SPACE 32 char print_buffer[PRINT_BUFFER_SIZE]; const uint32_t total_count = (m_block_start[block_cat] + m_block_count[block_cat]); uint32_t in_use = 0; uint32_t num_of_blocks = 0; uint32_t index = m_block_start[block_cat]; uint32_t column_number; // No statistic provided in case block category is not included. if (m_block_count[block_cat] != 0) { memset(print_buffer, ASCII_VALUE_FOR_SPACE, PRINT_BUFFER_SIZE); for (; index < total_count; index++) { if (is_block_free(index) == false) { num_of_blocks++; in_use += m_block_size[block_cat]; } } column_number = 0; snprintf(&print_buffer[column_number * PRINT_COLUMN_WIDTH], PRINT_COLUMN_WIDTH, "| %s", m_block_desc_str[block_cat]); column_number++; snprintf(&print_buffer[column_number * PRINT_COLUMN_WIDTH], PRINT_COLUMN_WIDTH, "| %d", m_block_size[block_cat]); column_number++; snprintf(&print_buffer[column_number * PRINT_COLUMN_WIDTH], PRINT_COLUMN_WIDTH, "| %d", m_block_count[block_cat]); column_number++; snprintf(&print_buffer[column_number * PRINT_COLUMN_WIDTH], PRINT_COLUMN_WIDTH, "| %d", num_of_blocks); column_number++; snprintf(&print_buffer[column_number * PRINT_COLUMN_WIDTH], PRINT_COLUMN_WIDTH, "| %d", m_min_size[block_cat]); column_number++; snprintf(&print_buffer[column_number * PRINT_COLUMN_WIDTH], PRINT_COLUMN_WIDTH, "| %d", m_max_size[block_cat]); column_number++; const uint32_t column_end = (column_number * PRINT_COLUMN_WIDTH); for (int j = 0; j < column_end; j ++) { if (print_buffer[j] == 0) { print_buffer[j] = 0x20; } } snprintf(&print_buffer[column_end], 2, "|"); NRF_LOG_BYTES_DEBUG(print_buffer, strlen(print_buffer)); (*p_mem_in_use) += in_use; } }
uint32_t nrf_mem_reserve(uint8_t ** pp_buffer, uint32_t * p_size) { VERIFY_MODULE_INITIALIZED(); NULL_PARAM_CHECK(pp_buffer); NULL_PARAM_CHECK(p_size); const uint32_t requested_size = (*p_size); VERIFY_REQUESTED_SIZE(requested_size); NRF_LOG_DEBUG("[MM]: >> nrf_mem_reserve, size 0x%04lX.\r\n", requested_size); MM_MUTEX_LOCK(); const uint32_t block_cat = get_block_cat(requested_size, TOTAL_BLOCK_COUNT); uint32_t block_index = m_block_start[block_cat]; uint32_t memory_index = m_block_mem_start[block_cat]; uint32_t err_code = (NRF_ERROR_NO_MEM | MEMORY_MANAGER_ERR_BASE); NRF_LOG_DEBUG("[MM]: Start index for the pool = 0x%08lX, total block count 0x%08X\r\n", block_index, TOTAL_BLOCK_COUNT); for (; block_index < TOTAL_BLOCK_COUNT; block_index++) { uint32_t block_size = get_block_size(block_index); if (is_block_free(block_index) == true) { NRF_LOG_DEBUG("[MM]: Reserving block 0x%08lX\r\n", block_index); // Search succeeded, found free block. err_code = NRF_SUCCESS; // Allocate block. block_allocate(block_index); (*pp_buffer) = &m_memory[memory_index]; (*p_size) = block_size; #ifdef MEM_MANAGER_ENABLE_DIAGNOSTICS (*p_min_size) = MIN((*p_min_size), requested_size); (*p_max_size) = MAX((*p_max_size), requested_size); #endif // MEM_MANAGER_ENABLE_DIAGNOSTICS break; } memory_index += block_size; } if (err_code != NRF_SUCCESS) { NRF_LOG_DEBUG ("[MM]: Memory reservation result %d, memory %p, size %d!", err_code, (uint32_t)(*pp_buffer), (*p_size)); #ifdef MEM_MANAGER_ENABLE_DIAGNOSTICS nrf_mem_diagnose(); #endif // MEM_MANAGER_ENABLE_DIAGNOSTICS } MM_MUTEX_UNLOCK(); NRF_LOG_DEBUG("[MM]: << nrf_mem_reserve %p, result 0x%08lX.\r\n", (uint32_t)(*pp_buffer), err_code); return err_code; }