Skip to content

Commit 6db9b75

Browse files
committed
Better MSVC support
* get rid of alloca() * no variable-sized arrays
1 parent 2b8681e commit 6db9b75

6 files changed

Lines changed: 22 additions & 20 deletions

File tree

‎getopt.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@ int optind=1, opterr=1, optopt, __optpos, optreset=0;
1111
static void __getopt_msg(const char *a, const char *b, const char *c, size_t l)
1212
{
1313
FILE *f = stderr;
14+
#if !defined(WIN32) && !defined(_WIN32)
1415
flockfile(f);
16+
#endif
1517
fputs(a, f);
1618
fwrite(b, strlen(b), 1, f);
1719
fwrite(c, 1, l, f);
1820
fputc('\n', f);
21+
#if !defined(WIN32) && !defined(_WIN32)
1922
funlockfile(f);
23+
#endif
2024
}
2125

2226
int getopt(int argc, char * const argv[], const char *optstring)

‎kalloc.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static size_t *morecore(kmem_t *km, size_t nu)
4646
up = (size_t*)malloc(rnu * sizeof(size_t));
4747
if (!up) { /* fail to allocate memory */
4848
km_stat(km);
49-
fprintf(stderr, "[morecore] %lu bytes requested but not available.\n", rnu * sizeof(size_t));
49+
fprintf(stderr, "[morecore] %lu bytes requested but not available.\n", (unsigned long)rnu * sizeof(size_t));
5050
exit(1);
5151
}
5252
/* put the pointer in km->list_head */
@@ -210,5 +210,5 @@ void km_stat(const void *_km)
210210
--n_blocks;
211211
frag = 1.0/1024.0 * n_units * sizeof(size_t) / n_blocks;
212212
fprintf(stderr, "[kr_stat] tot=%lu, free=%lu, n_block=%u, max_block=%lu, frag_len=%.3fK\n",
213-
km->total_allocated, n_units * sizeof(size_t), n_blocks, max_block * sizeof(size_t), frag);
213+
(unsigned long)km->total_allocated, (unsigned long)n_units * sizeof(size_t), n_blocks, (unsigned long)max_block * sizeof(size_t), frag);
214214
}

‎kdq.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44
#include <stdlib.h>
55
#include <string.h>
6+
#include <stdint.h>
67
#include "kalloc.h"
78

89
#define __KDQ_TYPE(type) \
910
typedef struct { \
10-
size_t front:58, bits:6, count, mask; \
11+
uint64_t front:58, bits:6, count, mask; \
1112
type *a; \
1213
void *km; \
1314
} kdq_##type##_t;

‎ksort.h‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
#include <stdlib.h>
3232
#include <string.h>
33+
#include <assert.h>
3334

