Browse Source
Remove malloc wrappers
Remove malloc wrappers
Do not use xmalloc-style die-on-error wrappers for memory allocation functions. Use standard functions and handle out-of-memory errors properly when needed.develop
7 changed files with 35 additions and 165 deletions
-
3Makefile.am
-
14lib/hexio.c
-
79lib/xmem.c
-
52lib/xmem.h
-
1src/asysex.c
-
7src/kmxtool.c
-
44src/midi.c
@ -1,79 +0,0 @@ |
|||
/* |
|||
* xmem - Incenp.org Notch Library: die-on-error memory functions |
|||
* Copyright (C) 2011 Damien Goutte-Gattat |
|||
* |
|||
* 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 3 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, see <http://www.gnu.org/licenses/>. |
|||
*/ |
|||
|
|||
#ifdef HAVE_CONFIG_H |
|||
#include <config.h> |
|||
#endif |
|||
|
|||
#include <xmem.h> |
|||
#include <stdio.h> |
|||
#include <string.h> |
|||
#include <err.h> |
|||
|
|||
void (*xmem_error)(const char *, unsigned int) = NULL; |
|||
|
|||
#define mem_error(f, l) \ |
|||
do { \ |
|||
if ( xmem_error ) \ |
|||
(*xmem_error)((f), (l)); \ |
|||
err(EXIT_FAILURE, "%s, %d", (f), (l)); \ |
|||
} while ( 0 ) |
|||
|
|||
void * |
|||
do_malloc(size_t s, const char *file, unsigned int line) |
|||
{ |
|||
void *p; |
|||
|
|||
if ( ! (p = malloc(s)) && s ) |
|||
mem_error(file, line); |
|||
|
|||
return p; |
|||
} |
|||
|
|||
void * |
|||
do_calloc(size_t n, size_t s, const char *file, unsigned int line) |
|||
{ |
|||
void *p; |
|||
|
|||
if ( ! (p = calloc(n, s)) && n && s ) |
|||
mem_error(file, line); |
|||
|
|||
return p; |
|||
} |
|||
|
|||
void * |
|||
do_realloc(void *p, size_t s, const char *file, unsigned int line) |
|||
{ |
|||
void *np; |
|||
|
|||
if ( ! (np = realloc(p, s)) && s ) |
|||
mem_error(file, line); |
|||
|
|||
return np; |
|||
} |
|||
|
|||
char * |
|||
do_strdup(const char *s, const char *file, unsigned int line) |
|||
{ |
|||
char *dup; |
|||
|
|||
if ( ! (dup = strdup(s)) && s ) |
|||
mem_error(file, line); |
|||
|
|||
return dup; |
|||
} |
@ -1,52 +0,0 @@ |
|||
/* |
|||
* xmem - Incenp.org Notch Library: die-on-error memory functions |
|||
* Copyright (C) 2011 Damien Goutte-Gattat |
|||
* |
|||
* 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 3 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, see <http://www.gnu.org/licenses/>. |
|||
*/ |
|||
|
|||
#ifndef ICP20110203_XMEM_H |
|||
#define ICP20110203_XMEM_H |
|||
|
|||
#include <stdlib.h> |
|||
#include <stdarg.h> |
|||
|
|||
#ifdef __cpluscplus |
|||
extern "C" { |
|||
#endif |
|||
|
|||
extern void (*xmem_error)(const char *, unsigned int); |
|||
|
|||
void * |
|||
do_malloc(size_t, const char *, unsigned int); |
|||
|
|||
void * |
|||
do_calloc(size_t, size_t, const char *, unsigned int); |
|||
|
|||
void * |
|||
do_realloc(void *, size_t, const char *, unsigned int); |
|||
|
|||
char * |
|||
do_strdup(const char *, const char *, unsigned int); |
|||
|
|||
#define xmalloc(s) do_malloc(s, __FILE__, __LINE__) |
|||
#define xcalloc(n,s) do_calloc(n, s, __FILE__, __LINE__) |
|||
#define xrealloc(p,s) do_realloc(p, s, __FILE__, __LINE__) |
|||
#define xstrdup(s) do_strdup(s, __FILE__, __LINE__) |
|||
|
|||
#ifdef __cplusplus |
|||
} |
|||
#endif |
|||
|
|||
#endif /* !ICP20110203_XMEM_H */ |
Write
Preview
Loading…
Cancel
Save
Reference in new issue