Replace uses of alloca() with malloc()

Gets rid of more #ifdef magic.
This commit is contained in:
Lassi Kortela 2019-08-09 23:45:10 +03:00
parent 41f6dbc1a2
commit 8a7ce61575
2 changed files with 17 additions and 27 deletions

View File

@ -11,9 +11,6 @@
#include "dtypes.h"
#include "bitvector.h"
// greater than this # of words we use malloc instead of alloca
#define MALLOC_CUTOFF 2000
#ifdef __INTEL_COMPILER
#define count_bits(b) _popcnt32(b)
#else
@ -385,7 +382,7 @@ void bitvector_reverse(uint32_t *b, uint32_t offs, uint32_t nbits)
return;
nw = (offs + nbits + 31) >> 5;
temp = (nw > MALLOC_CUTOFF) ? malloc(nw * 4) : alloca(nw * 4);
temp = malloc(nw * 4);
for (i = 0; i < nw / 2; i++) {
temp[i] = bitreverse(b[nw - i - 1]);
temp[nw - i - 1] = bitreverse(b[i]);
@ -395,8 +392,7 @@ void bitvector_reverse(uint32_t *b, uint32_t offs, uint32_t nbits)
tail = (offs + nbits) & 31;
bitvector_copy(b, offs, temp, (32 - tail) & 31, nbits);
if (nw > MALLOC_CUTOFF)
free(temp);
free(temp);
}
uint64_t bitvector_count(uint32_t *b, uint32_t offs, uint64_t nbits)
@ -527,8 +523,7 @@ static void adjust_offset_to(uint32_t *dest, uint32_t *src, uint32_t nw,
uint32_t boffs, uint32_t nbits) \
{ \
uint32_t nw = (doffs + nbits + 31) >> 5; \
uint32_t *temp = \
nw > MALLOC_CUTOFF ? malloc((nw + 1) * 4) : alloca((nw + 1) * 4); \
uint32_t *temp = malloc((nw + 1) * 4); \
uint32_t i, anw, bnw; \
if (aoffs == boffs) { \
anw = (aoffs + nbits + 31) >> 5; \
@ -548,8 +543,7 @@ static void adjust_offset_to(uint32_t *dest, uint32_t *src, uint32_t nw,
for (i = 0; i < anw; i++) \
temp[i] = OP(a[i], b[i]); \
bitvector_copy(dest, doffs, temp, aoffs, nbits); \
if (nw > MALLOC_CUTOFF) \
free(temp); \
free(temp); \
}
#define BV_AND(a, b) ((a) & (b))

View File

@ -23,17 +23,13 @@
#include <wchar.h>
#include <wctype.h>
#include "dtypes.h"
#ifdef WIN32
#include <malloc.h>
#define snprintf _snprintf
#else
#if !defined(__FreeBSD__) && !defined(__OpenBSD__)
#include <alloca.h>
#endif /* __FreeBSD__ && __OpenBSD__ */
#endif
#include "dtypes.h"
#include "utf8.h"
static const uint32_t offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL,
@ -618,26 +614,26 @@ int u8_is_locale_utf8(const char *locale)
size_t u8_vprintf(const char *fmt, va_list ap)
{
size_t cnt, sz = 0, nc, needfree = 0;
size_t nc;
char *buf;
uint32_t *wcs;
int cnt;
sz = 512;
buf = (char *)alloca(sz);
cnt = vsnprintf(buf, sz, fmt, ap);
if ((ssize_t)cnt < 0)
if ((cnt = vsnprintf(0, 0, fmt, ap)) < 0) {
return 0;
if (cnt >= sz) {
buf = (char *)malloc(cnt + 1);
needfree = 1;
}
if (!(buf = calloc(cnt + 1, 1))) {
vsnprintf(buf, cnt + 1, fmt, ap);
}
wcs = (uint32_t *)alloca((cnt + 1) * sizeof(uint32_t));
if (!(wcs = calloc(cnt + 1, sizeof(uint32_t)))) {
free(buf);
return 0;
}
nc = u8_toucs(wcs, cnt + 1, buf, cnt);
wcs[nc] = 0;
printf("%ls", (wchar_t *)wcs);
if (needfree)
free(buf);
free(wcs);
free(buf);
return nc;
}