mirror of
https://github.com/servalproject/serval-dna.git
synced 2025-01-18 02:39:44 +00:00
Refactor emalloc() etc. into mem.c/.h
This commit is contained in:
parent
3686a4ade4
commit
f42292ffc6
31
conf_om.c
31
conf_om.c
@ -26,37 +26,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
#include <stdarg.h>
|
||||
#include <assert.h>
|
||||
|
||||
#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;
|
||||
|
1
log.h
1
log.h
@ -22,6 +22,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <errno.h>
|
||||
|
||||
typedef unsigned int debugflags_t;
|
||||
|
||||
|
46
mem.c
Normal file
46
mem.c
Normal file
@ -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 <string.h>
|
||||
#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));
|
||||
}
|
46
mem.h
Normal file
46
mem.h
Normal file
@ -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 <sys/types.h>
|
||||
#include "log.h"
|
||||
|
||||
/* Equivalent to malloc(3), but logs an error before returning NULL.
|
||||
*
|
||||
* @author Andrew Bettison <andrew@servalproject.com>
|
||||
*/
|
||||
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 <andrew@servalproject.com>
|
||||
*/
|
||||
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
|
Loading…
Reference in New Issue
Block a user