Skip to content

Instantly share code, notes, and snippets.

@scovit
Last active January 6, 2019 16:19
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save scovit/7b529556e2c4a8b64e13c3af6da8b056 to your computer and use it in GitHub Desktop.
my @initial = (1..15).List.pick(15);
use NativeCall;
sub malloc(size_t --> Pointer) is native { * };
sub memcpy_A(Buf, Pointer, size_t) is native is symbol("memcpy") { };
sub memcpy_B(Pointer, Buf, size_t) is native is symbol("memcpy") { };
sub qsort(Pointer, size_t, size_t, &compar (Pointer, Pointer --> Pointer)) is native { * };
my $data = Buf[uint8].new(@initial);
my $ptr = malloc(15);
memcpy_B($ptr, $data, 15);
say [$ptr, $data];
qsort $ptr, 15, 1, -> Pointer $first, Pointer $second --> Pointer {
Pointer.new(nativecast(uint8, $first) - nativecast(uint8, $second))
};
memcpy_A($data, $ptr, 15);
say [$ptr, $data]; # this works
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment