aboutsummaryrefslogtreecommitdiff
path: root/backend/defines.h
blob: 9940b7b49cc73216c2f458304e46e1f9ac0e6ff7 (plain)
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
97
98
99
100
101
102
103
104
/*  defines.h -- a grab bag of preprocessor utilities
 *  Copyright (C) 2019 SEIKO EPSON Corporation
 *
 *  License: EPSON END USER SOFTWARE LICENSE
 *  Author : SEIKO EPSON Corporation
 *
 *  This file is part of Image Scan! for Linux.
 *  It is distributed under the terms of the EPSON END USER SOFTWARE LICENSE.
 *
 *  You should have received a verbatim copy of the EPSON END USER SOFTWARE
 *  LICENSE along with the software.
 */


#ifndef defines_h
#define defines_h

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef __cplusplus
extern "C"
{
#endif

/* Assorted constants.
 */

#define MM_PER_INCH     25.4    /* exactly */


#ifndef __cplusplus
/*! A C++ Boolean type and corresponding keywords for our C code.
 */
typedef enum {
  false = 0,
  true
} bool;
#endif


/*  Run-time contract validation.
 */
#include <stdlib.h>
#include "message.h"

#define _assert(type,condition)                                 \
  if (!(condition))                                             \
  {                                                             \
    err_fatal ("failed: %s (%s)", type, #condition);            \
    exit (EXIT_FAILURE);                                        \
  }
#define require(condition)      _assert ("require", condition)
#define promise(condition)      _assert ("promise", condition)


/*  "Typed" memory allocation convenience wrappers.
 *  These are meant to make the invocations consistent and take care of
 *  the casting for you.
 */
#include <alloca.h>
#include <stdlib.h>

#define t_alloca(sz,t)          ((t *) alloca ((sz) * sizeof (t)))
#define t_calloc(sz,t)          ((t *) calloc ((sz) , sizeof (t)))
#define t_malloc(sz,t)          ((t *) malloc ((sz) * sizeof (t)))
#define t_realloc(p,sz,t)       ((t *) realloc ((p), (sz) * sizeof (t)))

/*  Compute sizes of _statically_ allocated arrays easily.
 */
#define num_of(p)               (sizeof (p) / sizeof (*p))

/*  Safely release acquired resources.
 *  The const_delete() is meant for those rare cases where you need to
 *  clean up const t* typed memory areas.
 */
#define delete(p)               do { if (p) free (p); p = 0; } while (0)
#define const_delete(p,t)       do { if (p) free ((t) p); p = 0; } while (0)


/* Portable path and file name component separators.
 */
#ifdef __unix
#define PATH_SEP_STR ":"
#define PATH_SEP PATH_SEP_STR[0]
#define FILE_SEP_STR "/"
#define FILE_SEP FILE_SEP_STR[0]
#else
#define PATH_SEP_STR ";"
#define PATH_SEP PATH_SEP_STR[0]
#define FILE_SEP_STR "\\"
#define FILE_SEP FILE_SEP_STR[0]
#endif


  typedef unsigned char byte;


#ifdef __cplusplus
}       /* extern "C" */
#endif

#endif  /* !defined (defines_h) */