Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@ set(CMAKE_C_STANDARD 99)
include(CheckIPOSupported)
check_ipo_supported()

# Extra warnings, treat warnings as error
if(CMAKE_C_COMPILER_ID MATCHES "Clang|GNU")
SET(TH_WARNINGS -Wall -Wextra -Wpedantic -Werror)
message(STATUS "Using Clang/GNU compiler, enabling extra warnings: ${TH_WARNINGS}")
add_compile_options(${TH_WARNINGS})
endif()

# Run gperf
find_program(GPERF gperf)
if (GPERF)
Expand Down Expand Up @@ -92,6 +85,13 @@ add_library(tiny_http
)
add_library(tiny_http::tiny_http ALIAS tiny_http)

# Extra warnings, treat warnings as error
if(CMAKE_C_COMPILER_ID MATCHES "Clang|GNU")
SET(TH_WOPTIONS -Wall -Wextra -Wpedantic -Werror -Wstrict-prototypes -Wmissing-prototypes -Wshadow -Wconversion)
message(STATUS "Using Clang/GNU compiler, enabling extra warnings: ${TH_WOPTIONS}")
target_compile_options(tiny_http PRIVATE ${TH_WOPTIONS})
endif()

# Link time optimization
check_ipo_supported(RESULT ipo_supported)
if (CMAKE_BUILD_TYPE STREQUAL "Release")
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ handler(void* userp, const th_request* req, th_response* resp)
return TH_ERR_OK;
}

