Browse Source

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
Damien Goutte-Gattat 6 years ago
parent
commit
88e647c493
  1. 3
      Makefile.am
  2. 14
      lib/hexio.c
  3. 79
      lib/xmem.c
  4. 52
      lib/xmem.h
  5. 1
      src/asysex.c
  6. 7
      src/kmxtool.c
  7. 44
      src/midi.c

3
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)

14
lib/hexio.c

@ -26,10 +26,6 @@
#include <errno.h>
#include <ctype.h>
#ifdef EXIT_ON_ENOMEM
#include <xmem.h>
#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;

79
lib/xmem.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;
}

52
lib/xmem.h

@ -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 */

1
src/asysex.c

@ -27,7 +27,6 @@
#include <err.h>
#include <sysex.h>
#include <xmem.h>
#include <hexio.h>
#define ASYSEX_MODE_QUERY 0x00

7
src/kmxtool.c

@ -27,7 +27,6 @@
#include <err.h>
#include <hexio.h>
#include <xmem.h>
#include <microx.h>
@ -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",

44
src/midi.c

@ -35,8 +35,6 @@
#include <stdlib.h>
#include <assert.h>
#include <xmem.h>
#ifdef HAVE_ALSA
#include <alsa/asoundlib.h>
#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;
}

Loading…
Cancel
Save