kernel - Fix hold race and catch bad vm_object terminations
[dragonfly.git] / contrib / gdb-7 / gdb / vec.c
blob9bf9fcf20757af6775151dc97b9455aa89f2a25f
1 /* Vector API for GDB.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
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/>. */
21 #include "defs.h"
22 #include "vec.h"
24 struct vec_prefix
26 unsigned num;
27 unsigned alloc;
28 void *vec[1];
31 /* Calculate the new ALLOC value, making sure that abs(RESERVE) slots
32 are free. If RESERVE < 0 grow exactly, otherwise grow
33 exponentially. */
35 static inline unsigned
36 calculate_allocation (const struct vec_prefix *pfx, int reserve)
38 unsigned alloc = 0;
39 unsigned num = 0;
41 if (pfx)
43 alloc = pfx->alloc;
44 num = pfx->num;
46 else if (!reserve)
47 /* If there's no prefix, and we've not requested anything, then we
48 will create a NULL vector. */
49 return 0;
51 /* We must have run out of room. */
52 gdb_assert (alloc - num < (unsigned)(reserve < 0 ? -reserve : reserve));
54 if (reserve < 0)
55 /* Exact size. */
56 alloc = num + -reserve;
57 else
59 /* Exponential growth. */
60 if (!alloc)
61 alloc = 4;
62 else if (alloc < 16)
63 /* Double when small. */
64 alloc = alloc * 2;
65 else
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;
73 return alloc;
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. */
80 void *
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
89 elements. */
91 void *
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);
97 if (!alloc)
98 return NULL;
100 vec = xrealloc (vec, vec_offset + alloc * elt_size);
101 ((struct vec_prefix *)vec)->alloc = alloc;
102 if (!pfx)
103 ((struct vec_prefix *)vec)->num = 0;
105 return vec;
108 #if 0
109 /* Example uses. */
110 DEF_VEC_I (int);
111 typedef struct X
113 int i;
114 } obj_t;
115 typedef obj_t *ptr_t;
117 DEF_VEC_P (ptr_t);
118 DEF_VEC_O (obj_t);
119 #endif