string *readLinesFromStream(FILE *infile) { string *buffer, *nbuffer, line; int i, n, size; n = 0; size = INITIAL_BUFFER_SIZE; buffer = newArray(size, string); while (true) { line = readLine(infile); if (line == NULL) break; if (n == size) { size *= 2; nbuffer = newArray(size, string); for (i = 0; i < n; i++) { nbuffer[i] = buffer[i]; } freeBlock(buffer); buffer = nbuffer; } buffer[n++] = line; } nbuffer = newArray(n + 1, string); for (i = 0; i < n; i++) { nbuffer[i] = buffer[i]; } nbuffer[n] = NULL; freeBlock(buffer); return nbuffer; }
string readLine(FILE *infile) { string line, nline; int n, ch, size; n = 0; size = INITIAL_BUFFER_SIZE; line = (string) getBlock(size + 1); while (true) { ch = getc(infile); if (ch == '\n' || ch == EOF) break; if (ch == '\r') { ch = getc(infile); if (ch != '\n') ungetc(ch, infile); break; } if (n == size) { size *= 2; nline = (string) getBlock(size + 1); strncpy(nline, line, n); freeBlock(line); line = nline; } line[n++] = ch; } if (n == 0 && ch == EOF) { freeBlock(line); return NULL; } line[n] = '\0'; nline = (string) getBlock(n + 1); strcpy(nline, line); freeBlock(line); return nline; }
void writeINODEData(INODE * inodeP, char c) //往INODE数据区结尾写数据 { if((inodeP->length+1) % superBlockPointer->blockSize == 0){ //需要新增扇区 //获得为原来文件夹新增的扇区 BLOCK * blockP = createBlock(); //在原来文件夹的间接扇区上增加新增的扇区编号 inodeMemAddBlock(inodeP, blockP->blockNumber); char * mem = (char *)Malloc(superBlockPointer->blockSize); readBlock(blockP, mem); mem[(inodeP->length+1) % superBlockPointer->blockSize] = c; free(mem); freeBlock(blockP); inodeP->length ++; } else{ int blockNumber = getCurrentBlockNumber(inodeP); BLOCK * blockP = getBlock(blockNumber); char * mem = (char *)Malloc(superBlockPointer->blockSize); readBlock(blockP, mem); mem[(inodeP->length+1) % superBlockPointer->blockSize] = c; free(mem); freeBlock(blockP); inodeP->length ++; } }
void freeIterator(Iterator iterator) { Cell *cp; while ((cp = iterator->head) != NULL) { iterator->head = cp->link; freeBlock(cp); } freeBlock(iterator); }
///////////////////////////////////////////////////////////// // To free the cluster, find out which pool it came from and // put it back there ///////////////////////////////////////////////////////////// void freeCluster(cluster* cl) { deprintf("freeCluster %p\n",cl); if(cl->endpoints == 2) { drun(bcs--); deprintf("free binary\n"); freeBlock(currentTree->BClusters,(char*) cl); } else { deprintf("free non-bnary\n"); drun(ucs--); freeBlock(currentTree->UClusters,(char*) cl); } }
void freeVertexCluster(cluster* cl,tree_t* tree) { deprintf("freeCluster %p\n",cl); if(cl->endpoints == 2) { deprintf("free binary\n"); bcs--; freeBlock(tree->BClusters,(char*) cl); } else { deprintf("free non-bnary\n"); ucs--; freeBlock(tree->UClusters,(char*) cl); } }
void loadObject(string pathname) { HandleList hl; static char fullPath[MAX_PATH_NAME]; hl = newBlock(HandleList); switch (pathname[0]) { case '/': strcpy(fullPath, pathname); break; case '~': strcpy(fullPath, expandPathname(pathname)); break; default: getcwd(fullPath, MAX_PATH_NAME); strcat(fullPath, "/"); strcat(fullPath, pathname); break; } hl->handle = dlopen(fullPath, RTLD_NOW); if (hl->handle == NULL) { freeBlock(hl); error("loadObject: %s", dlerror()); } hl->link = handles; handles = hl; }
void writeRoot(SUPER_BLOCK * superBlockP) { //只有初始化时才会调用到这个文件,通常是调用下面的读方法 //inodeP的inodeNumber就是0 INODE * inodeP = createINODE(_755_AUTHORITY_DIR_); // 默认drwxr-xr-x 755 int blockNumber = getFreeBlockNumber(superBlockPointer); BLOCK * blockP = getBlock(blockNumber); //INODE增加分配的扇区的记录 inodeMemAddBlock(inodeP, blockNumber); initBlock(blockP); freeBlock(blockP); writeINODE(inodeP); char str[32]; //添加父文件夹节点到当前文件夹 memset(str, 0, 32); strcpy(str, ".."); *(unsigned int*)(str+28) = inodeP->inodeNumber; inodeDirAddFile(inodeP, str, 32); //添加当前文件节点到当前文件夹 memset(str, 0, 32); strcpy(str, "."); *(unsigned int*)(str+28) = inodeP->inodeNumber; inodeDirAddFile(inodeP, str, 32); superBlockP->inode = inodeP; }
void freeGObject(GObject gobj) { Vector contents; int i, n; if (gobj->type == GPOLYGON) { contents = gobj->u.polygonRep.vertices; n = sizeVector(contents); for (i = 0; i < n; i++) { freeBlock(getVector(contents, i)); } freeBlock(contents); } else if (gobj->type == GCOMPOUND) { freeBlock(gobj->u.compoundRep.contents); } freeBlock(gobj); }
inline extern void deleteDir (char *pattern) { struct dirEntryStruct *entry; entryIndex_t entryIndex = 0; while ((entry = getEntry (entryIndex++))) { /* only process non-deleted dirs */ if (entry->startBlock) { /* only delete dirs that match pattern */ if (filenameMatch (entry->fileName, pattern)) { block_t blockWithEntry = dirBufferBlock; /* only delete dirs */ if (entry->fileType == DIR) { getBlock (entry->startBlock); /* only delete empty dirs */ if (!getUsedEntryInCurrentBlock()) { /* delete directory */ getBlock (blockWithEntry); freeBlock (entry->startBlock); deleteEntry (entry); flushDirBuffer(); flushFreeBlockList(); /* begin counting at 0 because direntries may have been moved */ entryIndex = 0; } } } } } }
/* delGrid: * Close and free all grid resources. */ void delGrid(Grid * g) { dtclose(g->data); freeBlock(g->cellMem); free(g->listMem); free(g); }
static bool randomIntegerSeemsReasonable(int low, int high) { int i, k, rangeSize, *counts, outcome; bool ok; double expected; rangeSize = high - low + 1; counts = newArray(rangeSize, int); for (i = 0; i < rangeSize; i++) { counts[i] = 0; } ok = true; for (i = 0; ok && i < N_TRIALS; i++) { k = randomInteger(low, high); if (k < low || k > high) { reportError("randomInteger returned out of range value %d", k); ok = false; } else { counts[k - low]++; } } expected = (double) N_TRIALS / rangeSize; for (i = 0; ok && i < rangeSize; i++) { outcome = low + i; if (counts[i] < 0.5 * expected) { reportError("Low count for outcome %d", outcome); ok = false; } else if (counts[i] > 1.5 * expected) { reportError("High count for outcome %d", outcome); ok = false; } } freeBlock(counts); return ok; }
int deleteFile (char *pattern) { struct dirEntryStruct *entry; entryIndex_t entryIndex = 0; int count =0; while ((entry = getEntry (entryIndex++))) { /* only process non-deleted files */ if (entry->startBlock) { /* only delete files that match pattern */ if (filenameMatch (entry->fileName, pattern)) { /* only delete non-locked files */ if ((entry->fileType != DIR) && !(entry->readOnly)) { block_t tmpBufferBlock; bufferSize_t tmpBufferPtr; /* get inode of file to delete */ ATTENTION_OFF(); tmpBufferBlock = entry->startBlock; //ataGetBlock (tmpBufferBlock, tmpBuffer); tmpBufferPtr = 0; /* free all blocks belonging to file */ while (entry->fileSize) { // if at the end of inode, get next inode if (tmpBufferPtr == (BLOCKSIZE / sizeof (block_t)) - 1) { freeBlock (tmpBufferBlock); tmpBufferBlock = ((block_t *)tmpBuffer)[tmpBufferPtr]; //ataGetBlock (tmpBufferBlock, tmpBuffer);//get next block tmpBufferPtr = 0; } // free block freeBlock (((block_t *)tmpBuffer)[tmpBufferPtr++]); entry->fileSize--; } /* free last inode block */ freeBlock (tmpBufferBlock); /* remove entry from directory */ deleteEntry (entry); flushDirBuffer(); flushFreeBlockList(); count ++; /* begin counting at 0 because direntries may have been moved */ entryIndex = 0; }ATTENTION_ON(); } } } return count; }
/* freeBlock: * Free malloc'ed memory and block. * Recurse to next block */ static void freeBlock(block_t * b) { if (b) { block_t *next = b->next; free(b->mem); free(b); freeBlock(next); } }
void MarkedSpace::freeOrShrinkBlock(MarkedBlock* block) { if (!block->isEmpty()) { block->shrink(); return; } freeBlock(block); }
/////////////////////////////////////////////////////// // Go over the list of old trees, and for each one of // them push down their data onto the new trees /////////////////////////////////////////////////////// void pushDownList(clusterList* rootList) { while(rootList->head != NULL) { cluster* cl = removeCluster(rootList); pushDown(cl); freeBlock(currentTree->UClusters,(char*) cl); deprintf("done 1\n"); } deprintf("done\n"); }
int freeINode(int fd, int inodeNo){ struct INode in; int i; readINode(fd, inodeNo, &in); for(i=0; i<13 && in.i_blocks[i]>0; i++){ freeBlock(fd, in.i_blocks[i]); } lseek(fd, INODEBLOCKSTART + inodeNo*sizeof(struct INode), SEEK_SET); write(fd, &nullINode, sizeof(struct INode)); printf("Freed inode number %d\n", inodeNo); s.sb_nfreeinodes++; }
void freeBlocktree(block_t * bp) { block_t *child; block_t *next; for (child = bp->children.first; child; child = next) { next = child->next; freeBlocktree(child); } freeBlock(bp); }
/* * ======== balloc ======== * This allocation function allocates memory from the lowest addresses * first. */ static bool allocBlock(struct RMM_TargetObj *target, u32 segid, u32 size, u32 align, u32 *dspAddr) { struct RMM_Header *head; struct RMM_Header *prevhead = NULL; struct RMM_Header *next; u32 tmpalign; u32 alignbytes; u32 hsize; u32 allocsize; u32 addr; alignbytes = (align == 0) ? 1 : align; prevhead = NULL; head = target->freeList[segid]; do { hsize = head->size; next = head->next; addr = head->addr; /* alloc from the bottom */ /* align allocation */ (tmpalign = (u32) addr % alignbytes); if (tmpalign != 0) tmpalign = alignbytes - tmpalign; allocsize = size + tmpalign; if (hsize >= allocsize) { /* big enough */ if (hsize == allocsize && prevhead != NULL) { prevhead->next = next; MEM_Free(head); } else { head->size = hsize - allocsize; head->addr += allocsize; } /* free up any hole created by alignment */ if (tmpalign) freeBlock(target, segid, addr, tmpalign); *dspAddr = addr + tmpalign; return true; } prevhead = head; head = next; } while (head != NULL); return false; }
/* * ======== RMM_free ======== */ Bool RMM_free(RMM_Handle rmm, UInt32 addr, UInt32 size) { Bool status = TRUE; Assert_isTrue(rmm != NULL, (Assert_Id)NULL); Assert_isTrue(size > 0, (Assert_Id)NULL); /* Free memory */ status = freeBlock(rmm, addr, size); return (status); }
/////////////////////////////////////////////////////////////////////////// // remove the first cluster from the list /////////////////////////////////////////////////////////////////////////// cluster* removeCluster(clusterList* list) { if(list->head == NULL) return NULL; else { cluster* ret = list->head->cl; clusterNode* prev = list->head; list->head = list->head->next; freeBlock(theList,(char*)prev); return ret; } }
double getReal(void) { string line; double value; char termch; while (true) { line = getLine(); if (line == NULL) error("getReal: unexpected end of file"); switch (sscanf(line, " %lf %c", &value, &termch)) { case 1: freeBlock(line); return value; case 2: printf("Unexpected character: '%c'\n", termch); break; default: printf("Please enter a real number\n"); break; } freeBlock(line); printf("Retry: "); } }
/* * ======== RMM_free ======== */ Bool RMM_free(RMM_Handle rmm, UInt32 addr, UInt32 size) { Bool status = TRUE; //DBC_require(rmm != NULL); //DBC_require(size > 0); GT_assert(ti_sdo_fc_dman3_GTMask, rmm != NULL); GT_assert(ti_sdo_fc_dman3_GTMask, size > 0); /* Free memory */ status = freeBlock(rmm, addr, size); return (status); }
void deleteEntry (struct dirEntryStruct *entry) { block_t oldBlock = dirBufferBlock; /* if directory has more blocks in chain, move an entry from the last block in chain to this block */ if (dirBuffer.nextBlock) { struct dirEntryStruct *entryToMove; struct dirEntryStruct copyOfEntry; block_t prevBlock = oldBlock; /* find the last block in dir chain */ while (dirBuffer.nextBlock) { prevBlock = dirBufferBlock; getBlock (dirBuffer.nextBlock); } /* if no entries in last dir block, remove the last dir block */ if (!(entryToMove = getUsedEntryInCurrentBlock())) { getBlock (prevBlock); freeBlock (dirBuffer.nextBlock); flushFreeBlockList(); dirBuffer.nextBlock = 0; dirBufferChanged = TRUE; /* get an entry to move */ if (!(entryToMove = getUsedEntryInCurrentBlock())) { /* TODO: error recovery */ } } /* overwrite the entry to be deleted with the one just found */ if (dirBufferBlock != oldBlock) { memcpy (©OfEntry, entryToMove, sizeof (struct dirEntryStruct)); entryToMove->startBlock = 0; dirBufferChanged = TRUE; getBlock (oldBlock); memcpy (entry, ©OfEntry, sizeof (struct dirEntryStruct)); dirBufferChanged = TRUE; } else { /* delete entry */ entry->startBlock = 0;/* a eletet file has del type (0) and is not closed.*/ entry->fileType= DEL; entry->splat =FALSE; dirBufferChanged = TRUE; } } else { /* delete entry */ entry->startBlock = 0; /* a eletet file has del type (0) and is not closed.*/ dirBufferChanged = TRUE; entry->fileType= DEL; entry->splat =FALSE; } }
static bool stepListIterator(Iterator iterator, void *dst) { Cell *cp; void *dp; cp = iterator->head; if (cp == NULL) { iterator->tail = NULL; return false; } dp = ((char *) cp) + sizeof (Cell); memcpy(dst, dp, iterator->elementSize); iterator->head = cp->link; freeBlock(cp); return true; }
void inodeDirAddFile(INODE * inodeP, void * mem, int length) { if(length != 32){ return ; } void * _mem = Malloc(superBlockPointer->blockSize); //当前扇区上添加新增的文件数据。 及文件名和文件的INODE编号 BLOCK * blockP = getBlock(getCurrentBlockNumber(inodeP)); readBlock(blockP, _mem); memcpy((char*)_mem + inodeP->length % superBlockPointer->blockSize, mem, 32); writeBlock(blockP, _mem); freeBlock(blockP); inodeP->length += 32; writeINODE(inodeP); free(_mem); }
/* * ======== RMM_free ======== */ bool RMM_free(struct RMM_TargetObj *target, u32 segid, u32 addr, u32 size, bool reserved) { struct RMM_OvlySect *sect; bool retVal = true; DBC_Require(MEM_IsValidHandle(target, RMM_TARGSIGNATURE)); DBC_Require(reserved || segid < target->numSegs); DBC_Require(reserved || (addr >= target->segTab[segid].base && (addr + size) <= (target->segTab[segid].base + target->segTab[segid].length))); GT_5trace(RMM_debugMask, GT_ENTER, "RMM_free(0x%lx, 0x%lx, 0x%lx, 0x%lx, " "0x%lx)\n", target, segid, addr, size, reserved); /* * Free or unreserve memory. */ if (!reserved) { retVal = freeBlock(target, segid, addr, size); if (retVal) target->segTab[segid].number--; } else { /* Unreserve memory */ sect = (struct RMM_OvlySect *)LST_First(target->ovlyList); while (sect != NULL) { if (addr == sect->addr) { DBC_Assert(size == sect->size); /* Remove from list */ LST_RemoveElem(target->ovlyList, (struct LST_ELEM *)sect); MEM_Free(sect); break; } sect = (struct RMM_OvlySect *)LST_Next(target->ovlyList, (struct LST_ELEM *)sect); } if (sect == NULL) retVal = false; } return retVal; }
/* remove the front of the queue */ void* dequeue(Queue* queue) { assert (queue->front != queue->back); QNode* tmp = queue->front->next; void* ret = tmp->data; queue->front->next = tmp->next; // Check if this was the last node.X if (tmp == queue->back) queue->back = queue->front; freeBlock(queue->flist, (char*) tmp); deprintf("\n\nDequeued %d ,%p \n",((node *) ret)->nId, ret); return ret; }
void freeOperand(operandType *operand) { nullFree(operand); switch (operand->kindOfOperand) { case EXPRESSION_OPND: case IMMEDIATE_OPND: case INDIRECT_OPND: case POST_INDEXED_Y_OPND: case PRE_INDEXED_X_OPND: case X_INDEXED_OPND: case Y_INDEXED_OPND: freeExpression(operand->theOperand.expressionUnion); break; case A_REGISTER_OPND: case X_REGISTER_OPND: case Y_REGISTER_OPND: break; case X_SELECTED_OPND: case Y_SELECTED_OPND: case PRE_SELECTED_X_OPND: freeSelectionList(operand->theOperand.xSelectedUnion); break; case STRING_OPND: freeString(operand->theOperand.stringUnion); break; case BLOCK_OPND: freeBlock(operand->theOperand.blockUnion); break; default: botch("bad operand kind in freeOperand %d\n", operand->kindOfOperand); break; } freeOperand(operand->nextOperand); free(operand); }
INODE * createINODE(unsigned int authority) { int inodePos = getFreeInodeNumber(superBlockPointer); INODE * inodeP = (INODE*)Malloc(sizeof(INODE)); inodeP->mem = Malloc(superBlockPointer->blockSize); inodeP->inodeNumber = inodePos; inodeP->GID = currentUser->GID; inodeP->UID = currentUser->UID; time(&inodeP->cTime); //当前时间 memcpy(&inodeP->mTime, &inodeP->cTime, sizeof(time_t)); //保证三个时间一致 memcpy(&inodeP->aTime, &inodeP->cTime, sizeof(time_t)); //time(&inodeP->mTime); //修改时间 //time(&inodeP->aTime); //访问时间 inodeP->authority = authority; BLOCK * blockP = createBlock(); inodeP->blockNumber = blockP->blockNumber; freeBlock(blockP); inodeP->length = 0; return inodeP; }