Skip to content
Snippets Groups Projects
Commit d2af04a6 authored by Niels Möller's avatar Niels Möller
Browse files

(xalloc): New function. Use instead

of alloca, for better portability.

Rev: src/nettle/examples/nettle-benchmark.c:1.11
parent a45c20c8
No related branches found
No related tags found
No related merge requests found
...@@ -169,12 +169,25 @@ display(const char *name, const char *mode, ...@@ -169,12 +169,25 @@ display(const char *name, const char *mode,
1 / (speed * 1048576.0 / BENCH_BLOCK)); 1 / (speed * 1048576.0 / BENCH_BLOCK));
} }
static void *
xalloc(size_t size)
{
void *p = malloc(size);
if (!p)
{
fprintf(stderr, "Virtual memory exhausted.\n");
abort();
}
return p;
}
static void static void
time_hash(const struct nettle_hash *hash) time_hash(const struct nettle_hash *hash)
{ {
static uint8_t data[BENCH_BLOCK]; static uint8_t data[BENCH_BLOCK];
struct bench_hash_info info; struct bench_hash_info info;
info.ctx = alloca(hash->context_size); info.ctx = xalloc(hash->context_size);
info.update = hash->update; info.update = hash->update;
info.data = data; info.data = data;
...@@ -183,13 +196,15 @@ time_hash(const struct nettle_hash *hash) ...@@ -183,13 +196,15 @@ time_hash(const struct nettle_hash *hash)
display(hash->name, "Update", display(hash->name, "Update",
time_function(bench_hash, &info)); time_function(bench_hash, &info));
free(info.ctx);
} }
static void static void
time_cipher(const struct nettle_cipher *cipher) time_cipher(const struct nettle_cipher *cipher)
{ {
void *ctx = alloca(cipher->context_size); void *ctx = xalloc(cipher->context_size);
uint8_t *key = alloca(cipher->key_size); uint8_t *key = xalloc(cipher->key_size);
static uint8_t data[BENCH_BLOCK]; static uint8_t data[BENCH_BLOCK];
...@@ -226,7 +241,7 @@ time_cipher(const struct nettle_cipher *cipher) ...@@ -226,7 +241,7 @@ time_cipher(const struct nettle_cipher *cipher)
if (cipher->block_size) if (cipher->block_size)
{ {
uint8_t *iv = alloca(cipher->block_size); uint8_t *iv = xalloc(cipher->block_size);
/* Do CBC mode */ /* Do CBC mode */
{ {
...@@ -260,7 +275,10 @@ time_cipher(const struct nettle_cipher *cipher) ...@@ -260,7 +275,10 @@ time_cipher(const struct nettle_cipher *cipher)
display(cipher->name, "CBC decrypt", display(cipher->name, "CBC decrypt",
time_function(bench_cbc_decrypt, &info)); time_function(bench_cbc_decrypt, &info));
} }
free(iv);
} }
free(ctx);
free(key);
} }
#if HAVE_LIBCRYPTO #if HAVE_LIBCRYPTO
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment