Skip to content

Commit e021526

Browse files
committed
[FOLD]
1 parent 719561f commit e021526

3 files changed

Lines changed: 50 additions & 15 deletions

File tree

include/boost/http_proto/service/zlib_service.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ namespace zlib {
2424
struct stream_t
2525
{
2626
using alloc_func = void*(*)(void*, unsigned int, unsigned int);
27-
using free_func = void(*)(void*, void const*);
27+
using free_func = void(*)(void*, void*);
2828

29-
unsigned char const*next_in; // next input byte
29+
unsigned char* next_in; // next input byte
3030
unsigned int avail_in; // number of bytes available at next_in
3131
unsigned long total_in; // total number of input bytes read so far
3232

@@ -37,8 +37,8 @@ struct stream_t
3737
char const* msg; // last error message, NULL if no error
3838
void* state; // not visible by applications
3939

40-
alloc_func alloc; // used to allocate internal state
41-
free_func free; // used to deallocate internal state
40+
alloc_func zalloc; // used to allocate internal state
41+
free_func zfree; // used to deallocate internal state
4242
void* opaque; // private data object passed to zalloc and zfree
4343

4444
int data_type; // best guess about the data type: binary or text

src_zlib/service/stream_cast.hpp

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <boost/http_proto/detail/config.hpp>
1414
#include <boost/http_proto/service/zlib_service.hpp>
1515
#include <zlib.h>
16+
#include <cstddef>
1617

1718
namespace boost {
1819
namespace http_proto {
@@ -33,10 +34,10 @@ struct stream_cast_impl
3334
zs_.next_out = st_.next_out;
3435
zs_.avail_out = st_.avail_out;
3536
zs_.total_out = st_.total_out;
36-
zs_.msg = st_.msg;
37-
zs_.state = st_.state;
38-
zs_.alloc = st_.zalloc;
39-
zs_.free = st_.zfree;
37+
zs_.state = reinterpret_cast<
38+
internal_state*>(st_.state);
39+
zs_.zalloc = st_.zalloc;
40+
zs_.zfree = st_.zfree;
4041
zs_.opaque = st_.opaque;
4142
zs_.data_type = st_.data_type;
4243
zs_.adler = st_.adler;
@@ -53,18 +54,18 @@ struct stream_cast_impl
5354
st_.total_out = zs_.total_out;
5455
st_.msg = zs_.msg;
5556
st_.state = zs_.state;
56-
st_.alloc = zs_.zalloc;
57-
st_.free = zs_.zfree;
57+
st_.zalloc = zs_.zalloc;
58+
st_.zfree = zs_.zfree;
5859
st_.opaque = zs_.opaque;
5960
st_.data_type = zs_.data_type;
6061
st_.adler = zs_.adler;
6162
st_.reserved = zs_.reserved;
6263
}
6364

64-
z_stream_s&
65+
z_stream_s*
6566
get() noexcept
6667
{
67-
return *pzs_;
68+
return pzs_;
6869
}
6970

7071
private:
@@ -98,8 +99,42 @@ struct stream_cast_impl<false>
9899

99100
//------------------------------------------------
100101

102+
template<class T1, class T2>
103+
constexpr
104+
bool
105+
is_layout_identical()
106+
{
107+
#define SAME_FIELD(T1, T2, M)( \
108+
(offsetof(T1,M)==offsetof(T2,M)) && \
109+
(std::is_same<T1::M,T2::M>::value) && \
110+
(sizeof(T1::M)==sizeof(T2::M)) )
111+
return
112+
sizeof(T1) == sizeof(T2)
113+
#if 0
114+
SAME_FIELD(T1, T2, next_in)
115+
&&
116+
SAME_FIELD(T1, T2, avail_in) &&
117+
SAME_FIELD(T1, T2, total_in) &&
118+
SAME_FIELD(T1, T2, next_out) &&
119+
SAME_FIELD(T1, T2, avail_out) &&
120+
SAME_FIELD(T1, T2, total_out) &&
121+
SAME_FIELD(T1, T2, msg) &&
122+
SAME_FIELD(T1, T2, state) &&
123+
SAME_FIELD(T1, T2, zalloc) &&
124+
SAME_FIELD(T1, T2, zfree) &&
125+
SAME_FIELD(T1, T2, opaque) &&
126+
SAME_FIELD(T1, T2, data_type) &&
127+
SAME_FIELD(T1, T2, adler) &&
128+
SAME_FIELD(T1, T2, reserved)
129+
#endif
130+
;
131+
}
132+
133+
//------------------------------------------------
134+
101135
// VFALCO A pinch of undefined behavior here
102-
using stream_cast = stream_cast_impl<false>;
136+
using stream_cast = stream_cast_impl<
137+
is_layout_identical<stream_t, z_stream_s>()>;
103138

104139
} // zlib
105140
} // http_proto

src_zlib/service/zlib_service.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ void* zalloc(
169169
BOOST_ASSERT(same(&stream_t::total_out, &z_stream_s::total_out));
170170
BOOST_ASSERT(same(&stream_t::msg, &z_stream_s::msg));
171171
BOOST_ASSERT(same(&stream_t::state, &z_stream_s::state));
172-
BOOST_ASSERT(same(&stream_t::alloc, &z_stream_s::zalloc));
173-
BOOST_ASSERT(same(&stream_t::free, &z_stream_s::zfree));
172+
BOOST_ASSERT(same(&stream_t::zalloc, &z_stream_s::zalloc));
173+
BOOST_ASSERT(same(&stream_t::zfree, &z_stream_s::zfree));
174174
BOOST_ASSERT(same(&stream_t::opaque, &z_stream_s::opaque));
175175
BOOST_ASSERT(same(&stream_t::data_type, &z_stream_s::data_type));
176176
BOOST_ASSERT(same(&stream_t::adler, &z_stream_s::adler));

0 commit comments

Comments
 (0)