2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Nathan Sidwell <nathan@codesourcery.com>
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
31 /* Calculate the new ALLOC value, making sure that abs(RESERVE) slots
32 are free. If RESERVE < 0 grow exactly, otherwise grow
35 static inline unsigned
36 calculate_allocation (const struct vec_prefix *pfx, int reserve)
47 /* If there's no prefix, and we've not requested anything, then we
48 will create a NULL vector. */
51 /* We must have run out of room. */
52 gdb_assert (alloc - num < (unsigned)(reserve < 0 ? -reserve : reserve));
56 alloc = num + -reserve;
59 /* Exponential growth. */
63 /* Double when small. */
66 /* Grow slower when large. */
67 alloc = (alloc * 3 / 2);
69 /* If this is still too small, set it to the right size. */
70 if (alloc < num + reserve)
71 alloc = num + reserve;
76 /* Ensure there are at least abs(RESERVE) free slots in VEC. If
77 RESERVE < 0 grow exactly, else grow exponentially. As a special
78 case, if VEC is NULL, and RESERVE is 0, no vector will be created. */
81 vec_p_reserve (void *vec, int reserve)
83 return vec_o_reserve (vec, reserve,
84 offsetof (struct vec_prefix, vec), sizeof (void *));
87 /* As vec_p_reserve, but for object vectors. The vector's trailing
88 array is at VEC_OFFSET offset and consists of ELT_SIZE sized
92 vec_o_reserve (void *vec, int reserve, size_t vec_offset, size_t elt_size)
94 struct vec_prefix *pfx = vec;
95 unsigned alloc = calculate_allocation (pfx, reserve);
100 vec = xrealloc (vec, vec_offset + alloc * elt_size);
101 ((struct vec_prefix *)vec)->alloc = alloc;
103 ((struct vec_prefix *)vec)->num = 0;
115 typedef obj_t *ptr_t;