From 5befab7f3dcb4ea6765932250716af6774b74df7 Mon Sep 17 00:00:00 2001 From: Norman Feske Date: Thu, 19 Sep 2013 20:35:45 +0200 Subject: [PATCH] Add 'String' buffer type to 'util/string.h' The new 'String' buffer type is meant to replace the manually created character buffers that are scattered throughout Genode. It plainly holds a null-terminated string to be stored as a member variable (e.g., a session label) or passed as RPC argument. It is not intended to become a string API. --- base/include/base/trace/types.h | 20 ----------------- base/include/util/string.h | 30 ++++++++++++++++++++++++++ base/src/core/cpu_session_component.cc | 2 +- base/src/core/include/trace/root.h | 2 +- 4 files changed, 32 insertions(+), 22 deletions(-) diff --git a/base/include/base/trace/types.h b/base/include/base/trace/types.h index 670a105e8..bc4db2139 100644 --- a/base/include/base/trace/types.h +++ b/base/include/base/trace/types.h @@ -25,26 +25,6 @@ namespace Genode { namespace Trace { struct Traced_by_other_session : Exception { }; struct Subject_not_traced : Exception { }; - template - struct String - { - enum { MAX_SIZE = MAX }; - char buf[MAX]; - size_t length; - - String() : length(0) { } - - String(char const *str) : length(min(strlen(str) + 1, MAX)) - { - strncpy(buf, str, length); - } - - bool valid() const { - return (length < MAX) && (length > 0) && (buf[length - 1] == '\0'); } - - char const *string() const { return valid() ? buf : ""; } - }; - typedef String<160> Session_label; typedef String<64> Thread_name; diff --git a/base/include/util/string.h b/base/include/util/string.h index f47f7dc23..76eda970f 100644 --- a/base/include/util/string.h +++ b/base/include/util/string.h @@ -447,6 +447,36 @@ namespace Genode { return i; } + + /** + * Buffer that contains a null-terminated string + * + * \param SIZE buffer size + */ + template + class String + { + private: + + char _buf[SIZE]; + size_t _length; + + public: + + constexpr static size_t size() { return SIZE; } + + String() : _length(0) { } + + String(char const *str) : _length(min(strlen(str) + 1, SIZE)) + { + strncpy(_buf, str, _length); + } + + bool valid() const { + return (_length <= SIZE) && (_buf[_length - 1] == '\0'); } + + char const *string() const { return valid() ? _buf : ""; } + }; } #endif /* _INCLUDE__UTIL__STRING_H_ */ diff --git a/base/src/core/cpu_session_component.cc b/base/src/core/cpu_session_component.cc index a854e20f9..6592eb167 100644 --- a/base/src/core/cpu_session_component.cc +++ b/base/src/core/cpu_session_component.cc @@ -301,7 +301,7 @@ Cpu_session_component::Cpu_session_component(Rpc_entrypoint *thread_ep, _trace_sources(trace_sources) { /* remember session label */ - char buf[Session_label::MAX_SIZE]; + char buf[Session_label::size()]; Arg_string::find_arg(args, "label").string(buf, sizeof(buf), ""); _label = Session_label(buf); diff --git a/base/src/core/include/trace/root.h b/base/src/core/include/trace/root.h index c3a69d052..0b7c78742 100644 --- a/base/src/core/include/trace/root.h +++ b/base/src/core/include/trace/root.h @@ -38,7 +38,7 @@ class Genode::Trace::Root : public Genode::Root_component size_t arg_buffer_size = Arg_string::find_arg(args, "arg_buffer_size").ulong_value(0); unsigned parent_levels = Arg_string::find_arg(args, "parent_levels").ulong_value(0); - char label[Trace::Session_label::MAX_SIZE]; + char label[Trace::Session_label::size()]; Arg_string::find_arg(args, "label").string(label, sizeof(label), ""); if (arg_buffer_size > ram_quota)