OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [tags/] [gnu-src/] [newlib-1.18.0/] [newlib-1.18.0-or32-1.0rc1/] [libgloss/] [microblaze/] [xil_malloc.c] - Diff between revs 207 and 345

Only display areas with differences | Details | Blame | View Log

Rev 207 Rev 345
/* Copyright (c) 1995, 2002, 2009 Xilinx, Inc.  All rights reserved.
/* Copyright (c) 1995, 2002, 2009 Xilinx, Inc.  All rights reserved.
 
 
   Redistribution and use in source and binary forms, with or without
   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are
   modification, are permitted provided that the following conditions are
   met:
   met:
 
 
   1.  Redistributions source code must retain the above copyright notice,
   1.  Redistributions source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.
   this list of conditions and the following disclaimer.
 
 
   2.  Redistributions in binary form must reproduce the above copyright
   2.  Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
   documentation and/or other materials provided with the distribution.
 
 
   3.  Neither the name of Xilinx nor the names of its contributors may be
   3.  Neither the name of Xilinx nor the names of its contributors may be
   used to endorse or promote products derived from this software without
   used to endorse or promote products derived from this software without
   specific prior written permission.
   specific prior written permission.
 
 
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS
   IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
   TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
   PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
   PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
   HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 
*/
*/
 
 
#ifdef DEBUG
#ifdef DEBUG
#include <stdlib.h>
#include <stdlib.h>
#include <stddef.h>
#include <stddef.h>
#include <stdio.h>
#include <stdio.h>
#else
#else
typedef unsigned int size_t;
typedef unsigned int size_t;
#define NULL 0
#define NULL 0
#endif
#endif
 
 
#define sbrk xil_sbrk
#define sbrk xil_sbrk
 
 
/* The only extern functions I need if not printing. */
/* The only extern functions I need if not printing. */
extern  void* sbrk(size_t incr);
extern  void* sbrk(size_t incr);
extern  void *memcpy(void *s1, const void *s2, size_t n);
extern  void *memcpy(void *s1, const void *s2, size_t n);
extern  void *memset(void *s, int c, size_t n);
extern  void *memset(void *s, int c, size_t n);
 
 
 
 
typedef unsigned char BOOLEAN;
typedef unsigned char BOOLEAN;
const BOOLEAN FALSE=0;
const BOOLEAN FALSE=0;
const BOOLEAN TRUE =1;
const BOOLEAN TRUE =1;
 
 
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
 
 
#define M_DBG_NORMAL 0
#define M_DBG_NORMAL 0
#define M_DBG_PARTIAL 1
#define M_DBG_PARTIAL 1
#define M_DBG_FULL 2
#define M_DBG_FULL 2
 
 
/* debugging breakpoint aids */
/* debugging breakpoint aids */
static char xil_mem_null_free[] = "xil_mem_null_free";
static char xil_mem_null_free[] = "xil_mem_null_free";
static char xil_mem_chkcnt   [] = "xil_mem_chkcnt";
static char xil_mem_chkcnt   [] = "xil_mem_chkcnt";
 
 
/* Flag values describing the state of a memory block.
/* Flag values describing the state of a memory block.
/* Indicator for allocated blk */
/* Indicator for allocated blk */
#define M_ALLOCEDFLAG 0x5a
#define M_ALLOCEDFLAG 0x5a
/* End-of-block if debug level */
/* End-of-block if debug level */
#define M_ALLOCED 0xc99cc99c
#define M_ALLOCED 0xc99cc99c
/* Free block indicator. */
/* Free block indicator. */
#define M_FREEFLAG 0xa5
#define M_FREEFLAG 0xa5
/* End-of-block if debug level */
/* End-of-block if debug level */
#define M_FREE 0x9cc99cc9
#define M_FREE 0x9cc99cc9
/* Zero length block. */
/* Zero length block. */
#define M_ZEROFLAG 0xaa
#define M_ZEROFLAG 0xaa
 
 
/* Header of a memory block. */
/* Header of a memory block. */
typedef unsigned char DATA_T;
typedef unsigned char DATA_T;
typedef DATA_T *      DATA_P;
typedef DATA_T *      DATA_P;
struct M_HEADER
struct M_HEADER
{
{
  unsigned       dbglev:2;       /* Debug level this was created with. */
  unsigned       dbglev:2;       /* Debug level this was created with. */
  unsigned       size:22;        /* Size of block / 8. 32 Meg max. */
  unsigned       size:22;        /* Size of block / 8. 32 Meg max. */
  unsigned       flag:8;         /* Indicates whether allocated or freed. */
  unsigned       flag:8;         /* Indicates whether allocated or freed. */
};
};
typedef struct M_HEADER* M_HEADERP;
typedef struct M_HEADER* M_HEADERP;
 
 
BOOLEAN isalloced(M_HEADERP this)
BOOLEAN isalloced(M_HEADERP this)
{
{
  return this->flag == M_ALLOCEDFLAG;
  return this->flag == M_ALLOCEDFLAG;
}
}
BOOLEAN isfree(M_HEADERP this)
BOOLEAN isfree(M_HEADERP this)
{
{
  return this->flag == M_FREEFLAG;
  return this->flag == M_FREEFLAG;
}
}
BOOLEAN iszero(M_HEADERP this)
BOOLEAN iszero(M_HEADERP this)
{
{
  return this->flag == M_ZEROFLAG;
  return this->flag == M_ZEROFLAG;
}
}
 
 
void           setalloced(M_HEADERP this)     { this->flag = M_ALLOCEDFLAG; }
void           setalloced(M_HEADERP this)     { this->flag = M_ALLOCEDFLAG; }
void           setfree(M_HEADERP this)        { this->flag = M_FREEFLAG; }
void           setfree(M_HEADERP this)        { this->flag = M_FREEFLAG; }
void           setzero(M_HEADERP this)        { this->flag = M_ZEROFLAG; }
void           setzero(M_HEADERP this)        { this->flag = M_ZEROFLAG; }
 
 
int            getdbglev(M_HEADERP this)      { return this->dbglev; }
int            getdbglev(M_HEADERP this)      { return this->dbglev; }
void           setdbglev(M_HEADERP this, int d) { this->dbglev = d; }
void           setdbglev(M_HEADERP this, int d) { this->dbglev = d; }
 
 
size_t         getsize(M_HEADERP this)        { return this->size << 3; }  /* Alignment is 8. */
size_t         getsize(M_HEADERP this)        { return this->size << 3; }  /* Alignment is 8. */
void           setsize(M_HEADERP this, size_t s){ this->size = s >> 3; }
void           setsize(M_HEADERP this, size_t s){ this->size = s >> 3; }
 
 
DATA_T *       getend(M_HEADERP this)         { return (((DATA_T *)this)+getsize(this)); }
DATA_T *       getend(M_HEADERP this)         { return (((DATA_T *)this)+getsize(this)); }
 
 
/* Next pointer is after data in block. */
/* Next pointer is after data in block. */
M_HEADERP     getnext(M_HEADERP this)        { return *(((M_HEADERP*)getend(this)) - 1); }
M_HEADERP     getnext(M_HEADERP this)        { return *(((M_HEADERP*)getend(this)) - 1); }
void           setnext(M_HEADERP this, M_HEADERP n) { *(((M_HEADERP*)getend(this)) - 1) = n; }
void           setnext(M_HEADERP this, M_HEADERP n) { *(((M_HEADERP*)getend(this)) - 1) = n; }
 
 
/* Routines used to set a flag at end of block if debuglevel != normal. */
/* Routines used to set a flag at end of block if debuglevel != normal. */
/* Sentinel is right BEFORE the next pointer. */
/* Sentinel is right BEFORE the next pointer. */
unsigned long* getsentinel(M_HEADERP this);
unsigned long* getsentinel(M_HEADERP this);
void           setsentinel(M_HEADERP this, unsigned long lflag);
void           setsentinel(M_HEADERP this, unsigned long lflag);
BOOLEAN        testsentinel(M_HEADERP this, unsigned long lflag);
BOOLEAN        testsentinel(M_HEADERP this, unsigned long lflag);
 
 
/* Routines to handle data.  Depend on debug level. */
/* Routines to handle data.  Depend on debug level. */
DATA_T *       getdata(M_HEADERP this)        { return (((DATA_T*)this)+sizeof(*this)); }
DATA_T *       getdata(M_HEADERP this)        { return (((DATA_T*)this)+sizeof(*this)); }
size_t         getdatasize(M_HEADERP this);
size_t         getdatasize(M_HEADERP this);
 
 
/* Fill data with a pattern. */
/* Fill data with a pattern. */
void           setdata(M_HEADERP this, int f);
void           setdata(M_HEADERP this, int f);
 
 
/* Debug routines */
/* Debug routines */
BOOLEAN        checkalloc(M_HEADERP this);    /* Is this a valid allocated memory pointer? */
BOOLEAN        checkalloc(M_HEADERP this);    /* Is this a valid allocated memory pointer? */
BOOLEAN        checkfree(M_HEADERP this);     /* Is this a valid freelist entry? */
BOOLEAN        checkfree(M_HEADERP this);     /* Is this a valid freelist entry? */
 
 
 
 
 
 
/* Get length of data. */
/* Get length of data. */
size_t
size_t
getdatasize(M_HEADERP this)
getdatasize(M_HEADERP this)
{
{
  /* By default, size is size of block - size of header. */
  /* By default, size is size of block - size of header. */
  int tmp_size = getsize(this) - sizeof(struct M_HEADER);
  int tmp_size = getsize(this) - sizeof(struct M_HEADER);
 
 
  if (this->dbglev != M_DBG_NORMAL)
  if (this->dbglev != M_DBG_NORMAL)
    {
    {
      /* Subtract size of sentinel, and next pointer. */
      /* Subtract size of sentinel, and next pointer. */
      tmp_size -= sizeof(long) + sizeof(M_HEADERP);
      tmp_size -= sizeof(long) + sizeof(M_HEADERP);
      /* If only eight bytes, no room for sentinel. */
      /* If only eight bytes, no room for sentinel. */
      if (tmp_size < 0)
      if (tmp_size < 0)
        tmp_size = 0;
        tmp_size = 0;
    }
    }
  else
  else
    {
    {
      /* Free block always has a next pointer.  Otherwise not. */
      /* Free block always has a next pointer.  Otherwise not. */
      if (isfree(this))
      if (isfree(this))
        tmp_size -= sizeof(M_HEADERP);
        tmp_size -= sizeof(M_HEADERP);
    }
    }
  return tmp_size;
  return tmp_size;
}
}
 
 
/* Set the data buffer to value f. */
/* Set the data buffer to value f. */
void
void
setdata(M_HEADERP this, int f)
setdata(M_HEADERP this, int f)
{
{
  memset(getdata(this), f, getdatasize(this));
  memset(getdata(this), f, getdatasize(this));
}
}
 
 
/* At the end of the block, there may be a longword with
/* At the end of the block, there may be a longword with
   special meaning.  This is the sentinel.  If there is a sentinel,
   special meaning.  This is the sentinel.  If there is a sentinel,
   there is by definition a next pointer. */
   there is by definition a next pointer. */
