mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-11 05:33:47 +01:00
Add tor_calloc
This commit is contained in:
parent
ff0059b924
commit
806e0f7e19
@ -172,6 +172,35 @@ _tor_malloc_zero(size_t size DMALLOC_PARAMS)
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Allocate a chunk of <b>nmemb</b>*<b>size</b> bytes of memory, fill
|
||||
* the memory with zero bytes, and return a pointer to the result.
|
||||
* Log and terminate the process on error. (Same as
|
||||
* calloc(<b>nmemb</b>,<b>size</b>), but never returns NULL.)
|
||||
*
|
||||
* XXXX This implementation probably asserts in cases where it could
|
||||
* work, because it only tries dividing SIZE_MAX by size (according to
|
||||
* the calloc(3) man page, the size of an element of the nmemb-element
|
||||
* array to be allocated), not by nmemb (which could in theory be
|
||||
* smaller than size). Don't do that then.
|
||||
*/
|
||||
void *
|
||||
_tor_calloc(size_t nmemb, size_t size DMALLOC_PARAMS)
|
||||
{
|
||||
/* You may ask yourself, "wouldn't it be smart to use calloc instead of
|
||||
* malloc+memset? Perhaps libc's calloc knows some nifty optimization trick
|
||||
* we don't!" Indeed it does, but its optimizations are only a big win when
|
||||
* we're allocating something very big (it knows if it just got the memory
|
||||
* from the OS in a pre-zeroed state). We don't want to use tor_malloc_zero
|
||||
* for big stuff, so we don't bother with calloc. */
|
||||
void *result;
|
||||
size_t max_nmemb = (size == 0) ? SIZE_MAX : SIZE_MAX/size;
|
||||
|
||||
tor_assert(nmemb < max_nmemb);
|
||||
|
||||
result = _tor_malloc_zero((nmemb * size) DMALLOC_FN_ARGS);
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Change the size of the memory block pointed to by <b>ptr</b> to <b>size</b>
|
||||
* bytes long; return the new memory block. On error, log and
|
||||
* terminate. (Like realloc(ptr,size), but never returns NULL.)
|
||||
|
@ -73,6 +73,7 @@
|
||||
void *_tor_malloc(size_t size DMALLOC_PARAMS) ATTR_MALLOC;
|
||||
void *_tor_malloc_zero(size_t size DMALLOC_PARAMS) ATTR_MALLOC;
|
||||
void *_tor_malloc_roundup(size_t *size DMALLOC_PARAMS) ATTR_MALLOC;
|
||||
void *_tor_calloc(size_t nmemb, size_t size DMALLOC_PARAMS) ATTR_MALLOC;
|
||||
void *_tor_realloc(void *ptr, size_t size DMALLOC_PARAMS);
|
||||
char *_tor_strdup(const char *s DMALLOC_PARAMS) ATTR_MALLOC ATTR_NONNULL((1));
|
||||
char *_tor_strndup(const char *s, size_t n DMALLOC_PARAMS)
|
||||
@ -107,6 +108,7 @@ extern int dmalloc_free(const char *file, const int line, void *pnt,
|
||||
|
||||
#define tor_malloc(size) _tor_malloc(size DMALLOC_ARGS)
|
||||
#define tor_malloc_zero(size) _tor_malloc_zero(size DMALLOC_ARGS)
|
||||
#define tor_calloc(nmemb,size) _tor_calloc(nmemb, size DMALLOC_ARGS)
|
||||
#define tor_malloc_roundup(szp) _tor_malloc_roundup(szp DMALLOC_ARGS)
|
||||
#define tor_realloc(ptr, size) _tor_realloc(ptr, size DMALLOC_ARGS)
|
||||
#define tor_strdup(s) _tor_strdup(s DMALLOC_ARGS)
|
||||
|
Loading…
Reference in New Issue
Block a user