Browse Source

dtc: Move some functions to util.[ch]

Now that we have a util.[ch] file shared between dtc and
convert-dtsv0, move some functions which are currently duplicated in
the two to util files.  Specifically we move the die(), xmalloc() and
xrealloc() functions.

While we're at it, add standard double-include protection to util.h

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
main
David Gibson 16 years ago committed by Jon Loeliger
parent
commit
b2b4990bbf
  1. 20
      convert-dtsv0-lexer.l
  2. 30
      dtc.h
  3. 35
      util.h

20
convert-dtsv0-lexer.l

@ -52,26 +52,6 @@ static char *last_name; /* = NULL */


#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))


static inline void __attribute__((noreturn)) die(char * str, ...)
{
va_list ap;

va_start(ap, str);
fprintf(stderr, "FATAL ERROR: ");
vfprintf(stderr, str, ap);
exit(1);
}

static inline void *xmalloc(size_t len)
{
void *new = malloc(len);

if (! new)
die("malloc() failed\n");

return new;
}

const struct { const struct {
const char *pattern; const char *pattern;
int obase, width; int obase, width;

30
dtc.h

@ -53,36 +53,6 @@ extern int reservenum; /* Number of memory reservation slots */
extern int minsize; /* Minimum blob size */ extern int minsize; /* Minimum blob size */
extern int padsize; /* Additional padding to blob */ extern int padsize; /* Additional padding to blob */


static inline void __attribute__((noreturn)) die(char * str, ...)
{
va_list ap;

va_start(ap, str);
fprintf(stderr, "FATAL ERROR: ");
vfprintf(stderr, str, ap);
exit(1);
}

static inline void *xmalloc(size_t len)
{
void *new = malloc(len);

if (! new)
die("malloc() failed\n");

return new;
}

static inline void *xrealloc(void *p, size_t len)
{
void *new = realloc(p, len);

if (! new)
die("realloc() failed (len=%d)\n", len);

return new;
}

typedef uint32_t cell_t; typedef uint32_t cell_t;





35
util.h

@ -1,3 +1,6 @@
#ifndef _UTIL_H
#define _UTIL_H

/* /*
* Copyright 2008 Jon Loeliger, Freescale Semiconductor, Inc. * Copyright 2008 Jon Loeliger, Freescale Semiconductor, Inc.
* *
@ -17,4 +20,36 @@
* USA * USA
*/ */


static inline void __attribute__((noreturn)) die(char * str, ...)
{
va_list ap;

va_start(ap, str);
fprintf(stderr, "FATAL ERROR: ");
vfprintf(stderr, str, ap);
exit(1);
}

static inline void *xmalloc(size_t len)
{
void *new = malloc(len);

if (!new)
die("malloc() failed\n");

return new;
}

static inline void *xrealloc(void *p, size_t len)
{
void *new = realloc(p, len);

if (!new)
die("realloc() failed (len=%d)\n", len);

return new;
}

extern char *xstrdup(const char *s); extern char *xstrdup(const char *s);

#endif /* _UTIL_H */

Loading…
Cancel
Save