diff --git a/cord/tests/de.c b/cord/tests/de.c index 7d27537b6..87273661d 100644 --- a/cord/tests/de.c +++ b/cord/tests/de.c @@ -80,18 +80,20 @@ /* List of line number to position mappings, in descending order. */ /* There may be holes. */ -typedef struct LineMapRep { +struct LineMapRep { int line; size_t pos; struct LineMapRep *previous; -} * line_map; +}; +typedef struct LineMapRep *line_map; -/* List of file versions, one per edit operation */ -typedef struct HistoryRep { +/* List of file versions, one per edit operation. */ +struct HistoryRep { CORD file_contents; struct HistoryRep *previous; - line_map map; /* Invalid for first record "now" */ -} * history; + line_map map; /* invalid for the first record "now" */ +}; +typedef struct HistoryRep *history; history now = 0; CORD current; /* == now -> file_contents. */ diff --git a/finalize.c b/finalize.c index af1958704..e77c3f517 100644 --- a/finalize.c +++ b/finalize.c @@ -27,7 +27,7 @@ typedef void (*finalization_mark_proc)(ptr_t /* finalizable_obj_ptr */); # define HASH3(addr, size, log_size) \ ((size_t)((ADDR(addr) >> 3) ^ (ADDR(addr) >> (3 + (log_size)))) \ - & ((size)-1)) + & ((size) - (size_t)1)) # define HASH2(addr, log_size) HASH3(addr, (size_t)1 << (log_size), log_size) struct hash_chain_entry { diff --git a/include/gc/cord_pos.h b/include/gc/cord_pos.h index 835ad4c38..60576a64a 100644 --- a/include/gc/cord_pos.h +++ b/include/gc/cord_pos.h @@ -120,7 +120,8 @@ CORD_API void CORD__prev(CORD_pos); /* Advance position by n characters; n should be positive and less */ /* than CORD_pos_chars_left(p). */ -# define CORD_pos_advance(p, n) ((p)[0].cur_pos += (n)-1, CORD_next(p)) +# define CORD_pos_advance(p, n) \ + ((p)[0].cur_pos += (n) - (size_t)1, CORD_next(p)) /* Address of the current character in cache. */ # define CORD_pos_cur_char_addr(p) \ diff --git a/include/gc/gc.h b/include/gc/gc.h index 7e00d0409..2e19a21a2 100644 --- a/include/gc/gc.h +++ b/include/gc/gc.h @@ -2316,11 +2316,11 @@ GC_API void *GC_CALL GC_find_limit(void * /* start */, int /* up */); #if defined(GC_INITIAL_HEAP_SIZE) && !defined(CPPCHECK) /* Set heap size to the desired value at start-up */ -# define GC_INIT_CONF_INITIAL_HEAP_SIZE \ - { \ - size_t heap_size = GC_get_heap_size(); \ - if (heap_size < (GC_INITIAL_HEAP_SIZE)) \ - (void)GC_expand_hp((GC_INITIAL_HEAP_SIZE)-heap_size); \ +# define GC_INIT_CONF_INITIAL_HEAP_SIZE \ + { \ + size_t heap_size = GC_get_heap_size(); \ + if (heap_size < (size_t)(GC_INITIAL_HEAP_SIZE)) \ + (void)GC_expand_hp(((size_t)(GC_INITIAL_HEAP_SIZE)) - heap_size); \ } #else # define GC_INIT_CONF_INITIAL_HEAP_SIZE /* empty */ diff --git a/include/gc/gc_inline.h b/include/gc/gc_inline.h index e5f753b72..4ce06fb20 100644 --- a/include/gc/gc_inline.h +++ b/include/gc/gc_inline.h @@ -167,7 +167,7 @@ GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void *GC_CALL GC_end_stubborn_change(my_fl); \ GC_reachable_here(next); \ } \ - GC_ASSERT(GC_size(result) >= GC_GRANULE_BYTES * (lg)); \ + GC_ASSERT(GC_size(result) >= GC_RAW_BYTES_FROM_INDEX(lg)); \ GC_ASSERT((k) == GC_I_PTRFREE \ || 0 /* NULL */ == ((void **)result)[1]); \ break; \ @@ -188,7 +188,7 @@ GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void *GC_CALL k, my_fl); \ my_entry = *my_fl; \ if (0 /* NULL */ == my_entry) { \ - result = (*GC_get_oom_fn())(GC_GRANULE_BYTES * (lg)); \ + result = (*GC_get_oom_fn())(GC_RAW_BYTES_FROM_INDEX(lg)); \ break; \ } \ } \ @@ -203,12 +203,13 @@ GC_API GC_ATTR_MALLOC GC_ATTR_ALLOC_SIZE(1) void *GC_CALL /* allocator lock. The caller is responsible for supplying a cleared */ /* tiny_fl free-list array. For single-threaded applications, this may */ /* be a global array. */ -#define GC_MALLOC_WORDS_KIND(result, n, tiny_fl, k, init) \ - do { \ - size_t lg = GC_PTRS_TO_WHOLE_GRANULES(n); \ - \ - GC_FAST_MALLOC_GRANS(result, lg, tiny_fl, 0 /* num_direct */, k, \ - GC_malloc_kind((lg)*GC_GRANULE_BYTES, k), init); \ +#define GC_MALLOC_WORDS_KIND(result, n, tiny_fl, k, init) \ + do { \ + size_t lg = GC_PTRS_TO_WHOLE_GRANULES(n); \ + \ + GC_FAST_MALLOC_GRANS(result, lg, tiny_fl, 0 /* num_direct */, k, \ + GC_malloc_kind(GC_RAW_BYTES_FROM_INDEX(lg), k), \ + init); \ } while (0) #define GC_MALLOC_WORDS(result, n, tiny_fl) \ diff --git a/include/private/dbg_mlc.h b/include/private/dbg_mlc.h index 5c2322d7f..5225c5c62 100644 --- a/include/private/dbg_mlc.h +++ b/include/private/dbg_mlc.h @@ -177,8 +177,9 @@ GC_INNER int GC_has_other_debug_info(ptr_t base); (((GC_uintptr_t)GC_cptr_load((volatile ptr_t *)(base)) & 1) != 0 \ && GC_has_other_debug_info(base) > 0) # else -# define GC_HAS_DEBUG_INFO(base) \ - ((*(GC_uintptr_t *)(base)&1) != 0 && GC_has_other_debug_info(base) > 0) +# define GC_HAS_DEBUG_INFO(base) \ + (((*(GC_uintptr_t *)(base)) & 1) != 0 \ + && GC_has_other_debug_info(base) > 0) # endif #else # define GC_HAS_DEBUG_INFO(base) (GC_has_other_debug_info(base) > 0) diff --git a/include/private/gc_priv.h b/include/private/gc_priv.h index fbc198aac..a064d340b 100644 --- a/include/private/gc_priv.h +++ b/include/private/gc_priv.h @@ -929,10 +929,10 @@ EXTERN_C_BEGIN #endif #define BYTES_TO_GRANULES(lb) ((lb) / GC_GRANULE_BYTES) -#define GRANULES_TO_BYTES(lg) ((lg)*GC_GRANULE_BYTES) +#define GRANULES_TO_BYTES(lg) (GC_GRANULE_BYTES * (lg)) #define BYTES_TO_PTRS(lb) ((lb) / sizeof(ptr_t)) #define PTRS_TO_BYTES(lpw) ((lpw) * sizeof(ptr_t)) -#define GRANULES_TO_PTRS(lg) ((lg)*GC_GRANULE_PTRS) +#define GRANULES_TO_PTRS(lg) (GC_GRANULE_PTRS * (lg)) /* Convert size in bytes to that in pointers rounding up (but */ /* not adding extra byte at end). */ @@ -2032,7 +2032,7 @@ GC_INNER void GC_push_all_register_sections( # define MARK_BIT_OFFSET(sz) BYTES_TO_GRANULES(sz) # define FINAL_MARK_BIT(sz) \ ((sz) > MAXOBJBYTES ? MARK_BITS_PER_HBLK \ - : BYTES_TO_GRANULES((sz)*HBLK_OBJS(sz))) + : BYTES_TO_GRANULES(HBLK_OBJS(sz) * (sz))) #endif /* !MARK_BIT_PER_OBJ */ /* Important internal collector routines. */ @@ -2183,15 +2183,15 @@ ptr_t GC_save_regs_in_stack(void); } while (0) # ifdef THREADS -# define PS_COMPUTE_ADJUSTED_OFS(padj_ps_ofs, ps_ofs, ofs_sz_ull) \ - do { \ - if ((ofs_sz_ull) <= (ps_ofs) /* && ofs_sz_ull > 0 */) \ - ABORT_ARG2("Incorrect size of procedure stack", \ - ": ofs= %lu, size= %lu", (unsigned long)(ps_ofs), \ - (unsigned long)(ofs_sz_ull)); \ - *(padj_ps_ofs) = (ps_ofs) > PS_SYSCALL_TAIL_BYTES \ - ? (ps_ofs)-PS_SYSCALL_TAIL_BYTES \ - : 0; \ +# define PS_COMPUTE_ADJUSTED_OFS(padj_ps_ofs, ps_ofs, ofs_sz_ull) \ + do { \ + if ((ofs_sz_ull) <= (ps_ofs) /* && ofs_sz_ull > 0 */) \ + ABORT_ARG2("Incorrect size of procedure stack", \ + ": ofs= %lu, size= %lu", (unsigned long)(ps_ofs), \ + (unsigned long)(ofs_sz_ull)); \ + *(padj_ps_ofs) = (ps_ofs) > (unsigned)PS_SYSCALL_TAIL_BYTES \ + ? (ps_ofs) - (unsigned)PS_SYSCALL_TAIL_BYTES \ + : 0; \ } while (0) # else /* A simplified variant of the above assuming ps_ofs is a zero const. */ @@ -3165,7 +3165,10 @@ GC_INNER word GC_compute_root_size(void); static_assert(expr, "static assertion failed: " #expr) #elif defined(static_assert) && !defined(CPPCHECK) \ && (__STDC_VERSION__ >= 201112L) -# define GC_STATIC_ASSERT(expr) static_assert(expr, # expr) +# define GC_STATIC_ASSERT(expr) \ + do { /* wrap into do-while for proper formatting by clang-format */ \ + static_assert(expr, #expr); \ + } while (0) #elif defined(mips) && !defined(__GNUC__) && !defined(CPPCHECK) /* DOB: MIPSPro C gets an internal error taking the sizeof an array type. This code works correctly (ugliness is to avoid "unused var" warnings) */ diff --git a/include/private/gcconfig.h b/include/private/gcconfig.h index 8a8a04bcd..3e5c42758 100644 --- a/include/private/gcconfig.h +++ b/include/private/gcconfig.h @@ -781,10 +781,10 @@ EXTERN_C_BEGIN # define PTR_ALIGN_UP(p, b) __builtin_align_up(p, b) #else # define PTR_ALIGN_DOWN(p, b) \ - ((ptr_t)((GC_uintptr_t)(p) & ~(GC_uintptr_t)((b)-1))) -# define PTR_ALIGN_UP(p, b) \ - ((ptr_t)(((GC_uintptr_t)(p) + (GC_uintptr_t)((b)-1)) \ - & ~(GC_uintptr_t)((b)-1))) + ((ptr_t)((GC_uintptr_t)(p) & ~((GC_uintptr_t)(b) - (GC_uintptr_t)1))) +# define PTR_ALIGN_UP(p, b) \ + ((ptr_t)(((GC_uintptr_t)(p) + (GC_uintptr_t)(b) - (GC_uintptr_t)1) \ + & ~((GC_uintptr_t)(b) - (GC_uintptr_t)1))) #endif /* If available, we can use __builtin_unwind_init() to push the */ @@ -1446,7 +1446,7 @@ extern int _etext, _end; # ifndef USE_MMAP # define USE_MMAP 1 # endif -# define MAP_FAILED (void *)((GC_uintptr_t)-1) +# define MAP_FAILED ((void *)(~(GC_uintptr_t)0)) # define HEAP_START ((word)0x40000000) # endif /* DGUX */ # ifdef LINUX @@ -2357,7 +2357,7 @@ LONG64 durango_get_stack_bottom(void); # define PROT_EXEC 4 # define MAP_PRIVATE 2 # define MAP_FIXED 0x10 -# define MAP_FAILED ((void *)-1) +# define MAP_FAILED ((void *)(~(GC_uintptr_t)0)) # endif # ifdef MSWIN32 # define RETRY_GET_THREAD_CONTEXT @@ -2471,9 +2471,9 @@ void *emmalloc_memalign(size_t align, size_t lb); # ifdef WASI # define OS_TYPE "WASI" extern char __global_base, __heap_base; -# define STACKBOTTOM ((ptr_t)&__global_base) -# define DATASTART ((ptr_t)&__global_base) -# define DATAEND ((ptr_t)&__heap_base) +# define DATASTART ((ptr_t)(&__global_base)) +# define DATAEND ((ptr_t)(&__heap_base)) +# define STACKBOTTOM DATASTART # ifndef GC_NO_SIGSETJMP # define GC_NO_SIGSETJMP 1 /* no support of signals */ # endif @@ -3408,7 +3408,7 @@ extern ptr_t GC_data_start; # define NEED_FIXUP_POINTER #elif defined(DYNAMIC_POINTER_MASK) # define FIXUP_POINTER(p) \ - (p = (ptr_t)(((word)(p)&GC_pointer_mask) << GC_pointer_shift)) + (p = (ptr_t)((((word)(p)) & GC_pointer_mask) << GC_pointer_shift)) # undef POINTER_MASK # undef POINTER_SHIFT # define NEED_FIXUP_POINTER diff --git a/include/private/pthread_support.h b/include/private/pthread_support.h index 603f5d5da..5b98d2e95 100644 --- a/include/private/pthread_support.h +++ b/include/private/pthread_support.h @@ -60,7 +60,7 @@ typedef struct GC_NT_TIB_s GC_NT_TIB; # endif # endif -typedef struct GC_StackContext_Rep { +struct GC_StackContext_Rep { # if defined(THREAD_SANITIZER) && defined(SIGNAL_BASED_STOP_WORLD) /* A dummy field to avoid TSan false positive about the race */ /* between GC_has_other_debug_info and GC_suspend_handler_inner */ @@ -133,7 +133,8 @@ typedef struct GC_StackContext_Rep { /* Points to the "frame" data held in stack by the innermost */ /* GC_call_with_gc_active() of this stack (thread); may be NULL. */ struct GC_traced_stack_sect_s *traced_stack_sect; -} * GC_stack_context_t; +}; +typedef struct GC_StackContext_Rep *GC_stack_context_t; # ifdef GC_WIN32_THREADS typedef DWORD thread_id_t; @@ -150,7 +151,7 @@ typedef pthread_t thread_id_t; # define THREAD_ID_TO_VPTR(id) PTHREAD_TO_VPTR(id) # endif -typedef struct GC_Thread_Rep { +struct GC_Thread_Rep { union { # if !defined(GC_NO_THREADS_DISCOVERY) && defined(GC_WIN32_THREADS) /* Updated without a lock. We assert that each unused entry has */ @@ -278,7 +279,8 @@ typedef struct GC_Thread_Rep { /* needed for GetThreadContext() to succeed. */ word context_regs[PUSHED_REGS_COUNT]; # endif -} * GC_thread; +}; +typedef struct GC_Thread_Rep *GC_thread; # if defined(GC_PTHREADS) || defined(GC_PTHREADS_PARAMARK) /* Convert an opaque pthread_t value to a pointer identifying the thread. */ diff --git a/include/private/thread_local_alloc.h b/include/private/thread_local_alloc.h index 79765b2a3..fe95e1486 100644 --- a/include/private/thread_local_alloc.h +++ b/include/private/thread_local_alloc.h @@ -120,7 +120,7 @@ EXTERN_C_BEGIN /* allocation, equivalent to 0; */ /* - >=HBLKSIZE means pointer to nonempty free list. */ -typedef struct thread_local_freelists { +struct thread_local_freelists { /* Note: Preserve *_freelists names for some clients. */ void *_freelists[THREAD_FREELISTS_KINDS][GC_TINY_FREELISTS]; # define ptrfree_freelists _freelists[PTRFREE] @@ -133,7 +133,8 @@ typedef struct thread_local_freelists { /* Do not use local free lists for up to this much allocation. */ # define DIRECT_GRANULES (HBLKSIZE / GC_GRANULE_BYTES) -} * GC_tlfs; +}; +typedef struct thread_local_freelists *GC_tlfs; # if defined(USE_PTHREAD_SPECIFIC) # define GC_getspecific pthread_getspecific diff --git a/os_dep.c b/os_dep.c index a3afcc7ab..3809da7c4 100644 --- a/os_dep.c +++ b/os_dep.c @@ -1226,8 +1226,9 @@ os_main_stackbottom(void) /* low while the initialization code is running. */ if ((ADDR(__libc_stack_end) & 0xfff) + 0x10 < 0x1000) { return __libc_stack_end + 0x10; - } /* Otherwise it's not safe to add 16 bytes and we fall */ - /* back to using /proc. */ + } else { + /* It is not safe to add 16 bytes. Thus, fallback to using /proc. */ + } # elif defined(SPARC) /* Older versions of glibc for 64-bit SPARC do not set this */ /* variable correctly, it gets set to either zero or one. */ @@ -4872,7 +4873,7 @@ GC_mprotect_thread(void *arg) # endif } } /* switch */ - } /* for */ + } } /* All this SIGBUS code should not be necessary. All protection faults */ diff --git a/tests/gctest.c b/tests/gctest.c index 2965407e2..06e7e9106 100644 --- a/tests/gctest.c +++ b/tests/gctest.c @@ -381,10 +381,13 @@ gcj_cons(sexpr x, sexpr y) size_t cnt = (size_t)AO_fetch_and_add1(&extra_count); const void *d = (cnt & 1) != 0 ? &gcj_class_struct1 : &gcj_class_struct2; size_t lb = sizeof(struct SEXPR) + sizeof(struct fake_vtable *); - void *r = (cnt & 2) != 0 ? GC_GCJ_MALLOC_IGNORE_OFF_PAGE( - lb + (cnt <= HBLKSIZE / 2 ? cnt : 0), d) - : GC_GCJ_MALLOC(lb, d); + void *r; + if ((cnt & 2) != 0) { + r = GC_GCJ_MALLOC_IGNORE_OFF_PAGE(lb + (cnt <= HBLKSIZE / 2 ? cnt : 0), d); + } else { + r = GC_GCJ_MALLOC(lb, d); + } CHECK_OUT_OF_MEMORY(r); AO_fetch_and_add1(&collectable_count); /* Skip vtable pointer. */ @@ -2022,7 +2025,8 @@ test_long_mult(void) #endif } -#define NUMBER_ROUND_UP(v, bound) ((((v) + (bound)-1) / (bound)) * (bound)) +#define NUMBER_ROUND_UP(v, bound) \ + ((((v) + (bound) - (unsigned)1) / (bound)) * (bound)) static size_t initial_heapsize; diff --git a/tests/trace.c b/tests/trace.c index 152a6eb38..8f896120e 100644 --- a/tests/trace.c +++ b/tests/trace.c @@ -20,7 +20,9 @@ struct treenode { struct treenode *x; struct treenode *y; -} * root[10]; +}; + +static struct treenode *root[10]; static struct treenode * mktree(int i) diff --git a/win32_threads.c b/win32_threads.c index 4609b1672..a69a56ff0 100644 --- a/win32_threads.c +++ b/win32_threads.c @@ -645,7 +645,7 @@ GC_start_world(void) /* argument later and must not be used as the lower bound for sp */ /* check (since the stack may be bigger than 64 KiB). */ # define GC_wince_evaluate_stack_min(s) \ - (ptr_t)(((word)(s)-1) & ~(word)0xFFFF) + (ptr_t)(((word)(s) - (word)1) & ~(word)0xFFFF) # elif defined(GC_ASSERTIONS) # define GC_dont_query_stack_min FALSE # endif @@ -1233,7 +1233,7 @@ GC_start_mark_threads_inner(void) handle = _beginthreadex(NULL /* security_attr */, MARK_THREAD_STACK_SIZE, GC_mark_thread, NUMERIC_TO_VPTR(i), 0 /* flags */, &thread_id); - if (EXPECT(!handle || handle == (GC_uintptr_t)-1L, FALSE)) { + if (EXPECT(!handle || handle == ~(GC_uintptr_t)0, FALSE)) { WARN("Marker thread %" WARN_PRIdPTR " creation failed\n", (GC_signed_word)i); /* Don't try to create other marker threads. */