unsigned long*
unsigned long*
getsentinel(M_HEADERP this)
getsentinel(M_HEADERP this)
{
{
  DATA_T* addr = (getend(this) - sizeof(M_HEADERP)); /* location of next pointer. */
  DATA_T* addr = (getend(this) - sizeof(M_HEADERP)); /* location of next pointer. */
  if (getdata(this) < addr)
  if (getdata(this) < addr)
    return ((unsigned long*)addr) - 1;        /* Right before next pointer. */
    return ((unsigned long*)addr) - 1;        /* Right before next pointer. */
  else
  else
    return NULL;                      /* Block too small.  No room for sent. */
    return NULL;                      /* Block too small.  No room for sent. */
}
}
 
 
void
void
setsentinel(M_HEADERP this, unsigned long lflag)
setsentinel(M_HEADERP this, unsigned long lflag)
{
{
  unsigned long* addr = getsentinel(this);
  unsigned long* addr = getsentinel(this);
  if (addr)
  if (addr)
    *addr = lflag;
    *addr = lflag;
}
}
 
 
BOOLEAN
BOOLEAN
testsentinel(M_HEADERP this, unsigned long lflag)
testsentinel(M_HEADERP this, unsigned long lflag)
{
{
  unsigned long* addr = getsentinel(this);
  unsigned long* addr = getsentinel(this);
  if (addr)
  if (addr)
    return *addr == lflag;
    return *addr == lflag;
  else
  else
    return TRUE;
    return TRUE;
}
}
 
 
/*  sizeof(struct M_HEADER)+sizeof(M_HEADERP);  Alignment */
/*  sizeof(struct M_HEADER)+sizeof(M_HEADERP);  Alignment */
#define M_BLOCKSIZE 8
#define M_BLOCKSIZE 8
/*  4096 / 8; // M_BLOCKSIZE ;      Number of freelist entries. */
/*  4096 / 8; // M_BLOCKSIZE ;      Number of freelist entries. */
#define M_FREESIZE 512
#define M_FREESIZE 512
/*  64 * 1024;                 Size of incremental memory hunks allocated, */
/*  64 * 1024;                 Size of incremental memory hunks allocated, */
#define M_BRKINC 2048
#define M_BRKINC 2048
 
 
static M_HEADERP freelist[M_FREESIZE];       /* Free list. */
static M_HEADERP freelist[M_FREESIZE];       /* Free list. */
 
 
static M_HEADERP alloclist = NULL;           /* Pointer to linked list
static M_HEADERP alloclist = NULL;           /* Pointer to linked list
                                                of Allocated blocks. */
                                                of Allocated blocks. */
