serval-dna/mem.c

63 lines
1.6 KiB
C
Raw Permalink Normal View History

2012-11-29 06:42:43 +00:00
/*
Serval DNA memory management
Copyright (C) 2012 Serval Project Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <string.h>
#include "mem.h"
void *_emalloc(struct __sourceloc __whence, size_t bytes)
2012-11-29 06:42:43 +00:00
{
char *new = malloc(bytes);
2012-11-29 06:42:43 +00:00
if (!new) {
WHYF_perror("malloc(%lu)", (long)bytes);
2012-11-29 06:42:43 +00:00
return NULL;
}
return new;
}
void *_erealloc(struct __sourceloc __whence, void *ptr, size_t bytes)
{
char *new = realloc(ptr, bytes);
if (!new) {
WHYF_perror("realloc(%p, %lu)", ptr, (unsigned long)bytes);
return NULL;
}
return new;
}
void *_emalloc_zero(struct __sourceloc __whence, size_t bytes)
{
char *new = _emalloc(__whence, bytes);
if (new)
memset(new, 0, bytes);
return new;
}
2012-11-29 06:42:43 +00:00
char *_strn_edup(struct __sourceloc __whence, const char *str, size_t len)
{
char *new = _emalloc(__whence, len + 1);
2013-11-28 07:15:32 +00:00
if (new)
strncpy(new, str, len)[len] = '\0';
2012-11-29 06:42:43 +00:00
return new;
}
char *_str_edup(struct __sourceloc __whence, const char *str)
{
return _strn_edup(__whence, str, strlen(str));
}