void f_free(void * ptr) { struct f_malloc_block * blk; /* stats */ if (!ptr) { return; } blk = (struct f_malloc_block *)((uint8_t *)ptr - sizeof(struct f_malloc_block)); if (blk->magic == F_MALLOC_MAGIC) { blk->flags = 0u; /* stats */ f_malloc_stats[blk->flags & MEM_USER].free_calls++; f_malloc_stats[blk->flags & MEM_USER].objects_allocated--; f_malloc_stats[blk->flags & MEM_USER].mem_allocated -= (uint32_t)blk->size; /* Merge adjecent free blocks */ //XXX: How to check if actually adjacent? -> Pointer arithmetic? (sizeof(struct) + blk->size) ? // Or just assume for now that they are adjacent? -> Like FreeRTOS heap4 if ((blk->prev) && (!in_use(blk->prev))) { blk = merge_blocks(blk->prev, blk); } if ((blk->next) && (!in_use(blk->next))) { blk = merge_blocks(blk, blk->next); } } else { dbg_malloc("FREE ERR: %p is not a valid allocated pointer!\n", blk); } }
static void print_free(unsigned long group, char * bitmap, unsigned long num, unsigned long offset, int ratio) { int p = 0; unsigned long i; unsigned long j; offset /= ratio; offset += group * num; for (i = 0; i < num; i++) if (!in_use (bitmap, i)) { if (p) printf (", "); print_number((i + offset) * ratio); for (j = i; j < num && !in_use (bitmap, j); j++) ; if (--j != i) { fputc('-', stdout); print_number((j + offset) * ratio); i = j; } p = 1; } }
int sc_semaphore::trywait() { if( in_use() ) { return -1; } -- m_value; return 0; }
int sc_semaphore::wait() { while( in_use() ) { sc_core::wait( m_free, sc_get_curr_simcontext() ); } -- m_value; return 0; }
int uc_pthread_mutex_class::empty(){ if (in_use()) { return 0; } if (blocked_threads.size() > 0) { return 0; } return 1; }
int sc_mutex::trylock() { if ( m_owner == sc_get_current_process_b()) return 0; if( in_use() ) { return -1; } m_owner = sc_get_current_process_b(); return 0; }
int sc_mutex::lock() { if ( m_owner == sc_get_current_process_b()) return 0; while( in_use() ) { sc_core::wait( m_free, sc_get_curr_simcontext() ); } m_owner = sc_get_current_process_b(); return 0; }
// returns -1 if mutex could not be locked int uc_pthread_mutex_class::sc_trylock() { UC_thread_class *thread = qt_parent_rtos->get_current_cpu()->m_current_task->m_current_thread; if( in_use() ) { if (m_owner != thread || mutexattr->type != PTHREAD_MUTEX_RECURSIVE) { return EBUSY; } else { num_locks++; return 0; } } num_locks++; m_owner = thread; return 0; }
int uc_pthread_mutex_class::sc_lock(const struct timespec * abs_timeout){ UC_thread_class *thread = qt_parent_rtos->get_current_cpu()->m_current_task->m_current_thread; if ( in_use() ) { switch (mutexattr->type) { case PTHREAD_MUTEX_ERRORCHECK: if (m_owner != thread) { update_mutex_priority(thread); thread->block(&blocked_threads, abs_timeout); if (thread->m_timeout_expired == true) { return ETIMEDOUT; } return 1; } else { return EDEADLK; } case PTHREAD_MUTEX_RECURSIVE: if (m_owner != thread) { update_mutex_priority(thread); thread->block(&blocked_threads, abs_timeout); if (thread->m_timeout_expired == true) { return ETIMEDOUT; } return 1; } else { num_locks++; return 0; } default: update_mutex_priority(thread); thread->block(&blocked_threads, abs_timeout); if (thread->m_timeout_expired == true) { return ETIMEDOUT; } return 1; } } m_owner = thread; num_locks++; update_mutex_priority(thread); return 0; }
void xfree(char * p, int size) { char * q; /* At this point we could check that the in_use bit for this block is set, and also check that the size is right */ clear_use(p); /* mark this block as unused */ if (p == last_block) { while (1) { q = prev_block(p); if (q == p) { last_block = NULL; end_of_array = p; break; /* only happens when we get to the beginning */ } if (in_use(q)) { last_block = q; end_of_array = p; break; } p = q; } } space_in_use -= size; }
static struct f_malloc_block * f_find_best_fit(int user, size_t size, struct f_malloc_block ** last) { struct f_malloc_block *found = NULL, *blk = malloc_entry_kernel; if (user) blk = malloc_entry_user; /* See if we can find a free block that fits */ while (blk) /* last entry will break the loop */ { *last = blk; /* last travelled node */ if ((!in_use(blk)) && (blk->size >= size)) { /* found a fit - is it better than a possible previously found block? */ if ((!found) || (blk->size < found->size)) found = blk; } /* travel next block */ blk = blk->next; } return found; }
int uc_pthread_mutex_class::sc_lock(){ UC_thread_class *thread = qt_parent_rtos->get_current_cpu()->m_current_task->m_current_thread; if ( in_use() ) { switch (mutexattr->type) { case PTHREAD_MUTEX_ERRORCHECK: if (m_owner != thread) { update_mutex_priority(thread); thread->block(&blocked_threads); return 1; } else { return EDEADLK; } case PTHREAD_MUTEX_RECURSIVE: if (m_owner != thread) { update_mutex_priority(thread); thread->block(&blocked_threads); return 1; } else { num_locks++; return 0; } default: update_mutex_priority(thread); thread->block(&blocked_threads); return 1; } } m_owner = thread; num_locks++; update_mutex_priority(thread); return 0; }
/// readbitmap - cread and heck bitmap, reference dumpe2fs extern void readbitmap(char* device, image_head image_hdr, unsigned long* bitmap, int pui){ errcode_t retval; unsigned long group; unsigned long long current_block, block; unsigned long long free, gfree; char *block_bitmap=NULL; int block_nbytes; unsigned long long blk_itr; int bg_flags = 0; int start = 0; int bit_size = 1; int B_UN_INIT = 0; int ext4_gfree_mismatch = 0; log_mesg(2, 0, 0, fs_opt.debug, "%s: readbitmap %p\n", __FILE__, bitmap); fs_open(device); retval = ext2fs_read_bitmaps(fs); /// open extfs bitmap if (retval) log_mesg(0, 1, 1, fs_opt.debug, "%s: Couldn't find valid filesystem bitmap.\n", __FILE__); block_nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8; if (fs->block_map) block_bitmap = malloc(block_nbytes); /// initial image bitmap as 1 (all block are used) memset(bitmap, 0xFF, sizeof(unsigned long)*LONGS(image_hdr.totalblock)); free = 0; current_block = 0; blk_itr = fs->super->s_first_data_block; /// init progress progress_bar prog; /// progress_bar structure defined in progress.h progress_init(&prog, start, image_hdr.totalblock, image_hdr.totalblock, BITMAP, bit_size); /// each group for (group = 0; group < fs->group_desc_count; group++) { gfree = 0; B_UN_INIT = 0; if (block_bitmap) { ext2fs_get_block_bitmap_range(fs->block_map, blk_itr, block_nbytes << 3, block_bitmap); if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM){ #ifdef EXTFS_1_41 bg_flags = fs->group_desc[group].bg_flags; #else bg_flags = ext2fs_bg_flags(fs, group); #endif if (bg_flags&EXT2_BG_BLOCK_UNINIT){ log_mesg(1, 0, 0, fs_opt.debug, "%s: BLOCK_UNINIT for group %lu\n", __FILE__, group); B_UN_INIT = 1; } else { log_mesg(2, 0, 0, fs_opt.debug, "%s: BLOCK_INIT for group %lu\n", __FILE__, group); } } /// each block in group for (block = 0; ((block < fs->super->s_blocks_per_group) && (current_block < (image_hdr.totalblock-1))); block++) { current_block = block + blk_itr; /// check block is used or not if ((!in_use (block_bitmap, block)) || (B_UN_INIT)) { free++; gfree++; pc_clear_bit(current_block, bitmap); log_mesg(3, 0, 0, fs_opt.debug, "%s: free block %llu at group %lu init %i\n", __FILE__, current_block, group, (int)B_UN_INIT); } else { pc_set_bit(current_block, bitmap); log_mesg(3, 0, 0, fs_opt.debug, "%s: used block %llu at group %lu\n", __FILE__, current_block, group); } /// update progress update_pui(&prog, current_block, current_block, 0);//keep update } blk_itr += fs->super->s_blocks_per_group; } /// check free blocks in group #ifdef EXTFS_1_41 if (gfree != fs->group_desc[group].bg_free_blocks_count){ #else if (gfree != ext2fs_bg_free_blocks_count(fs, group)){ #endif if (!B_UN_INIT) log_mesg(0, 1, 1, fs_opt.debug, "%s: bitmap error at %lu group.\n", __FILE__, group); else ext4_gfree_mismatch = 1; } } /// check all free blocks in partition if (free != fs->super->s_free_blocks_count) { if ((fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) && (ext4_gfree_mismatch)) log_mesg(1, 0, 0, fs_opt.debug, "%s: EXT4 bitmap metadata mismatch\n", __FILE__); else log_mesg(0, 1, 1, fs_opt.debug, "%s: bitmap free count err, free:%llu\n", __FILE__, free); } fs_close(); /// update progress update_pui(&prog, 1, 1, 1);//finish } /// get extfs type static int test_extfs_type(char* device){ int ext2 = 1; int ext3 = 2; int ext4 = 3; int device_type; fs_open(device); if(fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM){ log_mesg(1, 0, 0, fs_opt.debug, "%s: test feature as EXT4\n", __FILE__); device_type = ext4; } else if (fs->super->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL){ log_mesg(1, 0, 0, fs_opt.debug, "%s: test feature as EXT3\n", __FILE__); device_type = ext3; } else { log_mesg(1, 0, 0, fs_opt.debug, "%s: test feature as EXT2\n", __FILE__); device_type = ext2; } fs_close(); return device_type; }