📄 memory.c
字号:
size += sizeof(struct longMemoryPtr); theLongMemory = ((struct longMemoryPtr *) ptr) - 1; if (theLongMemory->prev == NULL) { TopLongMemoryPtr = TopLongMemoryPtr->next; TopLongMemoryPtr->prev = NULL; } else { theLongMemory->prev->next = theLongMemory->next; if (theLongMemory->next != NULL) { theLongMemory->next->prev = theLongMemory->next; } }#endif#if MAC || IBM_ICB MemoryAmount -= size; MemoryCalls--; SpecialFree(ptr); return(0);#endif#if IBM_TBC || IBM_ZTC || IBM_SC MemoryAmount -= size; MemoryCalls--; SpecialFree(ptr); return(0);#endif#if IBM_MSC MemoryAmount -= size; MemoryCalls--; return(SpecialFree(ptr));#endif }#if IBM_TBC#pragma warn +rch#pragma warn +ccc#endif/*********************************//* MemUsed: C access routine for *//* the mem-requests command. *//*********************************/globle long int MemUsed() { return(MemoryAmount); }/*************************************//* MemRequests: C access routine for *//* the mem-requests command. *//*************************************/globle long int MemRequests() { return(MemoryCalls); }/******************************************************************************//* UpdateMemoryUsed: Allows the amount of memory used by CLIPS to be updated. *//******************************************************************************/globle long int UpdateMemoryUsed(value) long int value; { MemoryAmount += value; return(MemoryAmount); }/**********************************************//* UpdateMemoryRequests: Allows the number of *//* memory requests to CLIPS to be updated. *//**********************************************/globle long int UpdateMemoryRequests(value) long int value; { MemoryCalls += value; return(MemoryCalls); }/************************************//* ReleaseMem: C access routine for *//* the release-mem command. *//************************************/globle long int ReleaseMem(maximum,printMessage) long int maximum; int printMessage; { struct memoryPtr *tmpPtr, *memPtr; int i; long int amount = 0; if (printMessage == CLIPS_TRUE) { PrintCLIPS(WDIALOG,"\n*** DEALLOCATING MEMORY ***\n"); } for (i = (MEM_TABLE_SIZE - 1) ; i >= sizeof(char *) ; i--) { memPtr = MemoryTable[i]; while (memPtr != NULL) { tmpPtr = memPtr->next; genfree((VOID *) memPtr,(unsigned) i); memPtr = tmpPtr; amount += i; } MemoryTable[i] = NULL; if ((amount > maximum) && (maximum > 0)) { if (printMessage == CLIPS_TRUE) { PrintCLIPS(WDIALOG,"*** MEMORY DEALLOCATED ***\n"); } return(amount); } } if (printMessage == CLIPS_TRUE) { PrintCLIPS(WDIALOG,"*** MEMORY DEALLOCATED ***\n"); } return(amount); }/*****************************************************//* gm1: Allocates memory and sets all bytes to zero. *//*****************************************************/globle VOID *gm1(size) int size; { struct memoryPtr *memPtr; char *tmpPtr; int i; if (size < sizeof(char *)) size = sizeof(char *); if (size >= MEM_TABLE_SIZE) { tmpPtr = (char *) genalloc((unsigned) size); for (i = 0 ; i < size ; i++) { tmpPtr[i] = '\0'; } return((VOID *) tmpPtr); } memPtr = (struct memoryPtr *) MemoryTable[size]; if (memPtr == NULL) { tmpPtr = (char *) genalloc((unsigned) size); for (i = 0 ; i < size ; i++) { tmpPtr[i] = '\0'; } return((VOID *) tmpPtr); } MemoryTable[size] = memPtr->next; tmpPtr = (char *) memPtr; for (i = 0 ; i < size ; i++) { tmpPtr[i] = '\0'; } return ((VOID *) tmpPtr); }/*****************************************************//* gm2: Allocates memory and does not initialize it. *//*****************************************************/globle VOID *gm2(size) int size; { struct memoryPtr *memPtr; if (size < sizeof(char *)) size = sizeof(char *); if (size >= MEM_TABLE_SIZE) return(genalloc((unsigned) size)); memPtr = (struct memoryPtr *) MemoryTable[size]; if (memPtr == NULL) { return(genalloc((unsigned) size)); } MemoryTable[size] = memPtr->next; return ((VOID *) memPtr); }/*****************************************************//* gm3: Allocates memory and does not initialize it. *//*****************************************************/globle VOID *gm3(size) long size; { struct memoryPtr *memPtr; if (size < sizeof(char *)) size = sizeof(char *); if (size >= MEM_TABLE_SIZE) return(genlongalloc((unsigned long) size)); memPtr = (struct memoryPtr *) MemoryTable[(int) size]; if (memPtr == NULL) { return(genalloc((unsigned int) size)); } MemoryTable[(int) size] = memPtr->next; return ((VOID *) memPtr); }/**********************************************//* rm: Returns a block of memory to the CLIPS *//* maintained pool of free memory. *//**********************************************/globle int rm(str,size) VOID *str; int size; { struct memoryPtr *memPtr; if (size == 0) { CLIPSSystemError("MEMORY",1); ExitCLIPS(3); } if (size < sizeof(char *)) size = sizeof(char *); if (size >= MEM_TABLE_SIZE) return(genfree((VOID *) str,(unsigned) size)); memPtr = (struct memoryPtr *) str; memPtr->next = MemoryTable[size]; MemoryTable[size] = memPtr; return(1); }/******************************************************************//* rm3: Returns a block of memory to the CLIPS maintained pool of *//* free memory that's size is indicated with a long integer. *//******************************************************************/globle int rm3(str,size) VOID *str; long size; { struct memoryPtr *memPtr; if (size == 0) { CLIPSSystemError("MEMORY",1); ExitCLIPS(3); } if (size < sizeof(char *)) size = sizeof(char *); if (size >= MEM_TABLE_SIZE) return(genlongfree((VOID *) str,(unsigned long) size)); memPtr = (struct memoryPtr *) str; memPtr->next = MemoryTable[(int) size]; MemoryTable[(int) size] = memPtr; return(1); }/*********************************************************//* PoolSize: Returns number of bytes in CLIPS free pool. *//*********************************************************/globle unsigned long PoolSize() { register int i; struct memoryPtr *memPtr; unsigned long cnt = 0; for (i = sizeof(char *) ; i < MEM_TABLE_SIZE ; i++) { memPtr = MemoryTable[i]; while (memPtr != NULL) { cnt += (unsigned long) i; memPtr = memPtr->next; } } return(cnt); }/***************************************************************//* ActualPoolSize : Returns number of bytes DOS requires to *//* store the CLIPS free pool. This routine is functionally *//* equivalent to pool_size on anything other than the IBM-PC *//***************************************************************/globle unsigned long ActualPoolSize() {#if IBM_TBC register int i; struct memoryPtr *memPtr; unsigned long cnt = 0; for (i = sizeof(char *) ; i < MEM_TABLE_SIZE ; i++) { memPtr = MemoryTable[i]; while (memPtr != NULL) { /*==============================================================*/ /* For a block of size n, the Turbo-C Library routines require */ /* a header of size 8 bytes and further require that all memory */ /* allotments be paragraph (16-bytes) aligned. */ /*==============================================================*/ cnt += (((unsigned long) i) + 19L) & 0xfffffff0L; memPtr = memPtr->next; } } return(cnt);#else return(PoolSize());#endif }/********************************************//* SetConserveMemory: Allows the setting of *//* the memory conservation flag. *//********************************************/globle BOOLEAN SetConserveMemory(value) BOOLEAN value; { int ov; ov = ConserveMemory; ConserveMemory = value; return(ov); }/*******************************************//* GetConserveMemory: Returns the value of *//* the memory conservation flag. *//*******************************************/globle BOOLEAN GetConserveMemory() { return(ConserveMemory); }/**************************//* genmemcpy: *//**************************/globle VOID genmemcpy(dst,src,size) char HUGE_ADDR *dst; char HUGE_ADDR *src; unsigned long size; { unsigned long i; for (i = 0L ; i < size ; i++) dst[i] = src[i]; }/**************************//* BLOCK MEMORY FUNCTIONS *//**************************/#if BLOCK_MEMORY/***************************************************//* InitializeBlockMemory: Initializes block memory *//* management and allocates the first block. *//***************************************************/static int InitializeBlockMemory(requestSize) unsigned int requestSize; { struct chunkInfo *chunkPtr; unsigned int initialBlockSize, usableBlockSize; /*===========================================*/ /* The block memory routines depend upon the */ /* size of a character being 1 byte. */ /*===========================================*/ if (sizeof(char) != 1) { fprintf(stdout, "Size of character data is not 1\n"); fprintf(stdout, "Memory allocation functions may not work\n"); return(0); } ChunkInfoSize = sizeof(struct chunkInfo); ChunkInfoSize = (((ChunkInfoSize - 1) / STRICT_ALIGN_SIZE) + 1) * STRICT_ALIGN_SIZE; BlockInfoSize = sizeof(struct blockInfo); BlockInfoSize = (((BlockInfoSize - 1) / STRICT_ALIGN_SIZE) + 1) * STRICT_ALIGN_SIZE; initialBlockSize = (INITBLOCKSIZE > requestSize ? INITBLOCKSIZE : requestSize); initialBlockSize += ChunkInfoSize * 2 + BlockInfoSize; initialBlockSize = (((initialBlockSize - 1) / STRICT_ALIGN_SIZE) + 1) * STRICT_ALIGN_SIZE; usableBlockSize = initialBlockSize - (2 * ChunkInfoSize) - BlockInfoSize; /* make sure we get a buffer big enough to be usable */ if ((requestSize < INITBLOCKSIZE) && (usableBlockSize <= requestSize + ChunkInfoSize)) { initialBlockSize = requestSize + ChunkInfoSize * 2 + BlockInfoSize; initialBlockSize = (((initialBlockSize - 1) / STRICT_ALIGN_SIZE) + 1) * STRICT_ALIGN_SIZE; usableBlockSize = initialBlockSize - (2 * ChunkInfoSize) - BlockInfoSize; } TopMemoryBlock = (struct blockInfo *) malloc((CLIPS_STD_SIZE) initialBlockSize); if (TopMemoryBlock == NULL) { fprintf(stdout, "Unable to allocate initial memory pool\n"); return(0); } TopMemoryBlock->nextBlock = NULL; TopMemoryBlock->prevBlock = NULL; TopMemoryBlock->nextFree = (struct chunkInfo *) (((char *) TopMemoryBlock) + BlockInfoSize);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -