Use restrict on pointers where it makes sense. (requires C99 support,

which we already assume)
This commit is contained in:
Nils O. Selåsdal
2014-05-25 23:07:08 +02:00
parent fe7c152631
commit b3b1179c5a
12 changed files with 37 additions and 37 deletions
+8 -8
View File
@@ -47,7 +47,7 @@ void uc_dstr_destroy(struct DStr *str);
* *
* @return 0 if success, != 0 if allocation fails * @return 0 if success, != 0 if allocation fails
*/ */
int uc_dstr_copy(struct DStr *dest, const struct DStr *src); int uc_dstr_copy(struct DStr *restrict dest, const struct DStr *restrict src);
/** /**
* @return the available/unused tail bytes in the string. * @return the available/unused tail bytes in the string.
@@ -95,7 +95,7 @@ char *uc_dstr_put(struct DStr *str, size_t len);
* @param is_x ctype.h compatible function to use as a filter. * @param is_x ctype.h compatible function to use as a filter.
* @return number of chars appended * @return number of chars appended
*/ */
size_t uc_dstr_put_str_filter(struct DStr *str, const char *s, int (*is_x)(int c)); size_t uc_dstr_put_str_filter(struct DStr *str, const char *restrict s, int (*is_x)(int c));
/* Append a string of the give size. /* Append a string of the give size.
* (The nul terminator is not appended) * (The nul terminator is not appended)
@@ -104,14 +104,14 @@ size_t uc_dstr_put_str_filter(struct DStr *str, const char *s, int (*is_x)(int c
* @param len bytes to append (not including the nul terminator) * @param len bytes to append (not including the nul terminator)
* @return 0 on success (!= 0 if an allocation fail) * @return 0 on success (!= 0 if an allocation fail)
*/ */
int uc_dstr_put_str_sz(struct DStr *str, const char *s, size_t len); int uc_dstr_put_str_sz(struct DStr *str, const char *restrict s, size_t len);
/* Append a string. /* Append a string.
* (The nul terminator is not appended) * (The nul terminator is not appended)
* *
* @return 0 on success (!= 0 if an allocation fail) * @return 0 on success (!= 0 if an allocation fail)
*/ */
int uc_dstr_put_str(struct DStr *str, const char *s); int uc_dstr_put_str(struct DStr *str, const char *restrict s);
/* Append a single char. /* Append a single char.
* *
@@ -122,7 +122,7 @@ int uc_dstr_put_char(struct DStr *str, char c);
/* Make the DStr take ownership of the given dynamically allocated string. /* Make the DStr take ownership of the given dynamically allocated string.
* Any existing string in the DStr is freed. * Any existing string in the DStr is freed.
*/ */
void uc_dstr_own(struct DStr *str, char *s); void uc_dstr_own(struct DStr *str, char *restrict s);
/** Release the managed string. /** Release the managed string.
* The caller is now responsible for managing/free'ing the returned string. * The caller is now responsible for managing/free'ing the returned string.
@@ -133,7 +133,7 @@ void uc_dstr_own(struct DStr *str, char *s);
char *uc_dstr_steal(struct DStr *str); char *uc_dstr_steal(struct DStr *str);
/** Swap the A and B DStr */ /** Swap the A and B DStr */
void uc_str_swap(struct DStr *a, struct DStr *b); void uc_str_swap(struct DStr *restrict a, struct DStr *restrict b);
/** Replace all occurences in the DStr /** Replace all occurences in the DStr
* (This may be used to replace any embedded nul bytes too) * (This may be used to replace any embedded nul bytes too)
@@ -149,10 +149,10 @@ size_t uc_dstr_replace(struct DStr *str, char f, char r);
void uc_dstr_trim(struct DStr *str); void uc_dstr_trim(struct DStr *str);
/** Append formatted string (just as sprintf)*/ /** Append formatted string (just as sprintf)*/
int uc_dstr_sprintf(struct DStr *str, const char *fmt, ...) __attribute__((format(printf,2,3))); int uc_dstr_sprintf(struct DStr *str, const char *restrict fmt, ...) __attribute__((format(printf,2,3)));
/** va_list variant */ /** va_list variant */
int uc_dstr_vsprintf(struct DStr *str, const char *fmt, va_list ap_)__attribute__((format(printf,2,0))); int uc_dstr_vsprintf(struct DStr *str, const char *restrict fmt, va_list ap_)__attribute__((format(printf,2,0)));
#ifdef __cplusplus #ifdef __cplusplus
} }
+1 -1
View File
@@ -125,7 +125,7 @@ struct MBuf *uc_mbuf_copy_data(struct MBuf *mbuf);
* @param headroom the headroom in the buffer * @param headroom the headroom in the buffer
* @param flags The appropriate UC_MBUF_FLAGS * @param flags The appropriate UC_MBUF_FLAGS
*/ */
void uc_mbuf_init(struct MBuf *mbuf, uint8_t *buf, uint32_t len, uint32_t headroom, uint32_t flags); void uc_mbuf_init(struct MBuf *mbuf, uint8_t *restrict buf, uint32_t len, uint32_t headroom, uint32_t flags);
/** Zero out all the data in this mbuf, including the headroom /** Zero out all the data in this mbuf, including the headroom
* *
+6 -6
View File
@@ -18,7 +18,7 @@ extern "C" {
* @param out where to store result. Must hold at least len * 2 + 1 * @param out where to store result. Must hold at least len * 2 + 1
* @return returns one past the end of the converted data * @return returns one past the end of the converted data
*/ */
char* uc_hex_encode(const uint8_t *in, size_t len, char *out); char* uc_hex_encode(const uint8_t *restrict in, size_t len, char *restrict out);
/** Decode hex. Will stop at the first non-convertible hex-digit /** Decode hex. Will stop at the first non-convertible hex-digit
* spaces(' ','\t,'\r','\n') in input are skipped * spaces(' ','\t,'\r','\n') in input are skipped
@@ -32,11 +32,11 @@ char* uc_hex_encode(const uint8_t *in, size_t len, char *out);
* @param out where to store result. Must hold at least maxlen/2 + 1 * @param out where to store result. Must hold at least maxlen/2 + 1
* @return returns one past the end of the converted data * @return returns one past the end of the converted data
*/ */
uint8_t* uc_hex_decode(const char *in, size_t maxlen,uint8_t *out); uint8_t* uc_hex_decode(const char *restrict in, size_t maxlen,uint8_t *restrict out);
/** As uc_hex_encode , but inserts the @delim string between each converted /** As uc_hex_encode , but inserts the @delim string between each converted
* byte (each 2 hex chars) * byte (each 2 hex chars)
*/ */
char* uc_hex_encode_delim(const uint8_t *in, size_t inlen, char *out, int outlen, const char *delim); char* uc_hex_encode_delim(const uint8_t *restrict in, size_t inlen, char *restrict out, int outlen, const char *restrict delim);
/** /**
* Convert the string to lowercase, calls tolower() on each char. * Convert the string to lowercase, calls tolower() on each char.
@@ -67,7 +67,7 @@ void uc_str_toupper(char *s);
* if the number of ASCII digits is odd. This would usually be 0x0 or 0xf. * if the number of ASCII digits is odd. This would usually be 0x0 or 0xf.
* @return the number of bytes written to bcdout * @return the number of bytes written to bcdout
*/ */
size_t uc_ascii2bcd(const char *ascii, unsigned char *bcdout, unsigned char filler); size_t uc_ascii2bcd(const char *restrict ascii, unsigned char *restrict bcdout, unsigned char filler);
/** Convert the BCD digits to ascii. /** Convert the BCD digits to ascii.
* The resulting ascii string is 0 terminated. * The resulting ascii string is 0 terminated.
@@ -77,7 +77,7 @@ size_t uc_ascii2bcd(const char *ascii, unsigned char *bcdout, unsigned char fill
* @param asciiout buffer to place ASCII in, must be UC_ASCII_LEN(bcdlen)+1 big * @param asciiout buffer to place ASCII in, must be UC_ASCII_LEN(bcdlen)+1 big
* @return the number of characters written to bcdout (this will always be UC_ASCII_LEN(bcdlen) * @return the number of characters written to bcdout (this will always be UC_ASCII_LEN(bcdlen)
*/ */
size_t uc_bcd2ascii(const unsigned char *bcd, size_t bcdlen, char *asciiout); size_t uc_bcd2ascii(const unsigned char *restrict bcd, size_t bcdlen, char *restrict asciiout);
/** uc_sprintb for converting the values into ascii bit representation /** uc_sprintb for converting the values into ascii bit representation
* e.g. 0x3 -> "00000011". The result must be able to hold the max number * e.g. 0x3 -> "00000011". The result must be able to hold the max number
@@ -111,7 +111,7 @@ char * uc_human_bytesz_bin(uint64_t bytes, char *result, size_t result_len);
* is normally the size of the dst buffer) * is normally the size of the dst buffer)
* return strlen(src) which may be larger than the no. of bytes copied. * return strlen(src) which may be larger than the no. of bytes copied.
*/ */
size_t uc_strlcpy(char *dst, const char *src, size_t max); size_t uc_strlcpy(char *restrict dst, const char *restrict src, size_t max);
#ifdef __cplusplus #ifdef __cplusplus
} }
+3 -3
View File
@@ -52,7 +52,7 @@ int uc_strv_check_expand(struct UCStrv *strv);
* @param str String to append, the string will be copied. * @param str String to append, the string will be copied.
* @return 0 on success, otherwise failure * @return 0 on success, otherwise failure
**/ **/
int uc_strv_append(struct UCStrv *strv, const char *str); int uc_strv_append(struct UCStrv *strv, const char *restrict str);
/** /**
* Append a string to the strvec, nocopy variang. * Append a string to the strvec, nocopy variang.
@@ -63,7 +63,7 @@ int uc_strv_append(struct UCStrv *strv, const char *str);
* @param str the string to append * @param str the string to append
* @return 0 on success, otherwise failure * @return 0 on success, otherwise failure
**/ **/
int uc_strv_append_nocopy(struct UCStrv *strv, char *str); int uc_strv_append_nocopy(struct UCStrv *strv, char *restrict str);
/** /**
* Append a NULL pointer to the UCStrv, (cnt is not increased, * Append a NULL pointer to the UCStrv, (cnt is not increased,
@@ -98,7 +98,7 @@ void uc_strv_destroy(struct UCStrv *strv);
* 0 to just append the same pointer in b to a * 0 to just append the same pointer in b to a
* @return 0 on success, != 0 if appending/allocation fails * @return 0 on success, != 0 if appending/allocation fails
*/ */
int uc_strv_append_all(struct UCStrv *a, struct UCStrv *b, int copy); int uc_strv_append_all(struct UCStrv *restrict a, struct UCStrv *restrict b, int copy);
#ifdef __cplusplus #ifdef __cplusplus
} }
+1 -1
View File
@@ -48,6 +48,6 @@ const char *uc_val_2_str_bs(unsigned int val,
* @param array array to search * @param array array to search
* @return the string associated with @val or null * @return the string associated with @val or null
*/ */
const char *uc_str_2_str(const char *val, const struct UCStrStr *array); const char *uc_str_2_str(const char *restrict val, const struct UCStrStr *array);
#endif #endif
+2 -2
View File
@@ -4,7 +4,7 @@
#include "ucore/string.h" #include "ucore/string.h"
size_t size_t
uc_ascii2bcd(const char *ascii, unsigned char *bcdout, unsigned char filler) uc_ascii2bcd(const char *restrict ascii, unsigned char *restrict bcdout, unsigned char filler)
{ {
unsigned char *bcdp = bcdout; unsigned char *bcdp = bcdout;
int shift = 0; int shift = 0;
@@ -36,7 +36,7 @@ uc_ascii2bcd(const char *ascii, unsigned char *bcdout, unsigned char filler)
static const char hex[] = "0123456789ABCDEF"; static const char hex[] = "0123456789ABCDEF";
size_t size_t
uc_bcd2ascii(const unsigned char *bcd, size_t bcdlen, char *asciiout) uc_bcd2ascii(const unsigned char *restrict bcd, size_t bcdlen, char *restrict asciiout)
{ {
size_t i; size_t i;
size_t idx = 0; size_t idx = 0;
+7 -7
View File
@@ -106,7 +106,7 @@ char *uc_dstr_put(struct DStr *str, size_t len)
return start; return start;
} }
size_t uc_dstr_put_str_filter(struct DStr *str, const char *s, int (*is_x)(int c)) size_t uc_dstr_put_str_filter(struct DStr *str, const char *restrict s, int (*is_x)(int c))
{ {
size_t len = strlen(s); size_t len = strlen(s);
size_t i; size_t i;
@@ -128,7 +128,7 @@ size_t uc_dstr_put_str_filter(struct DStr *str, const char *s, int (*is_x)(int c
return idx; return idx;
} }
int uc_dstr_put_str_sz(struct DStr *str, const char *s, size_t len) int uc_dstr_put_str_sz(struct DStr *str, const char *restrict s, size_t len)
{ {
char *start = uc_dstr_put(str, len); char *start = uc_dstr_put(str, len);
@@ -140,7 +140,7 @@ int uc_dstr_put_str_sz(struct DStr *str, const char *s, size_t len)
return 0; return 0;
} }
int uc_dstr_put_str(struct DStr *str, const char *s) int uc_dstr_put_str(struct DStr *str, const char *restrict s)
{ {
size_t len = strlen(s); size_t len = strlen(s);
@@ -159,7 +159,7 @@ int uc_dstr_put_char(struct DStr *str, char c)
return 0; return 0;
} }
void uc_dstr_own(struct DStr *str, char *s) void uc_dstr_own(struct DStr *str, char *restrict s)
{ {
size_t len = strlen(s); size_t len = strlen(s);
@@ -181,7 +181,7 @@ char *uc_dstr_steal(struct DStr *str)
return s; return s;
} }
void uc_str_swap(struct DStr *a, struct DStr *b) void uc_str_swap(struct DStr *restrict a, struct DStr *restrict b)
{ {
struct DStr tmp; struct DStr tmp;
@@ -236,7 +236,7 @@ void uc_dstr_trim(struct DStr *str)
} }
} }
int uc_dstr_sprintf(struct DStr *str, const char *fmt, ...) int uc_dstr_sprintf(struct DStr *str, const char *restrict fmt, ...)
{ {
va_list ap; va_list ap;
int rc; int rc;
@@ -248,7 +248,7 @@ int uc_dstr_sprintf(struct DStr *str, const char *fmt, ...)
return rc; return rc;
} }
int uc_dstr_vsprintf(struct DStr *str, const char *fmt, va_list ap_) int uc_dstr_vsprintf(struct DStr *str, const char *restrict fmt, va_list ap_)
{ {
va_list ap; va_list ap;
int rc; int rc;
+3 -3
View File
@@ -8,7 +8,7 @@
static const char hx_chars[] = "0123456789ABCDEF" ; static const char hx_chars[] = "0123456789ABCDEF" ;
char* char*
uc_hex_encode(const uint8_t *in, size_t len, char *out) uc_hex_encode(const uint8_t *restrict in, size_t len, char *restrict out)
{ {
unsigned int i, t; unsigned int i, t;
@@ -28,7 +28,7 @@ uc_hex_encode(const uint8_t *in, size_t len, char *out)
} }
char* char*
uc_hex_encode_delim(const uint8_t *in, size_t inlen, char *out, int outlen, const char *delim) uc_hex_encode_delim(const uint8_t *restrict in, size_t inlen, char *restrict out, int outlen, const char *restrict delim)
{ {
size_t i; size_t i;
char *outp = out; char *outp = out;
@@ -76,7 +76,7 @@ static inline int char_to_bin(char c)
} }
uint8_t* uint8_t*
uc_hex_decode(const char *in, size_t maxlen,uint8_t *out) uc_hex_decode(const char *restrict in, size_t maxlen,uint8_t *restrict out)
{ {
size_t i, t; size_t i, t;
+1 -1
View File
@@ -40,7 +40,7 @@ uint8_t *uc_mbuf_pull(struct MBuf *mbuf, uint32_t size)
return data; return data;
} }
void uc_mbuf_init(struct MBuf *mbuf, uint8_t *buf, uint32_t len, uint32_t headroom, uint32_t flags) void uc_mbuf_init(struct MBuf *mbuf, uint8_t *restrict buf, uint32_t len, uint32_t headroom, uint32_t flags)
{ {
mbuf->bufstart = buf; mbuf->bufstart = buf;
mbuf->p1 = mbuf->p2 = mbuf->p3 = mbuf->p4 = NULL; mbuf->p1 = mbuf->p2 = mbuf->p3 = mbuf->p4 = NULL;
+1 -1
View File
@@ -1,6 +1,6 @@
#include <string.h> #include <string.h>
size_t uc_strlcpy(char *dst, const char *src, size_t max) size_t uc_strlcpy(char *restrict dst, const char *restrict src, size_t max)
{ {
size_t len = strlen(src); size_t len = strlen(src);
+3 -3
View File
@@ -36,7 +36,7 @@ int uc_strv_check_expand(struct UCStrv *strv)
return rc; return rc;
} }
int uc_strv_append(struct UCStrv *strv, const char *str) int uc_strv_append(struct UCStrv *strv, const char *restrict str)
{ {
int rc; int rc;
char *dup; char *dup;
@@ -57,7 +57,7 @@ int uc_strv_append(struct UCStrv *strv, const char *str)
return 0; return 0;
} }
int uc_strv_append_nocopy(struct UCStrv *strv, char *str) int uc_strv_append_nocopy(struct UCStrv *strv, char *restrict str)
{ {
int rc; int rc;
rc = uc_strv_check_expand(strv); rc = uc_strv_check_expand(strv);
@@ -105,7 +105,7 @@ void uc_strv_destroy(struct UCStrv *strv)
strv->strings = NULL; strv->strings = NULL;
} }
int uc_strv_append_all(struct UCStrv *a, struct UCStrv *b, int copy) int uc_strv_append_all(struct UCStrv *restrict a, struct UCStrv *restrict b, int copy)
{ {
size_t i; size_t i;
int rc; int rc;
+1 -1
View File
@@ -47,7 +47,7 @@ const char *uc_val_2_str_bs(unsigned int val,
return found; return found;
} }
const char *uc_str_2_str(const char *val, const struct UCStrStr *array) const char *uc_str_2_str(const char *restrict val, const struct UCStrStr *array)
{ {
const char *found = NULL; const char *found = NULL;