File: utils.h

package info (click to toggle)
kcov 4-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 268 kB
  • sloc: ansic: 1,996; makefile: 7
file content (96 lines) | stat: -rw-r--r-- 2,260 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
 * Copyright (C) 2010 Simon Kagstrom
 *
 * See COPYING for license details
 */
#ifndef __UTILS_H__
#define __UTILS_H__

#include <assert.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>

#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*x))

#define BUG_ON(cond)

#define error(x...) do \
  { \
    fprintf(stderr, "=============ERROR ERROR ERROR===========\n"); \
    fprintf(stderr, "%s:%d: ", __FILE__, __LINE__); fprintf(stderr, x); \
    fprintf(stderr, "=========================================\n"); \
    assert(0); \
    exit(1); \
  } while(0)

#define warning(x...) do \
  { \
    fprintf(stderr, "==============WARNING WARNING============\n"); \
    fprintf(stderr, "%s:%d: ", __FILE__, __LINE__); fprintf(stderr, x); \
    fprintf(stderr, "=========================================\n"); \
  } while(0)

#define panic(x...) do { error(x); abort(); } while(0)

#define panic_if(cond, x...) \
  do { if ((cond)) panic(x); } while(0)

#define warning_if(cond, x...) \
  do { if ((cond)) warning(x); } while(0)

static inline char *xstrdup(const char *s)
{
  char *out = strdup(s);

  panic_if(!out, "strdup failed");

  return out;
}

static inline void *xmalloc(size_t sz)
{
  void *out = malloc(sz);

  panic_if(!out, "malloc failed");
  memset(out, 0, sz);

  return out;
}

static inline void *xrealloc(void *ptr, size_t sz)
{
  void *out = realloc(ptr, sz);

  panic_if(!out, "malloc failed");

  return out;
}

#define xsnprintf(buf, size, fmt, x...) do { \
    int r = snprintf(buf, size, fmt, x); \
    panic_if(r < 0 || r >= (int)(size), "snprintf failed for %s with %d\n", fmt, r); \
} while(0)


extern int write_file(const void *data, size_t len, const char *fmt, ...);

extern void *read_file(size_t *out_size, const char *fmt, ...);

extern const char *dir_concat(const char *dir, const char *filename);

#define xwrite_file(data, len, dir...) do { \
	int r = write_file(data, len, dir); \
	panic_if (r != 0, "write_file failed with %d\n", r); \
} while(0)

extern int file_exists(const char *path);

extern int concat_files(const char *dst, const char *file_a, const char *file_b);

unsigned long get_aligned(unsigned long addr);

unsigned long get_aligned_4b(unsigned long addr);

#endif /* __UTILS_H__ */