Mercurial > dovecot > original-hg > dovecot-1.2
annotate src/lib/array.c @ 9451:9fff30644260 HEAD
istream-concat: Fixed a lot of bugs.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Mon, 26 Oct 2009 17:06:57 -0400 |
parents | 9ab0177cd004 |
children | 00cd9aacd03c |
rev | line source |
---|---|
8590
b9faf4db2a9f
Updated copyright notices to include year 2009.
Timo Sirainen <tss@iki.fi>
parents:
7912
diff
changeset
|
1 /* Copyright (c) 2003-2009 Dovecot authors, see the included COPYING file */ |
7857
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
2 |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
3 #include "lib.h" |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
4 #include "array.h" |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
5 |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
6 void *array_idx_modifiable_i(struct array *array, unsigned int idx) |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
7 { |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
8 size_t pos; |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
9 |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
10 pos = idx * array->element_size; |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
11 if (pos >= array->buffer->used) { |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
12 /* index doesn't exist yet, initialize with zero */ |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
13 buffer_append_zero(array->buffer, pos + array->element_size - |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
14 array->buffer->used); |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
15 } |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
16 return buffer_get_space_unsafe(array->buffer, pos, array->element_size); |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
17 } |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
18 |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
19 void array_idx_set_i(struct array *array, unsigned int idx, const void *data) |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
20 { |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
21 size_t pos; |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
22 |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
23 pos = idx * array->element_size; |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
24 if (pos > array->buffer->used) { |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
25 /* index doesn't exist yet, initialize with zero */ |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
26 buffer_append_zero(array->buffer, pos - array->buffer->used); |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
27 } |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
28 buffer_write(array->buffer, pos, data, array->element_size); |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
29 } |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
30 |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
31 void array_idx_clear_i(struct array *array, unsigned int idx) |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
32 { |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
33 size_t pos; |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
34 |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
35 pos = idx * array->element_size; |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
36 if (pos > array->buffer->used) { |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
37 /* index doesn't exist yet, initialize with zero */ |
8972
9ab0177cd004
array_idx_clear() was broken when clearing an index outside the existing array.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
38 buffer_append_zero(array->buffer, pos - array->buffer->used + |
9ab0177cd004
array_idx_clear() was broken when clearing an index outside the existing array.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
39 array->element_size); |
7857
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
40 } else { |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
41 buffer_write_zero(array->buffer, pos, array->element_size); |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
42 } |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
43 } |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
44 |
7858 | 45 void *array_insert_space_i(struct array *array, unsigned int idx) |
46 { | |
47 void *data; | |
48 size_t pos; | |
49 | |
50 pos = idx * array->element_size; | |
51 buffer_copy(array->buffer, pos + array->element_size, | |
52 array->buffer, pos, (size_t)-1); | |
53 | |
54 data = buffer_get_space_unsafe(array->buffer, pos, array->element_size); | |
55 memset(data, 0, array->element_size); | |
56 return data; | |
57 } | |
58 | |
7857
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
59 bool array_cmp_i(const struct array *array1, const struct array *array2) |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
60 { |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
61 if (!array_is_created_i(array1) || array1->buffer->used == 0) |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
62 return !array_is_created_i(array2) || array2->buffer->used == 0; |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
63 |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
64 if (!array_is_created_i(array2)) |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
65 return FALSE; |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
66 |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
67 return buffer_cmp(array1->buffer, array2->buffer); |
63df79ccfedb
Uninlined some of the more complex array functions.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
68 } |
7858 | 69 |
7912
81806d402514
Added more consts, ATTR_CONSTs and ATTR_PUREs.
Timo Sirainen <tss@iki.fi>
parents:
7858
diff
changeset
|
70 void array_reverse_i(struct array *array) |
7858 | 71 { |
72 const unsigned int element_size = array->element_size; | |
73 unsigned int i, count = array_count_i(array); | |
74 size_t size; | |
75 void *data, *tmp; | |
76 | |
77 data = buffer_get_modifiable_data(array->buffer, &size); | |
78 tmp = t_buffer_get(array->element_size); | |
79 for (i = 0; i+1 < count; i++, count--) { | |
80 memcpy(tmp, PTR_OFFSET(data, i * element_size), element_size); | |
81 memcpy(PTR_OFFSET(data, i * element_size), | |
82 PTR_OFFSET(data, (count-1) * element_size), | |
83 element_size); | |
84 memcpy(PTR_OFFSET(data, (count-1) * element_size), tmp, | |
85 element_size); | |
86 } | |
87 } |