summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--raul/DoubleBuffer.hpp2
-rw-r--r--raul/Exception.hpp5
-rw-r--r--raul/Process.hpp2
-rw-r--r--raul/SRMWQueue.hpp8
-rw-r--r--raul/SRSWQueue.hpp2
-rw-r--r--raul/Semaphore.hpp2
-rw-r--r--raul/Socket.hpp10
-rw-r--r--raul/TimeStamp.hpp14
-rw-r--r--raul/URI.hpp6
-rw-r--r--test/maid_test.cpp10
-rw-r--r--test/queue_test.cpp36
-rw-r--r--test/ringbuffer_test.cpp25
-rw-r--r--test/thread_test.cpp6
-rw-r--r--test/uri_test.cpp4
14 files changed, 67 insertions, 65 deletions
diff --git a/raul/DoubleBuffer.hpp b/raul/DoubleBuffer.hpp
index d6d6287..6a1bbd0 100644
--- a/raul/DoubleBuffer.hpp
+++ b/raul/DoubleBuffer.hpp
@@ -82,8 +82,8 @@ private:
LOCK_READ ///< Lock vals[0], Read vals[1]
};
- std::atomic<State> _state;
std::atomic<T*> _read_val;
+ std::atomic<State> _state;
T _vals[2];
};
diff --git a/raul/Exception.hpp b/raul/Exception.hpp
index b73a5a5..78e9f08 100644
--- a/raul/Exception.hpp
+++ b/raul/Exception.hpp
@@ -25,10 +25,11 @@ namespace Raul {
/** An exception (unexpected error). */
class Exception : public std::exception {
public:
- const char* what() const throw() { return _what.c_str(); }
- ~Exception() throw() {}
+ const char* what() const noexcept { return _what.c_str(); }
+
protected:
explicit Exception(const std::string& what) : _what(what) {}
+
private:
const std::string _what;
};
diff --git a/raul/Process.hpp b/raul/Process.hpp
index 7e1c5a0..61fb501 100644
--- a/raul/Process.hpp
+++ b/raul/Process.hpp
@@ -50,7 +50,7 @@ public:
struct rlimit max_fds;
getrlimit(RLIMIT_NOFILE, &max_fds);
for (rlim_t fd = 3; fd < max_fds.rlim_cur; ++fd) {
- close(fd);
+ close(static_cast<int>(fd));
}
// Fork child
diff --git a/raul/SRMWQueue.hpp b/raul/SRMWQueue.hpp
index b4d43ee..769364c 100644
--- a/raul/SRMWQueue.hpp
+++ b/raul/SRMWQueue.hpp
@@ -85,8 +85,8 @@ template<typename T>
SRMWQueue<T>::SRMWQueue(size_t size)
: _front(0)
, _back(0)
- , _write_space(size)
- , _size(size+1)
+ , _write_space(int(size))
+ , _size(unsigned(size) + 1)
, _objects((T*)calloc(_size, sizeof(T)))
, _valid(new std::atomic<bool>[_size])
{
@@ -145,7 +145,7 @@ SRMWQueue<T>::push(const T& elem)
} else {
// Note: _size must be a power of 2 for this to not explode when _back overflows
- const unsigned write_index = _back++ % _size;
+ const unsigned write_index = unsigned(_back++) % _size;
assert(!_valid[write_index]);
_objects[write_index] = elem;
@@ -182,8 +182,6 @@ SRMWQueue<T>::front() const
*
* It is a fatal error to call pop() if the queue is empty.
* Read thread only.
- *
- * @return true if queue is now empty, otherwise false.
*/
template <typename T>
inline void
diff --git a/raul/SRSWQueue.hpp b/raul/SRSWQueue.hpp
index 8b910da..5e2cf4e 100644
--- a/raul/SRSWQueue.hpp
+++ b/raul/SRSWQueue.hpp
@@ -131,8 +131,6 @@ SRSWQueue<T>::push(const T& elem)
/** Pop an item off the front of the queue - realtime-safe, not thread-safe.
*
* It is a fatal error to call pop() when the queue is empty.
- *
- * @returns the element popped.
*/
template <typename T>
inline void
diff --git a/raul/Semaphore.hpp b/raul/Semaphore.hpp
index 31739d2..ee79ba7 100644
--- a/raul/Semaphore.hpp
+++ b/raul/Semaphore.hpp
@@ -102,7 +102,7 @@ private:
inline bool
Semaphore::init(unsigned initial)
{
- if (semaphore_create(mach_task_self(), &_sem, SYNC_POLICY_FIFO, initial)) {
+ if (semaphore_create(mach_task_self(), &_sem, SYNC_POLICY_FIFO, int(initial))) {
return false;
}
return true;
diff --git a/raul/Socket.hpp b/raul/Socket.hpp
index 6246f02..a569511 100644
--- a/raul/Socket.hpp
+++ b/raul/Socket.hpp
@@ -95,10 +95,10 @@ public:
private:
bool set_addr(const Raul::URI& uri);
- Type _type;
Raul::URI _uri;
struct sockaddr* _addr;
socklen_t _addr_len;
+ Type _type;
int _sock;
};
@@ -108,10 +108,10 @@ private:
inline
Socket::Socket(Type t)
- : _type(t)
- , _uri(t == Type::UNIX ? "unix:" : "tcp:")
+ : _uri(t == Type::UNIX ? "unix:" : "tcp:")
, _addr(NULL)
, _addr_len(0)
+ , _type(t)
, _sock(-1)
{
switch (t) {
@@ -130,10 +130,10 @@ Socket::Socket(Type t,
struct sockaddr* addr,
socklen_t addr_len,
int fd)
- : _type(t)
- , _uri(uri)
+ : _uri(uri)
, _addr(addr)
, _addr_len(addr_len)
+ , _type(t)
, _sock(fd)
{
}
diff --git a/raul/TimeStamp.hpp b/raul/TimeStamp.hpp
index 179b6fe..ef0df5d 100644
--- a/raul/TimeStamp.hpp
+++ b/raul/TimeStamp.hpp
@@ -90,10 +90,13 @@ public:
dec = std::min(double(std::numeric_limits<uint32_t>::max()), dec);
double integral;
const double fractional = modf(dec, &integral);
- _ticks = integral;
- _subticks = fractional * unit.ppt();
+ _ticks = static_cast<uint32_t>(integral);
+ _subticks = static_cast<uint32_t>(fractional * unit.ppt());
}
+ inline TimeStamp(const TimeStamp&) = default;
+ TimeStamp& operator=(const TimeStamp&) = default;
+
inline TimeUnit unit() const { return _unit; }
inline uint32_t ticks() const { return _ticks; }
inline uint32_t subticks() const { return _subticks; }
@@ -106,13 +109,6 @@ public:
return _ticks == 0 && _subticks == 0;
}
- inline TimeStamp& operator=(const TimeStamp& rhs) {
- _ticks = rhs._ticks;
- _subticks = rhs._subticks;
- _unit = rhs._unit;
- return *this;
- }
-
inline TimeStamp& operator=(uint32_t ticks) {
_ticks = ticks;
_subticks = 0;
diff --git a/raul/URI.hpp b/raul/URI.hpp
index 84b5497..7bf2763 100644
--- a/raul/URI.hpp
+++ b/raul/URI.hpp
@@ -66,9 +66,9 @@ public:
* Note this is faster than constructing a URI from another URI's string
* since validation is unnecessary.
*/
- URI(const URI& uri)
- : std::basic_string<char>(uri)
- {}
+ URI(const URI& uri) = default;
+
+ URI& operator=(const URI& uri) = default;
/** Return true iff `c` is a valid URI start character. */
static inline bool is_valid_start_char(char c) {
diff --git a/test/maid_test.cpp b/test/maid_test.cpp
index 8bf4f45..e21ba04 100644
--- a/test/maid_test.cpp
+++ b/test/maid_test.cpp
@@ -33,10 +33,10 @@ static std::atomic<size_t> n_finished_threads(0);
class Junk : public Maid::Disposable {
public:
- explicit Junk(int v) : val(v) { ++n_junk; }
+ explicit Junk(size_t v) : val(v) { ++n_junk; }
~Junk() { --n_junk; }
- int val;
+ size_t val;
};
static void
@@ -57,9 +57,9 @@ test()
// Check basic single-threaded correctness
{
assert(n_junk == 0);
- Maid::managed_ptr<Junk> a = maid.make_managed<Junk>(1);
+ Maid::managed_ptr<Junk> a = maid.make_managed<Junk>(1U);
assert(n_junk == 1);
- Maid::managed_ptr<Junk> b = maid.make_managed<Junk>(2);
+ Maid::managed_ptr<Junk> b = maid.make_managed<Junk>(2U);
assert(n_junk == 2);
}
@@ -111,7 +111,7 @@ test()
assert(n_junk == 0);
// Allocate a new object, then let it and the Maid go out of scope
- Maid::managed_ptr<Junk> c = maid.make_managed<Junk>(5);
+ Maid::managed_ptr<Junk> c = maid.make_managed<Junk>(5U);
assert(n_junk == 1);
}
diff --git a/test/queue_test.cpp b/test/queue_test.cpp
index 55b8c76..8f9ab61 100644
--- a/test/queue_test.cpp
+++ b/test/queue_test.cpp
@@ -31,10 +31,12 @@
using namespace std;
using namespace Raul;
-static const unsigned NUM_DATA = 10;
-static const unsigned QUEUE_SIZE = 128;
-static const unsigned NUM_WRITERS = 2;
-static const unsigned PUSHES_PER_ITERATION = 3;
+namespace {
+
+const unsigned NUM_DATA = 10;
+const unsigned QUEUE_SIZE = 128;
+const unsigned NUM_WRITERS = 2;
+const unsigned PUSHES_PER_ITERATION = 3;
// Data to read/write using actions pumped through the queue
struct Record {
@@ -52,7 +54,7 @@ struct WriteAction {
inline void read() const {
++(data[index].read_count);
- };
+ }
unsigned index;
};
@@ -60,12 +62,12 @@ struct WriteAction {
// The victim
SRMWQueue<WriteAction> queue(QUEUE_SIZE);
-static void
+void
test_write(bool* exit_flag)
{
while (!*exit_flag) {
for (unsigned j=0; j < PUSHES_PER_ITERATION; ++j) {
- unsigned i = rand() % NUM_DATA;
+ unsigned i = unsigned(rand()) % NUM_DATA;
if (queue.push(WriteAction(i))) {
++(data[i].write_count);
//cout << "WRITE " << i << "\r\n";
@@ -76,29 +78,31 @@ test_write(bool* exit_flag)
}
cout << "Writer exiting." << endl;
-};
+}
// Returns 0 if all read count/write count pairs are equal,
// otherwise how far off total count was
-static unsigned
+unsigned
data_is_sane()
{
unsigned ret = 0;
for (unsigned i = 0; i < NUM_DATA; ++i) {
- unsigned diff = abs(data[i].read_count.load() - data[i].write_count.load());
- ret += diff;
+ ret += unsigned(abs(data[i].read_count.load() -
+ data[i].write_count.load()));
}
return ret;
}
+} // namespace
+
int
main()
{
size_t total_processed = 0;
cout << "Testing size" << endl;
- for (size_t i=0; i < queue.capacity(); ++i) {
+ for (unsigned i = 0; i < queue.capacity(); ++i) {
queue.push(i);
if (i == queue.capacity()-1) {
if (!queue.full()) {
@@ -185,15 +189,11 @@ main()
cout << "(Counter did NOT have to wrap)" << endl;
const unsigned diff = data_is_sane();
-
- if (diff == 0) {
- return EXIT_SUCCESS;
- } else {
+ if (diff != 0) {
cout << "FAILED BY " << diff << endl;
- return EXIT_FAILURE;
}
- return 0;
+ return diff == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
#if 0
diff --git a/test/ringbuffer_test.cpp b/test/ringbuffer_test.cpp
index 7537c85..ee80e6a 100644
--- a/test/ringbuffer_test.cpp
+++ b/test/ringbuffer_test.cpp
@@ -19,6 +19,7 @@
#include <cstdlib>
#include <cstring>
#include <iostream>
+#include <string>
#include "raul/RingBuffer.hpp"
@@ -27,11 +28,13 @@ using namespace Raul;
#define MSG_SIZE 20
+namespace {
+
Raul::RingBuffer* ring = 0;
size_t n_writes = 0;
bool ring_error = false;
-static int
+int
gen_msg(int* msg, int start)
{
for (int i = 0; i < MSG_SIZE; ++i) {
@@ -41,7 +44,7 @@ gen_msg(int* msg, int start)
return start;
}
-static int
+int
cmp_msg(int* msg1, int* msg2)
{
for (int i = 0; i < MSG_SIZE; ++i) {
@@ -54,7 +57,7 @@ cmp_msg(int* msg1, int* msg2)
return 1;
}
-static void*
+void*
reader(void* arg)
{
printf("Reader starting\n");
@@ -85,7 +88,7 @@ reader(void* arg)
return NULL;
}
-static void*
+void*
writer(void* arg)
{
printf("Writer starting\n");
@@ -108,6 +111,8 @@ writer(void* arg)
return NULL;
}
+} // namespace
+
int
main(int argc, char** argv)
{
@@ -116,21 +121,21 @@ main(int argc, char** argv)
return 1;
}
- int size = 1024;
+ size_t size = 1024;
if (argc > 1) {
- size = atoi(argv[1]);
+ size = std::stoul(argv[1]);
}
n_writes = size * 1024;
if (argc > 2) {
- n_writes = atoi(argv[2]);
+ n_writes = std::stoul(argv[2]);
}
- printf("Testing %zu writes of %d ints to a %d int ring...\n",
+ printf("Testing %zu writes of %u ints to a %zu int ring...\n",
n_writes, MSG_SIZE, size);
- ring = new Raul::RingBuffer(size);
- if (ring->capacity() < (unsigned)size - 1) {
+ ring = new Raul::RingBuffer(uint32_t(size));
+ if (ring->capacity() < size - 1) {
fprintf(stderr, "Ring capacity is smaller than expected\n");
return 1;
}
diff --git a/test/thread_test.cpp b/test/thread_test.cpp
index 3d1fc8b..9012b4c 100644
--- a/test/thread_test.cpp
+++ b/test/thread_test.cpp
@@ -23,10 +23,12 @@
using namespace std;
using namespace Raul;
+namespace {
+
thread_local int var(0);
std::atomic<int> n_errors(0);
-static void
+void
wait_for_sem(Semaphore* sem)
{
var = 41;
@@ -40,6 +42,8 @@ wait_for_sem(Semaphore* sem)
}
}
+} // namespace
+
int
main()
{
diff --git a/test/uri_test.cpp b/test/uri_test.cpp
index 19debab..5e1a648 100644
--- a/test/uri_test.cpp
+++ b/test/uri_test.cpp
@@ -46,7 +46,7 @@ main()
bool valid = true;
try {
URI uri("not/a/uri");
- } catch (const URI::BadURI& e) {
+ } catch (const URI::BadURI&) {
valid = false;
}
CHECK(!valid);
@@ -54,7 +54,7 @@ main()
valid = true;
try {
URI uri(std::string("/this/is/a/path"));
- } catch (const URI::BadURI& e) {
+ } catch (const URI::BadURI&) {
valid = false;
}
CHECK(!valid);