mirror of
https://github.com/hashcat/hashcat.git
synced 2024-12-23 15:18:16 +00:00
baeb51ad38
calloc is almost equivalent to malloc + memset(0) except that it's faster with big allocations because of OS trickery. It also protects against integer overflow and throws a null pointer on overflow whereas malloc does not.
69 lines
1004 B
C
69 lines
1004 B
C
/**
|
|
* Author......: See docs/credits.txt
|
|
* License.....: MIT
|
|
*/
|
|
|
|
#include "common.h"
|
|
#include "types.h"
|
|
#include "memory.h"
|
|
|
|
void *hccalloc (const size_t nmemb, const size_t sz)
|
|
{
|
|
void *p = calloc (nmemb, sz);
|
|
|
|
if (p == NULL)
|
|
{
|
|
fprintf (stderr, "%s\n", MSG_ENOMEM);
|
|
|
|
return (NULL);
|
|
}
|
|
|
|
return (p);
|
|
}
|
|
|
|
void *hcmalloc (const size_t sz)
|
|
{
|
|
//calloc is faster than malloc with big allocations, so just use that.
|
|
void *p = hccalloc (sz, 1);
|
|
|
|
return (p);
|
|
}
|
|
|
|
void *hcrealloc (void *ptr, const size_t oldsz, const size_t addsz)
|
|
{
|
|
void *p = realloc (ptr, oldsz + addsz);
|
|
|
|
if (p == NULL)
|
|
{
|
|
fprintf (stderr, "%s\n", MSG_ENOMEM);
|
|
|
|
return (NULL);
|
|
}
|
|
|
|
memset ((char *) p + oldsz, 0, addsz);
|
|
|
|
return (p);
|
|
}
|
|
|
|
char *hcstrdup (const char *s)
|
|
{
|
|
const size_t len = strlen (s);
|
|
|
|
char *b = (char *) hcmalloc (len + 1);
|
|
|
|
if (b == NULL) return (NULL);
|
|
|
|
memcpy (b, s, len);
|
|
|
|
b[len] = 0;
|
|
|
|
return (b);
|
|
}
|
|
|
|
void hcfree (void *ptr)
|
|
{
|
|
if (ptr == NULL) return;
|
|
|
|
free (ptr);
|
|
}
|