diff --git a/regressions/ck_barrier/benchmark/throughput.c b/regressions/ck_barrier/benchmark/throughput.c index 1a1c01395..7ccc72a6d 100644 --- a/regressions/ck_barrier/benchmark/throughput.c +++ b/regressions/ck_barrier/benchmark/throughput.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -99,10 +100,12 @@ main(int argc, char *argv[]) ck_error("ERROR: Could not allocate thread structures\n"); } - counters = calloc(sizeof(struct counter), nthr); + counters = common_aligned_alloc(common_alignof(struct counter), + sizeof(*counters) * nthr); if (counters == NULL) { ck_error("ERROR: Could not allocate counters\n"); } + memset(counters, 0, sizeof(*counters) * nthr); a.delta = atoi(argv[2]); diff --git a/regressions/ck_barrier/validate/barrier_combining.c b/regressions/ck_barrier/validate/barrier_combining.c index 98fa0cf33..be8d134c2 100644 --- a/regressions/ck_barrier/validate/barrier_combining.c +++ b/regressions/ck_barrier/validate/barrier_combining.c @@ -90,7 +90,8 @@ main(int argc, char *argv[]) ck_barrier_combining_group_t *init_root; int i; - init_root = malloc(sizeof(ck_barrier_combining_group_t)); + init_root = common_aligned_alloc(common_alignof(ck_barrier_combining_group_t), + sizeof(ck_barrier_combining_group_t)); if (init_root == NULL) { ck_error("ERROR: Could not allocate initial barrier structure\n"); } @@ -110,7 +111,8 @@ main(int argc, char *argv[]) ck_error("ERROR: Number of threads must be greater than 0\n"); } - groupings = malloc(sizeof(ck_barrier_combining_group_t) * ngroups); + groupings = common_aligned_alloc(common_alignof(ck_barrier_combining_group_t), + sizeof(ck_barrier_combining_group_t) * ngroups); if (groupings == NULL) { ck_error("Could not allocate thread barrier grouping structures\n"); } diff --git a/regressions/ck_cohort/benchmark/throughput.c b/regressions/ck_cohort/benchmark/throughput.c index 7c4776d3c..2dc07d483 100644 --- a/regressions/ck_cohort/benchmark/throughput.c +++ b/regressions/ck_cohort/benchmark/throughput.c @@ -172,7 +172,8 @@ main(int argc, char *argv[]) ck_error("ERROR: Could not allocate thread structures\n"); } - cohorts = malloc(sizeof(struct cohort_record) * n_cohorts); + cohorts = common_aligned_alloc(common_alignof(struct cohort_record), + sizeof(struct cohort_record) * n_cohorts); if (cohorts == NULL) { ck_error("ERROR: Could not allocate cohort structures\n"); } @@ -185,7 +186,8 @@ main(int argc, char *argv[]) a.delta = atoi(argv[2]); a.request = 0; - count = malloc(sizeof(*count) * nthr); + count = common_aligned_alloc(common_alignof(struct counters), + sizeof(struct counters) * nthr); if (count == NULL) { ck_error("ERROR: Could not create acquisition buffer\n"); } diff --git a/regressions/ck_epoch/validate/ck_epoch_poll.c b/regressions/ck_epoch/validate/ck_epoch_poll.c index 8aaed5a17..d6ba0494e 100644 --- a/regressions/ck_epoch/validate/ck_epoch_poll.c +++ b/regressions/ck_epoch/validate/ck_epoch_poll.c @@ -292,7 +292,8 @@ read_thread(void *unused CK_CC_UNUSED) ck_epoch_record_t *record CK_CC_CACHELINE; ck_stack_entry_t *cursor, *n; - record = malloc(sizeof *record); + record = common_aligned_alloc(common_alignof(ck_epoch_record_t), + sizeof(ck_epoch_record_t)); if (record == NULL) ck_error("record allocation failure"); @@ -348,7 +349,8 @@ write_thread(void *unused CK_CC_UNUSED) ck_epoch_record_t *record; ck_stack_entry_t *s; - record = malloc(sizeof *record); + record = common_aligned_alloc(common_alignof(ck_epoch_record_t), + sizeof(ck_epoch_record_t)); if (record == NULL) ck_error("record allocation failure"); ck_epoch_register(&stack_epoch, record, NULL); diff --git a/regressions/ck_epoch/validate/ck_epoch_section_2.c b/regressions/ck_epoch/validate/ck_epoch_section_2.c index 3aeb93a3f..98bea52dd 100644 --- a/regressions/ck_epoch/validate/ck_epoch_section_2.c +++ b/regressions/ck_epoch/validate/ck_epoch_section_2.c @@ -75,7 +75,8 @@ read_thread(void *unused CK_CC_UNUSED) */ ck_epoch_section_t section[2]; - record = malloc(sizeof *record); + record = common_aligned_alloc(common_alignof(ck_epoch_record_t), + sizeof(ck_epoch_record_t)); assert(record != NULL); ck_epoch_register(&epoch, record, NULL); @@ -162,7 +163,8 @@ write_thread(void *unused CK_CC_UNUSED) ck_epoch_record_t *record; unsigned long iterations = 0; - record = malloc(sizeof *record); + record = common_aligned_alloc(common_alignof(ck_epoch_record_t), + sizeof(ck_epoch_record_t)); if (record == NULL) ck_error("record allocation failure"); diff --git a/regressions/ck_epoch/validate/ck_epoch_synchronize.c b/regressions/ck_epoch/validate/ck_epoch_synchronize.c index 67e23a329..07ae875ea 100644 --- a/regressions/ck_epoch/validate/ck_epoch_synchronize.c +++ b/regressions/ck_epoch/validate/ck_epoch_synchronize.c @@ -91,7 +91,8 @@ read_thread(void *unused CK_CC_UNUSED) ck_stack_entry_t *n; unsigned int i; - record = malloc(sizeof *record); + record = common_aligned_alloc(common_alignof(ck_epoch_record_t), + sizeof(ck_epoch_record_t)); if (record == NULL) ck_error("record allocation failure"); ck_epoch_register(&stack_epoch, record, NULL); @@ -151,7 +152,8 @@ write_thread(void *unused CK_CC_UNUSED) ck_epoch_record_t *record; ck_stack_entry_t *s; - record = malloc(sizeof *record); + record = common_aligned_alloc(common_alignof(ck_epoch_record_t), + sizeof(ck_epoch_record_t)); if (record == NULL) ck_error("record allocation failure"); ck_epoch_register(&stack_epoch, record, NULL); diff --git a/regressions/ck_epoch/validate/torture.c b/regressions/ck_epoch/validate/torture.c index f49d41236..91170cdb9 100644 --- a/regressions/ck_epoch/validate/torture.c +++ b/regressions/ck_epoch/validate/torture.c @@ -117,7 +117,8 @@ read_thread(void *unused CK_CC_UNUSED) { ck_epoch_record_t *record; - record = malloc(sizeof *record); + record = common_aligned_alloc(common_alignof(ck_epoch_record_t), + sizeof(ck_epoch_record_t)); assert(record != NULL); ck_epoch_register(&epoch, record, NULL); @@ -149,7 +150,8 @@ write_thread(void *unused CK_CC_UNUSED) bool c = ck_pr_faa_uint(&first, 1); uint64_t ac = 0; - record = malloc(sizeof *record); + record = common_aligned_alloc(common_alignof(ck_epoch_record_t), + sizeof(ck_epoch_record_t)); assert(record != NULL); ck_epoch_register(&epoch, record, NULL); diff --git a/regressions/ck_fifo/validate/ck_fifo_mpmc.c b/regressions/ck_fifo/validate/ck_fifo_mpmc.c index a98185086..1aab62147 100644 --- a/regressions/ck_fifo/validate/ck_fifo_mpmc.c +++ b/regressions/ck_fifo/validate/ck_fifo_mpmc.c @@ -77,7 +77,9 @@ test(void *c) for (i = 0; i < ITERATIONS; i++) { for (j = 0; j < size; j++) { - fifo_entry = malloc(sizeof(ck_fifo_mpmc_entry_t)); + fifo_entry = common_aligned_alloc( + common_alignof(ck_fifo_mpmc_entry_t), + sizeof(ck_fifo_mpmc_entry_t)); entry = malloc(sizeof(struct entry)); entry->tid = context->tid; ck_fifo_mpmc_enqueue(&fifo, fifo_entry, entry); @@ -93,7 +95,9 @@ test(void *c) for (i = 0; i < ITERATIONS; i++) { for (j = 0; j < size; j++) { - fifo_entry = malloc(sizeof(ck_fifo_mpmc_entry_t)); + fifo_entry = common_aligned_alloc( + common_alignof(ck_fifo_mpmc_entry_t), + sizeof(ck_fifo_mpmc_entry_t)); entry = malloc(sizeof(struct entry)); entry->tid = context->tid; while (ck_fifo_mpmc_tryenqueue(&fifo, fifo_entry, entry) == false) @@ -187,12 +191,16 @@ main(int argc, char *argv[]) thread = malloc(sizeof(pthread_t) * nthr); assert(thread); - ck_fifo_mpmc_init(&fifo, malloc(sizeof(ck_fifo_mpmc_entry_t))); + ck_fifo_mpmc_init(&fifo, + common_aligned_alloc(common_alignof(ck_fifo_mpmc_entry_t), + sizeof(ck_fifo_mpmc_entry_t))); ck_fifo_mpmc_deinit(&fifo, &garbage); if (garbage == NULL) ck_error("ERROR: Expected non-NULL stub node on deinit.\n"); - free(garbage); - ck_fifo_mpmc_init(&fifo, malloc(sizeof(ck_fifo_mpmc_entry_t))); + common_aligned_free(garbage); + ck_fifo_mpmc_init(&fifo, + common_aligned_alloc(common_alignof(ck_fifo_mpmc_entry_t), + sizeof(ck_fifo_mpmc_entry_t))); for (i = 0; i < nthr; i++) { context[i].tid = i; @@ -210,7 +218,9 @@ main(int argc, char *argv[]) assert(entry != NULL); entry->tid = 0; ck_fifo_mpmc_enqueue(&fifo, - malloc(sizeof(ck_fifo_mpmc_entry_t)), entry); + common_aligned_alloc(common_alignof(ck_fifo_mpmc_entry_t), + sizeof(ck_fifo_mpmc_entry_t)), + entry); } ck_pr_store_uint(&barrier, 0); diff --git a/regressions/ck_fifo/validate/ck_fifo_mpmc_iterator.c b/regressions/ck_fifo/validate/ck_fifo_mpmc_iterator.c index 5ac81757f..a8605eefc 100644 --- a/regressions/ck_fifo/validate/ck_fifo_mpmc_iterator.c +++ b/regressions/ck_fifo/validate/ck_fifo_mpmc_iterator.c @@ -29,6 +29,8 @@ #include #include +#include "../../common.h" + #ifdef CK_F_FIFO_MPMC struct example { int x; @@ -43,13 +45,15 @@ main(void) struct example *examples; ck_fifo_mpmc_entry_t *stub, *entries, *entry, *next; - stub = malloc(sizeof(ck_fifo_mpmc_entry_t)); + stub = common_aligned_alloc(common_alignof(ck_fifo_mpmc_entry_t), + sizeof(ck_fifo_mpmc_entry_t)); if (stub == NULL) exit(EXIT_FAILURE); ck_fifo_mpmc_init(&mpmc_fifo, stub); - entries = malloc(sizeof(ck_fifo_mpmc_entry_t) * length); + entries = common_aligned_alloc(common_alignof(ck_fifo_mpmc_entry_t), + sizeof(ck_fifo_mpmc_entry_t) * length); if (entries == NULL) exit(EXIT_FAILURE); @@ -76,8 +80,8 @@ main(void) } free(examples); - free(entries); - free(stub); + common_aligned_free(entries); + common_aligned_free(stub); return (0); } diff --git a/regressions/ck_hp/validate/ck_hp_fifo_donner.c b/regressions/ck_hp/validate/ck_hp_fifo_donner.c index 1b52a3754..6a86c3c80 100644 --- a/regressions/ck_hp/validate/ck_hp_fifo_donner.c +++ b/regressions/ck_hp/validate/ck_hp_fifo_donner.c @@ -75,7 +75,8 @@ queue_50_50(void *elements) unsigned long j, element_count = *(unsigned long *)elements; unsigned int seed; - record = malloc(sizeof(ck_hp_record_t)); + record = common_aligned_alloc(common_alignof(ck_hp_record_t), + sizeof(ck_hp_record_t)); assert(record); slots = malloc(CK_HP_FIFO_SLOTS_SIZE); diff --git a/regressions/ck_rwcohort/benchmark/throughput.h b/regressions/ck_rwcohort/benchmark/throughput.h index 2870855b0..1d98c9884 100644 --- a/regressions/ck_rwcohort/benchmark/throughput.h +++ b/regressions/ck_rwcohort/benchmark/throughput.h @@ -190,7 +190,8 @@ main(int argc, char *argv[]) ck_error("ERROR: Could not allocate thread structures\n"); } - cohorts = malloc(sizeof(struct cohort_record) * n_cohorts); + cohorts = common_aligned_alloc(common_alignof(struct cohort_record), + sizeof(struct cohort_record) * n_cohorts); if (cohorts == NULL) { ck_error("ERROR: Could not allocate cohort structures\n"); } diff --git a/regressions/ck_spinlock/benchmark/throughput.h b/regressions/ck_spinlock/benchmark/throughput.h index 3e914f93a..36d62baa3 100644 --- a/regressions/ck_spinlock/benchmark/throughput.h +++ b/regressions/ck_spinlock/benchmark/throughput.h @@ -173,7 +173,8 @@ main(int argc, char *argv[]) a.delta = atoi(argv[2]); a.request = 0; - count = malloc(sizeof(*count) * nthr); + count = common_aligned_alloc(common_alignof(struct counters), + sizeof(struct counters) * nthr); if (count == NULL) { ck_error("ERROR: Could not create acquisition buffer\n"); exit(EXIT_FAILURE); diff --git a/regressions/ck_stack/validate/pair.c b/regressions/ck_stack/validate/pair.c index c0f1bb1a5..7f0b2fbb2 100644 --- a/regressions/ck_stack/validate/pair.c +++ b/regressions/ck_stack/validate/pair.c @@ -214,7 +214,8 @@ main(int argc, char *argv[]) affinerator.request = 0; affinerator.delta = d; - bucket = malloc(sizeof(struct entry) * nthr); + bucket = common_aligned_alloc(common_alignof(struct entry), + sizeof(struct entry) * nthr); assert(bucket != NULL); thread = malloc(sizeof(pthread_t) * nthr); diff --git a/regressions/common.h b/regressions/common.h index 1dc48a9af..cf2581259 100644 --- a/regressions/common.h +++ b/regressions/common.h @@ -56,6 +56,7 @@ #if defined(_WIN32) #include +#include #define NOMINMAX #include #define DELTA_EPOCH 11644473600000000ULL @@ -267,6 +268,36 @@ prefix##_common_win_alarm(void *unused) \ #define COMMON_ALARM_INIT(prefix, alarm_event_name, duration) #endif +#define common_alignof(T) __alignof__(T) + +/* + * Modelled after C11's aligned_alloc. However, these allocations must be freed + * with common_aligned_free(), not free(). + */ +CK_CC_INLINE static void * +common_aligned_alloc(size_t alignment, size_t size) +{ +#ifdef _WIN32 + return _aligned_malloc(size, alignment); +#else + void *p; + if (posix_memalign(&p, alignment, size) != 0) { + return NULL; + } + return p; +#endif +} + +CK_CC_INLINE static void +common_aligned_free(void *p) +{ +#ifdef _WIN32 + _aligned_free(p); +#else + free(p); +#endif +} + struct affinity { unsigned int delta; unsigned int request;