forked from rbrito/pkg-mp3packer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathptr.h
164 lines (144 loc) · 3.34 KB
/
ptr.h
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#ifndef PTR_H
#define PTR_H
//#define _XOPEN_SOURCE 700
// Needed for intrinsics
//#//include <malloc.h>
#include <stdio.h>
#include <caml/mlvalues.h>
#include <caml/signals.h>
#include <caml/alloc.h>
#include <caml/fail.h>
#include <caml/memory.h>
#include <caml/custom.h>
#if defined(__WIN32__) || defined(__WIN32)
#ifndef _WIN32
#define _WIN32
#endif
#endif
#ifndef _WIN32
#include <unistd.h>
#include <sys/mman.h>
#include <string.h>
#include <errno.h>
#endif
// Microsoft typing
#ifdef _WIN32
typedef __int16 int16_t;
typedef unsigned __int16 uint16_t;
#else
#include <stdint.h>
#define FALSE 0
#define TRUE 1
#endif
#ifdef __linux__
#include <byteswap.h>
#define uint32 uint32_t
#define uint64 uint64_t
#define int32 int32_t
#define int64 int64_t
#endif
#if defined(__APPLE__)
#include <libkern/OSByteOrder.h>
#endif
enum ptr_type {
PTR_MALLOC,
PTR_MMAP,
PTR_VIRTUALALLOC,
PTR_NULL // For ignoring
};
struct ptr_struct {
char *begin;
char *alloc_begin;
intnat length;
intnat align;
enum ptr_type type;
};
#define Struct_val(x) ((struct ptr_struct *)Data_custom_val(x))
#define Begin_val(x) Struct_val(x)->begin
#define Alloc_begin_val(x) Struct_val(x)->alloc_begin
#define Length_val(x) Struct_val(x)->length
#define Align_val(x) Struct_val(x)->align
#define Type_val(x) Struct_val(x)->type
static void ptr_finalize(value v) {
struct ptr_struct *p = Struct_val(v);
switch (p->type) {
case PTR_MALLOC:
free(p->alloc_begin);
break;
case PTR_MMAP:
#ifdef _WIN32
UnmapViewOfFile(p->alloc_begin);
break;
#else
munmap(p->alloc_begin, p->length);
break;
#endif
case PTR_VIRTUALALLOC:
#ifdef _WIN32
VirtualFree(p->alloc_begin, 0, MEM_RELEASE);
break;
#else
break;
#endif
case PTR_NULL:
break;
}
}
/* COMPARE */
static int ptr_compare(value a_val, value b_val) {
int len_a = Length_val(a_val);
int len_b = Length_val(b_val);
if(len_a != len_b) {
return((len_a < len_b) - (len_b < len_a));
} else {
return(memcmp(Begin_val(a_val), Begin_val(b_val), len_a));
}
}
static struct custom_operations generic_ptr_opts = {
"c_ptr",
ptr_finalize,
ptr_compare/*custom_compare_default*/,
custom_hash_default,
custom_serialize_default,
custom_deserialize_default
};
static inline uint32_t byteswap32(uint32_t n)
{
#if defined(__builtin_bswap32)
return __builtin_bswap32(n);
#elif defined(OSSwapInt32)
return OSSwapInt32(n);
#elif defined(bswap32)
return bswap32(n);
#elif defined(__swap32)
return __swap32(n);
#elif defined(bswap_32)
return bswap_32(n);
#elif defined(_byteswap_ulong)
return _byteswap_ulong(n);
#else
return (((n << 24) & 0xff000000 ) |
((n << 8) & 0x00ff0000 ) |
((n >> 8) & 0x0000ff00 ) |
((n >> 24) & 0x000000ff ));
#endif
}
static inline uint16_t byteswap16(uint16_t n)
{
#if defined(__builtin_bswap16)
return __builtin_bswap16(n);
#elif defined(OSSwapInt16)
return OSSwapInt16(n);
#elif defined(bswap16)
return bswap16(n);
#elif defined(__swap16)
return __swap16(n);
#elif defined(bswap_16)
return bswap_16(n);
#elif defined(_byteswap_ushort)
return _byteswap_ushort(n);
#else
return (((n & 0x00FF) << 8) | ((n & 0xFF00) >> 8));
#endif
}
#endif /* PTR_H */