hxt_message.h 3.3 KB
Newer Older
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
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
#ifndef _MESSAGE_H_
#define _MESSAGE_H_
#include <stdlib.h>

#define STR(x) #x
#define STRINGIFY(x) STR(x)

#define HXT_INFO(fmt,...)                 hxtMessageInfo(__func__, __FILE__, STRINGIFY(__LINE__), fmt, ## __VA_ARGS__ )
#define HXT_WARNING(fmt,...)              hxtMessageWarning(__func__, __FILE__, STRINGIFY(__LINE__), fmt, ## __VA_ARGS__ )

/* print an error message corresponding to a status */
#define HXT_ERROR_MSG(status, fmt,...)    hxtMessageError(status, __func__, __FILE__, STRINGIFY(__LINE__), fmt, ## __VA_ARGS__ )
#define HXT_ERROR(status)                 HXT_ERROR_MSG(status,NULL)

/* give trace message if status is not OK, but does not return */
#define HXT_TRACE_MSG(status, fmt, ...)   hxtMessageTraceError(status, __func__, __FILE__, STRINGIFY(__LINE__), fmt, ## __VA_ARGS__ )
#define HXT_TRACE(status)                 HXT_TRACE_MSG(status, NULL)

/* give trace message and return the error status if not ok */
#define HXT_CHECK_MSG(status, fmt, ...)   do{ HXTStatus _tmp_ = status; if(_tmp_<0) { if(_tmp_>HXT_STATUS_INTERNAL){ HXT_TRACE(_tmp_); } return _tmp_; }}while(0)
#define HXT_CHECK(status)                 HXT_CHECK_MSG(status,NULL)


typedef enum 
{
  // positive values mean a success         => HXT_CHECK does nothing for positive values
  HXT_STATUS_OK                    = 0,


  // ====== ERRORS
  // negatives values means errors

  // Fatal Errors                            => HXT_CHECK give trace message and return
  HXT_STATUS_FAILED                = -1,
  HXT_STATUS_OUT_OF_MEMORY         = -2,
  HXT_STATUS_ERROR                = -3,
  HXT_STATUS_FILE_CANNOT_BE_OPENED = -4,
  HXT_STATUS_ASSERTION_FAILED =-5,
   HXT_STATUS_POINTER_ERROR = -6,
  HXT_STATUS_INTERNAL              = -1024 
}HXTStatus;


HXTStatus  hxtMessageWarning    ( const char* func, const char* file, const char* line, const char *fmt, ...);
HXTStatus  hxtMessageError      ( HXTStatus status, const char* func, const char* file, const char* line, const char *fmt, ...);
HXTStatus  hxtMessageTraceError( HXTStatus status, const char* func, const char* file, const char* line, const char *fmt, ...);
static inline HXTStatus hxtMalloc(void* ptrToPtr, size_t size)
{
  void** p = (void**)ptrToPtr;
  *p = malloc(size);
  if (*p==NULL)
    return HXT_ERROR(HXT_STATUS_OUT_OF_MEMORY);
  return HXT_STATUS_OK;
}


// allocate num element of size size and zero the memory
static inline HXTStatus hxtCalloc(void* ptrToPtr, size_t num, size_t size)
{
  void** p = (void**)ptrToPtr;
  *p = calloc(num, size);
  if (*p==NULL)
    return HXT_ERROR(HXT_STATUS_OUT_OF_MEMORY);
  return HXT_STATUS_OK;
}


static inline HXTStatus hxtFree(void* ptrToPtr)
{
  void** p = (void**)ptrToPtr;
  free(*p);
  *p=NULL;
  return HXT_STATUS_OK;
}


static inline HXTStatus hxtRealloc(void* ptrToPtr, size_t size)
{
  void** p = (void**)ptrToPtr;
  void* newptr = realloc(*p, size);
  if (newptr==NULL && *p!=NULL && size!=0)
    return HXT_ERROR(HXT_STATUS_OUT_OF_MEMORY);
  *p = newptr;
  return HXT_STATUS_OK;
}

/* define SIMD ALIGNMENT */
#ifndef SIMD_ALIGN
#ifdef HXT_HAVE_ALIGNED_MALLOC
#ifdef __AVX512F__
#define SIMD_ALIGN 64
#elif defined(__AVX2__)
#define SIMD_ALIGN 32
#else
#define SIMD_ALIGN 16 /* we align to 16 anyway even if no sse */
#endif
#else
#define SIMD_ALIGN (sizeof(size_t))
#endif
#endif
#define hxtDeclareAligned  __attribute__((aligned(SIMD_ALIGN)))

#endif