Skip to content

Commit

Permalink
Merge branch 'main' into cbor-main
Browse files Browse the repository at this point in the history
  • Loading branch information
TingDaoK authored Jun 19, 2024
2 parents 7210cf2 + 4fccc79 commit 3610479
Show file tree
Hide file tree
Showing 25 changed files with 160 additions and 117 deletions.
11 changes: 4 additions & 7 deletions .github/workflows/clang-format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ on: [push]
jobs:
clang-format:

runs-on: ubuntu-20.04 # latest
runs-on: ubuntu-24.04 # latest

steps:
- name: Checkout Sources
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: clang-format lint
uses: DoozyX/clang-format-lint-action@v0.3.1
with:
# List of extensions to check
extensions: c,h,inl
source: 'source include tests verification'
run: |
./format-check.py
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ if (USE_CPU_EXTENSIONS)
)
endif()
elseif (AWS_ARCH_ARM64 OR AWS_ARCH_ARM32)
if (WINDOWS)
if (WIN32)
file(GLOB AWS_COMMON_ARCH_SRC
"source/arch/arm/windows/*.c"
)
Expand Down
47 changes: 47 additions & 0 deletions format-check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python3
import argparse
import os
from pathlib import Path
import re
from subprocess import list2cmdline, run
from tempfile import NamedTemporaryFile

CLANG_FORMAT_VERSION = '18.1.6'

INCLUDE_REGEX = re.compile(
r'^(include|source|tests|verification)/.*\.(c|h|inl)$')
EXCLUDE_REGEX = re.compile(r'^$')

arg_parser = argparse.ArgumentParser(description="Check with clang-format")
arg_parser.add_argument('-i', '--inplace-edit', action='store_true',
help="Edit files inplace")
args = arg_parser.parse_args()

os.chdir(Path(__file__).parent)

# create file containing list of all files to format
filepaths_file = NamedTemporaryFile(delete=False)
for dirpath, dirnames, filenames in os.walk('.'):
for filename in filenames:
# our regexes expect filepath to use forward slash
filepath = Path(dirpath, filename).as_posix()
if not INCLUDE_REGEX.match(filepath):
continue
if EXCLUDE_REGEX.match(filepath):
continue

filepaths_file.write(f"{filepath}\n".encode())
filepaths_file.close()

# use pipx to run clang-format from PyPI
# this is a simple way to run the same clang-format version regardless of OS
cmd = ['pipx', 'run', f'clang-format=={CLANG_FORMAT_VERSION}',
f'--files={filepaths_file.name}']
if args.inplace_edit:
cmd += ['-i']
else:
cmd += ['--Werror', '--dry-run']

print(f"{Path.cwd()}$ {list2cmdline(cmd)}")
if run(cmd).returncode:
exit(1)
24 changes: 0 additions & 24 deletions format-check.sh

This file was deleted.

