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);
|
|
|
|
}
|
2023-05-08 23:11:33 +00:00
|
|
|
|
|
|
|
void *hcmalloc_aligned (const size_t sz, const int align)
|
|
|
|
{
|
|
|
|
// store the original allocated address so we can later use it to free the memory
|
|
|
|
// this is convinient to use because we don't need to store two memory addresses
|
|
|
|
|
|
|
|
const int align1 = align - 1;
|
|
|
|
|
|
|
|
void *ptr1 = hcmalloc (sz + sizeof (void *) + align1);
|
|
|
|
|
|
|
|
void *ptr2 = (void **) ((uintptr_t) (ptr1 + sizeof (void *) + align1) & ~align1);
|
|
|
|
|
|
|
|
((void **) ptr2)[-1] = ptr1;
|
|
|
|
|
|
|
|
return ptr2;
|
|
|
|
}
|
|
|
|
|
|
|
|
void hcfree_aligned (void *ptr)
|
|
|
|
{
|
|
|
|
if (ptr == NULL) return;
|
|
|
|
|
|
|
|
free (((void **) ptr)[-1]);
|
|
|
|
}
|