Skip to content

Commit

Permalink
fix alt perl smokers; use 64-bit inet when available
Browse files Browse the repository at this point in the history
  • Loading branch information
FGasper committed Mar 30, 2022
1 parent de72fd4 commit 114dac1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/all.yaml
Expand Up @@ -90,7 +90,7 @@ jobs:
with:
submodules: recursive
- run: perl -V
- run: cpanm --notest --installdeps .
- run: curl -L https://cpanmin.us | perl --notest --installdeps .
- run: perl Makefile.PL
- run: make
- run: prove -wlvmb t
Expand Down
29 changes: 29 additions & 0 deletions Makefile.PL
@@ -1,4 +1,6 @@
use ExtUtils::MakeMaker;
use File::Temp;
use Config;

# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile that is written.
Expand All @@ -25,6 +27,13 @@ WriteMakefile1(

INC => '-Wall -I.',

DEFINE => join(
q< >,
map { "-D$_" } (
( _ntohll_exists() ? 'CBF_64BIT_INET' : () ),
),
),

OBJECT => [
'$(BASEEXT)$(OBJ_EXT)',
'cbor_free_common.o',
Expand Down Expand Up @@ -98,3 +107,23 @@ sub WriteMakefile1 { #Compatibility code for old versions of EU::MM. Written by

WriteMakefile(%params);
}

sub _ntohll_exists {
my $dir = File::Temp::tempdir( CLEANUP => 1 );
open my $fh, '>', "$dir/c.c";
syswrite $fh, <<CC;
#include <arpa/inet.h>
int main() {
uint64_t a = ntohll(0);
return 0;
}
CC
close $fh;

print "Checking for 64-bit inet functions (e.g., ntohll) …$/";
my $has = !system $Config{'cc'}, "$dir/c.c", '-o', "$dir/a.out";

print "\t" . ($has ? 'yup!' : 'nope.') . $/;

return $has;
}
38 changes: 10 additions & 28 deletions cbor_free_decode.c
@@ -1,3 +1,4 @@
#define NO_XSLOCKS
#include "easyxs/init.h"

#include "cbor_free_common.h"
Expand Down Expand Up @@ -32,36 +33,17 @@

// Basically ntohll(), but it accepts a pointer.
static inline UV _buffer_u64_to_uv( unsigned char *buffer ) {
UV num = 0;
#ifdef CBF_64BIT_INET
return ntohll( *( (uint64_t*) buffer ) );
#else

return (
#if IS_64_BIT
num |= *(buffer++);
num <<= 8;

num |= *(buffer++);
num <<= 8;

num |= *(buffer++);
num <<= 8;

num |= *(buffer++);
num <<= 8;
#else
buffer += 4;
( ((UV) ntohl( *( (uint32_t*) buffer ) )) << 32 ) +
#endif
ntohl( *( (uint32_t*) buffer + 1 ) )
);
#endif

num |= *(buffer++);
num <<= 8;

num |= *(buffer++);
num <<= 8;

num |= *(buffer++);
num <<= 8;

num |= *(buffer++);

return num;
}

const char *MAJOR_TYPE_DESCRIPTION[] = {
Expand Down Expand Up @@ -288,7 +270,7 @@ static inline UV _parse_for_uint_len2( pTHX_ decode_ctx* decstate ) {

#if !IS_64_BIT

if (decstate->curbyte[0] || decstate->curbyte[1] || decstate->curbyte[2] || decstate->curbyte[3]) {
if (*( (uint32_t*) decstate->curbyte )) {
_croak_cannot_decode_64bit( aTHX_ decstate );
}
#endif
Expand Down
10 changes: 9 additions & 1 deletion cbor_free_encode.c
@@ -1,4 +1,8 @@
#define NO_XSLOCKS 1
/* First prevent Perl from defining htons et al. as macros that
will require aTHX. That redfinition is problematic because we
use those functions in pure, non-XS functions here.
*/
#define NO_XSLOCKS

#include "easyxs/init.h"

Expand Down Expand Up @@ -81,6 +85,9 @@ static inline void _u32_to_buffer( UV num, unsigned char *buffer ) {
}

static inline void _u64_to_buffer( UV num, unsigned char *buffer ) {
#ifdef CBF_64BIT_INET
*( (uint64_t*) buffer ) = htonll((uint64_t) num);
#else
*( (uint32_t*) buffer ) =
#if IS_64_BIT
htonl((uint32_t) (num >> 32))
Expand All @@ -90,6 +97,7 @@ static inline void _u64_to_buffer( UV num, unsigned char *buffer ) {
;

*( (uint32_t*) (buffer + 4) ) = htonl((uint32_t) (num & 0xffffffff));
#endif
}

//----------------------------------------------------------------------
Expand Down

0 comments on commit 114dac1

Please sign in to comment.