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

📄 gmalloc.c

📁 UNIX下SH的实现源码
💻 C
📖 第 1 页 / 共 3 页
字号:
}/* Return memory to the heap.  */voidfree (ptr)     genptr_t ptr;{#ifdef RCHECK  struct hdr *hdr;#endif  nfree++;  if (ptr == 0)    return;#ifdef RCHECK  hdr = ((struct hdr *) ptr) - 1;  checkhdr (hdr);  hdr->magic = MAGICFREE;  zmemset (ptr, FREEFLOOD, hdr->size);  ifree (hdr);#else  ifree (ptr);#endif}/* Change the size of a block allocated by `malloc'. */#ifndef HAVE_MEMMOVE/* Snarfed directly from Emacs src/dispnew.c:   XXX Should use system bcopy if it handles overlap.  *//* Like bcopy except never gets confused by overlap.  */static voidmalloc_safe_bcopy (afrom, ato, size)     genptr_t afrom;     genptr_t ato;     size_t size;{  char *from, *to;  from = afrom;  to = ato;  if (size <= 0 || from == to)    return;  /* If the source and destination don't overlap, then bcopy can     handle it.  If they do overlap, but the destination is lower in     memory than the source, we'll assume bcopy can handle that.  */  if (to < from || from + size <= to)    bcopy (from, to, size);  /* Otherwise, we'll copy from the end.  */  else    {      register char *endf = from + size;      register char *endt = to + size;      /* If TO - FROM is large, then we should break the copy into	 nonoverlapping chunks of TO - FROM bytes each.  However, if	 TO - FROM is small, then the bcopy function call overhead	 makes this not worth it.  The crossover point could be about	 anywhere.  Since I don't think the obvious copy loop is too	 bad, I'm trying to err in its favor.  */      if (to - from < 64)	{	  do	    *--endt = *--endf;	  while (endf != from);	}      else	{	  for (;;)	    {	      endt -= (to - from);	      endf -= (to - from);	      if (endt < to)		break;	      bcopy (endf, endt, to - from);	    }	  /* If SIZE wasn't a multiple of TO - FROM, there will be a	     little left over.  The amount left over is	     (endt + (to - from)) - to, which is endt - from.  */	  bcopy (from, to, endt - from);	}    }}#endif /* !HAVE_MEMMOVE *//* Resize the given region to the new size, returning a pointer   to the (possibly moved) region.  This is optimized for speed;   some benchmarks seem to indicate that greater compactness is   achieved by unconditionally allocating and copying to a   new region.  This module has incestuous knowledge of the   internals of both free and malloc. */static genptr_tirealloc (ptr, size)     genptr_t ptr;     size_t size;{  genptr_t result;  int type;  size_t block, blocks, oldlimit;  if (size == 0)    {      ifree (ptr);      return imalloc (0);    }  else if (ptr == NULL)    return imalloc (size);  block = BLOCK (ptr);  type = _heapinfo[block].busy.type;  switch (type)    {    case 0:      /* Maybe reallocate a large block to a small fragment.  */      if (size <= BLOCKSIZE / 2)	{	  result = imalloc (size);	  if (result != NULL)	    {	      memcpy (result, ptr, size);	      ifree (ptr);	      return result;	    }	}      /* The new size is a large allocation as well;	 see if we can hold it in place. */      blocks = BLOCKIFY (size);      if (blocks < _heapinfo[block].busy.info.size)	{	  /* The new size is smaller; return	     excess memory to the free list. */	  _heapinfo[block + blocks].busy.type = 0;	  _heapinfo[block + blocks].busy.info.size	    = _heapinfo[block].busy.info.size - blocks;	  _heapinfo[block].busy.info.size = blocks;	  /* We have just created a new chunk by splitting a chunk in two.	     Now we will free this chunk; increment the statistics counter	     so it doesn't become wrong when ifree decrements it.  */	  ++chunks_used;	  ifree (ADDRESS (block + blocks));	  result = ptr;	}      else if (blocks == _heapinfo[block].busy.info.size)	/* No size change necessary.  */	result = ptr;      else	{	  /* Won't fit, so allocate a new region that will.	     Free the old region first in case there is sufficient	     adjacent free space to grow without moving. */	  blocks = _heapinfo[block].busy.info.size;	  /* Prevent free from actually returning memory to the system.  */	  oldlimit = _heaplimit;	  _heaplimit = 0;	  ifree (ptr);	  result = imalloc (size);	  if (_heaplimit == 0)	    _heaplimit = oldlimit;	  if (result == NULL)	    {	      /* Now we're really in trouble.  We have to unfree		 the thing we just freed.  Unfortunately it might		 have been coalesced with its neighbors.  */	      if (_heapindex == block)	        (void) imalloc (blocks * BLOCKSIZE);	      else		{		  genptr_t previous;		  previous  = imalloc ((block - _heapindex) * BLOCKSIZE);		  (void) imalloc (blocks * BLOCKSIZE);		  ifree (previous);		}	      return NULL;	    }	  if (ptr != result)	    memmove (result, ptr, blocks * BLOCKSIZE);	}      break;    default:      /* Old size is a fragment; type is logarithm	 to base two of the fragment size.  */      if (size > (size_t) (1 << (type - 1)) &&	  size <= (size_t) (1 << type))	/* The new size is the same kind of fragment.  */	result = ptr;      else	{	  /* The new size is different; allocate a new space,	     and copy the lesser of the new size and the old. */	  result = imalloc (size);	  if (result == NULL)	    return NULL;	  memcpy (result, ptr, min (size, (size_t) 1 << type));	  ifree (ptr);	}      break;    }  return result;}genptr_trealloc (ptr, size)     genptr_t ptr;     size_t size;{#ifdef RCHECK  struct hdr *hdr;  size_t osize;#endif  if (malloc_initialized == 0 && malloc_initialize () == 0)    return NULL;  nrealloc++;#ifdef RCHECK  hdr = ((struct hdr *) ptr) - 1;  osize = hdr->size;  checkhdr (hdr);  if (size < osize)    zmemset ((char *) ptr + size, FREEFLOOD, osize - size);  hdr = (struct hdr *) irealloc ((genptr_t) hdr, sizeof (struct hdr) + size + 1);  if (hdr == NULL)    return NULL;  hdr->size = size;  hdr->magic = MAGICWORD;  ((char *) &hdr[1])[size] = MAGICBYTE;  if (size > osize)    zmemset ((char *) (hdr + 1) + osize, MALLOCFLOOD, size - osize);  return (genptr_t) (hdr + 1);#else  return (irealloc (ptr, size));#endif}/* Allocate an array of NMEMB elements each SIZE bytes long.   The entire array is initialized to zeros.  */genptr_tcalloc (nmemb, size)     register size_t nmemb;     register size_t size;{  register genptr_t result;  result = malloc (nmemb * size);  if (result != NULL)    (void) memset (result, 0, nmemb * size);  return result;}/* Define the `cfree' alias for `free'.  */voidcfree (ptr)     genptr_t ptr;{  free (ptr);}genptr_tmemalign (alignment, size)     size_t alignment;     size_t size;{  genptr_t result;  unsigned long int adj, lastadj;  /* Allocate a block with enough extra space to pad the block with up to     (ALIGNMENT - 1) bytes if necessary.  */  result = malloc (size + alignment - 1);  if (result == NULL)    return NULL;  /* Figure out how much we will need to pad this particular block     to achieve the required alignment.  */  adj = (unsigned long int) ((char *) result - (char *) NULL) % alignment;  do    {      /* Reallocate the block with only as much excess as it needs.  */      free (result);      result = malloc (adj + size);      if (result == NULL)	/* Impossible unless interrupted.  */	return NULL;      lastadj = adj;      adj = (unsigned long int) ((char *) result - (char *) NULL) % alignment;      /* It's conceivable we might have been so unlucky as to get a	 different block with weaker alignment.  If so, this block is too	 short to contain SIZE after alignment correction.  So we must	 try again and get another block, slightly larger.  */    } while (adj > lastadj);  if (adj != 0)    {      /* Record this block in the list of aligned blocks, so that `free'	 can identify the pointer it is passed, which will be in the middle	 of an allocated block.  */      struct alignlist *l;      for (l = _aligned_blocks; l != NULL; l = l->next)	if (l->aligned == NULL)	  /* This slot is free.  Use it.  */	  break;      if (l == NULL)	{	  l = (struct alignlist *) imalloc (sizeof (struct alignlist));	  if (l == NULL)	    {	      free (result);	      return NULL;	    }	  l->next = _aligned_blocks;	  _aligned_blocks = l;	}      l->exact = result;      result = l->aligned = (char *) result + alignment - adj;    }  return result;}/* On some ANSI C systems, some libc functions call _malloc, _free   and _realloc.  Make them use the GNU functions.  */genptr_t_malloc (size)     size_t size;{  return malloc (size);}void_free (ptr)     genptr_t ptr;{  free (ptr);}genptr_t_realloc (ptr, size)     genptr_t ptr;     size_t size;{  return realloc (ptr, size);}struct mstatsmstats (){  struct mstats result;	    result.bytes_total = (char *) default_morecore (0) - _heapbase;  result.chunks_used = chunks_used;  result.bytes_used = bytes_used;  result.chunks_free = chunks_free;  result.bytes_free = bytes_free;  result.nmalloc = nmalloc;  result.nrealloc = nrealloc;  result.nfree = nfree;  result.nsbrk = nsbrk;  result.tsbrk = tsbrk;  result.negsbrk = negsbrk;  result.tnegsbrk = tnegsbrk;  return result;}#ifdef RCHECK/* Standard debugging hooks for `malloc'. */static voidzmemset (ptr, val, size)     genptr_t ptr;     int val;     size_t size;{  char *cp = ptr;  while (size--)    *cp++ = val;}static enum mcheck_statuscheckhdr (hdr)     const struct hdr *hdr;{  enum mcheck_status status;  switch (hdr->magic)    {    default:      status = MCHECK_HEAD;      break;    case MAGICFREE:      status = MCHECK_FREE;      break;    case MAGICWORD:      if (((char *) &hdr[1])[hdr->size] != MAGICBYTE)	status = MCHECK_TAIL;      else	status = MCHECK_OK;      break;    }  if (status != MCHECK_OK)    mabort (status);  return status;}#ifndef botchbotch (msg)     char *msg;{  fprintf (stderr, "mcheck: %s\n", msg);  fflush (stderr);  abort ();}#endifstatic voidmabort (status)     enum mcheck_status status;{  const char *msg;  switch (status)    {    case MCHECK_OK:      msg = "memory is consistent, library is buggy";      break;    case MCHECK_HEAD:      msg = "memory clobbered before allocated block";      break;    case MCHECK_TAIL:      msg = "memory clobbered past end of allocated block";      break;    case MCHECK_FREE:      msg = "block freed twice";      break;    default:      msg = "bogus mcheck_status, library is buggy";      break;    }  botch (msg);}enum mcheck_statusmprobe (ptr)     genptr_t ptr;{  return checkhdr ((struct hdr *)ptr);}#ifndef STDIO_H_INCLUDED#  include <stdio.h>#endifvoidprint_malloc_stats (s)     char *s;{  struct mstats ms;  ms = mstats ();  fprintf (stderr, "Memory allocation statistics: %s\n", s ? s : "");  fprintf (stderr, "\nTotal chunks in use: %d, total chunks free: %d\n",	   ms.chunks_used, ms.chunks_free);  fprintf (stderr, "Total bytes in use: %u, total bytes free: %u\n",	   ms.bytes_used, ms.bytes_free);  fprintf (stderr, "Total bytes (from heapbase): %d\n", ms.bytes_total);  fprintf (stderr, "Total mallocs: %d, total frees: %d, total reallocs: %d\n",	   ms.nmalloc, ms.nfree, ms.nrealloc);  fprintf (stderr, "Total sbrks: %d, total bytes via sbrk: %d\n",  	   ms.nsbrk, ms.tsbrk);  fprintf (stderr, "Total negative sbrks: %d, total bytes returned to kernel: %d\n",  	   ms.negsbrk, ms.tnegsbrk);}#endif /* RCHECK */

⌨️ 快捷键说明

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