6 changes: 2 additions & 4 deletions include/aws/common/atomics.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,12 @@ enum aws_memory_order {
/**
* Statically initializes an aws_atomic_var to a given size_t value.
*/
#define AWS_ATOMIC_INIT_INT(x) \
{ .value = (void *)(uintptr_t)(x) }
#define AWS_ATOMIC_INIT_INT(x) {.value = (void *)(uintptr_t)(x)}

/**
* Statically initializes an aws_atomic_var to a given void * value.
*/
#define AWS_ATOMIC_INIT_PTR(x) \
{ .value = (void *)(x) }
#define AWS_ATOMIC_INIT_PTR(x) {.value = (void *)(x)}

AWS_EXTERN_C_BEGIN

Expand Down
2 changes: 1 addition & 1 deletion include/aws/common/byte_buf.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ struct aws_byte_cursor {
* Helper Macro for initializing a byte cursor from a string literal
*/
#define AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(literal) \
{ .ptr = (uint8_t *)(const char *)(literal), .len = sizeof(literal) - 1 }
{.ptr = (uint8_t *)(const char *)(literal), .len = sizeof(literal) - 1}

/**
* Signature for function argument to trim APIs
Expand Down
6 changes: 2 additions & 4 deletions include/aws/common/condition_variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@ struct aws_condition_variable {
* CONDITION_VARIABLE_INIT.
*/
#ifdef _WIN32
# define AWS_CONDITION_VARIABLE_INIT \
{ .condition_handle = NULL, .initialized = true }
# define AWS_CONDITION_VARIABLE_INIT {.condition_handle = NULL, .initialized = true}
#else
# define AWS_CONDITION_VARIABLE_INIT \
{ .condition_handle = PTHREAD_COND_INITIALIZER, .initialized = true }
# define AWS_CONDITION_VARIABLE_INIT {.condition_handle = PTHREAD_COND_INITIALIZER, .initialized = true}
#endif

AWS_EXTERN_C_BEGIN
Expand Down
7 changes: 5 additions & 2 deletions include/aws/common/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ AWS_PUSH_SANE_WARNING_LEVEL
/* Each library gets space for 2^^10 error entries */
#define AWS_ERROR_ENUM_STRIDE_BITS 10
#define AWS_ERROR_ENUM_STRIDE (1U << AWS_ERROR_ENUM_STRIDE_BITS)
#define AWS_ERROR_ENUM_BEGIN_RANGE(x) ((x)*AWS_ERROR_ENUM_STRIDE)
#define AWS_ERROR_ENUM_BEGIN_RANGE(x) ((x) * AWS_ERROR_ENUM_STRIDE)
#define AWS_ERROR_ENUM_END_RANGE(x) (((x) + 1) * AWS_ERROR_ENUM_STRIDE - 1)

struct aws_error_info {
Expand All @@ -38,7 +38,10 @@ struct aws_error_info_list {

#define AWS_DEFINE_ERROR_INFO(C, ES, LN) \
{ \
.literal_name = #C, .error_code = (C), .error_str = (ES), .lib_name = (LN), \
.literal_name = #C, \
.error_code = (C), \
.error_str = (ES), \
.lib_name = (LN), \
.formatted_name = LN ": " #C ", " ES, \
}

Expand Down
4 changes: 2 additions & 2 deletions include/aws/common/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ enum {
AWS_LOG_SUBJECT_STRIDE_BITS = 10,
};
#define AWS_LOG_SUBJECT_STRIDE (1U << AWS_LOG_SUBJECT_STRIDE_BITS)
#define AWS_LOG_SUBJECT_BEGIN_RANGE(x) ((x)*AWS_LOG_SUBJECT_STRIDE)
#define AWS_LOG_SUBJECT_BEGIN_RANGE(x) ((x) * AWS_LOG_SUBJECT_STRIDE)
#define AWS_LOG_SUBJECT_END_RANGE(x) (((x) + 1) * AWS_LOG_SUBJECT_STRIDE - 1)

struct aws_log_subject_info {
Expand All @@ -72,7 +72,7 @@ struct aws_log_subject_info {
};

#define DEFINE_LOG_SUBJECT_INFO(id, name, desc) \
{ .subject_id = (id), .subject_name = (name), .subject_description = (desc) }
{.subject_id = (id), .subject_name = (name), .subject_description = (desc)}

struct aws_log_subject_info_list {
struct aws_log_subject_info *subject_list;
Expand Down
2 changes: 1 addition & 1 deletion include/aws/common/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,6 @@ enum { AWS_CACHE_LINE = 64 };
* this will get you back to the pointer of the object. member is the name of
* the instance of struct aws_linked_list_node in your struct.
*/
#define AWS_CONTAINER_OF(ptr, type, member) ((type *)((uint8_t *)(ptr)-offsetof(type, member)))
#define AWS_CONTAINER_OF(ptr, type, member) ((type *)((uint8_t *)(ptr) - offsetof(type, member)))

#endif /* AWS_COMMON_MACROS_H */
6 changes: 2 additions & 4 deletions include/aws/common/mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ struct aws_mutex {
};

#ifdef _WIN32
# define AWS_MUTEX_INIT \
{ .mutex_handle = NULL, .initialized = true }
# define AWS_MUTEX_INIT {.mutex_handle = NULL, .initialized = true}
#else
# define AWS_MUTEX_INIT \
{ .mutex_handle = PTHREAD_MUTEX_INITIALIZER, .initialized = true }
# define AWS_MUTEX_INIT {.mutex_handle = PTHREAD_MUTEX_INITIALIZER, .initialized = true}
#endif

AWS_EXTERN_C_BEGIN
Expand Down
6 changes: 2 additions & 4 deletions include/aws/common/rw_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ struct aws_rw_lock {
};

#ifdef _WIN32
# define AWS_RW_LOCK_INIT \
{ .lock_handle = NULL }
# define AWS_RW_LOCK_INIT {.lock_handle = NULL}
#else
# define AWS_RW_LOCK_INIT \
{ .lock_handle = PTHREAD_RWLOCK_INITIALIZER }
# define AWS_RW_LOCK_INIT {.lock_handle = PTHREAD_RWLOCK_INITIALIZER}
#endif

AWS_EXTERN_C_BEGIN
Expand Down
2 changes: 1 addition & 1 deletion include/aws/common/statistics.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ enum {
};

#define AWS_CRT_STATISTICS_CATEGORY_STRIDE (1U << AWS_CRT_STATISTICS_CATEGORY_STRIDE_BITS)
#define AWS_CRT_STATISTICS_CATEGORY_BEGIN_RANGE(x) ((x)*AWS_CRT_STATISTICS_CATEGORY_STRIDE)
#define AWS_CRT_STATISTICS_CATEGORY_BEGIN_RANGE(x) ((x) * AWS_CRT_STATISTICS_CATEGORY_STRIDE)
#define AWS_CRT_STATISTICS_CATEGORY_END_RANGE(x) (((x) + 1) * AWS_CRT_STATISTICS_CATEGORY_STRIDE - 1)

/**
Expand Down
3 changes: 1 addition & 2 deletions include/aws/common/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ struct aws_thread_options {
typedef union {
void *ptr;
} aws_thread_once;
# define AWS_THREAD_ONCE_STATIC_INIT \
{ NULL }
# define AWS_THREAD_ONCE_STATIC_INIT {NULL}
typedef unsigned long aws_thread_id_t;
#else
typedef pthread_once_t aws_thread_once;
Expand Down
8 changes: 4 additions & 4 deletions include/aws/testing/aws_test_harness.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ static int s_cunit_failure_message0(
if (assert_rv != AWS_OP_SUCCESS) { \
if (!PRINT_FAIL_INTERNAL0(__VA_ARGS__)) { \
PRINT_FAIL_INTERNAL0( \
"Expected success at %s; got return value %d with last error 0x%04d\n", \
"Expected success at %s; got return value %d with last error %d\n", \
#condition, \
assert_rv, \
aws_last_error()); \
Expand All @@ -150,7 +150,7 @@ static int s_cunit_failure_message0(
if (assert_rv != AWS_OP_ERR) { \
if (!PRINT_FAIL_INTERNAL0(__VA_ARGS__)) { \
PRINT_FAIL_INTERNAL0( \
"Expected failure at %s; got return value %d with last error 0x%04d\n", \
"Expected failure at %s; got return value %d with last error %d\n", \
#condition, \
assert_rv, \
aws_last_error()); \
Expand All @@ -167,7 +167,7 @@ static int s_cunit_failure_message0(
if (assert_rv != AWS_OP_ERR) { \
fprintf( \
AWS_TESTING_REPORT_FD, \
"%sExpected error but no error occurred; rv=%d, aws_last_error=%04d (expected %04d): ", \
"%sExpected error but no error occurred; rv=%d, aws_last_error=%d (expected %d): ", \
FAIL_PREFIX, \
assert_rv, \
assert_err, \
Expand All @@ -180,7 +180,7 @@ static int s_cunit_failure_message0(
if (assert_err != assert_err_expect) { \
fprintf( \
AWS_TESTING_REPORT_FD, \
"%sIncorrect error code; aws_last_error=%04d (expected %04d): ", \
"%sIncorrect error code; aws_last_error=%d (expected %d): ", \
FAIL_PREFIX, \
assert_err, \
assert_err_expect); \
Expand Down
2 changes: 1 addition & 1 deletion source/allocator.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ void *aws_mem_calloc(struct aws_allocator *allocator, size_t num, size_t size) {
return mem;
}

#define AWS_ALIGN_ROUND_UP(value, alignment) (((value) + ((alignment)-1)) & ~((alignment)-1))
#define AWS_ALIGN_ROUND_UP(value, alignment) (((value) + ((alignment) - 1)) & ~((alignment) - 1))

void *aws_mem_acquire_many(struct aws_allocator *allocator, size_t count, ...) {

Expand Down
4 changes: 3 additions & 1 deletion source/android/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ static int s_logcat_format(struct logcat_format_data *formatting_data, va_list a
return AWS_OP_SUCCESS;
}

static struct aws_logger_logcat { enum aws_log_level level; } s_logcat_impl;
static struct aws_logger_logcat {
enum aws_log_level level;
} s_logcat_impl;

static int s_logcat_log(
struct aws_logger *logger,
Expand Down
2 changes: 1 addition & 1 deletion source/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void aws_secure_zero(void *pBuf, size_t bufsize) {
#endif // #else not windows
}

#define AWS_DEFINE_ERROR_INFO_COMMON(C, ES) [(C)-0x0000] = AWS_DEFINE_ERROR_INFO(C, ES, "aws-c-common")
#define AWS_DEFINE_ERROR_INFO_COMMON(C, ES) [(C) - 0x0000] = AWS_DEFINE_ERROR_INFO(C, ES, "aws-c-common")
/* clang-format off */
static struct aws_error_info errors[] = {
AWS_DEFINE_ERROR_INFO_COMMON(
Expand Down
59 changes: 37 additions & 22 deletions source/memtrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,38 +131,53 @@ static void s_alloc_tracer_track(struct alloc_tracer *tracer, void *ptr, size_t
aws_high_res_clock_get_ticks(&alloc->time);

if (tracer->level == AWS_MEMTRACE_STACKS) {
/* capture stack frames, skip 2 for this function and the allocation vtable function */
/* capture stack frames,
* skip 2 for this function and the allocation vtable function if we have a full stack trace
* and otherwise just capture what ever stack trace we got
*/
AWS_VARIABLE_LENGTH_ARRAY(void *, stack_frames, (FRAMES_TO_SKIP + tracer->frames_per_stack));
size_t stack_depth = aws_backtrace(stack_frames, FRAMES_TO_SKIP + tracer->frames_per_stack);
if (stack_depth >= FRAMES_TO_SKIP) {
/* hash the stack pointers */
struct aws_byte_cursor stack_cursor =
aws_byte_cursor_from_array(stack_frames, stack_depth * sizeof(void *));
uint64_t stack_id = aws_hash_byte_cursor_ptr(&stack_cursor);
alloc->stack = stack_id; /* associate the stack with the alloc */

aws_mutex_lock(&tracer->mutex);
struct aws_hash_element *item = NULL;
int was_created = 0;
AWS_FATAL_ASSERT(
AWS_OP_SUCCESS ==
aws_hash_table_create(&tracer->stacks, (void *)(uintptr_t)stack_id, &item, &was_created));
/* If this is a new stack, save it to the hash */
if (was_created) {
struct stack_trace *stack = aws_mem_calloc(
aws_default_allocator(),
1,
sizeof(struct stack_trace) + (sizeof(void *) * tracer->frames_per_stack));
AWS_FATAL_ASSERT(stack);
AWS_FATAL_ASSERT(stack_depth > 0);

/* hash the stack pointers */
struct aws_byte_cursor stack_cursor = aws_byte_cursor_from_array(stack_frames, stack_depth * sizeof(void *));
uint64_t stack_id = aws_hash_byte_cursor_ptr(&stack_cursor);
alloc->stack = stack_id; /* associate the stack with the alloc */

aws_mutex_lock(&tracer->mutex);
struct aws_hash_element *item = NULL;
int was_created = 0;
AWS_FATAL_ASSERT(
AWS_OP_SUCCESS == aws_hash_table_create(&tracer->stacks, (void *)(uintptr_t)stack_id, &item, &was_created));
/* If this is a new stack, save it to the hash */
if (was_created) {
struct stack_trace *stack = aws_mem_calloc(
aws_default_allocator(), 1, sizeof(struct stack_trace) + (sizeof(void *) * tracer->frames_per_stack));
AWS_FATAL_ASSERT(stack);
/**
* Optimizations can affect the number of frames we get and in pathological cases we can
* get fewer than FRAMES_TO_SKIP frames, but always at least 1 because code has to start somewhere.
* (looking at you gcc with -O3 on aarch64)
* With optimizations on we cannot trust the stack trace too much.
* Memtracer makes an assumption that stack trace will be available in all cases if stack trace api
* works. So in the pathological case of stack_depth <= FRAMES_TO_SKIP lets record all the frames we
* have, to at least have an anchor for where allocation is comming from, however inaccurate it is.
*/
if (stack_depth <= FRAMES_TO_SKIP) {
memcpy((void **)&stack->frames[0], &stack_frames[0], (stack_depth) * sizeof(void *));
stack->depth = stack_depth;
item->value = stack;
} else {
memcpy(
(void **)&stack->frames[0],
&stack_frames[FRAMES_TO_SKIP],
(stack_depth - FRAMES_TO_SKIP) * sizeof(void *));
stack->depth = stack_depth - FRAMES_TO_SKIP;
item->value = stack;
}
aws_mutex_unlock(&tracer->mutex);
}

aws_mutex_unlock(&tracer->mutex);
}

aws_mutex_lock(&tracer->mutex);
Expand Down
2 changes: 1 addition & 1 deletion source/posix/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static int (*s_gettime_fn)(clockid_t clock_id, struct timespec *tp) = NULL;

static void s_do_osx_loads(void *user_data) {
(void)user_data;
s_gettime_fn = (int (*)(clockid_t clock_id, struct timespec * tp)) dlsym(RTLD_DEFAULT, "clock_gettime");
s_gettime_fn = (int (*)(clockid_t clock_id, struct timespec *tp))dlsym(RTLD_DEFAULT, "clock_gettime");
}

int aws_high_res_clock_get_ticks(uint64_t *timestamp) {
Expand Down
2 changes: 1 addition & 1 deletion source/priority_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include <string.h>

#define PARENT_OF(index) (((index)&1) ? (index) >> 1 : (index) > 1 ? ((index)-2) >> 1 : 0)
#define PARENT_OF(index) (((index) & 1) ? (index) >> 1 : (index) > 1 ? ((index) - 2) >> 1 : 0)
#define LEFT_OF(index) (((index) << 1) + 1)
#define RIGHT_OF(index) (((index) << 1) + 2)

Expand Down
Loading

0 comments on commit 3610479

Please sign in to comment.