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

📄 mem.c

📁 C语言库函数的源代码,是C语言学习参考的好文档。
💻 C
📖 第 1 页 / 共 2 页
字号:
    }
    dl->file = fil;
    dl->line = lin;
    dl->nbytes = n;
    dl->beforeval = BEFOREVAL;
#if SUN || SUN386 /* bus error if we store a long at an odd address */
    memcpy(&(dl->data[n]),&afterval,sizeof(AFTERVAL));
#else
    *(long *) &(dl->data[n]) = AFTERVAL;
#endif

    /* Add dl to start of allocation list */
    dl->next = mem_alloclist.next;
    dl->prev = &mem_alloclist;
    mem_alloclist.next = dl;
    if (dl->next != NULL)
      dl->next->prev = dl;

    mem_count++;
    mem_numalloc += n;
    if (mem_numalloc > mem_maxalloc)
      mem_maxalloc = mem_numalloc;
    return mem_dltoptr(dl);
}

void mem_free_debug(ptr,fil,lin)
void *ptr;
char *fil;
int lin;
{
      struct mem_debug *dl;

      if (ptr == NULL)
            return;
#if 0
      {     fprintf(ferr,"Freeing NULL pointer at ");
            goto err;
      }
#endif
      if (mem_count <= 0)
      {     fprintf(ferr,"More frees than allocs at ");
            goto err;
      }
      dl = mem_ptrtodl(ptr);
      if (dl->beforeval != BEFOREVAL)
      {
#if LPTR
            fprintf(ferr,"Pointer x%lx underrun\n",ptr);
#else
            fprintf(ferr,"Pointer x%x underrun\n",ptr);
#endif
            goto err2;
      }
#if SUN || SUN386 /* Bus error if we read a long from an odd address    */
      if (memcmp(&dl->data[dl->nbytes],&afterval,sizeof(AFTERVAL)) != 0)
#else
      if (*(long *) &dl->data[dl->nbytes] != AFTERVAL)
#endif
      {
#if LPTR
            fprintf(ferr,"Pointer x%lx overrun\n",ptr);
#else
            fprintf(ferr,"Pointer x%x overrun\n",ptr);
#endif
            goto err2;
      }
      mem_numalloc -= dl->nbytes;
      if (mem_numalloc < 0)
      {     fprintf(ferr,"error: mem_numalloc = %ld, dl->nbytes = %d\n",
                  mem_numalloc,dl->nbytes);
            goto err2;
      }

      /* Remove dl from linked list */
      if (dl->prev)
            dl->prev->next = dl->next;
      if (dl->next)
            dl->next->prev = dl->prev;

      /* Stomp on the freed storage to help detect references     */
      /* after the storage was freed.                       */
      memset((void *) dl,BADVAL,sizeof(*dl) + dl->nbytes);
      mem_count--;

      /* Some compilers can detect errors in the heap.      */
#if defined(DLC)
      {     int i;
            i = free(dl);
            assert(i == 0);
      }
#else
      free((void *) dl);
#endif
      return;

err2:
      mem_printdl(dl);
err:
      fprintf(ferr,"free'd from ");
      mem_fillin(fil,lin);
      assert(0);
      /* NOTREACHED */
}

/*******************
 * Debug version of mem_realloc().
 */

void *mem_realloc_debug(oldp,n,fil,lin)
void *oldp;
unsigned n;
char *fil;
int lin;
{   void *p;
    struct mem_debug *dl;

    if (n == 0)
    { mem_free_debug(oldp,fil,lin);
      p = NULL;
    }
    else if (oldp == NULL)
      p = mem_malloc_debug(n,fil,lin);
    else
    {
      p = mem_malloc_debug(n,fil,lin);
      if (p != NULL)
      {
          dl = mem_ptrtodl(oldp);
          if (dl->nbytes < n)
            n = dl->nbytes;
          memcpy(p,oldp,n);
          mem_free_debug(oldp,fil,lin);
      }
    }
    return p;
}

/***************************/

void mem_check()
{   register struct mem_debug *dl;

    for (dl = mem_alloclist.next; dl != NULL; dl = dl->next)
      mem_checkptr(mem_dltoptr(dl));
}

/***************************/