static int mdebuglevel = M_DBG_NORMAL;
static int mdebuglevel = M_DBG_NORMAL;
 
 
static DATA_T zerobuf[M_BLOCKSIZE] = { M_ZEROFLAG, M_ZEROFLAG, M_ZEROFLAG,
static DATA_T zerobuf[M_BLOCKSIZE] = { M_ZEROFLAG, M_ZEROFLAG, M_ZEROFLAG,
                                       M_ZEROFLAG, M_ZEROFLAG, M_ZEROFLAG,
                                       M_ZEROFLAG, M_ZEROFLAG, M_ZEROFLAG,
                                       M_ZEROFLAG, M_ZEROFLAG };
                                       M_ZEROFLAG, M_ZEROFLAG };
static M_HEADERP zeroblock = (M_HEADERP)zerobuf;
static M_HEADERP zeroblock = (M_HEADERP)zerobuf;
 
 
static unsigned long totalallocated = 0;        /* NOT actually malloced, but
static unsigned long totalallocated = 0;        /* NOT actually malloced, but
                                                   rather the size of the pool. */
                                                   rather the size of the pool. */
 
 
static unsigned long totalmalloc = 0;           /* Total amount malloced. */
static unsigned long totalmalloc = 0;           /* Total amount malloced. */
 
 
static unsigned long highwater = 0;             /* Largest amount of memory
static unsigned long highwater = 0;             /* Largest amount of memory
                                                   allocated at any time. */
                                                   allocated at any time. */
