Skip to content

Commit

Permalink
contrib: sync csnippets
Browse files Browse the repository at this point in the history
Signed-off-by: He Xian <hexian000@outlook.com>
  • Loading branch information
hexian000 committed Feb 19, 2025
1 parent c76732d commit 3a661db
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 131 deletions.
34 changes: 19 additions & 15 deletions contrib/csnippets/algo/cityhash.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ static inline uint64_t shift_mix(const uint64_t v)

// Hash 128 input bits down to 64 bits of output.
// This is intended to be a reasonably good hash function.
static inline uint64_t Hash128to64(const uint64_t x[2])
static inline uint64_t Hash128to64(const uint64_t x[restrict 2])
{
// Murmur-inspired hashing.
const uint64_t kMul = 0x9ddfea08eb382d69ULL;
Expand Down Expand Up @@ -113,7 +113,7 @@ static inline uint64_t HashLen16mul(uint64_t u, uint64_t v, uint64_t mul)
return b;
}

static uint64_t HashLen0to16(const unsigned char *s, size_t len)
static uint64_t HashLen0to16(const unsigned char *restrict s, size_t len)
{
if (len >= 8) {
uint64_t mul = k2 + len * 2;
Expand Down Expand Up @@ -142,7 +142,8 @@ static uint64_t HashLen0to16(const unsigned char *s, size_t len)

// This probably works well for 16-byte strings as well, but it may be overkill
// in that case.
static inline uint64_t HashLen17to32(const unsigned char *s, size_t len)
static inline uint64_t
HashLen17to32(const unsigned char *restrict s, size_t len)
{
uint64_t mul = k2 + len * 2;
uint64_t a = read_uint64(s) * k1;
Expand Down Expand Up @@ -181,7 +182,8 @@ static inline void WeakHashLen32WithSeedsStr(
}

// Return an 8-byte hash for 33 to 64 bytes.
static inline uint64_t HashLen33to64(const unsigned char *s, size_t len)
static inline uint64_t
HashLen33to64(const unsigned char *restrict s, size_t len)
{
uint64_t mul = k2 + len * 2;
uint64_t a = read_uint64(s) * k2;
Expand All @@ -203,7 +205,7 @@ static inline uint64_t HashLen33to64(const unsigned char *s, size_t len)
return b + x;
}

static uint64_t CityHash64(const void *ptr, size_t len)
static uint64_t CityHash64(const void *restrict ptr, size_t len)
{
const unsigned char *s = ptr;
if (len <= 32) {
Expand Down Expand Up @@ -248,13 +250,14 @@ static uint64_t CityHash64(const void *ptr, size_t len)
HashLen16(v[1], w[1]) + x);
}

static uint64_t
CityHash64WithSeeds(const void *ptr, size_t len, uint64_t seed0, uint64_t seed1)
static uint64_t CityHash64WithSeeds(
const void *restrict ptr, size_t len, uint64_t seed0, uint64_t seed1)
{
return HashLen16(CityHash64(ptr, len) - seed0, seed1);
}

static uint64_t CityHash64WithSeed(const void *ptr, size_t len, uint64_t seed)
static uint64_t
CityHash64WithSeed(const void *restrict ptr, size_t len, uint64_t seed)
{
return CityHash64WithSeeds(ptr, len, k2, seed);
}
Expand Down Expand Up @@ -297,8 +300,8 @@ static void CityMurmur(
}

static void CityHash128WithSeed(
unsigned char hash[16], const void *ptr, size_t len,
const unsigned char seed[16])
unsigned char hash[restrict 16], const void *restrict ptr, size_t len,
const unsigned char seed[restrict 16])
{
const unsigned char *s = ptr;
if (len < 128) {
Expand Down Expand Up @@ -385,20 +388,21 @@ static void CityHash128(unsigned char hash[16], const void *ptr, size_t len)
}
*/

uint64_t cityhash64_64(const void *ptr, const size_t len, const uint64_t seed)
uint64_t
cityhash64_64(const void *restrict ptr, const size_t len, const uint64_t seed)
{
return CityHash64WithSeed(ptr, len, seed);
}

uint32_t
cityhash64low_32(const void *ptr, const size_t len, const uint32_t seed)
uint32_t cityhash64low_32(
const void *restrict ptr, const size_t len, const uint32_t seed)
{
return (uint32_t)CityHash64WithSeed(ptr, len, seed);
}

void cityhash128_128(
unsigned char hash[16], const void *ptr, const size_t len,
const unsigned char seed[16])
unsigned char hash[restrict 16], const void *restrict ptr,
const size_t len, const unsigned char seed[restrict 16])
{
CityHash128WithSeed(hash, ptr, len, seed);
}
2 changes: 1 addition & 1 deletion contrib/csnippets/net/addr.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <stddef.h>
#include <string.h>

bool splithostport(char *str, char **host, char **port)
bool splithostport(char *str, char **restrict host, char **restrict port)
{
char *service = strrchr(str, ':');
if (service == NULL) {
Expand Down
8 changes: 4 additions & 4 deletions contrib/csnippets/net/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static const struct {
"The gateway did not receive a timely response from the upstream server or application." },
};

static char *skip_whitespace(char *s)
static char *skip_whitespace(char *restrict s)
{
while (*s == ' ' || *s == '\t') {
++s;
Expand Down Expand Up @@ -124,12 +124,12 @@ char *http_parsehdr(char *buf, char **key, char **value)
return next;
}

size_t http_date(char *buf, const size_t buf_size)
size_t http_date(char *restrict buf, const size_t buf_size)
{
/* RFC 7231: Section 7.1.1.1 */
static const char fmt[] = "%a, %d %b %Y %H:%M:%S GMT";
const time_t now = time(NULL);
const struct tm *gmt = gmtime(&now);
const struct tm *restrict gmt = gmtime(&now);
return strftime(buf, buf_size, fmt, gmt);
}

Expand All @@ -143,7 +143,7 @@ const char *http_status(const uint_least16_t code)
return NULL;
}

int http_error(char *buf, size_t buf_size, const uint_least16_t code)
int http_error(char *restrict buf, size_t buf_size, const uint_least16_t code)
{
const char *name = NULL, *info = NULL;
for (size_t i = 0; i < ARRAY_SIZE(http_resp); i++) {
Expand Down
20 changes: 11 additions & 9 deletions contrib/csnippets/net/url.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <stdint.h>
#include <string.h>

static void hex(char *p, uint_fast8_t c)
static void hex(char *restrict p, const uint_fast8_t c)
{
static const char hex[] = "0123456789ABCDEF";
p[1] = hex[c & UINT8_C(0xF)];
Expand Down Expand Up @@ -150,15 +150,15 @@ size_t url_escape_path(char *buf, size_t buf_size, const char *path)
return escape(buf, buf_size, path, SIZE_MAX, "-_.~$&+,/:;=@", false);
}

size_t url_escape_query(char *buf, size_t buf_size, const char *restrict query)
size_t url_escape_query(char *buf, size_t buf_size, const char *query)
{
const size_t cap = buf_size;
for (;;) {
const char *restrict next = strchr(query, '&');
const char *next = strchr(query, '&');
if (next == NULL) {
next = query + strlen(query);
}
const char *restrict eq = memchr(query, '=', next - query);
const char *eq = memchr(query, '=', next - query);
if (eq == NULL) {
return 0;
}
Expand Down Expand Up @@ -270,9 +270,9 @@ static bool unescape(char *str, const bool space)
return true;
}

static inline char *strlower(char *s)
static inline char *strlower(char *restrict s)
{
for (unsigned char *restrict p = (unsigned char *)s; *p != '\0'; ++p) {
for (unsigned char *p = (unsigned char *)s; *p != '\0'; ++p) {
*p = tolower(*p);
}
return s;
Expand Down Expand Up @@ -372,7 +372,7 @@ bool url_parse(char *raw, struct url *restrict url)
return true;
}

bool url_path_segment(char **path, char **segment)
bool url_path_segment(char **restrict path, char **restrict segment)
{
char *s = *path;
while (*s == '/') {
Expand All @@ -391,7 +391,8 @@ bool url_path_segment(char **path, char **segment)
return true;
}

bool url_query_component(char **query, struct url_query_component *comp)
bool url_query_component(
char **restrict query, struct url_query_component *restrict comp)
{
char *s = *query;
char *next = strchr(s, '&');
Expand Down Expand Up @@ -420,7 +421,8 @@ bool url_query_component(char **query, struct url_query_component *comp)
return true;
}

bool url_unescape_userinfo(char *raw, char **username, char **password)
bool url_unescape_userinfo(
char *raw, char **restrict username, char **restrict password)
{
const char valid_chars[] = "-._:~!$&\'()*+,;=%@'";
char *colon = NULL;
Expand Down
53 changes: 29 additions & 24 deletions contrib/csnippets/utils/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@
#include <stdlib.h>
#include <string.h>

int buf_vappendf(struct buffer *restrict buf, const char *format, va_list args)
int buf_vappendf(
struct buffer *restrict buf, const char *restrict format, va_list args)
{
char *b = (char *)(buf->data + buf->len);
const size_t maxlen = buf->cap - buf->len;
if (maxlen == 0) {
return 0;
}
const int ret = vsnprintf(b, maxlen, format, args);
char *restrict s = (char *)(buf->data + buf->len);
const int ret = vsnprintf(s, maxlen, format, args);
if (ret > 0) {
buf->len += MIN((size_t)ret, maxlen - 1);
}
return ret;
}

int buf_appendf(struct buffer *restrict buf, const char *format, ...)
int buf_appendf(struct buffer *restrict buf, const char *restrict format, ...)
{
va_list args;
va_start(args, format);
Expand All @@ -34,7 +35,7 @@ int buf_appendf(struct buffer *restrict buf, const char *format, ...)
}

struct vbuffer *
vbuf_grow(struct vbuffer *restrict vbuf, const size_t want, const size_t maxcap)
vbuf_grow(struct vbuffer *vbuf, const size_t want, const size_t maxcap)
{
size_t cap = 0, len = 0;
if (vbuf != NULL) {
Expand Down Expand Up @@ -63,8 +64,7 @@ vbuf_grow(struct vbuffer *restrict vbuf, const size_t want, const size_t maxcap)
cap += grow;
} while (cap < want);

struct vbuffer *restrict newbuf =
realloc(vbuf, sizeof(struct vbuffer) + cap);
struct vbuffer *newbuf = realloc(vbuf, sizeof(struct vbuffer) + cap);
if (newbuf == NULL) {
/* retry with minimal required capacity */
cap = want;
Expand All @@ -79,7 +79,7 @@ vbuf_grow(struct vbuffer *restrict vbuf, const size_t want, const size_t maxcap)
}

struct vbuffer *
vbuf_append(struct vbuffer *restrict vbuf, const void *data, size_t n)
vbuf_append(struct vbuffer *restrict vbuf, const void *restrict data, size_t n)
{
if (n == 0) {
return vbuf;
Expand All @@ -95,28 +95,33 @@ vbuf_append(struct vbuffer *restrict vbuf, const void *data, size_t n)
if (n > vbuf->cap - vbuf->len) {
n = vbuf->cap - vbuf->len;
}
(void)memcpy(vbuf->data + vbuf->len, data, n);
unsigned char *restrict b = vbuf->data + vbuf->len;
(void)memcpy(b, data, n);
vbuf->len += n;
/* null-byte is reserved by vbuf_alloc() */
vbuf->data[vbuf->len] = '\0';
b[n] = '\0';
return vbuf;
}

struct vbuffer *
vbuf_vappendf(struct vbuffer *restrict vbuf, const char *format, va_list args)
struct vbuffer *vbuf_vappendf(
struct vbuffer *restrict vbuf, const char *restrict format,
va_list args)
{
if (vbuf->cap == vbuf->len) {
/* allocation failure occurred, skip */
return vbuf;
}
char *b = (char *)(vbuf->data + vbuf->len);
size_t maxlen = vbuf->cap - vbuf->len;

va_list args0;
va_copy(args0, args);
/* null-byte is reserved by vbuf_alloc() */
int ret = vsnprintf(b, maxlen + 1, format, args0);
va_end(args0);
int ret;
{
char *restrict s = (char *)(vbuf->data + vbuf->len);
const size_t maxlen = vbuf->cap - vbuf->len;
va_list args0;
va_copy(args0, args);
ret = vsnprintf(s, maxlen + 1, format, args0);
va_end(args0);
}
if (ret <= 0) {
return vbuf;
}
Expand All @@ -129,9 +134,9 @@ vbuf_vappendf(struct vbuffer *restrict vbuf, const char *format, va_list args)
}
vbuf = vbuf_grow(vbuf, want, SIZE_MAX);
/* when failed, append as much as possible */
maxlen = vbuf->cap - vbuf->len;
b = (char *)(vbuf->data + vbuf->len);
ret = vsnprintf(b, maxlen + 1, format, args);
char *restrict s = (char *)(vbuf->data + vbuf->len);
const size_t maxlen = vbuf->cap - vbuf->len;
ret = vsnprintf(s, maxlen + 1, format, args);
if (ret > 0) {
if ((size_t)ret < maxlen) {
vbuf->len += (size_t)ret;
Expand All @@ -143,11 +148,11 @@ vbuf_vappendf(struct vbuffer *restrict vbuf, const char *format, va_list args)
}

struct vbuffer *
vbuf_appendf(struct vbuffer *restrict vbuf, const char *format, ...)
vbuf_appendf(struct vbuffer *restrict vbuf, const char *restrict format, ...)
{
va_list args;
va_start(args, format);
struct vbuffer *ret = vbuf_vappendf(vbuf, format, args);
vbuf = vbuf_vappendf(vbuf, format, args);
va_end(args);
return ret;
return vbuf;
}
11 changes: 5 additions & 6 deletions contrib/csnippets/utils/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ struct buffer {

/** @internal */
static inline size_t
buf_append(struct buffer *restrict buf, const void *data, size_t n)
buf_append(struct buffer *restrict buf, const void *restrict data, size_t n)
{
n = MIN(n, buf->cap - buf->len);
if (n == 0) {
return 0;
}
unsigned char *b = buf->data + buf->len;
(void)memcpy(b, data, n);
void *restrict dest = buf->data + buf->len;
(void)memcpy(dest, data, n);
buf->len += n;
return n;
}
Expand All @@ -60,16 +60,15 @@ struct vbuffer {
};

/** @internal */
static inline struct vbuffer *
vbuf_alloc(struct vbuffer *restrict vbuf, const size_t cap)
static inline struct vbuffer *vbuf_alloc(struct vbuffer *vbuf, const size_t cap)
{
if (cap == 0) {
free(vbuf);
return NULL;
}
const size_t len = (vbuf != NULL) ? vbuf->len : 0;
/* reserve 1 byte for null terminator */
struct vbuffer *restrict newbuf =
struct vbuffer *newbuf =
realloc(vbuf, sizeof(struct vbuffer) + cap + 1);
if (newbuf == NULL) {
return vbuf;
Expand Down
Loading

0 comments on commit 3a661db

Please sign in to comment.