Create an object at a given address: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Omit Pike. A Pike module written in C could deal directly with addresses, but there is no way in the base language)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 19:
<br><br>
 
=={{Headerheader|6502 Assembly}}==
In [[6502 Assembly]] memory is represented by either an 8-bit or a 16-bit address (i.e. $0000 - $FFFF). 8-bit address are reserved for the memory from $00 to $FF - known as zero page; access to this memory takes one less byte in the opcode and one less cycle to execute.
 
Line 51:
It should be noted that on the 6502 processor hardware is normally memory mapped, so this is often used for manipulating hardware.
 
=={{Headerheader|Ada}}==
In [[Ada]] object address can be specified using the address representation clause [http://www.adaic.org/standards/05rm/html/RM-13-3.html RM 13.3]:
<lang ada>
Line 80:
456
</pre>
 
=={{Header|Aikido}}==
=={{header|Aikido}}==
Aikido doesn't support getting the address of a variable. However, in the spirit of this task, it does support raw memory access using <code>peek</code> and <code>poke</code>. These can be used on both an integer representing an address (64 bit) or a value obtained from calling <code>malloc</code>.
<lang aikido>
Line 466 ⟶ 467:
Value is 52, address is 26431776
</pre>
 
=={{header|M2000 Interpreter}}==
In M2000 we can create two kind of buffers, one for data, and one for code. Buffer for code is immutable at execution time. We can execute code by using an offset. Buffer for data is always mutable, but can't execute code.
Line 561 ⟶ 563:
0 1 2 3 4 5 6 7
506097522914230528</pre>
 
=={{header|Perl 6}}==
Perl 6 has fairly comprehensive facilities for accessing allocating and accessing memory and also declaring C-style structs, via the NativeCall interface, as this example demonstrates.
<lang perl6>use v6;
use NativeCall;
use NativeCall::Types;
 
# bind to basic libc memory management
sub malloc(size_t) returns Pointer[uint8] is native {*};
sub memset(Pointer, uint32, size_t) is native {*};
sub free(Pointer[uint8]) is native {*};
 
my Pointer[uint8] $base-p = malloc(100);
memset($base-p, 0, 100);
 
# define object as a C struct that contains a short and an int
class SampleObject is repr('CStruct') {
has uint16 $.foo is rw;
has uint8 $.bar is rw;
}
 
# for arguments sake our object is at byte offset 64 in the
# allocated memory
 
my $offset-p = $base-p.clone.add(64);
my $object-p := nativecast(Pointer[SampleObject], $offset-p);
note "creating object at address {+$object-p}";
 
my $struct := $object-p.deref;
 
$struct.foo = 41;
$struct.bar = 99;
 
# check we can update
$struct.foo++; # 42
 
# Check that we're actually updating the memory
use Test;
 
# look at the bytes directly to verify we've written to memory. Don't be too exact, as
# the positions may vary on different platforms depending on endianess and field alignment.
 
my $rec-size = nativesizeof(SampleObject);
my uint8 @bytes-written = (0 ..^ $rec-size).map(-> $i {$base-p[64 + $i]}).grep: * > 0;
 
# first field 'foo' (amount is small enough to fit in one byte)
is @bytes-written[0], 42, 'object first field';
 
# second field 'bar'
is @bytes-written[1], 99, 'object second field';
 
# verify that changing the origin changes the object values
memset($base-p, 1, 100); # set every byte to 1
 
is $struct.foo, 256 + 1, 'short updated at origin';
is $struct.bar, 1, 'byte updated at origin';
 
# tidy up
free($base-p);
done-testing;
</lang>
{{out}}
<pre>creating object at address 94299589110352
ok 1 - object first field
ok 2 - object second field
ok 3 - short updated at origin
ok 4 - byte updated at origin
1..4
</pre>
 
=={{header|Phix}}==
Line 704 ⟶ 637:
;; object
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
Perl 6 has fairly comprehensive facilities for accessing allocating and accessing memory and also declaring C-style structs, via the NativeCall interface, as this example demonstrates.
<lang perl6>use v6;
use NativeCall;
use NativeCall::Types;
 
# bind to basic libc memory management
sub malloc(size_t) returns Pointer[uint8] is native {*};
sub memset(Pointer, uint32, size_t) is native {*};
sub free(Pointer[uint8]) is native {*};
 
my Pointer[uint8] $base-p = malloc(100);
memset($base-p, 0, 100);
 
# define object as a C struct that contains a short and an int
class SampleObject is repr('CStruct') {
has uint16 $.foo is rw;
has uint8 $.bar is rw;
}
 
# for arguments sake our object is at byte offset 64 in the
# allocated memory
 
my $offset-p = $base-p.clone.add(64);
my $object-p := nativecast(Pointer[SampleObject], $offset-p);
note "creating object at address {+$object-p}";
 
my $struct := $object-p.deref;
 
$struct.foo = 41;
$struct.bar = 99;
 
# check we can update
$struct.foo++; # 42
 
# Check that we're actually updating the memory
use Test;
 
# look at the bytes directly to verify we've written to memory. Don't be too exact, as
# the positions may vary on different platforms depending on endianess and field alignment.
 
my $rec-size = nativesizeof(SampleObject);
my uint8 @bytes-written = (0 ..^ $rec-size).map(-> $i {$base-p[64 + $i]}).grep: * > 0;
 
# first field 'foo' (amount is small enough to fit in one byte)
is @bytes-written[0], 42, 'object first field';
 
# second field 'bar'
is @bytes-written[1], 99, 'object second field';
 
# verify that changing the origin changes the object values
memset($base-p, 1, 100); # set every byte to 1
 
is $struct.foo, 256 + 1, 'short updated at origin';
is $struct.bar, 1, 'byte updated at origin';
 
# tidy up
free($base-p);
done-testing;
</lang>
{{out}}
<pre>creating object at address 94299589110352
ok 1 - object first field
ok 2 - object second field
ok 3 - short updated at origin
ok 4 - byte updated at origin
1..4
</pre>
 
=={{header|Rust}}==
Line 733 ⟶ 736:
 
It is rather unprofessional to deliver this kind of error-prone software. And we are wondering why this tasks is made.
 
=={{header|Tcl}}==
As noted in the [[Address Operations]] task, it is highly unusual to work with low-level addresses in Tcl. However it is possible to use Tcl's [[C]] API (specifically <code>[http://www.tcl.tk/man/tcl8.6/TclLib/LinkVar.htm Tcl_LinkVar]</code>) to couple Tcl variables to a particular address:
10,333

edits