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

📄 m_osip_list.c

📁 一个在linux下跟踪内存泄露的小程序,一般在linux下可以用 mtrace valgrind来做,但是在嵌入式上,由于内存的限制,mtrace valgrind都显得束手无策
💻 C
字号:
/*
  The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-)
  Copyright (C) 2001,2002,2003,2004,2005  Aymeric MOIZARD jack@atosc.org
  
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.
  
  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <stdio.h>
#include <stdlib.h>



#include "m_osip_list.h"

int m_osip_list_init (m_osip_list_t *li)
{
  if (li == NULL)
    return -1;
  memset (li, 0, sizeof (m_osip_list_t));
  return 0;                     /* ok */
}

void m_osip_list_special_free(m_osip_list_t * li, void *(*free_func) (void *))
{
  int pos = 0;
  void *element;

  if (li == NULL)
    return;
  while (!m_osip_list_eol (li, pos))
    {
      element = (void *) m_osip_list_get (li, pos);
      m_osip_list_remove (li, pos);
      if (free_func != NULL)
        free_func (element);
    }
  //free (li);
}

void
m_osip_list_ofchar_free(m_osip_list_t * li)
{
  int pos = 0;
  char *chain;

  if (li == NULL)
    return;
  while (!m_osip_list_eol (li, pos))
    {
      chain = (char *) m_osip_list_get (li, pos);
      m_osip_list_remove (li, pos);
      free (chain);
    }
  free (li);
}

int
m_osip_list_size (const m_osip_list_t * li)
{
  /* 
     Robin Nayathodan <roooot@softhome.net> 
     N.K Electronics INDIA

     NULL Checks  
   */

  if (li != NULL)
    return li->nb_elt;
  else
    return -1;
}

int
m_osip_list_eol (const m_osip_list_t * li, int i)
{
  if (li == NULL)
    return -1;
  if (i < li->nb_elt)
    return 0;                   /* not end of list */
  return 1;                     /* end of list */
}

/* index starts from 0; */
int
m_osip_list_add(m_osip_list_t * li, void *el, int pos)
{
  _m_node_t *ntmp;
  int i = 0;

  if (li == NULL)
    return -1;

  if (li->nb_elt == 0)
    {

      li->node = (_m_node_t *) malloc (sizeof (_m_node_t));
      if (li->node == NULL)
        return -1;
      li->node->element = el;
      li->node->next = NULL;
      li->nb_elt++;
      return li->nb_elt;
    }

  if (pos == -1 || pos >= li->nb_elt)
    {                           /* pos = -1insert at the end  */
      pos = li->nb_elt;
    }

  ntmp = li->node;              /* exist because nb_elt>0  */

  if (pos == 0)                 /* pos = 0 insert before first elt  */
    {
      li->node = (_m_node_t *) malloc (sizeof (_m_node_t));
      if (li->node == NULL)
        {
          /* leave the list unchanged */
          li->node = ntmp;
          return -1;
        }
      li->node->element = el;
      li->node->next = ntmp;
      li->nb_elt++;
      return li->nb_elt;
    }


  while (pos > i + 1)
    {
      i++;
      /* when pos>i next node exist  */
      ntmp = (_m_node_t *) ntmp->next;
    }

  /* if pos==nb_elt next node does not exist  */
  if (pos == li->nb_elt)
    {
      ntmp->next = (_m_node_t *) malloc (sizeof (_m_node_t));
      if (ntmp->next == NULL)
        return -1;              /* leave the list unchanged */
      ntmp = (_m_node_t *) ntmp->next;
      ntmp->element = el;
      ntmp->next = NULL;
      li->nb_elt++;
      return li->nb_elt;
    }

  /* here pos==i so next node is where we want to insert new node */
  {
    _m_node_t *nextnode = (_m_node_t *) ntmp->next;

    ntmp->next = (_m_node_t *) malloc (sizeof (_m_node_t));
    if (ntmp->next == NULL)
      {
        /* leave the list unchanged */
        ntmp->next = nextnode;
        return -1;
      }
    ntmp = (_m_node_t *) ntmp->next;
    ntmp->element = el;
    ntmp->next = nextnode;
    li->nb_elt++;
  }
  return li->nb_elt;
}

/* index starts from 0 */
void *
m_osip_list_get(const m_osip_list_t * li, int pos)
{
  _m_node_t *ntmp;
  int i = 0;

  if (li == NULL)
    return NULL;

  if (pos < 0 || pos >= li->nb_elt)
    /* element does not exist */
    return NULL;


  ntmp = li->node;              /* exist because nb_elt>0 */

  while (pos > i)
    {
      i++;
      ntmp = (_m_node_t *) ntmp->next;
    }
  return ntmp->element;
}

/* added by bennewit@cs.tu-berlin.de */
void *
m_osip_list_get_first(m_osip_list_t * li, m_osip_list_iterator_t * iterator)
{
  if (0 >= li->nb_elt)
    {
      iterator->actual = 0;
      return 0;
    }

  iterator->actual = li->node;
  iterator->prev = &li->node;
  iterator->li = li;
  iterator->pos = 0;

  return li->node->element;
}

/* added by bennewit@cs.tu-berlin.de */
void *
m_osip_list_get_next(m_osip_list_iterator_t * iterator)
{
  iterator->prev = (_m_node_t **) & (iterator->actual->next);
  iterator->actual = iterator->actual->next;
  ++(iterator->pos);

  if (m_osip_list_iterator_has_elem (*iterator))
    {
      return iterator->actual->element;
    }

  iterator->actual = 0;
  return 0;
}

/* added by bennewit@cs.tu-berlin.de */
void *
m_osip_list_iterator_remove(m_osip_list_iterator_t * iterator)
{
  if (m_osip_list_iterator_has_elem (*iterator))
    {
      --(iterator->li->nb_elt);

      *(iterator->prev) = iterator->actual->next;

      free (iterator->actual);
      iterator->actual = *(iterator->prev);
    }

  if (m_osip_list_iterator_has_elem (*iterator))
    {
      return iterator->actual->element;
    }

  return 0;
}

/* return -1 if failed */
int
m_osip_list_remove (m_osip_list_t * li, int pos)
{

  _m_node_t *ntmp;
  int i = 0;

  if (li == NULL)
    return -1;

  if (pos < 0 || pos >= li->nb_elt)
    /* element does not exist */
    return -1;

  ntmp = li->node;              /* exist because nb_elt>0 */

  if ((pos == 0))
    {                           /* special case  */
      li->node = (_m_node_t *) ntmp->next;
      li->nb_elt--;
      free (ntmp);
      return li->nb_elt;
    }

  while (pos > i + 1)
    {
      i++;
      ntmp = (_m_node_t *) ntmp->next;
    }

  /* insert new node */
  {
    _m_node_t *remnode;

    remnode = (_m_node_t *) ntmp->next;
    ntmp->next = ((_m_node_t *) ntmp->next)->next;
    free (remnode);
    li->nb_elt--;
  }
  return li->nb_elt;
}

⌨️ 快捷键说明

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