diff --git a/conf_om.c b/conf_om.c index d17b9fb4..7dcdd421 100644 --- a/conf_om.c +++ b/conf_om.c @@ -26,37 +26,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include #include +#include "mem.h" #include "str.h" #include "strbuf.h" -#include "strbuf_helpers.h" #include "log.h" #include "config.h" -void *emalloc(size_t len) -{ - char *new = malloc(len + 1); - if (!new) { - WHYF_perror("malloc(%lu)", (long)len); - return NULL; - } - return new; -} - -char *strn_emalloc(const char *str, size_t len) -{ - char *new = emalloc(len + 1); - if (new) { - strncpy(new, str, len); - new[len] = '\0'; - } - return new; -} - -char *str_emalloc(const char *str) -{ - return strn_emalloc(str, strlen(str)); -} - static const char *cf_find_keyend(const char *const key, const char *const fullkeyend) { const char *s = key; @@ -111,7 +86,7 @@ static int cf_om_make_child(struct cf_om_node **const parentp, const char *const for (j = (*parentp)->nodc - 1; j > i; --j) (*parentp)->nodv[j] = (*parentp)->nodv[j-1]; (*parentp)->nodv[i] = child; - if (!(child->fullkey = strn_emalloc(fullkey, keyend - fullkey))) { + if (!(child->fullkey = strn_edup(fullkey, keyend - fullkey))) { free(child); return -1; } @@ -203,7 +178,7 @@ struct cf_om_node *cf_parse_to_om(const char *source, const char *buf, size_t le continue; } ++p; - if (!(node->text = strn_emalloc(p, lend - p))) + if (!(node->text = strn_edup(p, lend - p))) break; // out of memory node->source = source; node->line_number = lineno; diff --git a/log.h b/log.h index aa976f09..3b7ac830 100644 --- a/log.h +++ b/log.h @@ -22,6 +22,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include #include #include +#include typedef unsigned int debugflags_t; diff --git a/mem.c b/mem.c new file mode 100644 index 00000000..02ad8a9a --- /dev/null +++ b/mem.c @@ -0,0 +1,46 @@ +/* +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 +#include "mem.h" + +void *_emalloc(struct __sourceloc __whence, size_t len) +{ + char *new = malloc(len + 1); + if (!new) { + WHYF_perror("malloc(%lu)", (long)len); + return NULL; + } + return new; +} + +char *_strn_edup(struct __sourceloc __whence, const char *str, size_t len) +{ + char *new = _emalloc(__whence, len + 1); + if (new) { + strncpy(new, str, len); + new[len] = '\0'; + } + return new; +} + +char *_str_edup(struct __sourceloc __whence, const char *str) +{ + return _strn_edup(__whence, str, strlen(str)); +} diff --git a/mem.h b/mem.h new file mode 100644 index 00000000..355b3b97 --- /dev/null +++ b/mem.h @@ -0,0 +1,46 @@ +/* +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. +*/ + +#ifndef __SERVALDNA__MEM_H +#define __SERVALDNA__MEM_H + +#include +#include "log.h" + +/* Equivalent to malloc(3), but logs an error before returning NULL. + * + * @author Andrew Bettison + */ +void *_emalloc(struct __sourceloc, size_t bytes); + +/* Equivalent to strdup(3)/strndup(3), but logs an error before returning NULL. + * + * Why aren't these in str.h? Because str.c must not depend on log.h/log.c! str.c is used in link + * contexts where log.c is not present. + * + * @author Andrew Bettison + */ +char *_str_edup(struct __sourceloc, const char *str); +char *_strn_edup(struct __sourceloc, const char *str, size_t len); + +#define emalloc(bytes) _emalloc(__HERE__, (bytes)) +#define str_edup(str) _str_edup(__HERE__, (str)) +#define strn_edup(str, len) _strn_edup(__HERE__, (str), (len)) + +#endif // __SERVALDNA__MEM_H