Commit | Line | Data |
---|---|---|
350da6ee DJ |
1 | /* Vector API for GDB. |
2 | Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc. | |
3 | Contributed by Nathan Sidwell <nathan@codesourcery.com> | |
4 | ||
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
20 | Boston, MA 02110-1301, USA. */ | |
21 | ||
350da6ee | 22 | #include "defs.h" |
8aceae7e | 23 | #include "vec.h" |
350da6ee DJ |
24 | |
25 | struct vec_prefix | |
26 | { | |
27 | unsigned num; | |
28 | unsigned alloc; | |
29 | void *vec[1]; | |
30 | }; | |
31 | ||
32 | /* Calculate the new ALLOC value, making sure that abs(RESERVE) slots | |
33 | are free. If RESERVE < 0 grow exactly, otherwise grow | |
34 | exponentially. */ | |
35 | ||
36 | static inline unsigned | |
37 | calculate_allocation (const struct vec_prefix *pfx, int reserve) | |
38 | { | |
39 | unsigned alloc = 0; | |
40 | unsigned num = 0; | |
41 | ||
42 | if (pfx) | |
43 | { | |
44 | alloc = pfx->alloc; | |
45 | num = pfx->num; | |
46 | } | |
47 | else if (!reserve) | |
48 | /* If there's no prefix, and we've not requested anything, then we | |
49 | will create a NULL vector. */ | |
50 | return 0; | |
51 | ||
52 | /* We must have run out of room. */ | |
53 | gdb_assert (alloc - num < (unsigned)(reserve < 0 ? -reserve : reserve)); | |
54 | ||
55 | if (reserve < 0) | |
56 | /* Exact size. */ | |
57 | alloc = num + -reserve; | |
58 | else | |
59 | { | |
60 | /* Exponential growth. */ | |
61 | if (!alloc) | |
62 | alloc = 4; | |
63 | else if (alloc < 16) | |
64 | /* Double when small. */ | |
65 | alloc = alloc * 2; | |
66 | else | |
67 | /* Grow slower when large. */ | |
68 | alloc = (alloc * 3 / 2); | |
69 | ||
70 | /* If this is still too small, set it to the right size. */ | |
71 | if (alloc < num + reserve) | |
72 | alloc = num + reserve; | |
73 | } | |
74 | return alloc; | |
75 | } | |
76 | ||
77 | /* Ensure there are at least abs(RESERVE) free slots in VEC. If | |
78 | RESERVE < 0 grow exactly, else grow exponentially. As a special | |
79 | case, if VEC is NULL, and RESERVE is 0, no vector will be created. */ | |
80 | ||
81 | void * | |
82 | vec_p_reserve (void *vec, int reserve) | |
83 | { | |
84 | return vec_o_reserve (vec, reserve, | |
85 | offsetof (struct vec_prefix, vec), sizeof (void *)); | |
86 | } | |
87 | ||
88 | /* As vec_p_reserve, but for object vectors. The vector's trailing | |
89 | array is at VEC_OFFSET offset and consists of ELT_SIZE sized | |
90 | elements. */ | |
91 | ||
92 | void * | |
93 | vec_o_reserve (void *vec, int reserve, size_t vec_offset, size_t elt_size) | |
94 | { | |
95 | struct vec_prefix *pfx = vec; | |
96 | unsigned alloc = calculate_allocation (pfx, reserve); | |
97 | ||
98 | if (!alloc) | |
99 | return NULL; | |
100 | ||
101 | vec = xrealloc (vec, vec_offset + alloc * elt_size); | |
102 | ((struct vec_prefix *)vec)->alloc = alloc; | |
103 | if (!pfx) | |
104 | ((struct vec_prefix *)vec)->num = 0; | |
105 | ||
106 | return vec; | |
107 | } | |
108 | ||
109 | #if 0 | |
110 | /* Example uses. */ | |
111 | DEF_VEC_I (int); | |
112 | typedef struct X | |
113 | { | |
114 | int i; | |
115 | } obj_t; | |
116 | typedef obj_t *ptr_t; | |
117 | ||
118 | DEF_VEC_P (ptr_t); | |
119 | DEF_VEC_O (obj_t); | |
120 | #endif |