memalloc.c

来自「clips源代码」· C语言 代码 · 共 1,000 行 · 第 1/3 页

C
1,000
字号
   if (size >= MEM_TABLE_SIZE)     {      tmpPtr = (char *) genalloc(theEnv,(unsigned) size);      for (i = 0 ; i < size ; i++)        { tmpPtr[i] = '\0'; }      return((void *) tmpPtr);     }   memPtr = (struct memoryPtr *) MemoryData(theEnv)->MemoryTable[size];   if (memPtr == NULL)     {      tmpPtr = (char *) genalloc(theEnv,(unsigned) size);      for (i = 0 ; i < size ; i++)        { tmpPtr[i] = '\0'; }      return((void *) tmpPtr);     }   MemoryData(theEnv)->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(  void *theEnv,  size_t size)  {   struct memoryPtr *memPtr;      if (size < sizeof(char *)) size = sizeof(char *);   if (size >= MEM_TABLE_SIZE) return(genalloc(theEnv,(unsigned) size));   memPtr = (struct memoryPtr *) MemoryData(theEnv)->MemoryTable[size];   if (memPtr == NULL)     {      return(genalloc(theEnv,(unsigned) size));     }   MemoryData(theEnv)->MemoryTable[size] = memPtr->next;   return ((void *) memPtr);  }/*****************************************************//* gm3: Allocates memory and does not initialize it. *//*****************************************************/globle void *gm3(  void *theEnv,  size_t size)  {   struct memoryPtr *memPtr;   if (size < (long) sizeof(char *)) size = sizeof(char *);   if (size >= MEM_TABLE_SIZE) return(genalloc(theEnv,size));   memPtr = (struct memoryPtr *) MemoryData(theEnv)->MemoryTable[(int) size];   if (memPtr == NULL)     { return(genalloc(theEnv,size)); }   MemoryData(theEnv)->MemoryTable[(int) size] = memPtr->next;   return ((void *) memPtr);  }/****************************************//* rm: Returns a block of memory to the *//*   maintained pool of free memory.    *//****************************************/globle int rm(  void *theEnv,  void *str,  size_t size)  {   struct memoryPtr *memPtr;   if (size == 0)     {      SystemError(theEnv,"MEMORY",1);      EnvExitRouter(theEnv,EXIT_FAILURE);     }   if (size < sizeof(char *)) size = sizeof(char *);   if (size >= MEM_TABLE_SIZE) return(genfree(theEnv,(void *) str,(unsigned) size));   memPtr = (struct memoryPtr *) str;   memPtr->next = MemoryData(theEnv)->MemoryTable[size];   MemoryData(theEnv)->MemoryTable[size] = memPtr;   return(1);  }/********************************************//* rm3: Returns a block of memory to the    *//*   maintained pool of free memory that's  *//*   size is indicated with a long integer. *//********************************************/globle int rm3(  void *theEnv,  void *str,  size_t size)  {   struct memoryPtr *memPtr;   if (size == 0)     {      SystemError(theEnv,"MEMORY",1);      EnvExitRouter(theEnv,EXIT_FAILURE);     }   if (size < (long) sizeof(char *)) size = sizeof(char *);   if (size >= MEM_TABLE_SIZE) return(genfree(theEnv,(void *) str,(unsigned long) size));   memPtr = (struct memoryPtr *) str;   memPtr->next = MemoryData(theEnv)->MemoryTable[(int) size];   MemoryData(theEnv)->MemoryTable[(int) size] = memPtr;   return(1);  }/***************************************************//* PoolSize: Returns number of bytes in free pool. *//***************************************************/globle unsigned long PoolSize(  void *theEnv)  {   register int i;   struct memoryPtr *memPtr;   unsigned long cnt = 0;   for (i = sizeof(char *) ; i < MEM_TABLE_SIZE ; i++)     {      memPtr = MemoryData(theEnv)->MemoryTable[i];      while (memPtr != NULL)        {         cnt += (unsigned long) i;         memPtr = memPtr->next;        }     }   return(cnt);  }/***************************************************************//* ActualPoolSize : Returns number of bytes DOS requires to    *//*   store the free pool.  This routine is functionally        *//*   equivalent to pool_size on anything other than the IBM-PC *//***************************************************************/globle unsigned long ActualPoolSize(  void *theEnv)  {#if IBM_TBC   register int i;   struct memoryPtr *memPtr;   unsigned long cnt = 0;   for (i = sizeof(char *) ; i < MEM_TABLE_SIZE ; i++)     {      memPtr = MemoryData(theEnv)->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(theEnv));#endif  }/********************************************//* EnvSetConserveMemory: Allows the setting *//*    of the memory conservation flag.      *//********************************************/globle intBool EnvSetConserveMemory(  void *theEnv,  intBool value)  {   int ov;   ov = MemoryData(theEnv)->ConserveMemory;   MemoryData(theEnv)->ConserveMemory = value;   return(ov);  }/*******************************************//* EnvGetConserveMemory: Returns the value *//*    of the memory conservation flag.     *//*******************************************/globle intBool EnvGetConserveMemory(  void *theEnv)  {   return(MemoryData(theEnv)->ConserveMemory);  }/**************************//* genmemcpy:             *//**************************/globle void genmemcpy(  char *dst,  char *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(  void *theEnv,  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);     }   MemoryData(theEnv)->ChunkInfoSize = sizeof(struct chunkInfo);   MemoryData(theEnv)->ChunkInfoSize = (int) ((((MemoryData(theEnv)->ChunkInfoSize - 1) / STRICT_ALIGN_SIZE) + 1) * STRICT_ALIGN_SIZE);   MemoryData(theEnv)->BlockInfoSize = sizeof(struct blockInfo);   MemoryData(theEnv)->BlockInfoSize = (int) ((((MemoryData(theEnv)->BlockInfoSize - 1) / STRICT_ALIGN_SIZE) + 1) * STRICT_ALIGN_SIZE);   initialBlockSize = (INITBLOCKSIZE > requestSize ? INITBLOCKSIZE : requestSize);   initialBlockSize += MemoryData(theEnv)->ChunkInfoSize * 2 + MemoryData(theEnv)->BlockInfoSize;   initialBlockSize = (((initialBlockSize - 1) / STRICT_ALIGN_SIZE) + 1) * STRICT_ALIGN_SIZE;   usableBlockSize = initialBlockSize - (2 * MemoryData(theEnv)->ChunkInfoSize) - MemoryData(theEnv)->BlockInfoSize;   /* make sure we get a buffer big enough to be usable */   if ((requestSize < INITBLOCKSIZE) &&       (usableBlockSize <= requestSize + MemoryData(theEnv)->ChunkInfoSize))     {      initialBlockSize = requestSize + MemoryData(theEnv)->ChunkInfoSize * 2 + MemoryData(theEnv)->BlockInfoSize;      initialBlockSize = (((initialBlockSize - 1) / STRICT_ALIGN_SIZE) + 1) * STRICT_ALIGN_SIZE;      usableBlockSize = initialBlockSize - (2 * MemoryData(theEnv)->ChunkInfoSize) - MemoryData(theEnv)->BlockInfoSize;     }   MemoryData(theEnv)->TopMemoryBlock = (struct blockInfo *) malloc((STD_SIZE) initialBlockSize);   if (MemoryData(theEnv)->TopMemoryBlock == NULL)     {      fprintf(stdout, "Unable to allocate initial memory pool\n");      return(0);     }   MemoryData(theEnv)->TopMemoryBlock->nextBlock = NULL;   MemoryData(theEnv)->TopMemoryBlock->prevBlock = NULL;   MemoryData(theEnv)->TopMemoryBlock->nextFree = (struct chunkInfo *) (((char *) MemoryData(theEnv)->TopMemoryBlock) + MemoryData(theEnv)->BlockInfoSize);   MemoryData(theEnv)->TopMemoryBlock->size = (long) usableBlockSize;   chunkPtr = (struct chunkInfo *) (((char *) MemoryData(theEnv)->TopMemoryBlock) + MemoryData(theEnv)->BlockInfoSize + MemoryData(theEnv)->ChunkInfoSize + usableBlockSize);   chunkPtr->nextFree = NULL;   chunkPtr->lastFree = NULL;   chunkPtr->prevChunk = MemoryData(theEnv)->TopMemoryBlock->nextFree;   chunkPtr->size = 0;   MemoryData(theEnv)->TopMemoryBlock->nextFree->nextFree = NULL;   MemoryData(theEnv)->TopMemoryBlock->nextFree->lastFree = NULL;   MemoryData(theEnv)->TopMemoryBlock->nextFree->prevChunk = NULL;   MemoryData(theEnv)->TopMemoryBlock->nextFree->size = (long) usableBlockSize;   MemoryData(theEnv)->BlockMemoryInitialized = TRUE;   return(1);  }/***************************************************************************//* AllocateBlock: Adds a new block of memory to the list of memory blocks. *//***************************************************************************/static int AllocateBlock(  void *theEnv,  struct blockInfo *blockPtr,  unsigned int requestSize)  {   unsigned int blockSize, usableBlockSize;   struct blockInfo *newBlock;   struct chunkInfo *newTopChunk;   /*============================================================*/   /* Determine the size of the block that needs to be allocated */   /* to satisfy the request. Normally, a default block size is  */   /* used, but if the requested size is larger than the default */   /* size, then the requested size is used for the block size.  */   /*============================================================*/   blockSize = (BLOCKSIZE > requestSize ? BLOCKSIZE : requestSize);   blockSize += MemoryData(theEnv)->BlockInfoSize + MemoryData(theEnv)->ChunkInfoSize * 2;   blockSize = (((blockSize - 1) / STRICT_ALIGN_SIZE) + 1) * STRICT_ALIGN_SIZE;   usableBlockSize = blockSize - MemoryData(theEnv)->BlockInfoSize - (2 * MemoryData(theEnv)->ChunkInfoSize);   /*=========================*/   /* Allocate the new block. */   /*=========================*/   newBlock = (struct blockInfo *) malloc((STD_SIZE) blockSize);   if (newBlock == NULL) return(0);   /*======================================*/   /* Initialize the block data structure. */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?