OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gdb-6.8/] [gdb/] [vec.c] - Diff between revs 827 and 840

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 827 Rev 840
/* Vector API for GDB.
/* Vector API for GDB.
   Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
   Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
   Contributed by Nathan Sidwell <nathan@codesourcery.com>
   Contributed by Nathan Sidwell <nathan@codesourcery.com>
 
 
   This file is part of GDB.
   This file is part of GDB.
 
 
   This program is free software; you can redistribute it and/or modify
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   (at your option) any later version.
 
 
   This program is distributed in the hope that it will be useful,
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   GNU General Public License for more details.
 
 
   You should have received a copy of the GNU General Public License
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 
#include "defs.h"
#include "defs.h"
#include "vec.h"
#include "vec.h"
 
 
struct vec_prefix
struct vec_prefix
{
{
  unsigned num;
  unsigned num;
  unsigned alloc;
  unsigned alloc;
  void *vec[1];
  void *vec[1];
};
};
 
 
/* Calculate the new ALLOC value, making sure that abs(RESERVE) slots
/* Calculate the new ALLOC value, making sure that abs(RESERVE) slots
   are free.  If RESERVE < 0 grow exactly, otherwise grow
   are free.  If RESERVE < 0 grow exactly, otherwise grow
   exponentially.  */
   exponentially.  */
 
 
static inline unsigned
static inline unsigned
calculate_allocation (const struct vec_prefix *pfx, int reserve)
calculate_allocation (const struct vec_prefix *pfx, int reserve)
{
{
  unsigned alloc = 0;
  unsigned alloc = 0;
  unsigned num = 0;
  unsigned num = 0;
 
 
  if (pfx)
  if (pfx)
    {
    {
      alloc = pfx->alloc;
      alloc = pfx->alloc;
      num = pfx->num;
      num = pfx->num;
    }
    }
  else if (!reserve)
  else if (!reserve)
    /* If there's no prefix, and we've not requested anything, then we
    /* If there's no prefix, and we've not requested anything, then we
       will create a NULL vector.  */
       will create a NULL vector.  */
    return 0;
    return 0;
 
 
  /* We must have run out of room.  */
  /* We must have run out of room.  */
  gdb_assert (alloc - num < (unsigned)(reserve < 0 ? -reserve : reserve));
  gdb_assert (alloc - num < (unsigned)(reserve < 0 ? -reserve : reserve));
 
 
  if (reserve < 0)
  if (reserve < 0)
    /* Exact size.  */
    /* Exact size.  */
    alloc = num + -reserve;
    alloc = num + -reserve;
  else
  else
    {
    {
      /* Exponential growth. */
      /* Exponential growth. */
      if (!alloc)
      if (!alloc)
        alloc = 4;
        alloc = 4;
      else if (alloc < 16)
      else if (alloc < 16)
        /* Double when small.  */
        /* Double when small.  */
        alloc = alloc * 2;
        alloc = alloc * 2;
      else
      else
        /* Grow slower when large.  */
        /* Grow slower when large.  */
        alloc = (alloc * 3 / 2);
        alloc = (alloc * 3 / 2);
 
 
      /* If this is still too small, set it to the right size. */
      /* If this is still too small, set it to the right size. */
      if (alloc < num + reserve)
      if (alloc < num + reserve)
        alloc = num + reserve;
        alloc = num + reserve;
    }
    }
  return alloc;
  return alloc;
}
}
 
 
/* Ensure there are at least abs(RESERVE) free slots in VEC.  If
/* Ensure there are at least abs(RESERVE) free slots in VEC.  If
   RESERVE < 0 grow exactly, else grow exponentially.  As a special
   RESERVE < 0 grow exactly, else grow exponentially.  As a special
   case, if VEC is NULL, and RESERVE is 0, no vector will be created. */
   case, if VEC is NULL, and RESERVE is 0, no vector will be created. */
 
 
void *
void *
vec_p_reserve (void *vec, int reserve)
vec_p_reserve (void *vec, int reserve)
{
{
  return vec_o_reserve (vec, reserve,
  return vec_o_reserve (vec, reserve,
                        offsetof (struct vec_prefix, vec), sizeof (void *));
                        offsetof (struct vec_prefix, vec), sizeof (void *));
}
}
 
 
/* As vec_p_reserve, but for object vectors.  The vector's trailing
/* As vec_p_reserve, but for object vectors.  The vector's trailing
   array is at VEC_OFFSET offset and consists of ELT_SIZE sized
   array is at VEC_OFFSET offset and consists of ELT_SIZE sized
   elements.  */
   elements.  */
 
 
void *
void *
vec_o_reserve (void *vec, int reserve, size_t vec_offset, size_t elt_size)
vec_o_reserve (void *vec, int reserve, size_t vec_offset, size_t elt_size)
{
{
  struct vec_prefix *pfx = vec;
  struct vec_prefix *pfx = vec;
  unsigned alloc = calculate_allocation (pfx, reserve);
  unsigned alloc = calculate_allocation (pfx, reserve);
 
 
  if (!alloc)
  if (!alloc)
    return NULL;
    return NULL;
 
 
  vec = xrealloc (vec, vec_offset + alloc * elt_size);
  vec = xrealloc (vec, vec_offset + alloc * elt_size);
  ((struct vec_prefix *)vec)->alloc = alloc;
  ((struct vec_prefix *)vec)->alloc = alloc;
  if (!pfx)
  if (!pfx)
    ((struct vec_prefix *)vec)->num = 0;
    ((struct vec_prefix *)vec)->num = 0;
 
 
  return vec;
  return vec;
}
}
 
 
#if 0
#if 0
/* Example uses.  */
/* Example uses.  */
DEF_VEC_I (int);
DEF_VEC_I (int);
typedef struct X
typedef struct X
{
{
  int i;
  int i;
} obj_t;
} obj_t;
typedef obj_t *ptr_t;
typedef obj_t *ptr_t;
 
 
DEF_VEC_P (ptr_t);
DEF_VEC_P (ptr_t);
DEF_VEC_O (obj_t);
DEF_VEC_O (obj_t);
#endif
#endif
 
 

powered by: WebSVN 2.1.0

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