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

📄 mms.c

📁 ADAM2 sources (modified by Oleg)
💻 C
📖 第 1 页 / 共 3 页
字号:
   else 
    {
    if (last == NULL)                /* PTR WILL BE FIRST IN THE LIST     */
      {
      ptr->next_ptr  = free;
      free       = ptr;
      }
     else                            /* PTR IS IN THE MIDDLE OF THE LIST  */
      {
      ptr->next_ptr  = current;
      last->next_ptr = ptr;
      }
    }
  }

/*****************************************************************************/
/*                                                                           */
/* MREMOVE - REMOVE AN ITEM FROM THE FREE LIST.                              */
/*                                                                           */
/*****************************************************************************/
static void mremove(PACKET *ptr)
  {
  register PACKET *current = free;
  register PACKET *last    = NULL;

  /*-----------------------------------------------------------------------*/
  /* SCAN THROUGH LIST, LOOKING FOR PACKET TO REMOVE                       */
  /*-----------------------------------------------------------------------*/
  while (current && current != ptr)
    {
    last    = current;
    current = current->next_ptr;
    }

  /*-----------------------------------------------------------------------*/
  /* REMOVE THE PACKET FROM THE LIST.   THERE ARE TWO CASES :              */
  /*   THE OLD POINTER WILL EITHER BE THE FIRST, OR NOT THE FIRST.         */
  /*-----------------------------------------------------------------------*/
  if (current == NULL) 
    free = NULL;                                        /* NOT FOUND   */
   else 
    {
    if (last == NULL) 
      free       = ptr->next_ptr;                      /* 1ST IN LIST */
     else                      
      last->next_ptr = ptr->next_ptr;                      /* MID OF LIST */
    }
  }

void sys_memcpy(void *new, void *packet, int oldsize)
  {
  char *dp=new;
  char *sp=packet;
  while(oldsize--)
    *dp++=*sp++;
  }

/*****************************************************************************/
/*                                                                           */
/*  MINIT - This function can be called by the user to completely reset the  */
/*          memory management system.                                        */
/*                                                                           */
/*****************************************************************************/
void minit(unsigned adr, unsigned size)
  {

  _memory_size=size;
  _sys_memory=(char *)adr;

    /*-----------------------------------------------------------------------*/
    /* TO INITIALIZE THE MEMORY SYSTEM, SET UP THE FREE LIST TO POINT TO     */
    /* THE ENTIRE HEAP, AND INITIALIZE HEAP TO A SINGLE EMPTY PACKET.        */
    /*-----------------------------------------------------------------------*/
  free = (PACKET *)_sys_memory;
  free->packet_size   = _memory_size - BLOCK_OVERHEAD;
  free->next_ptr      = NULL;
  free->mflag         = 0x11111111;
  free->this_ptr      = free;
  }

void *mallocc(unsigned size)
  {
  unsigned char *current = sys_malloc(size);
  unsigned char *save;
  unsigned i;

  if (current == 0) 
    return(0);
  save=current;
  for(i=0;i<size;i++) *current++=0;
  return(save);
  }


/*****************************************************************************/
/*                                                                           */
/*  MALLOC - Allocate a packet of a given size, and return a pointer to it.  */
/*           This function only allocates in multiples of MIN_BLOCK bytes.   */
/*                                                                           */
/*****************************************************************************/
void* sys_malloc(unsigned size)
  {
  register PACKET       *current = free;
  register unsigned int  newsize = (size + BLOCK_MASK) & ~BLOCK_MASK;
  register unsigned int  oldsize = 0;

  if (size <= 0) 
    return(0);
  /*-----------------------------------------------------------------------*/
  /* SCAN THROUGH FREE LIST FOR PACKET LARGE ENOUGH TO CONTAIN PACKET      */
  /*-----------------------------------------------------------------------*/
  while (current && current->packet_size < newsize)
    {
    current = current->next_ptr;
    }

  if (!current) return(0);

  oldsize = current->packet_size;         /* REMEMBER OLD SIZE             */
  mremove(current);                       /* REMOVE PACKET FROM FREE LIST  */
  /*-----------------------------------------------------------------------*/
  /* IF PACKET IS LARGER THAN NEEDED, FREE EXTRA SPACE AT END              */
  /* BY INSERTING REMAINING SPACE INTO FREE LIST.                          */
  /*-----------------------------------------------------------------------*/
  if ((oldsize - newsize) >= (MIN_BLOCK + BLOCK_OVERHEAD))
    {
    register PACKET *next =
               (PACKET *) ((char *) current + BLOCK_OVERHEAD + newsize);

    next->packet_size    = oldsize - newsize - BLOCK_OVERHEAD;
    minsert(next);
    current->packet_size = newsize;
    }
  current->packet_size |= BLOCK_USED;
  current->mflag        = 0x11111111;
  current->this_ptr     = current;
  return(((char *)current) + BLOCK_OVERHEAD);
  }

