aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichał Łyszczek <michal.lyszczek@bofc.pl>2018-09-25 19:58:17 +0200
committerMichał Łyszczek <michal.lyszczek@bofc.pl>2018-09-25 19:58:17 +0200
commit00c021390fbbede8d360d2ae4ade579b303a4f29 (patch)
tree3e883050bdfbb67f901491326da3626b562144f7
parentf0e03c810e649e4ab585f32a410b393cf97728bf (diff)
downloadlibrb-00c021390fbbede8d360d2ae4ade579b303a4f29.tar.gz
librb-00c021390fbbede8d360d2ae4ade579b303a4f29.tar.bz2
librb-00c021390fbbede8d360d2ae4ade579b303a4f29.zip
add: rb_array_size macro
-rw-r--r--man/rb_array_size.363
-rw-r--r--man/rb_clear.31
-rw-r--r--man/rb_destroy.31
-rw-r--r--man/rb_discard.31
-rw-r--r--man/rb_header_size.31
-rw-r--r--man/rb_new.31
-rw-r--r--man/rb_overview.71
-rw-r--r--man/rb_read.31
-rw-r--r--man/rb_space.31
-rw-r--r--man/rb_version.31
-rw-r--r--man/rb_write.31
-rw-r--r--rb.h2
-rw-r--r--tests.c20
13 files changed, 85 insertions, 10 deletions
diff --git a/man/rb_array_size.3 b/man/rb_array_size.3
new file mode 100644
index 0000000..ff326ec
--- /dev/null
+++ b/man/rb_array_size.3
@@ -0,0 +1,63 @@
+.TH "rb_array_size" "3" "25 September 2018 (v1.0.1)" "bofc.pl"
+.SH NAME
+.PP
+.B rb_array_size
+- gets number of elements from given array
+.SH SYNOPSIS
+.PP
+.BI "#define rb_array_size(" a ")"
+.SH DESCRIPTION
+This simple macro can be used to quickly calculate number of elements needed
+by
+.BR rb_write (3)
+or
+.BR rb_read (3)
+functions from array.
+.PP
+Let's analyze
+.B int arr[8]
+array.
+Calling rb function like this:
+.B rb_read(rb, arr, sizeof(arr))
+is invalid, as
+.B rb
+will try to copy (assuming
+.B int
+is 4 bytes long) 4 * 8 = 32 elements, and that would cause out of boud write
+to
+.IR arr.
+Instead you need to pass number of elements array contains.
+Now, you could hold define somewhere with array size - or you could use this
+macro to calculate it like that
+.B rb_read(rb, arr, rb_array_size(arr))
+and that will return correct number of elements - 8.
+.PP
+This works only on arrays, don't even think about passing pointer!
+.SH RETURN VALUES
+Number of elements of
+.I a
+array during compile time.
+This cannot fail.
+.SH SEE ALSO
+.PP
+.BR rb_overview (7),
+.BR rb_new (3),
+.BR rb_init (3),
+.BR rb_destroy (3),
+.BR rb_cleanup (3),
+.BR rb_discard (3),
+.BR rb_stop (3),
+.BR rb_stop_signal (3),
+.BR rb_read (3),
+.BR rb_recv (3),
+.BR rb_write (3),
+.BR rb_send (3),
+.BR rb_posix_read (3),
+.BR rb_posix_recv (3),
+.BR rb_posix_write (3),
+.BR rb_posix_send (3),
+.BR rb_clear (3),
+.BR rb_count (3),
+.BR rb_space (3),
+.BR rb_header_size (3),
+.BR rb_version (3)
diff --git a/man/rb_clear.3 b/man/rb_clear.3
index 42c8607..a403a3d 100644
--- a/man/rb_clear.3
+++ b/man/rb_clear.3
@@ -51,4 +51,5 @@ is invalid (null).
.BR rb_count (3),
.BR rb_space (3),
.BR rb_header_size (3),
+.BR rb_array_size (3),
.BR rb_version (3)
diff --git a/man/rb_destroy.3 b/man/rb_destroy.3
index 9fe7a1e..587705c 100644
--- a/man/rb_destroy.3
+++ b/man/rb_destroy.3
@@ -204,4 +204,5 @@ Function not implemented (feature macro not set)
.BR rb_count (3),
.BR rb_space (3),
.BR rb_header_size (3),
+.BR rb_array_size (3),
.BR rb_version (3)
diff --git a/man/rb_discard.3 b/man/rb_discard.3
index 60717f6..6786657 100644
--- a/man/rb_discard.3
+++ b/man/rb_discard.3
@@ -69,4 +69,5 @@ is not a valid object.
.BR rb_count (3),
.BR rb_space (3),
.BR rb_header_size (3),
+.BR rb_array_size (3),
.BR rb_version (3)
diff --git a/man/rb_header_size.3 b/man/rb_header_size.3
index e75c5bd..8e53c59 100644
--- a/man/rb_header_size.3
+++ b/man/rb_header_size.3
@@ -35,4 +35,5 @@ Size of the buffer with data is not included.
.BR rb_clear (3),
.BR rb_count (3),
.BR rb_space (3),
+.BR rb_array_size (3),
.BR rb_version (3)
diff --git a/man/rb_new.3 b/man/rb_new.3
index 19caa12..cd2478e 100644
--- a/man/rb_new.3
+++ b/man/rb_new.3
@@ -176,4 +176,5 @@ object can effectively hold
.BR rb_count (3),
.BR rb_space (3),
.BR rb_header_size (3),
+.BR rb_array_size (3),
.BR rb_version (3)
diff --git a/man/rb_overview.7 b/man/rb_overview.7
index 4625f8c..390bc1b 100644
--- a/man/rb_overview.7
+++ b/man/rb_overview.7
@@ -204,4 +204,5 @@ Please note, that example is missing error handling for simplicity.
.BR rb_count (3),
.BR rb_space (3),
.BR rb_header_size (3),
+.BR rb_array_size (3),
.BR rb_version (3)
diff --git a/man/rb_read.3 b/man/rb_read.3
index fd4e4f7..4fe2797 100644
--- a/man/rb_read.3
+++ b/man/rb_read.3
@@ -344,4 +344,5 @@ Function is not implemented (was not compiled in).
.BR rb_count (3),
.BR rb_space (3),
.BR rb_header_size (3),
+.BR rb_array_size (3),
.BR rb_version (3)
diff --git a/man/rb_space.3 b/man/rb_space.3
index 32598ae..ba781f9 100644
--- a/man/rb_space.3
+++ b/man/rb_space.3
@@ -48,4 +48,5 @@ object is invalid.
.BR rb_posix_send (3),
.BR rb_clear (3),
.BR rb_header_size (3),
+.BR rb_array_size (3),
.BR rb_version (3)
diff --git a/man/rb_version.3 b/man/rb_version.3
index 317f358..2cfb125 100644
--- a/man/rb_version.3
+++ b/man/rb_version.3
@@ -53,4 +53,5 @@ Function returns pointer to statically allocated string with full version, like
.BR rb_clear (3),
.BR rb_count (3),
.BR rb_header_size (3),
+.BR rb_array_size (3),
.BR rb_space (3),
diff --git a/man/rb_write.3 b/man/rb_write.3
index 0912a04..57ea8e7 100644
--- a/man/rb_write.3
+++ b/man/rb_write.3
@@ -334,4 +334,5 @@ Function is not implemented (was not compiled in).
.BR rb_count (3),
.BR rb_space (3),
.BR rb_header_size (3),
+.BR rb_array_size (3),
.BR rb_version (3)
diff --git a/rb.h b/rb.h
index e077d2d..f519d5a 100644
--- a/rb.h
+++ b/rb.h
@@ -24,6 +24,8 @@
#define O_MULTITHREAD 0x10000000L
struct rb;
+#define rb_array_size(a) (sizeof(a)/sizeof(*(a)))
+
struct rb *rb_new(size_t count, size_t object_size, unsigned long flags);
struct rb *rb_init(size_t count, size_t object_size, unsigned long flags,
void *mem);
diff --git a/tests.c b/tests.c
index d6c0604..cd376d3 100644
--- a/tests.c
+++ b/tests.c
@@ -146,7 +146,7 @@ static void *multi_producer(void *arg)
index = multi_index++;
pthread_mutex_unlock(&multi_mutex);
- if (index >= (sizeof(data)/sizeof(*data)))
+ if (index >= rb_array_size(data))
{
return NULL;
}
@@ -189,7 +189,7 @@ static void *multi_pipe_producer(void *arg)
index = multi_index++;
pthread_mutex_unlock(&multi_mutex);
- if (index >= (sizeof(data)/sizeof(*data)))
+ if (index >= rb_array_size(data))
{
close(fd);
return NULL;
@@ -221,7 +221,7 @@ static void *multi_consumer(void *arg)
return NULL;
}
- overflow = index >= sizeof(data)/sizeof(*data);
+ overflow = index >= rb_array_size(data);
if (overflow)
{
@@ -291,7 +291,7 @@ static void *multi_pipe_consumer(void *arg)
index = multi_index_count;
pthread_mutex_unlock(&multi_mutex_count);
- if (index >= sizeof(data)/sizeof(*data))
+ if (index >= rb_array_size(data))
{
/*
* we have consumed all there was to consume
@@ -312,7 +312,7 @@ static void *multi_pipe_consumer(void *arg)
return NULL;
}
- overflow = index >= sizeof(data)/sizeof(*data);
+ overflow = index >= rb_array_size(data);
if (overflow)
{
@@ -359,7 +359,7 @@ static void multi_producers_consumers(void)
/*
* wait until all indexes has been consumed
*/
- while (count < sizeof(data)/sizeof(*data))
+ while (count < rb_array_size(data))
{
int buf[16];
@@ -389,7 +389,7 @@ static void multi_producers_consumers(void)
rb_destroy(tdata.rb);
- for (r = 0, i = 0; i < sizeof(data)/sizeof(*data); ++i)
+ for (r = 0, i = 0; i < rb_array_size(data); ++i)
{
r += (data[i] != 1);
}
@@ -491,7 +491,7 @@ static void multi_file_consumer_producer(void)
}
}
- while(count < sizeof(data)/sizeof(*data))
+ while(count < rb_array_size(data))
{
int buf[16];
@@ -527,7 +527,7 @@ static void multi_file_consumer_producer(void)
rb_destroy(rb);
- for (r = 0, i = 0; i < sizeof(data)/sizeof(*data); ++i)
+ for (r = 0, i = 0; i < rb_array_size(data); ++i)
{
r += data[i] != 1;
}
@@ -727,7 +727,7 @@ static void peeking(void)
int d[8];
int i;
- for (i = 0; i != sizeof(v)/sizeof(*v); ++i)
+ for (i = 0; i != rb_array_size(v); ++i)
{
d[i] = i;
}