1
mirror of https://github.com/jedisct1/libsodium.git synced 2024-12-19 18:15:18 -07:00

Use a simple memory pool for benchmarks

In the test suite, a significant amount of time is spent in memory
allocations. A memory pool helps achieve more relevant results with
less iterations.
This commit is contained in:
Frank Denis 2017-12-16 13:05:49 +01:00
parent 77e7d88d89
commit 18d5940bc6
2 changed files with 58 additions and 0 deletions

View File

@ -42,6 +42,60 @@ int xmain(void);
# define ITERATIONS 128
# endif
struct {
void *pnt;
size_t size;
} mempool[1024];
static size_t mempool_idx;
static __attribute__((malloc)) void *mempool_alloc(size_t size)
{
size_t i;
if (size >= (size_t) 0x80000000 - (size_t) 0x00000fff) {
return NULL;
}
size = (size + (size_t) 0x00000fff) & ~ (size_t) 0x00000fff;
for (i = 0U; i < mempool_idx; i++) {
if (mempool[i].size >= (size | (size_t) 0x80000000)) {
mempool[i].size &= ~ (size_t) 0x80000000;
return mempool[i].pnt;
}
}
if (mempool_idx >= sizeof mempool / sizeof mempool[0]) {
return NULL;
}
mempool[mempool_idx].size = size;
return mempool[mempool_idx++].pnt = malloc(size);
}
static void mempool_free(void *pnt)
{
size_t i;
for (i = 0U; i < mempool_idx; i++) {
if (mempool[i].pnt == pnt) {
if ((mempool[i].size & (size_t) 0x80000000) != (size_t) 0x0) {
break;
}
mempool[i].size |= (size_t) 0x80000000;
return;
}
}
abort();
}
static __attribute__((malloc)) void *mempool_allocarray(size_t count, size_t size)
{
if (count > (size_t) 0U && size >= (size_t) SIZE_MAX / count) {
return NULL;
}
return mempool_alloc(count * size);
}
#define sodium_malloc(X) mempool_alloc(X)
#define sodium_free(X) mempool_free(X)
#define sodium_allocarray(X, Y) mempool_allocarray((X), (Y))
static unsigned long long now(void)
{
struct timeval tp;

View File

@ -12,6 +12,10 @@
# warning The sodium_utils2 test is expected to fail with address sanitizer
#endif
#undef sodium_malloc
#undef sodium_free
#undef sodium_allocarray
__attribute__((noreturn)) static void
segv_handler(int sig)
{