3435
typedef struct {
3536
void *left, *right;
@@ -78,6 +79,7 @@ typedef const char *ksstr_t;
7879
#define KSORT_INIT_STR KSORT_INIT(str, ksstr_t, ks_lt_str)
7980

8081
#define RS_MIN_SIZE 64
82+
#define RS_MAX_BITS 8
8183

8284
#define KRADIX_SORT_INIT(name, rstype_t, rskey, sizeof_key) \
8385
typedef struct { \
@@ -98,7 +100,8 @@ typedef const char *ksstr_t;
98100
{ \
99101
rstype_t *i; \
100102
int size = 1<<n_bits, m = size - 1; \
101-
rsbucket_##name##_t *k, b[size], *be = b + size; \
103+
rsbucket_##name##_t *k, b[1<<RS_MAX_BITS], *be = b + size; \
104+
assert(n_bits <= RS_MAX_BITS); \
102105
for (k = b; k != be; ++k) k->b = k->e = beg; \
103106
for (i = beg; i != end; ++i) ++b[rskey(*i)>>s&m].e; \
104107
for (k = b + 1; k != be; ++k) \
@@ -127,7 +130,7 @@ typedef const char *ksstr_t;
127130
void radix_sort_##name(rstype_t *beg, rstype_t *end) \
128131
{ \
129132
if (end - beg <= RS_MIN_SIZE) rs_insertsort_##name(beg, end); \
130-
else rs_sort_##name(beg, end, 8, sizeof_key * 8 - 8); \
133+
else rs_sort_##name(beg, end, RS_MAX_BITS, (sizeof_key - 1) * RS_MAX_BITS); \
131134
}
132135

133136
#endif

‎kthread.c‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
#include <limits.h>
44
#include <stdint.h>
55

6-
#if defined(WIN32) || defined(_WIN32)
7-
#include <malloc.h>
8-
#define alloca _alloca
6+
#if (defined(WIN32) || defined(_WIN32)) && defined(_MSC_VER)
97
#define __sync_fetch_and_add(ptr, addend) _InterlockedExchangeAdd((void*)ptr, addend)
108
#endif
119

@@ -59,12 +57,13 @@ void kt_for(int n_threads, void (*func)(void*,long,int), void *data, long n)
5957
kt_for_t t;
6058
pthread_t *tid;
6159
t.func = func, t.data = data, t.n_threads = n_threads, t.n = n;
62-
t.w = (ktf_worker_t*)alloca(n_threads * sizeof(ktf_worker_t));
63-
tid = (pthread_t*)alloca(n_threads * sizeof(pthread_t));
60+
t.w = (ktf_worker_t*)calloc(n_threads, sizeof(ktf_worker_t));
61+
tid = (pthread_t*)calloc(n_threads, sizeof(pthread_t));
6462
for (i = 0; i < n_threads; ++i)
6563
t.w[i].t = &t, t.w[i].i = i;
6664
for (i = 0; i < n_threads; ++i) pthread_create(&tid[i], 0, ktf_worker, &t.w[i]);
6765
for (i = 0; i < n_threads; ++i) pthread_join(tid[i], 0);
66+
free(tid); free(t.w);
6867
} else {
6968
long j;
7069
for (j = 0; j < n; ++j) func(data, j, 0);
@@ -142,16 +141,17 @@ void kt_pipeline(int n_threads, void *(*func)(void*, int, void*), void *shared_d
142141
pthread_mutex_init(&aux.mutex, 0);
143142
pthread_cond_init(&aux.cv, 0);
144143

145-
aux.workers = (ktp_worker_t*)alloca(n_threads * sizeof(ktp_worker_t));
144+
aux.workers = (ktp_worker_t*)calloc(n_threads, sizeof(ktp_worker_t));
146145
for (i = 0; i < n_threads; ++i) {
147146
ktp_worker_t *w = &aux.workers[i];
148147
w->step = 0; w->pl = &aux; w->data = 0;
149148
w->index = aux.index++;
150149
}
151150

152-
tid = (pthread_t*)alloca(n_threads * sizeof(pthread_t));
151+
tid = (pthread_t*)calloc(n_threads, sizeof(pthread_t));
153152
for (i = 0; i < n_threads; ++i) pthread_create(&tid[i], 0, ktp_worker, &aux.workers[i]);
154153
for (i = 0; i < n_threads; ++i) pthread_join(tid[i], 0);
154+
free(tid); free(aux.workers);
155155

156156
pthread_mutex_destroy(&aux.mutex);
157157
pthread_cond_destroy(&aux.cv);

‎sketch.c‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
#include "kvec.h"
66
#include "minimap.h"
77

8-
#if defined(WIN32) || defined(_WIN32)
9-
#include <malloc.h>
10-
#define alloca _alloca
11-
#endif
12-
138
unsigned char seq_nt4_table[256] = {
149
0, 1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
1510
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
@@ -82,11 +77,10 @@ void mm_sketch(void *km, const char *str, int len, int w, int k, uint32_t rid, i
8277
{
8378
uint64_t shift1 = 2 * (k - 1), mask = (1ULL<<2*k) - 1, kmer[2] = {0,0};
8479
int i, j, l, buf_pos, min_pos, kmer_span = 0;
85-
mm128_t *buf, min = { UINT64_MAX, UINT64_MAX };
80+
mm128_t buf[256], min = { UINT64_MAX, UINT64_MAX };
8681
tiny_queue_t tq;
8782

88-
assert(len > 0 && w > 0 && k > 0 && k <= 28); // 56 bits for k-mer; could use long k-mers, but 28 enough in practice
89-
buf = (mm128_t*)alloca(w * 16);
83+
assert(len > 0 && (w > 0 && w < 256) && (k > 0 && k <= 28)); // 56 bits for k-mer; could use long k-mers, but 28 enough in practice
9084
memset(buf, 0xff, w * 16);
9185
memset(&tq, 0, sizeof(tiny_queue_t));
9286
kv_resize(mm128_t, km, *p, p->n + len/w);

0 commit comments

Comments
 (0)