⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 memory_util.c

📁 关于遗传算法的一些见地。特别是关于简单遗传程序设计的实现。
💻 C
📖 第 1 页 / 共 4 页
字号:
        break;      case 1:        printf("Bound Check level set to 1 (check upon memory allocation)\n");        break;      case 2:        printf("Bound Check level set to 2 (check upon memory deallocation)\n");        break;      case 3:        printf("Bound Check level set to 3 (check upon memory allocation or deallocation)\n");        break;      default:        printf("No Bound Check Level %d.\n",i);        return FALSE;      }    }  memory_bounds = i;  return TRUE;  }/**********************************************************************  void memory_set_strict()  synopsis:     Sets memory strictness level which determines action		when garbage pointers encountered  parameters:   New memory strictness level.  return:       None.  last updated: 01/12/98 **********************************************************************/void memory_set_strict(int i)  {  if (i<0 || i>3) {printf("No Memory Strictness Level %d.\n", i); return;}  memory_strict=i;  if (memory_strict==0) printf("Memory Strictness set to Zero= garbage pointers treated as NULL\n");  if (memory_strict==1) printf("Memory Strictness set to One= warn then garbage pointers treated as NULL\n");  if (memory_strict==2) printf("Memory Strictness set to Two= garbage pointers prevent memory assignment\n");  if (memory_strict==3) printf("Memory Strictness set to Three= garbage pointers terminate program\n");  return;  }/**********************************************************************  int memory_check_all_bounds()  synopsis:     Checks memory padding for all entries in table.  parameters:   None.  return:       None.  last updated: 03/12/98 **********************************************************************/int memory_check_all_bounds(void)  {  int	k=0;/*  int	i, j;*/  if (memory_verbose>2) printf("Checking memory bounds.\n");  if (memory_reset_bv==0) memory_count_bv = 0;  avltree_traverse(memtree, bounds_traverse, NULL);#if 0  for(i=0;i<num_mem;i++)    {    j=memory_check_bounds(mem[i].mptr);    if (memory_verbose>0)      {      switch (j)        {        case (-1):          printf("Pointer wasn't found in memory table.\n");          break;        case (0):          if (memory_verbose>2) printf("Pointer passed bounds check.\n");          break;        case (1):          printf("low bounds violation found, value=\"");          for (k=0; k<mem[i].pad_ls; k+=sizeof(char)) printf("%c", mem[i].pad_low[k]);          printf("\"\nentry=%d name=%s func=%s file=%s line=%d\n", i, mem[i].label, mem[i].func, mem[i].file, mem[i].line);          break;        case (2):          printf("high bounds violation found, value=\"");          for (k=0; k<mem[i].pad_hs; k+=sizeof(char)) printf("%c", mem[i].pad_high[k]);          printf("\"\nentry=%d name=%s func=%s file=%s line=%d\n", i, mem[i].label, mem[i].func, mem[i].file, mem[i].line);          break;        case (3):          printf("high and low bounds violations found, low value=\"");          for (k=0; k<mem[i].pad_ls; k+=sizeof(char)) printf("%c", mem[i].pad_low[k]);          printf("\", high value=\"");          for (k=0; k<mem[i].pad_hs; k+=sizeof(char)) printf("%c", mem[i].pad_high[k]);          printf("\"\nentry=%d name=%s func=%s file=%s line=%d\n", i, mem[i].label, mem[i].func, mem[i].file, mem[i].line);          break;        default:          printf("WARNING: internal error in memory_util.c\n");        }      }        if (j>0) k++;    }#endif  if (k==1)    {    printf("WARNING: A bounds violation has been detected.\n");    }  else if (k>1)    {    printf("WARNING: %d bounds violations have been detected.\n",k);    }  else if(memory_verbose>2)    {    printf("Memory bounds are undisturbed.\n");    }  return k;  }/**********************************************************************  int memory_check_bounds()  synopsis:     Checks memory padding.  Resets padding, if required.  parameters:   pointer in memory table.  return:       None.  last updated: 03/12/98 **********************************************************************/int memory_check_bounds(void* mptr)  {  mem_record	*j;  int		m,n;  j=match_mptr(mptr);  if (j==NULL)    {    printf("WARNING: Pointer not found in the memory table!\n");    return (-1);    }/* Update violations count, and reset padding, if appropriate. */  m=check_pad_mptr_low(j);  if (m)    {    memory_count_bv++;    if (memory_reset_bv) pad_mptr_low(j);    }  n=check_pad_mptr_high(j);    {    memory_count_bv++;    if (memory_reset_bv) pad_mptr_high(j);    }  return (2*n+m);  }/**********************************************************************  void memory_set_padding()  synopsis:     Sets memory padding level.  parameters:   New memory padding level.  return:       None.  last updated: 01/12/98 **********************************************************************/void memory_set_padding(int i)  {  if (i > 0 && i < 4) memory_padding = i; else memory_padding = 0;  if (memory_padding == 0) printf("memory padding turned off\n");  if (memory_padding == 1) printf("memory now to be padded, high and low\n");  if (memory_padding == 2) printf("memory now to be padded, high \n");  if (memory_padding == 3) printf("memory now to be padded, low\n");  return;  }/**********************************************************************  void *s_malloc_safe()  synopsis:     Wrapper around system's malloc() function.  Will		never return upon failure so there is no need to		check the return value.  parameters:  return:  last updated:	14/08/00 **********************************************************************/void *s_malloc_safe(	size_t size,			const char *funcname, const char *filename, const int linenum)  {  void	*ptr;	/* Pointer to new memory */     memory_count_malloc++;  if (size==0)    {    printf("WARNING: Memory allocation of 0 bytes requested at func=%s file=%s line=%d\n",           funcname, filename, linenum);    return NULL;    }  if ( !(ptr = malloc(size)) )    {    printf("Memory allocation of %lu bytes failed at func=%s file=%s line=%d\n",           (unsigned long) size, funcname, filename, linenum);    perror("malloc");    exit(EXIT_FAILURE);    }  return ptr;  }/**********************************************************************  void *s_calloc_safe()  synopsis:     Wrapper around system's calloc() function.  Will		never return upon failure so there is no need to		check the return value.  parameters:  return:  last updated:	14/08/00 **********************************************************************/void *s_calloc_safe(	size_t num, size_t size,			const char *funcname, const char *filename, const int linenum)  {  void	*ptr;	/* Pointer to new memory */  memory_count_calloc++;  if (size==0 || num==0)    {    printf("WARNING: Memory allocation of 0 bytes requested at func=%s file=%s line=%d\n",           funcname, filename, linenum);    return NULL;    }  if ( !(ptr = calloc(num, size)) )    {    printf("Memory allocation of %lu bytes failed at func=%s file=%s line=%d\n",           (unsigned long) num*size, funcname, filename, linenum);    perror("calloc");    exit(EXIT_FAILURE);    }  return ptr;  }/**********************************************************************  void *s_realloc_safe()  synopsis:     Wrapper around system's realloc() function.  Will		never return upon failure so there is no need to		check the return value.  parameters:  return:  last updated:	14/08/00 **********************************************************************/void *s_realloc_safe(	void *oldptr, size_t size,			const char *funcname, const char *filename, const int linenum)  {  void	*ptr;	/* Pointer to new memory */     memory_count_realloc++;  if ( !(ptr = realloc(oldptr, size)) )    {    printf("Memory reallocation of %lu bytes failed at func=%s file=%s line=%d\n",           (unsigned long) size, funcname, filename, linenum);    perror("realloc");    exit(EXIT_FAILURE);    }  return ptr;  }/**********************************************************************  char *s_strdup_safe()  synopsis:     Wrapper around system's strdup() function.  Will		never return upon failure so there is no need to		check the return value.  parameters:  return:  last updated:	15/11/00 **********************************************************************/char *s_strdup_safe(	const char *src,			const char *funcname, const char *filename, const int linenum)  {  void		*dest;	/* Pointer to new string */  size_t	len;	/* String length */  memory_count_strdup++;  if (!src)    {    printf("WARNING: strdup() of NULL string requested at func=%s file=%s line=%d\n",           funcname, filename, linenum);    return NULL;    }  len = strlen(src)+1;   /*  if ( !(dest = strdup(src)) )    {    printf("String duplication of %d chars failed at func=%s file=%s line=%d\n",           len, funcname, filename, linenum);    perror("strdup");    exit(EXIT_FAILURE);    }*/  if ( !(dest = malloc(len*sizeof(char))) )    {    printf("String duplication of %lu chars failed at func=%s file=%s line=%d\n",           (unsigned long) len, funcname, filename, linenum);    perror("strdup");    exit(EXIT_FAILURE);    }  memcpy(dest, src, len*sizeof(char));  return dest;  }/**********************************************************************  char *s_strndup_safe()  synopsis:	strdup()-like function.  String will be null-terminated.  parameters:  return:  last updated:	01/03/01 **********************************************************************/char *s_strndup_safe(	const char *src, size_t length,			const char *funcname, const char *filename, const int linenum )  {  void		*dest;	/* Pointer to new string */  size_t	len;	/* String length */  memory_count_strdup++;  if (!src)    {    printf("WARNING: strndup() of NULL string requested at func=%s file=%s line=%d\n",           funcname, filename, linenum);    return NULL;    }  if (!length)    {    printf("WARNING: strndup() of zero-length string requested at func=%s file=%s line=%d\n",           funcname, filename, linenum);    return NULL;    }  len = strlen(src)+1;  if (length < len) len = length;     if ( !(dest = malloc(len*sizeof(char))) )    {    printf("String duplication of %lu chars failed at func=%s file=%s line=%d\n",           (unsigned long) len, funcname, filename, linenum);    perror("strdup");    exit(EXIT_FAILURE);    }  len--;  memcpy(dest, src, len*sizeof(char));  ((char *) dest)[len] = '\0';  return dest;  }/**********************************************************************  void s_free_safe()  synopsis:     Wrapper around system's free() function.  Will		never return upon failure so there is no need to		check the return value.  parameters:  return:  last updated:	10/02/05 **********************************************************************/void s_free_safe(void *ptr,                 const char *funcname,                 const char *filename,                 const int linenum)  {  memory_count_free++;  if (ptr)    free(ptr);  else    printf("Unable to free NULL pointer at func=%s file=%s line=%d\n",           funcname, filename, linenum);  return;  }

⌨️ 快捷键说明

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