3 * \brief Contains common functions/macros/defines used throughout
4 * the octopus client library.
8 * Copyright (c) 2011, ETH Zurich.
11 * This file is distributed under the terms in the attached LICENSE file.
12 * If you do not find this file, copies can be found by writing to:
13 * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
16 #ifndef OCTOPUS_COMMON_H_
17 #define OCTOPUS_COMMON_H_
24 #include <barrelfish/event_mutex.h>
26 #include <octopus/definitions.h>
28 // TODO saw some TODOs in event_mutex_* so this will probably not work
29 // as expected right now
30 // TODO: Barrier/Lock code calls this a number of times because
31 // we currently use the existing getset API there
32 #define OCT_LOCK_BINDING(cl) event_mutex_threaded_lock(&(cl)->b->mutex)
33 #define OCT_UNLOCK_BINDING(cl) event_mutex_unlock(&(cl)->b->mutex)
36 // Make sure args come right after query
37 #define FORMAT_QUERY(query, args, buf) do { \
39 va_start(args, query); \
40 err = allocate_string(query, args, &length, &buf); \
42 if(err_is_fail(err)) { \
45 va_start(args, query); \
46 size_t bytes_written = vsnprintf(buf, length+1, query, args); \
48 assert(bytes_written == length); \
51 static inline errval_t allocate_string(const char *fmt, va_list args,
52 size_t *length, char **buf)
54 *length = vsnprintf(NULL, 0, fmt, args);
56 if (*length >= MAX_QUERY_LENGTH) {
57 return OCT_ERR_QUERY_SIZE;
60 *buf = malloc((*length) + 1); // include \0
62 return LIB_ERR_MALLOC_FAIL;
69 * \brief Thread-safe variant of strtok as provided by POSIX
71 * \see http://linux.die.net/man/3/strtok
73 * \param str String to tokenize.
74 * \param delim Tokenize by delimiter.
75 * \param saveptr Used by strtok_r to store state.
76 * \return Tokenized strings.
78 static inline char* strtok_r(char *s, const char *delim, char **saveptr)
81 s = *saveptr; /* recommence just after last token */
82 s += strspn(s, delim); /* skip leading delimeters */
85 *saveptr = s + strcspn(s, delim); /* remember end of token string */
86 if ((**saveptr) != '\0')
87 *(*(saveptr))++ = '\0'; /* terminate token string */
91 #endif /* OCTOPUS_COMMON_H_ */