/** @brief 分配新的DBD空间 */ struct dbd* fss_dbd_malloc(struct fss_file * who) { struct dbd * ret = NULL; ke_spin_lock(&cache_desc()->dbd_stack_lock); if (cache_desc()->dbd_stack_top != NULL) { ret = cache_desc()->dbd_stack_top; cache_desc()->dbd_stack_top = (struct dbd *)cache_desc()->dbd_stack_top->list.prev; } ke_spin_unlock(&cache_desc()->dbd_stack_lock); if (ret != NULL) { memset(ret, 0, sizeof(struct dbd)); ke_spin_init(&ret->lock); /* The dbd may be deleted before entering the LRU lit, so it's empty */ INIT_LIST_HEAD(&ret->list); /* Dirty list is empty, we will insert it to global dirty list */ INIT_LIST_HEAD(&ret->dirty_list); /* No map */ INIT_LIST_HEAD(&ret->map_list); ke_spin_init(&ret->map_lock); ret->who = who; } return ret; }
/** @brief DB cache初始化 */ bool fss_db_init(void) { int i = 0; struct dbd *dbd_prev = NULL; cache_desc()->cache_size = FSS_CACHE_SIZE; cache_desc()->db_count = cache_desc()->cache_size / FSS_CACHE_DB_SIZE; /* The db virtual memory */ #ifdef _MSC_VER cache_desc()->db_cache = (char*)malloc(cache_desc()->cache_size); #else cache_desc()->db_cache = km_alloc_virtual(cache_desc()->cache_size, KM_PROT_READ | KM_PROT_WRITE); printf("fss_db_cache = %x", cache_desc()->db_cache); #endif if (cache_desc()->db_cache == NULL) goto err1; /* 分配DB栈 */ #ifdef _MSC_VER cache_desc()->db_stack = (unsigned long *)malloc(cache_desc()->db_count * sizeof(unsigned long)); #else cache_desc()->db_stack = (unsigned long *)km_valloc(cache_desc()->db_count * sizeof(unsigned long)); printf(", fss_db_stack = %x.\n", cache_desc()->db_stack); #endif if (cache_desc()->db_stack == NULL) goto err2; /* DB节点入栈 */ for (i = 0; i < cache_desc()->db_count; i++) cache_desc()->db_stack[i] = (unsigned long)cache_desc()->db_cache + (i * FSS_CACHE_DB_SIZE); cache_desc()->db_stack_top = cache_desc()->db_count - 1; /* 分配DBD */ #ifdef _MSC_VER cache_desc()->dbd_cache = (struct dbd*)malloc((cache_desc()->db_count) * sizeof(struct dbd)); #else cache_desc()->dbd_cache = (struct dbd*)km_valloc((cache_desc()->db_count) * sizeof(struct dbd)); #endif if (cache_desc()->dbd_cache == NULL) goto err3; /* DBD节点入栈 */ dbd_prev = NULL; for (i = 0; i < (cache_desc()->db_count); i++) { cache_desc()->dbd_stack_top = &(cache_desc()->dbd_cache[i]); cache_desc()->dbd_stack_top->buffer = NULL; cache_desc()->dbd_stack_top->list.prev = (void*)dbd_prev; dbd_prev = cache_desc()->dbd_stack_top; } ke_spin_init(&cache_desc()->db_stack_lock); ke_spin_init(&cache_desc()->dbd_stack_lock); ke_spin_init(&cache_desc()->dirty_list_lock); INIT_LIST_HEAD(&cache_desc()->sort_list); INIT_LIST_HEAD(&cache_desc()->dirty_list); ke_spin_init(&cache_desc()->sort_lock); //TODO: //Write back thread return true; err3: km_vfree(cache_desc()->db_stack); err2: km_dealloc_virtual(cache_desc()->db_cache); cache_desc()->db_count = 0; err1: return false; }