forked from WojciechMula/pyDAWG
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommon.h
More file actions
129 lines (111 loc) · 2.91 KB
/
Copy pathcommon.h
File metadata and controls
129 lines (111 loc) · 2.91 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
This is part of pydawg Python module.
common definitions and includes
Author : Wojciech Muła, wojciech_mula@poczta.onet.pl
WWW : http://0x80.pl/proj/pydawg/
License : public domain
*/
#ifndef dawg_common_h_included__
#define dawg_common_h_included__
#include <Python.h>
#include <structmember.h> // PyMemberDef
#include <iso646.h>
// setup supported character set
#ifdef DAWG_UNICODE
# ifdef Py_UNICODE_WIDE
// Python use UCS-4
# define DAWG_LETTER_TYPE Py_UNICODE
# define DAWG_LETTER_SIZE 4
# else
// Python use UCS-2
# define DAWG_LETTER_TYPE Py_UNICODE
# define DAWG_LETTER_SIZE 2
# endif
#else
// only bytes are supported
# define DAWG_LETTER_TYPE uint8_t
# define DAWG_LETTER_SIZE 1
#endif
#ifdef __GNUC__
# define LIKELY(x) __builtin_expect(x, 1)
# define UNLIKELY(x) __builtin_expect(x, 0)
# define ALWAYS_INLINE __attribute__((always_inline))
# define PURE __attribute__((pure))
# define UNUSED __attribute__((__unused__))
#else
# define LIKELY(x) x
# define UNLIKELY(x) x
# define ALWAYS_INLINE
# define PURE
# define UNUSED
#endif
#ifdef DEBUG
# include <assert.h>
# define ASSERT(expr) do {if (!(expr)) {printf("%s:%s:%d - '%s' failed!\n", __FILE__, __FUNCTION__, __LINE__, #expr); abort();} }while(0)
#else
# define ASSERT(expr)
#endif
#if defined(__SIZEOF_POINTER__)
# define PYDAWG_POINTER_SIZE __SIZEOF_POINTER__
#elif defined(_WIN64)
# define PYDAWG_POINTER_SIZE 8
#elif defined(_WIN32)
# define PYDAWG_POINTER_SIZE 4
#else
# error "Can't obtain the pointer size"
#endif
#if PYDAWG_POINTER_SIZE == 4
# define MACHINE32BIT
typedef uint32_t nodeid_t;
#elif PYDAWG_POINTER_SIZE == 8
# define MACHINE64BIT
typedef uint64_t nodeid_t;
#else
# error "Unsupported pointer size"
#endif
//#define DEBUG_MEM
#ifdef DEBUG_MEM
void* memalloc(size_t size) {
void* addr = PyMem_Malloc(size);
printf("alloc %p %u\n", addr, size);
return addr;
}
void memfree(void* addr) {
ASSERT(addr != NULL); // It's OK to call PyMem_Free with NULL, however such a call indicates mistakes.
printf("free %p\n", addr);
PyMem_Free(addr);
}
void *memcalloc(size_t nmemb, size_t size) {
# if PY_VERSION_HEX >= 0x03050000
void* addr = PyMem_Calloc(nmemb, size);
# else
void *addr = memalloc(nmemb*size);
memset(addr, 0, nmemb*size);
# endif
printf("calloc %p %u\n", addr, nmemb*size);
return addr;
}
#else
# define memalloc PyMem_Malloc
# define memfree PyMem_Free
# if PY_VERSION_HEX >= 0x03050000
# define memcalloc PyMem_Calloc
# else
static inline void *memcalloc(size_t nmemb, size_t size) {
void *addr = memalloc(nmemb*size);
memset(addr, 0, nmemb*size);
return addr;
}
# endif
#endif
#if defined(_WIN32) || defined(_WIN64)
# define PY_OBJECT_HEAD_INIT PyVarObject_HEAD_INIT(NULL, 0)
#else
# define PY_OBJECT_HEAD_INIT PyVarObject_HEAD_INIT(&PyType_Type, 0)
#endif
#ifndef __cplusplus
typedef char bool;
# define true 1
# define false 0
#endif
#endif