diff options
author | Werner Koch <wk@gnupg.org> | 2017-11-23 19:15:41 +0100 |
---|---|---|
committer | Werner Koch <wk@gnupg.org> | 2017-11-24 10:10:18 +0100 |
commit | 1f6b2f6099ebcfd785e2d2ae0aeca810394dbbac (patch) | |
tree | 3682f438908521e87b1bdf5c6bc5b2add2dc85ab /src/secmem.c | |
parent | 0abd1031bc0d889f21ddbf4ced8764d2191075fe (diff) | |
download | libgcrypt-1f6b2f6099ebcfd785e2d2ae0aeca810394dbbac.tar.gz |
api: Add GCRYCTL_AUTO_EXPAND_SECMEM.
* src/gcrypt.h.in (GCRYCTL_AUTO_EXPAND_SECMEM): New enum.
* src/global.c (_gcry_vcontrol): Implement that.
* src/secmem.c (auto_expand): New var.
(_gcry_secmem_set_auto_expand): New.
(_gcry_secmem_malloc_internal): Act upon AUTO_EXPAND.
--
GnuPG-bug-id: 3530
Signed-off-by: Werner Koch <wk@gnupg.org>
Diffstat (limited to 'src/secmem.c')
-rw-r--r-- | src/secmem.c | 26 |
1 files changed, 23 insertions, 3 deletions
diff --git a/src/secmem.c b/src/secmem.c index f7ad1f66..79c135ff 100644 --- a/src/secmem.c +++ b/src/secmem.c @@ -91,7 +91,7 @@ typedef struct pooldesc_s static pooldesc_t mainpool; -/* A couple of flags whith some being set early. */ +/* A couple of flags with some being set early. */ static int disable_secmem; static int show_warning; static int not_locked; @@ -99,6 +99,8 @@ static int no_warning; static int suspend_warning; static int no_mlock; static int no_priv_drop; +static unsigned int auto_expand; + /* Lock protecting accesses to the memory pools. */ GPGRT_LOCK_DEFINE (secmem_lock); @@ -458,6 +460,24 @@ init_pool (pooldesc_t *pool, size_t n) mb->flags = 0; } + +/* Enable overflow pool allocation in all cases. CHUNKSIZE is a hint + * on how large to allocate overflow pools. */ +void +_gcry_secmem_set_auto_expand (unsigned int chunksize) +{ + /* Round up to a multiple of the STANDARD_POOL_SIZE. */ + chunksize = ((chunksize + (2*STANDARD_POOL_SIZE) - 1) + / STANDARD_POOL_SIZE ) * STANDARD_POOL_SIZE; + if (chunksize < STANDARD_POOL_SIZE) /* In case of overflow. */ + chunksize = STANDARD_POOL_SIZE; + + SECMEM_LOCK; + auto_expand = chunksize; + SECMEM_UNLOCK; +} + + void _gcry_secmem_set_flags (unsigned flags) { @@ -617,7 +637,7 @@ _gcry_secmem_malloc_internal (size_t size, int xhint) /* If we are called from xmalloc style function resort to the * overflow pools to return memory. We don't do this in FIPS mode, * though. */ - if (xhint && !fips_mode ()) + if ((xhint || auto_expand) && !fips_mode ()) { for (pool = pool->next; pool; pool = pool->next) { @@ -635,7 +655,7 @@ _gcry_secmem_malloc_internal (size_t size, int xhint) pool = calloc (1, sizeof *pool); if (!pool) return NULL; /* Not enough memory for a new pool descriptor. */ - pool->size = STANDARD_POOL_SIZE; + pool->size = auto_expand? auto_expand : STANDARD_POOL_SIZE; pool->mem = malloc (pool->size); if (!pool->mem) return NULL; /* Not enough memory available for a new pool. */ |