|
3 | 3 | #include <limits.h> |
4 | 4 | #include <stdint.h> |
5 | 5 |
|
6 | | -#if defined(WIN32) || defined(_WIN32) |
7 | | -#include <malloc.h> |
8 | | -#define alloca _alloca |
| 6 | +#if (defined(WIN32) || defined(_WIN32)) && defined(_MSC_VER) |
9 | 7 | #define __sync_fetch_and_add(ptr, addend) _InterlockedExchangeAdd((void*)ptr, addend) |
10 | 8 | #endif |
11 | 9 |
|
@@ -59,12 +57,13 @@ void kt_for(int n_threads, void (*func)(void*,long,int), void *data, long n) |
59 | 57 | kt_for_t t; |
60 | 58 | pthread_t *tid; |
61 | 59 | 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)); |
64 | 62 | for (i = 0; i < n_threads; ++i) |
65 | 63 | t.w[i].t = &t, t.w[i].i = i; |
66 | 64 | for (i = 0; i < n_threads; ++i) pthread_create(&tid[i], 0, ktf_worker, &t.w[i]); |
67 | 65 | for (i = 0; i < n_threads; ++i) pthread_join(tid[i], 0); |
| 66 | + free(tid); free(t.w); |
68 | 67 | } else { |
69 | 68 | long j; |
70 | 69 | 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 |
142 | 141 | pthread_mutex_init(&aux.mutex, 0); |
143 | 142 | pthread_cond_init(&aux.cv, 0); |
144 | 143 |
|
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)); |
146 | 145 | for (i = 0; i < n_threads; ++i) { |
147 | 146 | ktp_worker_t *w = &aux.workers[i]; |
148 | 147 | w->step = 0; w->pl = &aux; w->data = 0; |
149 | 148 | w->index = aux.index++; |
150 | 149 | } |
151 | 150 |
|
152 | | - tid = (pthread_t*)alloca(n_threads * sizeof(pthread_t)); |
| 151 | + tid = (pthread_t*)calloc(n_threads, sizeof(pthread_t)); |
153 | 152 | for (i = 0; i < n_threads; ++i) pthread_create(&tid[i], 0, ktp_worker, &aux.workers[i]); |
154 | 153 | for (i = 0; i < n_threads; ++i) pthread_join(tid[i], 0); |
| 154 | + free(tid); free(aux.workers); |
155 | 155 |
|
156 | 156 | pthread_mutex_destroy(&aux.mutex); |
157 | 157 | pthread_cond_destroy(&aux.cv); |
|
0 commit comments