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

📄 memory_util.c

📁 关于遗传算法的一些见地。特别是关于简单遗传程序设计的实现。
💻 C
📖 第 1 页 / 共 4 页
字号:
      mtemp=bump_up(mtemp,size_low);/* * Realloc may default to same memory location.  So, I could save a few cycles * by not removing the record from the tree and then replacing it again. */      avltree_remove_key(memtree, (AVLKey) j->mptr);      if (!check_mptr(mtemp,NULL)) printf("Dodgy %s\n", label);/*printf("REPLACING j->mptr %p with mtemp %p j %p\n", j->mptr, mtemp, j);*/      allocated_mem+=memsize*numvars-j->mem;      if (allocated_mem > most_mem) most_mem = allocated_mem;      j->mptr=mtemp;      j->mem=memsize*numvars;      strncpy(j->label,label,64);      strncpy(j->func,name,64);      strncpy(j->file,file,64);      j->line=line;      j->rmem=j->mem;                  j->pad_ls=size_low;      j->pad_hs=size_high;      if (size_high)        {        for (k=0; k<size_high; k++) j->pad_high[k]=rand_high;        pad_mptr_high(j);        }      if (size_low)        {        for (k=0; k<size_low; k++) j->pad_low[k]=rand_low;        pad_mptr_low(j);        }/*printf("INSERT (2) mtemp = %p j = %p j->mptr = %p\n", mtemp, j, j->mptr);*/      avltree_insert(memtree, (vpointer)j);      }    else      {/* no new memory is required, so just reuse current */      if (type==MEMORY_CALLOC)        {        memset(mtemp,0,memsize*numvars);        }      j->rmem=memsize*numvars; /* note rmem!=mem */      if(j->pad_hs)        {         for (k=0; k<size_high; k++) j->pad_high[k]=rand_high;        pad_mptr_high(j);        }      if (j->pad_ls)        {        for (k=0; k<size_low; k++) j->pad_low[k]=rand_low;        pad_mptr_low(j);        }      }    }  if (memory_verbose>1)    {    if (memory_verbose>2)      {      printf("%s allocation call from %s, file \"%s\", line %d\n", label, name, file, line);      }    printf("s_alloc_debug(): %s has %lu used, %lu allocated, total memory allocated = %d\n",                label, (unsigned long) j->rmem, (unsigned long) j->mem, allocated_mem);    }/*  printf("DEBUG: tree now contains %d nodes.  %d records.\n", avltree_num_nodes(memtree), num_mem);  if (avltree_num_nodes(memtree) != num_mem) die("Oh dear.");*/  return mtemp;   }/**********************************************************************  void *s_free_debug()  synopsis:     My replacement for free().  parameters:   None.  return:       None.  last updated: 17/07/00 **********************************************************************/void *s_free_debug(void *mptr, const char *name, const char *file, const int line)  {  mem_record	*j;	/* Index of current memory record. *//* Increment call counter */  memory_count_free++;/* If set to do so, check for bounds violations */  if (memory_bounds==2 || memory_bounds==3) memory_check_all_bounds();/* Do some checks on pointer *//* Is it non-NULL? */  if (mptr==NULL)    {    printf("WARNING: Passed NULL pointer!\n");    printf("Not attempting to deallocate memory.\n");    printf("function \"%s\" file \"%s\" line %d\n", name, file, line);    memory_count_if++;    return (NULL);    }/* Is it in the table? */  j=match_mptr(mptr);  if(j==NULL)    {     printf("WARNING: Pointer not in memory table!\n");    printf("Not attempting to deallocate memory.\n");    printf("function \"%s\" file \"%s\" line %d\n", name, file, line);    memory_count_if++;    return (mptr);    }/* Does it have a positive number of bytes associated? */  if (j->mem < 1)    {    printf("WARNING: Pointer has zero bytes associated!\n");    printf("Not attempting to deallocate memory.\n");    printf("function \"%s\" file \"%s\" line %d\n", name, file, line);    memory_count_if++;    return (mptr);    }/* * Remove this entry from memory tree. *//*printf("REMOVING mptr = %p j = %p j->mptr = %p\n", mptr, j, j->mptr);*/  avltree_remove_key(memtree, (AVLKey) mptr);/* DEBUG:  if (match_mptr(mptr)) printf("WARNING: pointer is still in table.\n");*//* * Pointer seems okay, so deallocate this memory. */  free_padded(j);  allocated_mem-=j->mem;/* report on deallocation, if required */  if (memory_verbose>1)    {    if (memory_verbose>2)      {      printf("deallocation call from %s, file \"%s\", line %d\n", name, file, line);      printf("orig. \"%s\" allocation call from %s, file \"%s\", line %d\n", j->label, j->func, j->file, j->line);      }    printf("s_free_debug(): deallocated %zd bytes successfully, total memory allocated now %d\n", j->mem, allocated_mem);    }/* * Remove the unused entry from memory record chunk. */  mem_record_free(j);  return (NULL);	/* Successful deallocation. */  }/**********************************************************************  int memory_total()  synopsis:     Return the total memory currently allocated through		these routines.  parameters:   None.  return:       int	allocated_mem  last updated: 07/12/98 **********************************************************************/int memory_total(void)  {  if (memory_verbose>0)    printf("Total memory allocated:\t%d bytes.\n", allocated_mem);  return(allocated_mem);  }/**********************************************************************  void print_memory_alloc_to(void* mptr)  synopsis:	Print the memory allocated to pointer mptr if is		listed in the memory table.  parameters:   pointer.  return:	none.  last updated: 07/12/98 **********************************************************************/void memory_print_alloc_to(void* mptr)  {  mem_record	*j;  if(!mptr) { printf("Passed pointer is NULL!\n"); return;}  j=match_mptr(mptr);  if(j==NULL)    {    printf("Requested pointer not found in the memory table!\n");    return;    }  printf("Total memory in %s is %zd, used = %zd\n", j->label, j->mem, j->rmem);  return;  }/**********************************************************************  void memory_alloc_to()  synopsis:	Return the number of bytes allocated to pointer mptr		if it is listed in the memory table.  parameters:   pointer.  return:       int.  last updated: 07/12/98 **********************************************************************/int memory_alloc_to(void* mptr)  {  mem_record	*j;  if(!mptr) return 0;  j=match_mptr(mptr);  if(j==NULL)    {    printf("Requested pointer not found in the memory table!\n");    return 0;    }  return(j->mem);  }/**********************************************************************  void memory_used_mptr()  synopsis:     Return the number of bytes allocated to a pointer.  parameters:   pointer.  return:       int.  last updated: 07/12/98 **********************************************************************/int memory_used_mptr(void *mptr)  {  mem_record	*j;  if (mptr==NULL)    {    printf("WARNING: Passed null pointer!\n");    return 0;    }  j=match_mptr(mptr);  if (j==NULL)    {    printf("Requested pointer not found in the memory table!\n");    return 0;    }  return j->rmem;  }/**********************************************************************  void memory_display_status()  synopsis:     Display information about status of memory allocation		routines.  parameters:   None.  return:       None.  last updated: 14/08/00 **********************************************************************/void memory_display_status(void)  {  printf("=== Memory Stats =============================\n");  printf("Number of entries in memory table:  %d\n", num_mem);  printf("Number of entries in memory tree:   %d\n", avltree_num_nodes(memtree));  printf("Current size of memory table:       %d\n", max_mem);  printf("Current total memory allocated:     %d bytes\n", allocated_mem);  printf("Maximum total memory allocated:     %d bytes\n", most_mem);  printf("----------------------------------------------\n");  printf("Report level:                       %d\n", memory_verbose);  printf("Strictness level:                   %d\n", memory_strict);  printf("Padding flag:                       %d\n", memory_padding);  printf("Size of padding:                    %zd\n", memory_size_pad);  printf("Bounds check level:                 %d\n", memory_bounds);  printf("Bounds violation reset flag:        %d\n", memory_reset_bv);  printf("----------------------------------------------\n");  printf("Total number of malloc() calls:     %ld\n", memory_count_malloc);  printf("Total number of calloc() calls:     %ld\n", memory_count_calloc);  printf("Total number of realloc() calls:    %ld\n", memory_count_realloc);  printf("Total number of strdup() calls:     %ld\n", memory_count_strdup);  printf("Total number of free() calls:       %ld\n", memory_count_free);  printf("----------------------------------------------\n");  printf("Total number of bounds violations:  %d\n", memory_count_bv);  printf("Total number of invalid 'frees':    %d\n", memory_count_if);  printf("==============================================\n");  return;  }/**********************************************************************  void memory_display_table()  synopsis:     List all the memory allocated along with the entries		labels.  WARNING: Table may be very long!  parameters:   None.  return:       None.  last updated: 03/12/98 **********************************************************************/void memory_display_table(void)  {  if (num_mem==0)    {    printf("Memory allocation table is empty.\n");    }  else    {    node_count=0;    printf("Memory tree contains %d nodes. (Should contain %d)\n",           avltree_num_nodes(memtree), num_mem);    printf("=== Memory Allocation Table ==================\n");    printf("num  label\t  function\t  file\t  line\t  mem\t  rmem\t  (mptr)\n");    avltree_traverse(memtree, table_traverse, NULL);    printf("==============================================\n");    printf("Counted %d nodes.\n", node_count);    }  return;  }/**********************************************************************  void memory_set_mptr_label()  synopsis:	Sets label for a table entry  parameters:	pointer		char *label  return:	none  updated:	20/01/99 **********************************************************************/void memory_set_mptr_label(void *mptr, char *label)  {  mem_record	*j;  j=match_mptr(mptr);  if(j==NULL)    {    printf("Requested pointer not found in memory table.  Can not reassign pointer label.\n");    return;    }  strncpy(j->label,label,64);  if (memory_verbose>2)    printf("Label set to \"%s\"\n", label);  return;  }/**********************************************************************  void memory_set_verbose()  synopsis:     Sets memory reporting level.		0 = Only reasonably severe errors reported.		3 = Full reporting.		1,2 = Somewhere inbetween.  parameters:   New memory reporting level.  return:       None.  last updated: 03/12/98 **********************************************************************/void memory_set_verbose(int i)  {  if (i<0 || i>3) {printf("No reporting level %d.\n",i); return;}  memory_verbose=i;  if (memory_verbose > 0) printf("Reporting level set to %d\n", memory_verbose);  return;  }/**********************************************************************  void memory_set_bounds()  synopsis:     Sets memory bound checking level.  parameters:   New memory bound level.  return:       TRUE on success, otherwise FALSE.  last updated: 01/12/98 **********************************************************************/boolean memory_set_bounds(int i)  {  if (memory_verbose > 0)    {    switch (memory_bounds)      {      case 0:        printf("Bound Check level set to 0 (only check upon explicit request)\n");

⌨️ 快捷键说明

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