2016-09-05 19:47:26 +00:00
|
|
|
/**
|
2016-09-11 20:20:15 +00:00
|
|
|
* Author......: See docs/credits.txt
|
2016-09-05 19:47:26 +00:00
|
|
|
* License.....: MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
2016-10-10 09:03:11 +00:00
|
|
|
#include "types.h"
|
2016-09-05 19:47:26 +00:00
|
|
|
#include "memory.h"
|
|
|
|
|
2016-11-20 21:54:52 +00:00
|
|
|
void *hccalloc (const size_t nmemb, const size_t sz)
|
2016-09-05 19:47:26 +00:00
|
|
|
{
|
2016-10-10 09:03:11 +00:00
|
|
|
void *p = calloc (nmemb, sz);
|
2016-09-05 19:47:26 +00:00
|
|
|
|
|
|
|
if (p == NULL)
|
|
|
|
{
|
2016-11-20 21:54:52 +00:00
|
|
|
fprintf (stderr, "%s\n", MSG_ENOMEM);
|
2016-09-05 19:47:26 +00:00
|
|
|
|
2016-10-13 08:07:04 +00:00
|
|
|
return (NULL);
|
2016-09-05 19:47:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
2016-11-20 21:54:52 +00:00
|
|
|
void *hcmalloc (const size_t sz)
|
2016-09-05 19:47:26 +00:00
|
|
|
{
|
2017-10-02 00:46:59 +00:00
|
|
|
//calloc is faster than malloc with big allocations, so just use that.
|
|
|
|
void *p = hccalloc (sz, 1);
|
2016-09-05 19:47:26 +00:00
|
|
|
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
2016-11-20 21:54:52 +00:00
|
|
|
void *hcrealloc (void *ptr, const size_t oldsz, const size_t addsz)
|
2016-09-05 19:47:26 +00:00
|
|
|
{
|
2016-10-10 09:03:11 +00:00
|
|
|
void *p = realloc (ptr, oldsz + addsz);
|
2016-09-05 19:47:26 +00:00
|
|
|
|
|
|
|
if (p == NULL)
|
|
|
|
{
|
2016-11-20 21:54:52 +00:00
|
|
|
fprintf (stderr, "%s\n", MSG_ENOMEM);
|
2016-09-05 19:47:26 +00:00
|
|
|
|
2016-10-13 08:07:04 +00:00
|
|
|
return (NULL);
|
2016-09-05 19:47:26 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 09:03:11 +00:00
|
|
|
memset ((char *) p + oldsz, 0, addsz);
|
2016-09-05 19:47:26 +00:00
|
|
|
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
2016-11-20 21:54:52 +00:00
|
|
|
char *hcstrdup (const char *s)
|
2016-09-05 19:47:26 +00:00
|
|
|
{
|
|
|
|
const size_t len = strlen (s);
|
|
|
|
|
2016-11-20 21:54:52 +00:00
|
|
|
char *b = (char *) hcmalloc (len + 1);
|
2016-10-10 09:03:11 +00:00
|
|
|
|
2016-10-13 08:07:04 +00:00
|
|
|
if (b == NULL) return (NULL);
|
|
|
|
|
|
|
|
memcpy (b, s, len);
|
2016-09-05 19:47:26 +00:00
|
|
|
|
2016-10-10 09:03:11 +00:00
|
|
|
b[len] = 0;
|
2016-09-05 19:47:26 +00:00
|
|
|
|
|
|
|
return (b);
|
|
|
|
}
|
2016-10-10 09:03:11 +00:00
|
|
|
|
|
|
|
void hcfree (void *ptr)
|
|
|
|
{
|
|
|
|
if (ptr == NULL) return;
|
|
|
|
|
|
|
|
free (ptr);
|
|
|
|
}
|