int main()
int main(void)
{
th_server* server;
th_server_create(&server, NULL);
Expand Down
14 changes: 7 additions & 7 deletions include/th.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,13 @@ typedef struct th_bind_opt {
* @brief Date struct corresponding to the Date header in HTTP. Always in GMT.
*/
typedef struct th_date {
unsigned year : 16;
unsigned month : 8;
unsigned day : 8;
unsigned weekday : 8;
unsigned hour : 8;
unsigned minute : 8;
unsigned second : 8;
unsigned int year : 16;
unsigned int month : 8;
unsigned int day : 8;
unsigned int weekday : 8;
unsigned int hour : 8;
unsigned int minute : 8;
unsigned int second : 8;
} th_date;

typedef struct th_duration {
Expand Down
6 changes: 3 additions & 3 deletions src/th_align.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

#include <stdint.h>

#define TH_ALIGNOF(type) offsetof(struct { char c; type member; }, member)
#define TH_ALIGNAS(align, ptr) ((void*)(((uintptr_t)(ptr) + ((align) - 1)) & ~((align) - 1)))
#define TH_ALIGNUP(n, align) (((n) + (align) - 1) & ~((align) - 1))
#define TH_ALIGNOF(type) ((size_t)&(((struct { char c; type member; }*)0)->member))
#define TH_ALIGNAS(align, ptr) ((void*)(((uintptr_t)(ptr) + ((uintptr_t)(align) - 1)) & ~((uintptr_t)(align) - 1)))
#define TH_ALIGNUP(n, align) (((n) + (size_t)(align) - 1) & ~((size_t)(align) - 1))
#define TH_ALIGNDOWN(n, align) ((n) & ~((align) - 1))

typedef long double th_max_align;
Expand Down
8 changes: 4 additions & 4 deletions src/th_allocator.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ th_default_allocator_set(th_allocator* allocator)

/* th_arena_allocator implementation begin */

TH_PRIVATE(void*)
TH_LOCAL(void*)
th_arena_allocator_alloc(void* self, size_t size)
{
th_arena_allocator* allocator = self;
Expand All @@ -73,7 +73,7 @@ th_arena_allocator_alloc(void* self, size_t size)
return ptr;
}

TH_PRIVATE(void*)
TH_LOCAL(void*)
th_arena_allocator_realloc(void* self, void* ptr, size_t size)
{
th_arena_allocator* allocator = self;
Expand Down Expand Up @@ -103,7 +103,7 @@ th_arena_allocator_realloc(void* self, void* ptr, size_t size)
return newp;
}

static void
TH_LOCAL(void)
th_arena_allocator_free(void* self, void* ptr)
{
th_arena_allocator* allocator = self;
Expand All @@ -124,7 +124,7 @@ th_arena_allocator_init_with_alignment(th_arena_allocator* allocator, void* buf,
allocator->base.realloc = th_arena_allocator_realloc;
allocator->base.free = th_arena_allocator_free;
allocator->allocator = fallback;
allocator->alignment = alignment;
allocator->alignment = (uint16_t)alignment;
void* aligned = TH_ALIGNAS(alignment, buf);
allocator->size = size - (size_t)((uint8_t*)aligned - (uint8_t*)buf);
allocator->buf = aligned;
Expand Down
3 changes: 2 additions & 1 deletion src/th_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define TH_ALLOCATOR_H

#include <stddef.h>
#include <stdint.h>

#include "th_config.h"
#include "th_list.h"
Expand Down Expand Up @@ -37,7 +38,7 @@ typedef struct th_arena_allocator {
size_t size;
size_t pos;
size_t prev_pos;
unsigned int alignment;
uint16_t alignment;
} th_arena_allocator;

/** th_arena_allocator_init
Expand Down
28 changes: 14 additions & 14 deletions src/th_date.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ th_date_now(void)
struct tm tm = {0};
gmtime_r(&t, &tm);
th_date date = {0};
date.year = tm.tm_year;
date.month = tm.tm_mon;
date.day = tm.tm_mday;
date.weekday = tm.tm_wday;
date.hour = tm.tm_hour;
date.minute = tm.tm_min;
date.second = tm.tm_sec;
date.year = (unsigned int)tm.tm_year & 0xFFFF;
date.month = (unsigned int)tm.tm_mon & 0xFF;
date.day = (unsigned int)tm.tm_mday & 0xFF;
date.weekday = (unsigned int)tm.tm_wday & 0xFF;
date.hour = (unsigned int)tm.tm_hour & 0xFF;
date.minute = (unsigned int)tm.tm_min & 0xFF;
date.second = (unsigned int)tm.tm_sec & 0xFF;
return date;
}

Expand All @@ -60,12 +60,12 @@ th_date_add(th_date date, th_duration d)
t += d.seconds;
gmtime_r(&t, &tm);
th_date new_date = {0};
new_date.year = (unsigned int)tm.tm_year;
new_date.month = (unsigned int)tm.tm_mon;
new_date.day = (unsigned int)tm.tm_mday;
new_date.weekday = (unsigned int)tm.tm_wday;
new_date.hour = (unsigned int)tm.tm_hour;
new_date.minute = (unsigned int)tm.tm_min;
new_date.second = (unsigned int)tm.tm_sec;
new_date.year = (unsigned int)tm.tm_year & 0xFFFF;
new_date.month = (unsigned int)tm.tm_mon & 0xFF;
new_date.day = (unsigned int)tm.tm_mday & 0xFF;
new_date.weekday = (unsigned int)tm.tm_wday & 0xFF;
new_date.hour = (unsigned int)tm.tm_hour & 0xFF;
new_date.minute = (unsigned int)tm.tm_min & 0xFF;
new_date.second = (unsigned int)tm.tm_sec & 0xFF;
return new_date;
}
4 changes: 2 additions & 2 deletions src/th_fcache.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ th_fcache_id_eq(th_fcache_id a, th_fcache_id b)
return a.dir == b.dir && th_string_eq(a.path, b.path);
}

TH_INLINE(uint32_t)
TH_INLINE(size_t)
th_fcache_id_hash(th_fcache_id id)
{
return th_string_hash(id.path) + id.dir->fd;
return th_string_hash(id.path) + (size_t)id.dir->fd;
}

TH_DEFINE_HASHMAP(th_fcache_map, th_fcache_id, th_fcache_entry*, th_fcache_id_hash, th_fcache_id_eq, (th_fcache_id){0})
Expand Down
6 changes: 3 additions & 3 deletions src/th_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ th_file_mmap_mmap_posix(th_file_mmap* view, th_file* file, size_t offset, size_t
{
size_t page_size = (size_t)sysconf(_SC_PAGESIZE);
size_t moffset = TH_ALIGNDOWN(offset, page_size);
void* addr = mmap(NULL, len, PROT_READ, MAP_PRIVATE, file->fd, moffset);
void* addr = mmap(NULL, len, PROT_READ, MAP_PRIVATE, file->fd, (off_t)moffset);
if (addr == MAP_FAILED) {
return TH_ERR_SYSTEM(errno);
}
Expand Down Expand Up @@ -194,7 +194,7 @@ TH_PRIVATE(th_err)
th_file_read(th_file* stream, void* addr, size_t len, size_t offset, size_t* read)
{
#if defined(TH_CONFIG_OS_POSIX)
off_t ret = pread(stream->fd, addr, len, offset);
off_t ret = pread(stream->fd, addr, len, (off_t)offset);
if (ret == -1) {
*read = 0;
return TH_ERR_SYSTEM(errno);
Expand All @@ -216,7 +216,7 @@ TH_PRIVATE(th_err)
th_file_write(th_file* stream, const void* addr, size_t len, size_t offset, size_t* written)
{
#if defined(TH_CONFIG_OS_POSIX)
off_t ret = pwrite(stream->fd, addr, len, offset);
off_t ret = pwrite(stream->fd, addr, len, (off_t)offset);
if (ret == -1) {
*written = 0;
return TH_ERR_SYSTEM(errno);
Expand Down
8 changes: 4 additions & 4 deletions src/th_fmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ th_fmt_uint_to_str(char* buf, size_t len, unsigned int value)

buf[len - 1] = '\0';
size_t i = len - 2;
unsigned v = value;
unsigned int v = value;
while (v > 0 && i > 0) {
buf[i--] = '0' + (v % 10);
buf[i--] = '0' + (char)(v % 10);
v /= 10;
}
return &buf[i + 1];
Expand All @@ -91,9 +91,9 @@ th_fmt_uint_to_str_ex(char* buf, size_t len, unsigned int val, size_t* out_len)

buf[len - 1] = '\0';
size_t i = len - 2;
unsigned v = val;
unsigned int v = val;
while (v > 0 && i > 0) {
buf[i--] = '0' + (v % 10);
buf[i--] = '0' + (char)(v % 10);
v /= 10;
}
*out_len = len - i - 2;
Expand Down
6 changes: 3 additions & 3 deletions src/th_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
* @brief Fowler-Noll-Vo hash function (FNV-1a).
* See https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
*/
TH_INLINE(uint32_t)
TH_INLINE(size_t)
th_hash_bytes(const void* data, size_t len)
{
uint32_t hash = 2166136261u;
size_t hash = 2166136261u;
const uint8_t* bytes = (const uint8_t*)data;
for (size_t i = 0; i < len; ++i) {
hash ^= bytes[i];
Expand All @@ -23,7 +23,7 @@ th_hash_bytes(const void* data, size_t len)
return hash;
}

TH_INLINE(uint32_t)
TH_INLINE(size_t)
th_hash_cstr(const char* str)
{
return th_hash_bytes(str, strlen(str));
Expand Down
Loading