static int yaffs_CreateTnodes(yaffs_Device * dev, int nTnodes) { int i; int tnodeSize; yaffs_Tnode *newTnodes; __u8 *mem; yaffs_Tnode *curr; yaffs_Tnode *next; yaffs_TnodeList *tnl; if (nTnodes < 1) return YAFFS_OK; /* Calculate the tnode size in bytes for variable width tnode support. * Must be a multiple of 32-bits */ //计算每个yaffs_Tnode的大小,yaffs_Tnode可能用于YAFFS_NTNODES_INTERNAL个存储指针或者存储YAFFS_NTNODES_LEVEL0个页块号,结果取两者中较大的值。 tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8; if(tnodeSize < sizeof(yaffs_Tnode)) tnodeSize = sizeof(yaffs_Tnode); /* make these things */ //为nTnodes个yaffs_Tnode分配内存 newTnodes = (yaffs_Tnode *)YMALLOC(nTnodes * tnodeSize); mem = (__u8 *)newTnodes; if (!newTnodes) { T(YAFFS_TRACE_ERROR, (TSTR("yaffs: Could not allocate Tnodes" TENDSTR))); return YAFFS_FAIL; } /* Hook them into the free list */ #if 0 for (i = 0; i < nTnodes - 1; i++) { newTnodes[i].internal[0] = &newTnodes[i + 1]; #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG newTnodes[i].internal[YAFFS_NTNODES_INTERNAL] = (void *)1; #endif } newTnodes[nTnodes - 1].internal[0] = dev->freeTnodes; #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG newTnodes[nTnodes - 1].internal[YAFFS_NTNODES_INTERNAL] = (void *)1; #endif dev->freeTnodes = newTnodes; #else /* New hookup for wide tnodes */ //将这些yaffs_Tnode链接起来 for(i = 0; i < nTnodes -1; i++) { curr = (yaffs_Tnode *) &mem[i * tnodeSize]; next = (yaffs_Tnode *) &mem[(i+1) * tnodeSize]; curr->internal[0] = next; } curr = (yaffs_Tnode *) &mem[(nTnodes - 1) * tnodeSize]; curr->internal[0] = dev->freeTnodes; dev->freeTnodes = (yaffs_Tnode *)mem;//将创建的空闲yaffs_Tnode接到dev->freeTnodes #endif dev->nFreeTnodes += nTnodes; dev->nTnodesCreated += nTnodes; /* Now add this bunch of tnodes to a list for freeing up. * NB If we can't add this to the management list it isn't fatal * but it just means we can't free this bunch of tnodes later. */ //记录连续yaffs_Tnode内存空间块的起始位置,并链接到dev->allocatedTnodeList,将来可能用于释放内存 tnl = (yaffs_TnodeList *)YMALLOC(sizeof(yaffs_TnodeList)); if (!tnl) { T(YAFFS_TRACE_ERROR, (TSTR ("yaffs: Could not add tnodes to management list" TENDSTR))); return YAFFS_FAIL; } else { tnl->tnodes = newTnodes; tnl->next = dev->allocatedTnodeList; dev->allocatedTnodeList = tnl; } T(YAFFS_TRACE_ALLOCATE, (TSTR("yaffs: Tnodes added" TENDSTR))); return YAFFS_OK; } /* GetTnode gets us a clean tnode. Tries to make allocate more if we run out */ //些函数用于得到一个未使用的yaffs_Tnode static yaffs_Tnode *yaffs_GetTnodeRaw(yaffs_Device * dev) { yaffs_Tnode *tn = NULL; /* If there are none left make more */ //如果没有空闲yaffs_Tnode的可用,则创建新的 if (!dev->freeTnodes) { yaffs_CreateTnodes(dev, YAFFS_ALLOCATION_NTNODES); } if (dev->freeTnodes) { tn = dev->freeTnodes; #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG if (tn->internal[YAFFS_NTNODES_INTERNAL] != (void *)1) { /* Hoosterman, this thing looks like it isn't in the list */ T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Tnode list bug 1" TENDSTR))); } #endif //取出yaffs_Tnode并将dev->freeTnodes指针指向下一个未使用的yaffs_Tnode dev->freeTnodes = dev->freeTnodes->internal[0]; dev->nFreeTnodes--; } //Checkpoint的大小可能发生改变,需要重新计算 dev->nCheckpointBlocksRequired = 0; /* force recalculation*/ return tn; } //些函数用于得到一个未使用的yaffs_Tnode,并初始化为空 static yaffs_Tnode *yaffs_GetTnode(yaffs_Device * dev) { yaffs_Tnode *tn = yaffs_GetTnodeRaw(dev); int tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8; if(tnodeSize < sizeof(yaffs_Tnode)) tnodeSize = sizeof(yaffs_Tnode); if(tn) memset(tn, 0, tnodeSize); return tn; } //标记一个yaffs_Tnode为空闲,将它链接到yaffs_Tnode空闲列表 /* FreeTnode frees up a tnode and puts it back on the free list */ static void yaffs_FreeTnode(yaffs_Device * dev, yaffs_Tnode * tn) { if (tn) { #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG if (tn->internal[YAFFS_NTNODES_INTERNAL] != 0) { /* Hoosterman, this thing looks like it is already in the list */ T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Tnode list bug 2" TENDSTR))); } tn->internal[YAFFS_NTNODES_INTERNAL] = (void *)1; #endif //将它链接到yaffs_Tnode空闲列表 tn->internal[0] = dev->freeTnodes; dev->freeTnodes = tn; dev->nFreeTnodes++; } dev->nCheckpointBlocksRequired = 0; /* force recalculation*/ } //释放所有yaffs_Tnode链表内存空间 static void yaffs_DeinitialiseTnodes(yaffs_Device * dev) { /* Free the list of allocated tnodes */ yaffs_TnodeList *tmp; while (dev->allocatedTnodeList) { tmp = dev->allocatedTnodeList->next; YFREE(dev->allocatedTnodeList->tnodes); YFREE(dev->allocatedTnodeList); dev->allocatedTnodeList = tmp; } dev->freeTnodes = NULL; dev->nFreeTnodes = 0; } //初始化Tnode相关变量 static void yaffs_InitialiseTnodes(yaffs_Device * dev) { dev->allocatedTnodeList = NULL; dev->freeTnodes = NULL; dev->nFreeTnodes = 0; dev->nTnodesCreated = 0; }
Tnodes创建和释放
最新推荐文章于 2022-11-28 11:52:19 发布