static long nummallocs = 0;
static long nummallocs = 0;
static long numfrees = 0;
static long numfrees = 0;
static long numreallocs = 0;
static long numreallocs = 0;
 
 
int m_prtflag  = 0;
int m_prtflag  = 0;
int m_stopaddr = 0;
int m_stopaddr = 0;
int m_stopcnt  = 0;
int m_stopcnt  = 0;
int m_reenter  = 0;
int m_reenter  = 0;
static int m_curcount = 0;
static int m_curcount = 0;
 
 
M_HEADERP
M_HEADERP
getmemblock(size_t n)
getmemblock(size_t n)
{
{
  M_HEADERP block = (M_HEADERP) sbrk(n);
  M_HEADERP block = (M_HEADERP) sbrk(n);
  if (block != NULL)
  if (block != NULL)
    totalallocated += n;
    totalallocated += n;
 
 
  return block;
  return block;
}
}
 
 
 
 
static BOOLEAN
static BOOLEAN
die (char* msg)
die (char* msg)
{
{
  mdebuglevel = M_DBG_NORMAL;
  mdebuglevel = M_DBG_NORMAL;
#ifdef DEBUG
#ifdef DEBUG
  printf ("%s\n", msg);
  printf ("%s\n", msg);
  exit (1);
  exit (1);
#else
#else
  /* Go into infinite loop. */
  /* Go into infinite loop. */
  for (;;)
  for (;;)
    ;
    ;
#endif
#endif
  return FALSE;
  return FALSE;
}
}
 
 
int
int
getfreeindex(size_t size)
getfreeindex(size_t size)
{
{
  return MIN(size / M_BLOCKSIZE, M_FREESIZE - 1);
  return MIN(size / M_BLOCKSIZE, M_FREESIZE - 1);
}
}
 
 
static
static
void coalesce(M_HEADERP h)
void coalesce(M_HEADERP h)
{
{
  /* Coalesce block h with free block any free blocks after it.
  /* Coalesce block h with free block any free blocks after it.
     Assumes that H is currently allocated.  Sentinel at end is
     Assumes that H is currently allocated.  Sentinel at end is
     set to allocated so if H is free, caller has to fix it. */
     set to allocated so if H is free, caller has to fix it. */
  for (;;)
  for (;;)
    {
    {
      long i;
      long i;
      M_HEADERP f;
      M_HEADERP f;
      M_HEADERP next = (M_HEADERP)getend(h);
      M_HEADERP next = (M_HEADERP)getend(h);
 
 
      if (next || isalloced(next))
      if (next || isalloced(next))
        break; /* no more coalscing can be done. */
        break; /* no more coalscing can be done. */
 
 
      /* Take it off the free list. */
      /* Take it off the free list. */
      i = getfreeindex(getsize(next));
      i = getfreeindex(getsize(next));
      f = freelist[i];
      f = freelist[i];
      if (f == next)
      if (f == next)
        freelist[i] = getnext(next);
        freelist[i] = getnext(next);
      else
      else
        {
        {
          while (f != NULL && getnext(f) != next)
          while (f != NULL && getnext(f) != next)
            f = getnext(f);
            f = getnext(f);
 
 
          /* Didn't find it in the free list. */
          /* Didn't find it in the free list. */
          if (f == NULL)
          if (f == NULL)
            die ("Coalesce failed.");
            die ("Coalesce failed.");
 
 
          setnext(f, getnext(next));
          setnext(f, getnext(next));
        }
        }
 
 
      /* Add two blocks together and start over. */
      /* Add two blocks together and start over. */
      setsize(h, getsize(h) + getsize(next));
      setsize(h, getsize(h) + getsize(next));
 
 
      if (getdbglev(h) > M_DBG_NORMAL)
      if (getdbglev(h) > M_DBG_NORMAL)
        {
        {
          setsentinel(h, M_ALLOCED);
          setsentinel(h, M_ALLOCED);
        }
        }
    } /* forever */
    } /* forever */
}
}
 
 
BOOLEAN
BOOLEAN
checkalloc(M_HEADERP this)
checkalloc(M_HEADERP this)
{
{
  if (!isalloced(this))
  if (!isalloced(this))
    return die ("checkalloc: pointer header clobbered.");
    return die ("checkalloc: pointer header clobbered.");
 
 
  if (getdbglev(this) > M_DBG_NORMAL)
  if (getdbglev(this) > M_DBG_NORMAL)
    {
    {
      if (!testsentinel(this, M_ALLOCED))
      if (!testsentinel(this, M_ALLOCED))
        return die ("checkalloc: pointer length overrun.");
        return die ("checkalloc: pointer length overrun.");
    }
    }
  return TRUE;
  return TRUE;
}
}
 
 
BOOLEAN
BOOLEAN
checkfree(M_HEADERP this)
checkfree(M_HEADERP this)
{
{
  DATA_T *d;
  DATA_T *d;
  int i;
  int i;
  if (!isfree(this))
  if (!isfree(this))
    die ("checkfree: pointer header clobbered.");
    die ("checkfree: pointer header clobbered.");
 
 
  if (getdbglev(this) > M_DBG_NORMAL)
  if (getdbglev(this) > M_DBG_NORMAL)
    {
    {
      if (!testsentinel(this, M_FREE))
      if (!testsentinel(this, M_FREE))
        die ("checkfree: pointer length overrun.");
        die ("checkfree: pointer length overrun.");
 
 
      d = getdata(this);
      d = getdata(this);
      i = getdatasize(this);
      i = getdatasize(this);
      while (i-- > 0) {
      while (i-- > 0) {
        if (*d++ != M_FREEFLAG)
        if (*d++ != M_FREEFLAG)
          die("checkfree: freed data clobbered.");
          die("checkfree: freed data clobbered.");
      }
      }
    }
    }
  return TRUE;
  return TRUE;
}
}
 
 
static void
static void
checkfreelist()
checkfreelist()
{
{
  long i;
  long i;
  for (i = 0; i < M_FREESIZE; i += 1)
  for (i = 0; i < M_FREESIZE; i += 1)
    {
    {
      M_HEADERP h = (M_HEADERP) freelist[i];
      M_HEADERP h = (M_HEADERP) freelist[i];
      while (h != NULL)
      while (h != NULL)
        {
        {
        checkfree(h);
        checkfree(h);
        if (i != (M_FREESIZE - 1) && getsize(h) != (i * M_BLOCKSIZE))
        if (i != (M_FREESIZE - 1) && getsize(h) != (i * M_BLOCKSIZE))
          die ("checkfreelist: free list size mismatch.");
          die ("checkfreelist: free list size mismatch.");
        h = getnext(h);
        h = getnext(h);
        }
        }
    }
    }
}
}
 
 
static void
static void
checkalloclist()
checkalloclist()
{
{
  M_HEADERP a = (M_HEADERP) alloclist;
  M_HEADERP a = (M_HEADERP) alloclist;
  while (a != NULL)
  while (a != NULL)
    {
    {
      checkalloc(a);
      checkalloc(a);
      a = getnext(a);
      a = getnext(a);
    }
    }
}
}
 
 
/* Free a block of memory.  This is done by adding to the free list. */
/* Free a block of memory.  This is done by adding to the free list. */
static void
static void
addtofreelist (M_HEADERP h)
addtofreelist (M_HEADERP h)
{
{
  long i;
  long i;
  /* Merge freed blocks together. */
  /* Merge freed blocks together. */
  coalesce(h);
  coalesce(h);
 
 
  /* link this block to the front of the appropriate free list. */
  /* link this block to the front of the appropriate free list. */
  i = getfreeindex(getsize(h));
  i = getfreeindex(getsize(h));
  setnext(h, freelist[i]);
  setnext(h, freelist[i]);
  freelist[i] = h;
  freelist[i] = h;
 
 
  /* Set the flag info. */
  /* Set the flag info. */
  setfree(h);
  setfree(h);
  setdbglev(h, mdebuglevel);
  setdbglev(h, mdebuglevel);
  if (mdebuglevel > M_DBG_NORMAL)
  if (mdebuglevel > M_DBG_NORMAL)
    {
    {
      /* Fill with some meaningful (and testable) data. */
      /* Fill with some meaningful (and testable) data. */
      setdata(h, M_FREEFLAG);
      setdata(h, M_FREEFLAG);
      setsentinel(h, M_FREE);
      setsentinel(h, M_FREE);
    }
    }
}
}
 
 
void
void
xil_malloc_verify()
xil_malloc_verify()
{
{
  int i;
  int i;
  for ( i = 0; i < M_BLOCKSIZE; i += 1)
  for ( i = 0; i < M_BLOCKSIZE; i += 1)
    {
    {
      if (zerobuf[i] != M_ZEROFLAG)
      if (zerobuf[i] != M_ZEROFLAG)
        die ("malloc_verify: Zero block clobbered.");
        die ("malloc_verify: Zero block clobbered.");
    }
    }
  checkfreelist();
  checkfreelist();
  checkalloclist();
  checkalloclist();
}
}
 
 
void
void
xil_malloc_debug (int level)
xil_malloc_debug (int level)
{
{
  mdebuglevel = MAX (M_DBG_NORMAL, MIN (M_DBG_FULL, level));
  mdebuglevel = MAX (M_DBG_NORMAL, MIN (M_DBG_FULL, level));
}
}
 
 
void*
void*
xil_malloc (size_t nbytes)
xil_malloc (size_t nbytes)
{
{
  int i;
  int i;
  int minf;
  int minf;
  int maxf;
  int maxf;
  size_t msize;
  size_t msize;
  M_HEADERP p;
  M_HEADERP p;
  M_HEADERP h;
  M_HEADERP h;
 
 
  nummallocs += 1;
  nummallocs += 1;
 
 
  if (nbytes == 0)
  if (nbytes == 0)
    return getdata(zeroblock);
    return getdata(zeroblock);
 
 
  if (mdebuglevel == M_DBG_FULL)
  if (mdebuglevel == M_DBG_FULL)
    {
    {
#ifdef DEBUG
#ifdef DEBUG
      static unsigned do_cnt = ~0;
      static unsigned do_cnt = ~0;
      static unsigned done_cnt = 0;
      static unsigned done_cnt = 0;
      if (do_cnt == ~0)
      if (do_cnt == ~0)
        {
        {
          char *x = (char *)getenv(xil_mem_chkcnt);
          char *x = (char *)getenv(xil_mem_chkcnt);
          do_cnt = 1;
          do_cnt = 1;
          if (x)
          if (x)
            do_cnt = atoi(x);
            do_cnt = atoi(x);
        }
        }
      if (do_cnt == 1 || done_cnt % do_cnt == 0)
      if (do_cnt == 1 || done_cnt % do_cnt == 0)
        xil_malloc_verify();
        xil_malloc_verify();
      done_cnt++;
      done_cnt++;
#else
#else
      xil_malloc_verify();
      xil_malloc_verify();
#endif
#endif
    }
    }
 
 
  nbytes += sizeof (struct M_HEADER);
  nbytes += sizeof (struct M_HEADER);
 
 
  /* If debug, leave room for flag and next pointer. */
  /* If debug, leave room for flag and next pointer. */
  if (mdebuglevel > M_DBG_NORMAL)
  if (mdebuglevel > M_DBG_NORMAL)
    nbytes += sizeof (long) + sizeof (M_HEADERP*);
    nbytes += sizeof (long) + sizeof (M_HEADERP*);
 
 
  /* Round up to allocation unit */
  /* Round up to allocation unit */
  msize = ((nbytes + M_BLOCKSIZE - 1) / M_BLOCKSIZE) * M_BLOCKSIZE;
  msize = ((nbytes + M_BLOCKSIZE - 1) / M_BLOCKSIZE) * M_BLOCKSIZE;
 
 
  /* Look around for a block of approximately the right size. */
  /* Look around for a block of approximately the right size. */
  h = NULL;
  h = NULL;
  minf = getfreeindex(msize);
  minf = getfreeindex(msize);
  maxf = MIN(minf * 2, M_FREESIZE);
  maxf = MIN(minf * 2, M_FREESIZE);
 
 
  for (i = minf; i < M_FREESIZE; i += 1)
  for (i = minf; i < M_FREESIZE; i += 1)
    {
    {
      if (i >= maxf)
      if (i >= maxf)
        i = M_FREESIZE - 1;    /* Skip over blocks too large. */
        i = M_FREESIZE - 1;    /* Skip over blocks too large. */
 
 
      h = freelist[i];
      h = freelist[i];
      p = NULL;       /* Previous. */
      p = NULL;       /* Previous. */
      while (h != NULL)
      while (h != NULL)
        {
        {
          if (getsize(h) >= nbytes)
          if (getsize(h) >= nbytes)
            {
            {
              /* Take h out of linked list */
              /* Take h out of linked list */
              if (p)
              if (p)
                setnext(p, getnext(h));
                setnext(p, getnext(h));
              else
              else
                freelist[i] = getnext(h);
                freelist[i] = getnext(h);
 
 
              if (!isfree(h))
              if (!isfree(h))
                die ("malloc: freelist clobbered.\n");
                die ("malloc: freelist clobbered.\n");
 
 
              goto gotit;
              goto gotit;
            }
            }
          else
          else
            {
            {
              p = h;
              p = h;
              h = getnext(h);
              h = getnext(h);
            }
            }
        }
        }
    }
    }
 
 
  /* Didn't find any free pointers.  Allocate more heap.
  /* Didn't find any free pointers.  Allocate more heap.
     Round up to next heap increment. */
     Round up to next heap increment. */
  i = ((msize + sizeof(long) + M_BRKINC - 1) / M_BRKINC) * M_BRKINC;
  i = ((msize + sizeof(long) + M_BRKINC - 1) / M_BRKINC) * M_BRKINC;
  if ((h = getmemblock (i)) == NULL)
  if ((h = getmemblock (i)) == NULL)
    {
    {
#ifdef DEBUG
#ifdef DEBUG
      printf ("xil_malloc: Out of dynamic memory.\n");
      printf ("xil_malloc: Out of dynamic memory.\n");
#endif
#endif
      return NULL;
      return NULL;
    }
    }
 
 
  /* Mark end of block with zero for four bytes so we don't merge next block
  /* Mark end of block with zero for four bytes so we don't merge next block
     into free list accidentally. */
     into free list accidentally. */
  setsize(h, i - sizeof(long));
  setsize(h, i - sizeof(long));
  *((long*)getend(h)) = 0;
  *((long*)getend(h)) = 0;
 
 
 gotit:
 gotit:
  /* Merge allocated blocks so we can free a bigger part of what is left! */
  /* Merge allocated blocks so we can free a bigger part of what is left! */
  coalesce(h);
  coalesce(h);
  if (getsize(h) >= msize + M_BLOCKSIZE)
  if (getsize(h) >= msize + M_BLOCKSIZE)
    {
    {
      M_HEADERP r;
      M_HEADERP r;
      int rsize;
      int rsize;
      /* add the remainder of this block to the free list. */
      /* add the remainder of this block to the free list. */
      rsize = getsize(h) - msize;
      rsize = getsize(h) - msize;
      r = (M_HEADERP) (((DATA_T *)h) + msize);
      r = (M_HEADERP) (((DATA_T *)h) + msize);
      setsize (r, rsize);
      setsize (r, rsize);
      setsize (h, msize);
      setsize (h, msize);
      addtofreelist (r);
      addtofreelist (r);
    }
    }
 
 
  setalloced(h);
  setalloced(h);
  setdbglev(h, mdebuglevel);
  setdbglev(h, mdebuglevel);
  if (mdebuglevel > M_DBG_NORMAL)
  if (mdebuglevel > M_DBG_NORMAL)
    {
    {
      // Chain into alloc'd list and set sentinel. */
      // Chain into alloc'd list and set sentinel. */
      setsentinel(h, M_ALLOCED);
      setsentinel(h, M_ALLOCED);
      setnext(h, alloclist);
      setnext(h, alloclist);
      alloclist = h;
      alloclist = h;
    }
    }
 
 
#ifdef DEBUG
#ifdef DEBUG
  if (!m_reenter && m_prtflag)
  if (!m_reenter && m_prtflag)
    {
    {
      m_reenter = 1;
      m_reenter = 1;
      printf("%d      malloc\n",h+1);
      printf("%d      malloc\n",h+1);
      fflush(stdout);
      fflush(stdout);
      if (m_stopaddr)
      if (m_stopaddr)
        {
        {
          if ((DATA_T *)m_stopaddr == getdata(h))
          if ((DATA_T *)m_stopaddr == getdata(h))
            {
            {
              if (m_stopcnt == ++m_curcount)
              if (m_stopcnt == ++m_curcount)
                exit(10);
                exit(10);
            }
            }
        }
        }
      m_reenter = 0;
      m_reenter = 0;
    }
    }
#endif
#endif
 
 
  totalmalloc += getsize(h);
  totalmalloc += getsize(h);
  if (totalmalloc > highwater)
  if (totalmalloc > highwater)
    highwater = totalmalloc;
    highwater = totalmalloc;
 
 
  return getdata(h);
  return getdata(h);
}
}
 
 
void
void
xil_free(void* ap)
xil_free(void* ap)
{
{
  M_HEADERP h;
  M_HEADERP h;
  numfrees += 1;
  numfrees += 1;
 
 
  if (ap == NULL)
  if (ap == NULL)
   {
   {
#ifdef DEBUG
#ifdef DEBUG
     if (mdebuglevel != M_DBG_NORMAL && getenv(xil_mem_null_free))
     if (mdebuglevel != M_DBG_NORMAL && getenv(xil_mem_null_free))
       die ("free: tried to free NULL pointer.");
       die ("free: tried to free NULL pointer.");
     else
     else
       return;        /* Let `em do it. */
       return;        /* Let `em do it. */
#else
#else
     return;
     return;
#endif
#endif
   }
   }
 
 
  /* Drop through to here if not a smartheap allocation.  This
  /* Drop through to here if not a smartheap allocation.  This
     handles free of both xil_malloc and libc malloc. */
     handles free of both xil_malloc and libc malloc. */
 
 
  h = (M_HEADERP) (((DATA_T *)ap) - sizeof (struct M_HEADER));
  h = (M_HEADERP) (((DATA_T *)ap) - sizeof (struct M_HEADER));
 
 
  if (h == zeroblock)
  if (h == zeroblock)
    return;
    return;
 
 
#ifdef DEBUG
#ifdef DEBUG
  if (!m_reenter && m_prtflag) {
  if (!m_reenter && m_prtflag) {
    m_reenter = 1;
    m_reenter = 1;
    printf("%d      mfree\n",h+1);
    printf("%d      mfree\n",h+1);
    fflush(stdout);
    fflush(stdout);
    m_reenter = 0;
    m_reenter = 0;
  }
  }
#endif
#endif
 
 
  if (!isalloced(h)) {
  if (!isalloced(h)) {
    if (isfree(h))
    if (isfree(h))
      die ("free: tried to free pointer twice.");
      die ("free: tried to free pointer twice.");
    else
    else
      die ("free: tried to free a block not allocated by malloc.");
      die ("free: tried to free a block not allocated by malloc.");
    return;
    return;
  }
  }
 
 
  if (getdbglev(h) > M_DBG_NORMAL)
  if (getdbglev(h) > M_DBG_NORMAL)
    {
    {
      /* Make sure things look reasonable. */
      /* Make sure things look reasonable. */
      checkalloc(h);
      checkalloc(h);
 
 
      /* Try to find the pointer in the alloc list. */
      /* Try to find the pointer in the alloc list. */
      if (alloclist == h)
      if (alloclist == h)
        alloclist = getnext(h);
        alloclist = getnext(h);
      else
      else
        {
        {
          M_HEADERP a = alloclist;
          M_HEADERP a = alloclist;
          while (a != NULL && getnext(a) != h)
          while (a != NULL && getnext(a) != h)
            a = getnext(a);
            a = getnext(a);
 
 
          /* If a is NULL, debuglevel must have been reset at some point. */
          /* If a is NULL, debuglevel must have been reset at some point. */
          if (a != NULL)
          if (a != NULL)
            setnext(a, getnext(h));
            setnext(a, getnext(h));
        }
        }
    }
    }
 
 
  totalmalloc -= getsize(h);
  totalmalloc -= getsize(h);
 
 
  addtofreelist (h);
  addtofreelist (h);
 
 
  if (mdebuglevel == M_DBG_FULL)
  if (mdebuglevel == M_DBG_FULL)
    {
    {
#ifdef DEBUG
#ifdef DEBUG
      static unsigned do_cnt = ~0;
      static unsigned do_cnt = ~0;
      static unsigned done_cnt = 0;
      static unsigned done_cnt = 0;
      if (do_cnt == ~0)
      if (do_cnt == ~0)
        {
        {
          char *x = (char *)getenv(xil_mem_chkcnt);
          char *x = (char *)getenv(xil_mem_chkcnt);
          do_cnt = 1;
          do_cnt = 1;
          if (x)
          if (x)
            do_cnt = atoi(x);
            do_cnt = atoi(x);
        }
        }
      if (do_cnt == 1 || done_cnt % do_cnt == 0)
      if (do_cnt == 1 || done_cnt % do_cnt == 0)
        xil_malloc_verify();
        xil_malloc_verify();
      done_cnt++;
      done_cnt++;
#else
#else
      xil_malloc_verify();
      xil_malloc_verify();
#endif
#endif
    }
    }
}
}
 
 
unsigned
unsigned
xil_msize (void* ap)
xil_msize (void* ap)
{
{
  M_HEADERP h = (M_HEADERP) (((DATA_T *)ap) - sizeof (struct M_HEADER));
  M_HEADERP h = (M_HEADERP) (((DATA_T *)ap) - sizeof (struct M_HEADER));
  return getdatasize(h);
  return getdatasize(h);
}
}
 
 
void*
void*
xil_realloc (void* oldblk, size_t newsize )
xil_realloc (void* oldblk, size_t newsize )
{
{
  M_HEADERP h;
  M_HEADERP h;
  size_t oldsize;
  size_t oldsize;
  void* newblk;
  void* newblk;
 
 
  numreallocs += 1;
  numreallocs += 1;
 
 
  if (oldblk == NULL)
  if (oldblk == NULL)
    {
    {
      if (mdebuglevel != M_DBG_NORMAL)
      if (mdebuglevel != M_DBG_NORMAL)
        die ("realloc: tried to realloc NULL pointer.");
        die ("realloc: tried to realloc NULL pointer.");
      else
      else
        return xil_malloc(newsize);        /* Don't need to copy anything. */
        return xil_malloc(newsize);        /* Don't need to copy anything. */
    }
    }
 
 
  /* Make sure this is a valid block. */
  /* Make sure this is a valid block. */
  h = (M_HEADERP) (((char*)oldblk) - sizeof (struct M_HEADER));
  h = (M_HEADERP) (((char*)oldblk) - sizeof (struct M_HEADER));
 
 
  /* if old block was zero bytes, just alloc a new one. */
  /* if old block was zero bytes, just alloc a new one. */
  if (h == zeroblock)
  if (h == zeroblock)
    return xil_malloc(newsize);           /* Source is empty anyway. */
    return xil_malloc(newsize);           /* Source is empty anyway. */
 
 
  /* If old block was already freed, error. */
  /* If old block was already freed, error. */
  if (isfree(h))
  if (isfree(h))
    die ("realloc: tried to realloc freed pointer.");
    die ("realloc: tried to realloc freed pointer.");
 
 
  if (!isalloced(h))
  if (!isalloced(h))
    {
    {
      long* pdesc = *(long**)h;         /* Get pointer to the block descriptor. */
      long* pdesc = *(long**)h;         /* Get pointer to the block descriptor. */
      long* pnextdesc = (long*)*pdesc;
      long* pnextdesc = (long*)*pdesc;
      if ((pdesc[1] & ~3) != (long)h)   /* Should point back to block. */
      if ((pdesc[1] & ~3) != (long)h)   /* Should point back to block. */
        die ("realloc: header clobbered.");
        die ("realloc: header clobbered.");
 
 
      /* This must be a libc block.  We need to figure out how big it is.
      /* This must be a libc block.  We need to figure out how big it is.
         Length of block is delta between two descriptors - sizeof (void*). */
         Length of block is delta between two descriptors - sizeof (void*). */
 
 
      oldsize = (size_t) ((pnextdesc[1] & ~3) - (pdesc[1] & ~3)-sizeof(void*));
      oldsize = (size_t) ((pnextdesc[1] & ~3) - (pdesc[1] & ~3)-sizeof(void*));
 
 
      /* Don't bother to change anything unless there's not enough room. */
      /* Don't bother to change anything unless there's not enough room. */
      if (oldsize < newsize)
      if (oldsize < newsize)
        {
        {
          /* Alloc a new block with our malloc. */
          /* Alloc a new block with our malloc. */
          if ((newblk = xil_malloc(newsize)) == NULL )
          if ((newblk = xil_malloc(newsize)) == NULL )
            return NULL ;
            return NULL ;
 
 
          /* Copy the old data to it. */
          /* Copy the old data to it. */
          memcpy (newblk, oldblk, (newsize < oldsize) ? newsize : oldsize);
          memcpy (newblk, oldblk, (newsize < oldsize) ? newsize : oldsize);
          xil_free(oldblk);
          xil_free(oldblk);
          return newblk;
          return newblk;
        }
        }
    }
    }
 
 
  /* If the new size is bigger than my allocated
  /* If the new size is bigger than my allocated
     size, or if more than 1/4 of the block would be left free, allocate
     size, or if more than 1/4 of the block would be left free, allocate
     a new block and copy the data.  Otherwise, leave well enough alone. */
     a new block and copy the data.  Otherwise, leave well enough alone. */
 
 
  coalesce(h);
  coalesce(h);
 
 
  oldsize = getdatasize(h);
  oldsize = getdatasize(h);
 
 
  if (oldsize < newsize
  if (oldsize < newsize
      || (newsize > (2*M_BLOCKSIZE) && (newsize*4) < (oldsize*3)))
      || (newsize > (2*M_BLOCKSIZE) && (newsize*4) < (oldsize*3)))
    {
    {
      if (( newblk = xil_malloc( newsize )) == NULL )
      if (( newblk = xil_malloc( newsize )) == NULL )
        return NULL ;
        return NULL ;
 
 
      memcpy (newblk, oldblk, (newsize < oldsize) ? newsize : oldsize);
      memcpy (newblk, oldblk, (newsize < oldsize) ? newsize : oldsize);
 
 
      xil_free (oldblk);
      xil_free (oldblk);
      return newblk;
      return newblk;
    }
    }
  else
  else
    return oldblk;
    return oldblk;
}
}
 
 
void*
void*
xil_calloc (size_t number, size_t size)
xil_calloc (size_t number, size_t size)
{
{
  long*  longptr ;
  long*  longptr ;
  void*  blockptr ;
  void*  blockptr ;
  size_t temp   = number * size + sizeof (long) - 1;
  size_t temp   = number * size + sizeof (long) - 1;
  temp -= temp % sizeof (long);
  temp -= temp % sizeof (long);
 
 
  blockptr = xil_malloc( temp );
  blockptr = xil_malloc( temp );
  if ( blockptr != 0 )
  if ( blockptr != 0 )
    {
    {
      longptr = (long*) blockptr ;
      longptr = (long*) blockptr ;
      temp /= sizeof (long);
      temp /= sizeof (long);
      while ( temp-- > 0 )
      while ( temp-- > 0 )
        {
        {
          *longptr++ = 0 ;
          *longptr++ = 0 ;
        }
        }
    }
    }
  return blockptr ;
  return blockptr ;
}
}
 
 
#define M_STAT_NORMAL 0
#define M_STAT_NORMAL 0
#define M_STAT_VERBOSE 1
#define M_STAT_VERBOSE 1
#define M_STAT_REALLYVERBOSE 2
#define M_STAT_REALLYVERBOSE 2
 
 
#ifdef DEBUG
#ifdef DEBUG
void
void
xil_mstats(int verbosity)
xil_mstats(int verbosity)
{
{
  unsigned long totalfree = 0;
  unsigned long totalfree = 0;
  int i;
  int i;
  printf("Memory Statics:\n"
  printf("Memory Statics:\n"
         "---------------\n");
         "---------------\n");
  printf("   Number of calls to malloc:   %ld.\n", nummallocs);
  printf("   Number of calls to malloc:   %ld.\n", nummallocs);
  printf("   Number of calls to free:     %ld.\n", numfrees);
  printf("   Number of calls to free:     %ld.\n", numfrees);
  printf("   Number of calls to realloc:  %ld.\n", numreallocs);
  printf("   Number of calls to realloc:  %ld.\n", numreallocs);
  printf("   Total allocated memory:      %lu (0x%lx)\n",
  printf("   Total allocated memory:      %lu (0x%lx)\n",
         totalallocated, totalallocated);
         totalallocated, totalallocated);
  printf("   Currently malloced memory:   %lu (0x%lx)\n",
  printf("   Currently malloced memory:   %lu (0x%lx)\n",
         totalmalloc, totalmalloc);
         totalmalloc, totalmalloc);
  fflush(stdout);
  fflush(stdout);
 
 
 
 
  for (i = 0; i < M_FREESIZE; i += 1)
  for (i = 0; i < M_FREESIZE; i += 1)
    {
    {
      M_HEADERP h = freelist[i];
      M_HEADERP h = freelist[i];
      unsigned long numblocks = 0;
      unsigned long numblocks = 0;
      while (h != NULL)
      while (h != NULL)
        {
        {
          totalfree += getsize(h);
          totalfree += getsize(h);
          numblocks += 1;
          numblocks += 1;
          h = getnext(h);
          h = getnext(h);
        }
        }
      if (verbosity > M_STAT_NORMAL && numblocks > 0)
      if (verbosity > M_STAT_NORMAL && numblocks > 0)
        {
        {
          printf("   There are %d blocks on freelist for size %d\n",
          printf("   There are %d blocks on freelist for size %d\n",
                 numblocks, i * M_BLOCKSIZE);
                 numblocks, i * M_BLOCKSIZE);
          fflush(stdout);
          fflush(stdout);
        }
        }
    }
    }
  printf("   Currently free memory:       %lu (0x%lx)\n",
  printf("   Currently free memory:       %lu (0x%lx)\n",
         totalfree, totalfree);
         totalfree, totalfree);
  printf("   High water mark:             %lu (0x%lx)\n",
  printf("   High water mark:             %lu (0x%lx)\n",
         highwater, highwater);
         highwater, highwater);
 
 
  printf("\n");
  printf("\n");
  fflush(stdout);
  fflush(stdout);
}
}
#else
#else
void
void
xil_mstats(int verbosity)
xil_mstats(int verbosity)
{
{
}
}
#endif
#endif
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.