/*****************************************************************************/
/*                                                                           */
/*  CALLOC - Allocate a packet of a given size, set the data in the packet   */
/*           to nulls, and return a pointer to it.                           */
/*                                                                           */
/*****************************************************************************/
void* sys_calloc(unsigned num, unsigned size)
  {
  register unsigned      i     = size * num;
  register bit32u *current = sys_malloc(i);
  register char       *save    = (char *) current;

  if (current == 0) 
    return(0);

  i = ((i + BLOCK_MASK) & ~BLOCK_MASK) / sizeof(bit32u);

  while (i--) *current++ = 0;
  return(save);
  }

/*****************************************************************************/
/*                                                                           */
/*  REALLOC - Reallocate a packet to a new size.                             */
/*                                                                           */
/*****************************************************************************/
void* sys_realloc(void *packet, unsigned size)
  {
  register char *pptr    = (char *) packet - BLOCK_OVERHEAD;
  register int   newsize = (size + BLOCK_MASK) & ~BLOCK_MASK;
  register int   oldsize;

  if (packet == 0)
    return(sys_malloc(size));
  if (size   == 0)
    { 
    sys_free(packet); 
    return(0); 
    }

  oldsize = ((PACKET *)pptr)->packet_size;

  if (!(oldsize & BLOCK_USED))  return 0;
  if (newsize == --oldsize)     return packet;

  /*-----------------------------------------------------------------------*/
  /* IF NEW SIZE IS LESS THAN CURRENT SIZE, TRUNCATE PACKET AND RETURN END */
  /* TO FREE LIST                                                          */
  /*-----------------------------------------------------------------------*/
  if (newsize < oldsize)
    {
    if ((oldsize - newsize) < (MIN_BLOCK + BLOCK_OVERHEAD))
      return(packet);
    ((PACKET *)pptr)->packet_size = newsize | BLOCK_USED;
    ((PACKET *)pptr)->this_ptr = (PACKET *)pptr;
    ((PACKET *)pptr)->mflag    = 0x11111111;

    oldsize -= newsize + BLOCK_OVERHEAD;
    pptr    += newsize + BLOCK_OVERHEAD;
    ((PACKET *)pptr)->packet_size = oldsize | BLOCK_USED;
    ((PACKET *)pptr)->this_ptr = (PACKET *)pptr;
    ((PACKET *)pptr)->mflag    = 0x11111111;
    sys_free(pptr + BLOCK_OVERHEAD);
    return(packet);
    }

    /*-----------------------------------------------------------------------*/
    /* IF NEW SIZE IS BIGGER THAN CURRENT PACKET,                            */
    /*  1) CHECK NEXT PACKET IN LIST, SEE IF PACKET CAN BE EXPANDED          */
    /*  2) IF NOT, MOVE PACKET TO NEW LOCATION.                              */
    /*-----------------------------------------------------------------------*/
   else
    {
    PACKET *next = (PACKET *)(pptr + oldsize + BLOCK_OVERHEAD);
    int     temp;

    if (((char *)next < &_sys_memory[_memory_size - BLOCK_OVERHEAD]) &&
        (!(next->packet_size & BLOCK_USED))                           &&
        ((temp = oldsize + next->packet_size +BLOCK_OVERHEAD -newsize) >= 0))
      {
      mremove(next);
      ((PACKET *)pptr)->mflag    = 0x11111111;
      ((PACKET *)pptr)->this_ptr = (PACKET *)pptr;
      if (temp < MIN_BLOCK + BLOCK_OVERHEAD)
        {
        ((PACKET *)pptr)->packet_size = (newsize + temp) | BLOCK_USED;
        return(packet);
        }

      ((PACKET *)pptr)->packet_size = newsize | BLOCK_USED;
      pptr += newsize + BLOCK_OVERHEAD;
      ((PACKET *)pptr)->packet_size = temp - BLOCK_OVERHEAD;
      minsert((PACKET *)pptr);
      return(packet);
      }
     else
      {
      /*---------------------------------------------------------------*/
      /* ALLOCATE NEW PACKET AND MOVE DATA INTO IT.                    */
      /*---------------------------------------------------------------*/
      register char *new = sys_malloc(size);
      if (new == 0) return 0;
      sys_memcpy(new, packet, oldsize);

⌨️ 快捷键说明

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