Fix memory leaks and alignment issues (thanks rixed at happyleptic.org)
[ocaml-bitstring.git] / bitstring_c.c
1 /* Bitstring library.
2  * Copyright (C) 2008 Red Hat Inc., Richard W.M. Jones
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version,
8  * with the OCaml linking exception described in COPYING.LIB.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * $Id: bitstring.ml 146 2008-08-20 16:58:33Z richard.wm.jones $
20  */
21
22 /* This file contains hand-coded, optimized C implementations of
23  * certain very frequently used functions.
24  */
25
26 #include <config.h>
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdint.h>
31 #include <byteswap.h>
32 #include <string.h>
33
34 #include <caml/mlvalues.h>
35 #include <caml/fail.h>
36
37 /* Fastpath functions.  These are used in the common case for reading
38  * ints where the following conditions are known to be true:
39  * (a) the int size is a whole number of bytes (eg. 16, 24, 32, etc bits)
40  * (b) the access in the match is byte-aligned
41  * (c) the access in the underlying bitstring is byte-aligned
42  *
43  * These functions are all "noalloc" meaning they must not perform
44  * any OCaml allocations.  For this reason, when the function returns
45  * an int32 or int64, the OCaml code passes in the pre-allocated pointer
46  * to the return value.
47  *
48  * The final offset in the string is calculated by the OCaml (caller)
49  * code.  All we need to do is to read the string+offset and byteswap,
50  * sign-extend as necessary.
51  *
52  * There is one function for every combination of:
53  * (i) int size: 16, 32, 64 bits
54  * (ii) endian: bigendian, littleendian, nativeendian
55  * (iii) signed and unsigned
56  *
57  * XXX Future work: Expand this to 24, 40, 48, 56 bits.  This
58  * requires some extra work because sign-extension won't "just happen".
59  */
60
61 #ifdef WORDS_BIGENDIAN
62 #define swap_be(size,v)
63 #define swap_le(size,v) v = bswap_##size (v)
64 #define swap_ne(size,v)
65 #else
66 #define swap_be(size,v) v = bswap_##size (v)
67 #define swap_le(size,v)
68 #define swap_ne(size,v)
69 #endif
70
71 #define fastpath1(size,endian,signed,type)                              \
72   CAMLprim value                                                        \
73   ocaml_bitstring_extract_fastpath_int##size##_##endian##_##signed      \
74   (value strv, value offv)                                              \
75   {                                                                     \
76     type *ptr = (type *) ((void *) String_val (strv) + Int_val (offv)); \
77     type r;                                                             \
78     memcpy(&r, ptr, sizeof(r));                                 \
79     swap_##endian(size,r);                                              \
80     return Val_int (r);                                                 \
81   }
82
83 fastpath1(16,be,unsigned,uint16_t)
84 fastpath1(16,le,unsigned,uint16_t)
85 fastpath1(16,ne,unsigned,uint16_t)
86 fastpath1(16,be,signed,int16_t)
87 fastpath1(16,le,signed,int16_t)
88 fastpath1(16,ne,signed,int16_t)
89
90 #define fastpath2(size,endian,signed,type,rval)                         \
91   CAMLprim value                                                        \
92   ocaml_bitstring_extract_fastpath_int##size##_##endian##_##signed      \
93   (value strv, value offv, value rv)                                    \
94   {                                                                     \
95     type *ptr = (type *) ((void *) String_val (strv) + Int_val (offv)); \
96     type r;                                                             \
97     memcpy(&r, ptr, sizeof(r));                                 \
98     swap_##endian(size,r);                                              \
99     rval(rv) = r;                                                       \
100     return rv;                                                          \
101   }
102
103 fastpath2(32,be,unsigned,uint32_t,Int32_val)
104 fastpath2(32,le,unsigned,uint32_t,Int32_val)
105 fastpath2(32,ne,unsigned,uint32_t,Int32_val)
106 fastpath2(32,be,signed,int32_t,Int32_val)
107 fastpath2(32,le,signed,int32_t,Int32_val)
108 fastpath2(32,ne,signed,int32_t,Int32_val)
109
110 /* Special care needs to be taken on ARCH_ALIGN_INT64 platforms
111    (hppa and sparc in Debian). */
112
113 #ifdef ARCH_ALIGN_INT64
114 #include <caml/memory.h>
115 #include <caml/alloc.h>
116 #define fastpath3(size,endian,signed,type,rval)                         \
117   CAMLprim value                                                        \
118   ocaml_bitstring_extract_fastpath_int##size##_##endian##_##signed      \
119   (value strv, value offv, value rv)                                    \
120   {                                                                     \
121     type *ptr = (type *) ((void *) String_val (strv) + Int_val (offv)); \
122     type r;                                                             \
123     memcpy(&r, ptr, sizeof(r));                                 \
124     swap_##endian(size,r);                                              \
125     memcpy(Data_custom_val(rv), &r, sizeof(r));                 \
126     return rv;                                                          \
127   }
128
129 #else
130 #define fastpath3 fastpath2
131 #endif
132
133 fastpath3(64,be,unsigned,uint64_t,Int64_val)
134 fastpath3(64,le,unsigned,uint64_t,Int64_val)
135 fastpath3(64,ne,unsigned,uint64_t,Int64_val)
136 fastpath3(64,be,signed,int64_t,Int64_val)
137 fastpath3(64,le,signed,int64_t,Int64_val)
138 fastpath3(64,ne,signed,int64_t,Int64_val)