Mercurial > projects > ldc
annotate dmd/array.c @ 1187:a95fc9fcad5c
Make sure -defaultlib and -debuglib don't get cut off if longer than 63 chars,
and clean up some overly verbose code.
author | Frits van Bommel <fvbommel wxs.nl> |
---|---|
date | Wed, 01 Apr 2009 00:52:31 +0200 |
parents | df386fd4030e |
children |
rev | line source |
---|---|
1 | 1 |
2 // Copyright (c) 1999-2006 by Digital Mars | |
3 // All Rights Reserved | |
4 // written by Walter Bright | |
5 // www.digitalmars.com | |
6 // License for redistribution is by either the Artistic License | |
7 // in artistic.txt, or the GNU General Public License in gnu.txt. | |
8 // See the included readme.txt for details. | |
9 | |
10 #include <stdio.h> | |
11 #include <stdlib.h> | |
12 #include <stdarg.h> | |
13 #include <string.h> | |
14 #include <assert.h> | |
15 | |
872
aa953cc960b6
Apply BlueZeniX's patch for OpenSolaris compatibility. Fixes #158.
Christian Kamm <kamm incasoftware de>
parents:
285
diff
changeset
|
16 #if (defined (__SVR4) && defined (__sun)) |
aa953cc960b6
Apply BlueZeniX's patch for OpenSolaris compatibility. Fixes #158.
Christian Kamm <kamm incasoftware de>
parents:
285
diff
changeset
|
17 #include <alloca.h> |
aa953cc960b6
Apply BlueZeniX's patch for OpenSolaris compatibility. Fixes #158.
Christian Kamm <kamm incasoftware de>
parents:
285
diff
changeset
|
18 #endif |
aa953cc960b6
Apply BlueZeniX's patch for OpenSolaris compatibility. Fixes #158.
Christian Kamm <kamm incasoftware de>
parents:
285
diff
changeset
|
19 |
285
297690b5d4a5
[svn r306] Fixed: it's now possible to compile and link llvmdc with MinGW32 and msys on Win32 :D I tried it myself ;) Building the runtime still needs some work, but it's a step in the right direction.
lindquist
parents:
1
diff
changeset
|
20 #if _MSC_VER || __MINGW32__ |
1 | 21 #include <malloc.h> |
22 #endif | |
23 | |
24 #if IN_GCC | |
25 #include "gdc_alloca.h" | |
26 #endif | |
27 | |
28 #if _WIN32 | |
29 #include <windows.h> | |
30 #endif | |
31 | |
32 #ifndef _WIN32 | |
33 #include <sys/types.h> | |
34 #include <sys/stat.h> | |
35 #include <fcntl.h> | |
36 #include <errno.h> | |
37 #include <unistd.h> | |
38 #include <utime.h> | |
39 #endif | |
40 | |
41 #include "port.h" | |
42 #include "root.h" | |
43 #include "dchar.h" | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
872
diff
changeset
|
44 #include "rmem.h" |
1 | 45 |
46 | |
47 /********************************* Array ****************************/ | |
48 | |
49 Array::Array() | |
50 { | |
51 data = NULL; | |
52 dim = 0; | |
53 allocdim = 0; | |
54 } | |
55 | |
56 Array::~Array() | |
57 { | |
58 mem.free(data); | |
59 } | |
60 | |
61 void Array::mark() | |
62 { unsigned u; | |
63 | |
64 mem.mark(data); | |
65 for (u = 0; u < dim; u++) | |
66 mem.mark(data[u]); // BUG: what if arrays of Object's? | |
67 } | |
68 | |
69 void Array::reserve(unsigned nentries) | |
70 { | |
71 //printf("Array::reserve: size = %d, offset = %d, nbytes = %d\n", size, offset, nbytes); | |
72 if (allocdim - dim < nentries) | |
73 { | |
74 allocdim = dim + nentries; | |
75 data = (void **)mem.realloc(data, allocdim * sizeof(*data)); | |
76 } | |
77 } | |
78 | |
79 void Array::setDim(unsigned newdim) | |
80 { | |
81 if (dim < newdim) | |
82 { | |
83 reserve(newdim - dim); | |
84 } | |
85 dim = newdim; | |
86 } | |
87 | |
88 void Array::fixDim() | |
89 { | |
90 if (dim != allocdim) | |
91 { data = (void **)mem.realloc(data, dim * sizeof(*data)); | |
92 allocdim = dim; | |
93 } | |
94 } | |
95 | |
96 void Array::push(void *ptr) | |
97 { | |
98 reserve(1); | |
99 data[dim++] = ptr; | |
100 } | |
101 | |
102 void *Array::pop() | |
103 { | |
104 return data[--dim]; | |
105 } | |
106 | |
107 void Array::shift(void *ptr) | |
108 { | |
109 reserve(1); | |
110 memmove(data + 1, data, dim * sizeof(*data)); | |
111 data[0] = ptr; | |
112 dim++; | |
113 } | |
114 | |
115 void Array::insert(unsigned index, void *ptr) | |
116 { | |
117 reserve(1); | |
118 memmove(data + index + 1, data + index, (dim - index) * sizeof(*data)); | |
119 data[index] = ptr; | |
120 dim++; | |
121 } | |
122 | |
123 | |
124 void Array::insert(unsigned index, Array *a) | |
125 { | |
126 if (a) | |
127 { unsigned d; | |
128 | |
129 d = a->dim; | |
130 reserve(d); | |
131 if (dim != index) | |
132 memmove(data + index + d, data + index, (dim - index) * sizeof(*data)); | |
133 memcpy(data + index, a->data, d * sizeof(*data)); | |
134 dim += d; | |
135 } | |
136 } | |
137 | |
138 | |
139 /*********************************** | |
140 * Append array a to this array. | |
141 */ | |
142 | |
143 void Array::append(Array *a) | |
144 { | |
145 insert(dim, a); | |
146 } | |
147 | |
148 void Array::remove(unsigned i) | |
149 { | |
1183
df386fd4030e
Commit requested by lindquist.
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
150 memmove(data + i, data + i + 1, (dim - i - 1) * sizeof(data[0])); |
1 | 151 dim--; |
152 } | |
153 | |
154 char *Array::toChars() | |
155 { | |
156 unsigned len; | |
157 unsigned u; | |
158 char **buf; | |
159 char *str; | |
160 char *p; | |
161 | |
162 buf = (char **)alloca(dim * sizeof(char *)); | |
163 len = 2; | |
164 for (u = 0; u < dim; u++) | |
165 { | |
166 buf[u] = ((Object *)data[u])->toChars(); | |
167 len += strlen(buf[u]) + 1; | |
168 } | |
169 str = (char *)mem.malloc(len); | |
170 | |
171 str[0] = '['; | |
172 p = str + 1; | |
173 for (u = 0; u < dim; u++) | |
174 { | |
175 if (u) | |
176 *p++ = ','; | |
177 len = strlen(buf[u]); | |
178 memcpy(p,buf[u],len); | |
179 p += len; | |
180 } | |
181 *p++ = ']'; | |
182 *p = 0; | |
183 return str; | |
184 } | |
185 | |
186 void Array::zero() | |
187 { | |
188 memset(data,0,dim * sizeof(data[0])); | |
189 } | |
190 | |
191 void *Array::tos() | |
192 { | |
193 return dim ? data[dim - 1] : NULL; | |
194 } | |
195 | |
196 int | |
197 #if _WIN32 | |
198 __cdecl | |
199 #endif | |
200 Array_sort_compare(const void *x, const void *y) | |
201 { | |
202 Object *ox = *(Object **)x; | |
203 Object *oy = *(Object **)y; | |
204 | |
205 return ox->compare(oy); | |
206 } | |
207 | |
208 void Array::sort() | |
209 { | |
210 if (dim) | |
211 { | |
212 qsort(data, dim, sizeof(Object *), Array_sort_compare); | |
213 } | |
214 } | |
215 | |
216 Array *Array::copy() | |
217 { | |
218 Array *a = new Array(); | |
219 | |
220 a->setDim(dim); | |
221 memcpy(a->data, data, dim * sizeof(void *)); | |
222 return a; | |
223 } | |
224 |