mirror of
https://github.com/genodelabs/genode.git
synced 2025-01-19 11:16:57 +00:00
Add 'String<SIZE>' 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.
This commit is contained in:
parent
339193a887
commit
5befab7f3d
@ -25,26 +25,6 @@ namespace Genode { namespace Trace {
|
||||
struct Traced_by_other_session : Exception { };
|
||||
struct Subject_not_traced : Exception { };
|
||||
|
||||
template <size_t MAX>
|
||||
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;
|
||||
|
||||
|
@ -447,6 +447,36 @@ namespace Genode {
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
/**
|
||||
* Buffer that contains a null-terminated string
|
||||
*
|
||||
* \param SIZE buffer size
|
||||
*/
|
||||
template <size_t SIZE>
|
||||
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_ */
|
||||
|
@ -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);
|
||||
|
||||
|
@ -38,7 +38,7 @@ class Genode::Trace::Root : public Genode::Root_component<Session_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)
|
||||
|
Loading…
Reference in New Issue
Block a user