diff --git a/Makefile.am b/Makefile.am index 7162b9e..e70d1b5 100644 --- a/Makefile.am +++ b/Makefile.am @@ -4,8 +4,7 @@ dist_doc_DATA = AUTHORS COPYING NEWS README EXTRA_DIST = Doxyfile -convlib_sources = lib/err.compat.h lib/compat.h lib/xmem.c lib/xmem.h \ - lib/hexio.c lib/hexio.h +convlib_sources = lib/err.compat.h lib/compat.h lib/hexio.c lib/hexio.h common_sources = src/midi.c src/midi.h src/sysex.c src/sysex.h \ $(convlib_sources) diff --git a/lib/hexio.c b/lib/hexio.c index 0542f99..27cd2f1 100644 --- a/lib/hexio.c +++ b/lib/hexio.c @@ -26,10 +26,6 @@ #include #include -#ifdef EXIT_ON_ENOMEM -#include -#endif - #define BLOCK_SIZE 512 int @@ -185,10 +181,6 @@ freadha(FILE *f, unsigned char **buffer, size_t *len) while ( (n = freadh(f, buf, sizeof(buf))) > 0 ) { if ( *len < m + n ) { -#ifdef EXIT_ON_ENOMEM - *len += BLOCK_SIZE; - *buffer = xrealloc(*buffer, *len); -#else unsigned char *p; if ( ! (p = realloc(*buffer, *len + BLOCK_SIZE)) ) @@ -196,7 +188,6 @@ freadha(FILE *f, unsigned char **buffer, size_t *len) *buffer = p; *len += BLOCK_SIZE; -#endif } memcpy(*buffer + m, buf, n); m += n; @@ -281,10 +272,6 @@ freadhda(FILE *f, unsigned char **buffer, size_t *len) while ( (n = freadhd(f, buf, sizeof(buf), &s)) > 0 ) { if ( *len < m + n ) { -#ifdef EXIT_ON_ENOMEM - *len += BLOCK_SIZE; - *buffer = xrealloc(*buffer, *len); -#else unsigned char *p; if ( ! (p = realloc(*buffer, *len + BLOCK_SIZE)) ) @@ -292,7 +279,6 @@ freadhda(FILE *f, unsigned char **buffer, size_t *len) *buffer = p; *len += BLOCK_SIZE; -#endif } memcpy(*buffer + m, buf, n); m += n; diff --git a/lib/xmem.c b/lib/xmem.c deleted file mode 100644 index 70a4ac7..0000000 --- a/lib/xmem.c +++ /dev/null @@ -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 . - */ - -#ifdef HAVE_CONFIG_H -#include -#endif - -#include -#include -#include -#include - -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; -} diff --git a/lib/xmem.h b/lib/xmem.h deleted file mode 100644 index e84ae11..0000000 --- a/lib/xmem.h +++ /dev/null @@ -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 . - */ - -#ifndef ICP20110203_XMEM_H -#define ICP20110203_XMEM_H - -#include -#include - -#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 */ diff --git a/src/asysex.c b/src/asysex.c index a364617..e57e072 100644 --- a/src/asysex.c +++ b/src/asysex.c @@ -27,7 +27,6 @@ #include #include -#include #include #define ASYSEX_MODE_QUERY 0x00 diff --git a/src/kmxtool.c b/src/kmxtool.c index d7d827d..4918fe2 100644 --- a/src/kmxtool.c +++ b/src/kmxtool.c @@ -27,7 +27,6 @@ #include #include -#include #include @@ -165,7 +164,8 @@ do_list_programs(midi_io_t *midi) dump.bank = KMX_MICROX_DUMP_ALL; len = kmx_microx_get_dump_size(&dump); - reply = xmalloc(len); + if ( ! (reply = malloc(len)) ) + err(EXIT_FAILURE, "cannot dump data"); if ( (n = kmx_microx_dump(midi, &dump, reply, len)) < 0 ) errx(EXIT_FAILURE, "cannot dump data: %s", kmx_microx_error(midi, n)); @@ -317,7 +317,8 @@ main(int argc, char **argv) errx(EXIT_FAILURE, "invalid dump request: %s", param); n = kmx_microx_get_dump_size(&dump); - data = xmalloc(n); + if ( ! (data = malloc(n)) ) + err(EXIT_FAILURE, "cannot dump data"); if ( (n = kmx_microx_dump(midi, &dump, data, n)) < 0 ) errx(EXIT_FAILURE, "cannot dump data: %s", diff --git a/src/midi.c b/src/midi.c index 0dd1d95..b1aa9ec 100644 --- a/src/midi.c +++ b/src/midi.c @@ -35,8 +35,6 @@ #include #include -#include - #ifdef HAVE_ALSA #include #endif @@ -176,7 +174,9 @@ alsa_midi_open(const char *name) { midi_io_t *midi; - midi = xmalloc(sizeof(*midi)); + if ( ! (midi = malloc(sizeof(*midi))) ) + return NULL; + midi->alsa.in = midi->alsa.out = NULL; midi->error = midi->pos = midi->len = midi->status = 0; @@ -216,13 +216,20 @@ alsa_midi_get_ports(char ***ports, size_t *n, size_t *max) while ( snd_ctl_rawmidi_next_device(ctl, &device) >= 0 && device >= 0 ) { snprintf(name, sizeof(name), "hw:%d,%d", card, device); - if ( *n >= *max ) { + if ( *n + 1 >= *max ) { + char **tmp; + + if ( ! (tmp = realloc(*ports, *max + 10)) ) + goto err; + + *ports = tmp; *max += 10; - *ports = xrealloc(*ports, *max); } - (*ports)[(*n)++] = xstrdup(name); + if ( ((*ports)[*n] = strdup(name)) ) + *n += 1; } +err: snd_ctl_close(ctl); } } @@ -286,7 +293,9 @@ oss_midi_open(const char *name) assert(name != NULL); - midi = xmalloc(sizeof(*midi)); + if ( ! (midi = malloc(sizeof(*midi))) ) + return NULL; + midi->oss.fd = -1; midi->error = midi->pos = midi->len = midi->status = 0; @@ -321,11 +330,19 @@ oss_midi_get_ports(char ***ports, size_t *n, size_t *max) snprintf(name, sizeof(name), "/dev/%s", namelist[k]->d_name); free(namelist[k]); - if ( *n >= *max ) { + if ( *n + 1 >= *max ) { + char **tmp; + + if ( ! (tmp = realloc(*ports, *max + 10)) ) + goto err; + *max += 10; - *ports = xrealloc(*ports, *max); + *ports = tmp; } - (*ports)[(*n)++] = xstrdup(name); + if ( ((*ports)[*n] = strdup(name)) ) + *n += 1; +err: + ; } } } @@ -484,11 +501,10 @@ midi_get_ports(void) oss_midi_get_ports(&ports, &n, &max); #endif - if ( n >= max ) { - max += 1; - ports = xrealloc(ports, max); + if ( ports ) { + assert (n < max); + ports[n] = NULL; } - ports[n++] = NULL; return ports; }