@@ -1,156 +1,156 @@
|
|
1 |
#include <assert.h>
|
2 |
#include <stdlib.h>
|
3 |
#include <pthread.h>
|
4 |
|
5 |
-
#include "
|
6 |
|
7 |
|
8 |
-
typedef struct
|
9 |
{
|
10 |
void** elements;
|
11 |
size_t capacity;
|
12 |
size_t count;
|
13 |
pthread_rwlock_t rwlock;
|
14 |
-
}
|
15 |
|
16 |
// Private declarations
|
17 |
-
size_t
|
18 |
|
19 |
|
20 |
-
|
21 |
{
|
22 |
assert(capacity);
|
23 |
|
24 |
-
|
25 |
if ( array == NULL )
|
26 |
return NULL;
|
27 |
|
28 |
array->capacity = capacity;
|
29 |
array->count = 0;
|
30 |
|
31 |
pthread_rwlock_init( &array->rwlock, NULL );
|
32 |
|
33 |
array->elements = (void**)malloc( capacity * sizeof(void*) );
|
34 |
if ( array->elements == NULL )
|
35 |
return (void)free(array), NULL;
|
36 |
|
37 |
return array;
|
38 |
}
|
39 |
|
40 |
-
void
|
41 |
{
|
42 |
assert(array);
|
43 |
|
44 |
pthread_rwlock_destroy(&array->rwlock);
|
45 |
|
46 |
free(array->elements);
|
47 |
free(array);
|
48 |
}
|
49 |
|
50 |
-
int
|
51 |
{
|
52 |
assert(array);
|
53 |
|
54 |
size_t new_capacity = 10 * array->capacity;
|
55 |
void** new_elements = (void**)realloc( array->elements, new_capacity * sizeof(void*) );
|
56 |
if ( new_elements == NULL )
|
57 |
return -1;
|
58 |
|
59 |
array->capacity = new_capacity;
|
60 |
array->elements = new_elements;
|
61 |
|
62 |
return 0; // Success
|
63 |
}
|
64 |
|
65 |
-
int
|
66 |
{
|
67 |
assert(array);
|
68 |
|
69 |
size_t new_capacity = array->capacity / 10;
|
70 |
if ( new_capacity < 10 )
|
71 |
return 0;
|
72 |
|
73 |
void** new_elements = (void**)realloc( array->elements, new_capacity * sizeof(void*) );
|
74 |
if ( new_elements == NULL )
|
75 |
return -1;
|
76 |
|
77 |
array->capacity = new_capacity;
|
78 |
array->elements = new_elements;
|
79 |
|
80 |
return 0; // Success
|
81 |
}
|
82 |
|
83 |
-
size_t
|
84 |
{
|
85 |
assert(array);
|
86 |
|
87 |
pthread_rwlock_rdlock( &array->rwlock );
|
88 |
size_t result = array->count;
|
89 |
pthread_rwlock_unlock( &array->rwlock );
|
90 |
return result;
|
91 |
}
|
92 |
|
93 |
-
void*
|
94 |
{
|
95 |
assert(array);
|
96 |
-
assert( index <
|
97 |
|
98 |
pthread_rwlock_rdlock( &array->rwlock );
|
99 |
void* result = array->elements[index];
|
100 |
pthread_rwlock_unlock( &array->rwlock );
|
101 |
return result;
|
102 |
}
|
103 |
|
104 |
-
int
|
105 |
{
|
106 |
assert(array);
|
107 |
|
108 |
pthread_rwlock_wrlock( &array->rwlock );
|
109 |
if ( array->count == array->capacity )
|
110 |
-
if (
|
111 |
return (void)pthread_rwlock_unlock( &array->rwlock ), -1;
|
112 |
|
113 |
assert( array->count < array->capacity );
|
114 |
array->elements[array->count++] = element;
|
115 |
pthread_rwlock_unlock( &array->rwlock );
|
116 |
return 0; // Success
|
117 |
}
|
118 |
|
119 |
-
size_t
|
120 |
{
|
121 |
assert( array );
|
122 |
|
123 |
pthread_rwlock_rdlock( &array->rwlock );
|
124 |
-
size_t result =
|
125 |
pthread_rwlock_unlock( &array->rwlock );
|
126 |
return result;
|
127 |
}
|
128 |
|
129 |
-
size_t
|
130 |
{
|
131 |
assert( array );
|
132 |
|
133 |
for ( size_t index = start_pos; index < array->count; ++index )
|
134 |
if ( array->elements[index] == element )
|
135 |
return index;
|
136 |
|
137 |
-
return
|
138 |
}
|
139 |
|
140 |
-
int
|
141 |
{
|
142 |
assert( array );
|
143 |
|
144 |
pthread_rwlock_wrlock( &array->rwlock );
|
145 |
-
size_t index =
|
146 |
-
if ( index ==
|
147 |
return (void)pthread_rwlock_unlock( &array->rwlock ), -1;
|
148 |
|
149 |
for ( --array->count; index < array->count; ++index )
|
150 |
array->elements[index] = array->elements[index + 1];
|
151 |
if ( array->count == array->capacity / 10 )
|
152 |
-
|
153 |
|
154 |
pthread_rwlock_unlock( &array->rwlock );
|
155 |
return 0; // Removed
|
156 |
}
|
1 |
#include <assert.h>
|
2 |
#include <stdlib.h>
|
3 |
#include <pthread.h>
|
4 |
|
5 |
+
#include "array_rwlock.h"
|
6 |
|
7 |
|
8 |
+
typedef struct array_rwlock
|
9 |
{
|
10 |
void** elements;
|
11 |
size_t capacity;
|
12 |
size_t count;
|
13 |
pthread_rwlock_t rwlock;
|
14 |
+
} array_rwlock_t;
|
15 |
|
16 |
// Private declarations
|
17 |
+
size_t array_rwlock_find_first_private(array_rwlock_t* array, const void* element, size_t start_pos);
|
18 |
|
19 |
|
20 |
+
array_rwlock_t* array_rwlock_create(size_t capacity)
|
21 |
{
|
22 |
assert(capacity);
|
23 |
|
24 |
+
array_rwlock_t* array = calloc(1, sizeof(array_rwlock_t));
|
25 |
if ( array == NULL )
|
26 |
return NULL;
|
27 |
|
28 |
array->capacity = capacity;
|
29 |
array->count = 0;
|
30 |
|
31 |
pthread_rwlock_init( &array->rwlock, NULL );
|
32 |
|
33 |
array->elements = (void**)malloc( capacity * sizeof(void*) );
|
34 |
if ( array->elements == NULL )
|
35 |
return (void)free(array), NULL;
|
36 |
|
37 |
return array;
|
38 |
}
|
39 |
|
40 |
+
void array_rwlock_destroy(array_rwlock_t* array)
|
41 |
{
|
42 |
assert(array);
|
43 |
|
44 |
pthread_rwlock_destroy(&array->rwlock);
|
45 |
|
46 |
free(array->elements);
|
47 |
free(array);
|
48 |
}
|
49 |
|
50 |
+
int array_rwlock_increase_capacity(array_rwlock_t* array)
|
51 |
{
|
52 |
assert(array);
|
53 |
|
54 |
size_t new_capacity = 10 * array->capacity;
|
55 |
void** new_elements = (void**)realloc( array->elements, new_capacity * sizeof(void*) );
|
56 |
if ( new_elements == NULL )
|
57 |
return -1;
|
58 |
|
59 |
array->capacity = new_capacity;
|
60 |
array->elements = new_elements;
|
61 |
|
62 |
return 0; // Success
|
63 |
}
|
64 |
|
65 |
+
int array_rwlock_decrease_capacity(array_rwlock_t* array)
|
66 |
{
|
67 |
assert(array);
|
68 |
|
69 |
size_t new_capacity = array->capacity / 10;
|
70 |
if ( new_capacity < 10 )
|
71 |
return 0;
|
72 |
|
73 |
void** new_elements = (void**)realloc( array->elements, new_capacity * sizeof(void*) );
|
74 |
if ( new_elements == NULL )
|
75 |
return -1;
|
76 |
|
77 |
array->capacity = new_capacity;
|
78 |
array->elements = new_elements;
|
79 |
|
80 |
return 0; // Success
|
81 |
}
|
82 |
|
83 |
+
size_t array_rwlock_get_count(array_rwlock_t* array)
|
84 |
{
|
85 |
assert(array);
|
86 |
|
87 |
pthread_rwlock_rdlock( &array->rwlock );
|
88 |
size_t result = array->count;
|
89 |
pthread_rwlock_unlock( &array->rwlock );
|
90 |
return result;
|
91 |
}
|
92 |
|
93 |
+
void* array_rwlock_get_element(array_rwlock_t* array, size_t index)
|
94 |
{
|
95 |
assert(array);
|
96 |
+
assert( index < array_rwlock_get_count(array) );
|
97 |
|
98 |
pthread_rwlock_rdlock( &array->rwlock );
|
99 |
void* result = array->elements[index];
|
100 |
pthread_rwlock_unlock( &array->rwlock );
|
101 |
return result;
|
102 |
}
|
103 |
|
104 |
+
int array_rwlock_append(array_rwlock_t* array, void* element)
|
105 |
{
|
106 |
assert(array);
|
107 |
|
108 |
pthread_rwlock_wrlock( &array->rwlock );
|
109 |
if ( array->count == array->capacity )
|
110 |
+
if ( array_rwlock_increase_capacity(array) )
|
111 |
return (void)pthread_rwlock_unlock( &array->rwlock ), -1;
|
112 |
|
113 |
assert( array->count < array->capacity );
|
114 |
array->elements[array->count++] = element;
|
115 |
pthread_rwlock_unlock( &array->rwlock );
|
116 |
return 0; // Success
|
117 |
}
|
118 |
|
119 |
+
size_t array_rwlock_find_first(array_rwlock_t* array, const void* element, size_t start_pos)
|
120 |
{
|
121 |
assert( array );
|
122 |
|
123 |
pthread_rwlock_rdlock( &array->rwlock );
|
124 |
+
size_t result = array_rwlock_find_first_private(array, element, start_pos);
|
125 |
pthread_rwlock_unlock( &array->rwlock );
|
126 |
return result;
|
127 |
}
|
128 |
|
129 |
+
size_t array_rwlock_find_first_private(array_rwlock_t* array, const void* element, size_t start_pos)
|
130 |
{
|
131 |
assert( array );
|
132 |
|
133 |
for ( size_t index = start_pos; index < array->count; ++index )
|
134 |
if ( array->elements[index] == element )
|
135 |
return index;
|
136 |
|
137 |
+
return array_rwlock_not_found;
|
138 |
}
|
139 |
|
140 |
+
int array_rwlock_remove_first(array_rwlock_t* array, const void* element, size_t start_pos)
|
141 |
{
|
142 |
assert( array );
|
143 |
|
144 |
pthread_rwlock_wrlock( &array->rwlock );
|
145 |
+
size_t index = array_rwlock_find_first_private(array, element, start_pos);
|
146 |
+
if ( index == array_rwlock_not_found )
|
147 |
return (void)pthread_rwlock_unlock( &array->rwlock ), -1;
|
148 |
|
149 |
for ( --array->count; index < array->count; ++index )
|
150 |
array->elements[index] = array->elements[index + 1];
|
151 |
if ( array->count == array->capacity / 10 )
|
152 |
+
array_rwlock_decrease_capacity(array);
|
153 |
|
154 |
pthread_rwlock_unlock( &array->rwlock );
|
155 |
return 0; // Removed
|
156 |
}
|