void mem_checkptr(p)
register void *p;
{   register struct mem_debug *dl;

    for (dl = mem_alloclist.next; dl != NULL; dl = dl->next)
    {
      if (p >= (void *) &(dl->data[0]) &&
          p < (void *)((char *)dl + sizeof(struct mem_debug)-1 + dl->nbytes))
          goto L1;
    }
    assert(0);

L1:
    dl = mem_ptrtodl(p);
    if (dl->beforeval != BEFOREVAL)
    {
#if LPTR
          fprintf(ferr,"Pointer x%lx underrun\n",p);
#else
          fprintf(ferr,"Pointer x%x underrun\n",p);
#endif
          goto err2;
    }
#if SUN || SUN386 /* Bus error if we read a long from an odd address    */
    if (memcmp(&dl->data[dl->nbytes],&afterval,sizeof(AFTERVAL)) != 0)
#else
    if (*(long *) &dl->data[dl->nbytes] != AFTERVAL)
#endif
    {
#if LPTR
          fprintf(ferr,"Pointer x%lx overrun\n",p);
#else
          fprintf(ferr,"Pointer x%x overrun\n",p);
#endif
          goto err2;
    }
    return;

err2:
    mem_printdl(dl);
    assert(0);
}

#else

/***************************/

void *mem_malloc(numbytes)
unsigned numbytes;
{     void *p;

      if (numbytes == 0)
            return NULL;
      while (1)
      {
            p = malloc(numbytes);
            if (p == NULL)
            {     if (mem_exception())
                        continue;
            }
            else
                  mem_count++;
            break;
      }
      /*printf("malloc(%d) = x%lx\n",numbytes,p);*/
      return p;
}

/***************************/

void *mem_calloc(numbytes)
unsigned numbytes;
{     void *p;

      if (numbytes == 0)
            return NULL;
      while (1)
      {
            p = calloc(numbytes,1);
            if (p == NULL)
            {     if (mem_exception())
                        continue;
            }
            else
                  mem_count++;
            break;
      }
      /*printf("calloc(%d) = x%lx\n",numbytes,p);*/
      return p;
}

/***************************/

void *mem_realloc(oldmem_ptr,newnumbytes)
void *oldmem_ptr;
unsigned newnumbytes;
{   void *p;

    if (oldmem_ptr == NULL)
      p = mem_malloc(newnumbytes);
    else if (newnumbytes == 0)
    { mem_free(oldmem_ptr);
      p = NULL;
    }
    else
    {
      do
          p = realloc(oldmem_ptr,newnumbytes);
      while (p == NULL && mem_exception());
    }
    /*printf("realloc(x%lx,%d) = x%lx\n",oldmem_ptr,newnumbytes,p);*/
    return p;
}

/***************************/

void mem_free(ptr)
void *ptr;
{
    /*printf("free(x%lx)\n",ptr);*/
    if (ptr != NULL)
    { assert(mem_count > 0);
      mem_count--;
#if defined(DLC) && DLC
      {     int i;

            i = free(ptr);
            assert(i == 0);
      }
#else
      free(ptr);
#endif
    }
}

#endif /* MEM_DEBUG */

/***************************/

void mem_init()
{
      if (mem_inited == 0)
      {     mem_count = 0;
#if defined(MEM_DEBUG) && MEM_DEBUG
            mem_numalloc = 0;
            mem_maxalloc = 0;
            mem_alloclist.next = NULL;
#endif
#if defined(__ZTC__) || defined(__SC__)
            /* Necessary if mem_sfree() calls free() before any   */
            /* calls to malloc().                           */
            free(malloc(1));  /* initialize storage allocator     */
#endif
            mem_inited++;
      }
}

/***************************/

void mem_term()
{

      if (mem_inited)
      {
#if defined(MEM_DEBUG) && MEM_DEBUG
            register struct mem_debug *dl;

            for (dl = mem_alloclist.next; dl; dl = dl->next)
            {     fprintf(ferr,"Unfreed pointer: ");
                  mem_printdl(dl);
            }
#if 0
            fprintf(ferr,"Max amount ever allocated == %ld bytes\n",
                  mem_maxalloc);
#endif
#else
            if (mem_count)
                  fprintf(ferr,"%d unfreed items\n",mem_count);
            if (mem_scount)
                  fprintf(ferr,"%d unfreed s items\n",mem_scount);
#endif /* MEM_DEBUG */
            assert(mem_count == 0 && mem_scount == 0);
            mem_inited = 0;
      }
}

#undef next
#undef prev
#undef file
#undef line
#undef nbytes
#undef beforeval

⌨️ 快捷键说明

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