hxt_message.h 4.25 KB
Newer Older
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1
2
/*
 * Marblesbag - Copyright (C) <2010-2018>
3
4
 * <Universite catholique de Louvain (UCL), Belgium>
 * <Universite de Montpellier, France>
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 * 	
 * List of the contributors to the development of SLIM: see AUTHORS file.
 * Description and complete License: see LICENSE file.
 * 	
 * This program (Marblesbag) 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 (see COPYING file).  If not, 
 * see <http://www.gnu.org/licenses/>.
 */

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#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