diff options
Diffstat (limited to 'src/libmdsserver')
| -rw-r--r-- | src/libmdsserver/fd-table.c | 6 | ||||
| -rw-r--r-- | src/libmdsserver/macros.h | 27 | ||||
| -rw-r--r-- | src/libmdsserver/mds-message.c | 21 | ||||
| -rw-r--r-- | src/libmdsserver/util.c | 28 | ||||
| -rw-r--r-- | src/libmdsserver/util.h | 12 | 
5 files changed, 72 insertions, 22 deletions
| diff --git a/src/libmdsserver/fd-table.c b/src/libmdsserver/fd-table.c index eb681f9..0852eb6 100644 --- a/src/libmdsserver/fd-table.c +++ b/src/libmdsserver/fd-table.c @@ -161,8 +161,7 @@ size_t fd_table_put(fd_table_t* restrict this, int key, size_t value)      {        size_t* old_values = this->values;        size_t old_bitcap, new_bitcap; -      this->values = realloc(this->values, (this->capacity << 1) * sizeof(size_t)); -      if (this->values == NULL) +      if (xrealloc(this->values, this->capacity << 1, size_t))  	{  	  this->values = old_values;  	  return 0; @@ -177,8 +176,7 @@ size_t fd_table_put(fd_table_t* restrict this, int key, size_t value)        if (new_bitcap > old_bitcap)  	{  	  uint64_t* old_used = this->used; -	  this->used = realloc(this->used, new_bitcap * sizeof(size_t)); -	  if (this->used == NULL) +	  if (xrealloc(this->used, new_bitcap, size_t))  	    {  	      this->used = old_used;  	      this->capacity >>= 1; diff --git a/src/libmdsserver/macros.h b/src/libmdsserver/macros.h index 1623950..95f705f 100644 --- a/src/libmdsserver/macros.h +++ b/src/libmdsserver/macros.h @@ -77,6 +77,21 @@    instructions                           \    errno = pthread_mutex_unlock(&(mutex)) +/** + * Wrapper for `pthread_mutex_lock` and `pthread_mutex_unlock` with an embedded if-statement + *  + * @param  mutex:pthread_mutex_t  The mutex + * @parma  condition              The condition to test + * @param  instructions           The instructions to run while the mutex is locked + */ +#define with_mutex_if(mutex, condition, instructions)	\ +  errno = pthread_mutex_lock(&(mutex));			\ +  if (condition)					\ +    {							\ +      instructions					\ +    }							\ +  errno = pthread_mutex_unlock(&(mutex)) +  /**   * Return the maximum value of two values @@ -296,6 +311,18 @@  /** + * `remalloc` wrapper that returns whether the allocation was not successful + *   + * @param   var       The variable to which to assign the reallocation + * @param   elements  The number of elements to allocate + * @param   type      The data type of the elements for which to create an allocation + * @return  :int      Evaluates to true if an only if the allocation failed + */ +#define xrealloc(var, elements, type)  \ +  ((var = realloc(var, (elements) * sizeof(type))) == NULL) + + +/**   * Go to the label `pfail` if a condition is met   *    * @param  CONDITION  The condition diff --git a/src/libmdsserver/mds-message.c b/src/libmdsserver/mds-message.c index f6dca1a..bc71ca6 100644 --- a/src/libmdsserver/mds-message.c +++ b/src/libmdsserver/mds-message.c @@ -163,22 +163,13 @@ int mds_message_read(mds_message_t* restrict this, int fd)  	       that it does not need to be reallocated again and again. */  	    if (header_commit_buffer == 0)  	      { +		char** old_headers = this->headers;  		header_commit_buffer = 8; -		if (this->header_count == 0) +		n = this->header_count + header_commit_buffer; +		if (xrealloc(this->headers, n, char*))  		  { -		    if (xmalloc(this->headers, header_commit_buffer, char*)) -		      return -1; -		  } -		else -		  { -		    char** old_headers = this->headers; -		    n = this->header_count + header_commit_buffer; -		    this->headers = realloc(this->headers, n * sizeof(char*)); -		    if (this->headers == NULL) -		      { -			this->headers = old_headers; +		    this->headers = old_headers;  			return -1; -		      }  		  }  	      } @@ -261,9 +252,7 @@ int mds_message_read(mds_message_t* restrict this, int fd)        if (n < 128)  	{  	  char* old_buffer = this->buffer; -	  this->buffer_size <<= 1; -	  this->buffer = realloc(this->buffer, this->buffer_size * sizeof(char)); -	  if (this->buffer == NULL) +	  if (xrealloc(this->buffer, this->buffer_size <<= 1, char))  	    {  	      this->buffer = old_buffer;  	      this->buffer_size >>= 1; diff --git a/src/libmdsserver/util.c b/src/libmdsserver/util.c index 3b6cde1..41c6ff1 100644 --- a/src/libmdsserver/util.c +++ b/src/libmdsserver/util.c @@ -126,6 +126,7 @@ size_t send_message(int socket, const char* message, size_t length)    size_t sent = 0;    ssize_t just_sent; +  errno = 0;    while (length > 0)      if ((just_sent = send(socket, message + sent, min(block_size, length), MSG_NOSIGNAL)) < 0)        { @@ -222,8 +223,7 @@ char* full_read(int fd)        if (state_buf_size == state_buf_ptr)  	{  	  char* old_buf = state_buf; -	  state_buf = realloc(state_buf, (state_buf_size <<= 1) * sizeof(char)); -	  if (state_buf == NULL) +	  if (xrealloc(state_buf, state_buf_size <<= 1, char))  	    {  	      free(old_buf);  	      return NULL; @@ -245,3 +245,27 @@ char* full_read(int fd)    return state_buf;  } + +/** + * Check whether a string begins with a specific string, + * where neither of the strings are necessarily NUL-terminated + *  + * @param   haystack    The string that should start with the other string + * @param   needle      The string the first string should start with + * @param   haystack_n  The length of `haystack` + * @param   needle_n    The length of `needle` + * @return              Whether the `haystack` begins with `needle` + */ +int startswith_n(const char* haystack, const char* needle, size_t haystack_n, size_t needle_n) +{ +  size_t i; +  if (haystack_n < needle_n) +    return 0; +   +  for (i = 0; i < needle_n; i++) +    if (haystack[i] != needle[i]) +      return 0; +   +  return 1; +} + diff --git a/src/libmdsserver/util.h b/src/libmdsserver/util.h index da2cb9f..1b533bb 100644 --- a/src/libmdsserver/util.h +++ b/src/libmdsserver/util.h @@ -92,6 +92,18 @@ int full_write(int fd, const char* buffer, size_t length);   */  char* full_read(int fd); +/** + * Check whether a string begins with a specific string, + * where neither of the strings are necessarily NUL-terminated + *  + * @param   haystack    The string that should start with the other string + * @param   needle      The string the first string should start with + * @param   haystack_n  The length of `haystack` + * @param   needle_n    The length of `needle` + * @return              Whether the `haystack` begins with `needle` + */ +int startswith_n(const char* haystack, const char* needle, size_t haystack_n, size_t needle_n) __attribute__((pure)); +  #endif | 
