diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/util/samba_util.h | 4 | ||||
-rw-r--r-- | lib/util/util.c | 28 |
2 files changed, 32 insertions, 0 deletions
diff --git a/lib/util/samba_util.h b/lib/util/samba_util.h index 9c12f6e8dd..0c3fd1aeaf 100644 --- a/lib/util/samba_util.h +++ b/lib/util/samba_util.h @@ -751,6 +751,10 @@ _PUBLIC_ void *realloc_array(void *ptr, size_t el_size, unsigned count, bool fre void *malloc_array(size_t el_size, unsigned int count); +void *memalign_array(size_t el_size, size_t align, unsigned int count); + +void *calloc_array(size_t size, size_t nmemb); + /* The following definitions come from lib/util/fsusage.c */ diff --git a/lib/util/util.c b/lib/util/util.c index 960bda0f9b..89d5fc3611 100644 --- a/lib/util/util.c +++ b/lib/util/util.c @@ -646,6 +646,34 @@ void *malloc_array(size_t el_size, unsigned int count) return realloc_array(NULL, el_size, count, false); } +/**************************************************************************** + Type-safe memalign +****************************************************************************/ + +void *memalign_array(size_t el_size, size_t align, unsigned int count) +{ + if (count*el_size >= MAX_MALLOC_SIZE) { + return NULL; + } + + return memalign(align, el_size*count); +} + +/**************************************************************************** + Type-safe calloc. +****************************************************************************/ + +void *calloc_array(size_t size, size_t nmemb) +{ + if (nmemb >= MAX_MALLOC_SIZE/size) { + return NULL; + } + if (size == 0 || nmemb == 0) { + return NULL; + } + return calloc(nmemb, size); +} + /** Trim the specified elements off the front and back of a string. **/ |