summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKristian Høgsberg <krh@bitplanet.net>2012-03-04 13:40:49 -0500
committerKristian Høgsberg <krh@bitplanet.net>2012-03-04 13:40:49 -0500
commit8e2cac7ae4b36a81324a744d229a3a29690c214b (patch)
treed56e4b2e0074465b67bb59c7d5288c4a87318838
parentab0c7c58b9437f4aacdd039f5885534a575ff60e (diff)
Add wl_array_for_each
-rw-r--r--src/data-device.c10
-rw-r--r--src/wayland-util.h5
-rw-r--r--tests/array-test.c18
3 files changed, 27 insertions, 6 deletions
diff --git a/src/data-device.c b/src/data-device.c
index 40aa439..57f31ca 100644
--- a/src/data-device.c
+++ b/src/data-device.c
@@ -99,7 +99,7 @@ wl_data_source_send_offer(struct wl_data_source *source,
struct wl_resource *target)
{
struct wl_data_offer *offer;
- char **p, **end;
+ char **p;
offer = malloc(sizeof *offer);
if (offer == NULL)
@@ -122,8 +122,7 @@ wl_data_source_send_offer(struct wl_data_source *source,
wl_data_device_send_data_offer(target, &offer->resource);
- end = source->mime_types.data + source->mime_types.size;
- for (p = source->mime_types.data; p < end; p++)
+ wl_array_for_each(p, &source->mime_types)
wl_data_offer_send_offer(&offer->resource, *p);
return &offer->resource;
@@ -401,10 +400,9 @@ destroy_data_source(struct wl_resource *resource)
{
struct wl_data_source *source =
container_of(resource, struct wl_data_source, resource);
- char **p, **end;
+ char **p;
- end = source->mime_types.data + source->mime_types.size;
- for (p = source->mime_types.data; p < end; p++)
+ wl_array_for_each(p, &source->mime_types)
free(*p);
wl_array_release(&source->mime_types);
diff --git a/src/wayland-util.h b/src/wayland-util.h
index 7aa2166..1eca058 100644
--- a/src/wayland-util.h
+++ b/src/wayland-util.h
@@ -148,6 +148,11 @@ struct wl_array {
void *data;
};
+#define wl_array_for_each(pos, array) \
+ for (pos = (array)->data; \
+ (const char *) pos < ((const char *) (array)->data + (array)->size); \
+ (pos)++)
+
void wl_array_init(struct wl_array *array);
void wl_array_release(struct wl_array *array);
void *wl_array_add(struct wl_array *array, int size);
diff --git a/tests/array-test.c b/tests/array-test.c
index a7bf8a9..cb62713 100644
--- a/tests/array-test.c
+++ b/tests/array-test.c
@@ -116,3 +116,21 @@ TEST(array_copy)
wl_array_release(&source);
wl_array_release(&copy);
}
+
+TEST(array_for_each)
+{
+ static const int elements[] = { 77, 12, 45192, 53280, 334455 };
+ struct wl_array array;
+ int *p, i;
+
+ wl_array_init(&array);
+ for (i = 0; i < 5; i++) {
+ p = wl_array_add(&array, sizeof *p);
+ *p = elements[i];
+ }
+
+ i = 0;
+ wl_array_for_each(p, &array)
+ assert(*p == elements[i++]);
+ assert(i == 5);
+}