Allocating Memory

LayGO for Perl5 contains 4 functions which make it easy to allocate memory suitable for passing to the C language functions of the LayGO API.

my $maxBuffer = lgo_MaxBufferSize($cid);
my $inBuffer  = lgo_BufferNew($maxBuffer);
my $outBuffer = lgo_BufferNew($maxBuffer);
my ($i, $j, $result);

for ($i = 0; $i < $maxBuffer; $i++)
{
    lgo_BufferSet($outBuffer, $i, 65 + ($i % 26));
}

$result = lgo_Write($cid, $outBuffer, $maxBuffer);

if ($result < 0)
{
    printf("Error writing: %s\n", lgo_ErrorMessage($result));
}
else
{
    printf("$maxBuffer bytes written.\n");

    for ($i = 0; $i < $result; $i += 16)
    {
        for ($j = $i; $j < $result && $j - $i < 16; $j++)
        {
            printf("%02X ", lgo_BufferGet($outBuffer, $j));
        }
        printf("\n");
    }

    do
    {
        sleep(1);

        $result = lgo_Read($cid, $inBuffer, $maxBuffer);
    }
    while ($result <= 0);

    printf("$result bytes received.\n");

    for ($i = 0; $i < $result; $i += 16)
    {
        for ($j = $i; $j < $result && $j - $i < 16; $j++)
        {
            printf("%02X ", lgo_BufferGet($inBuffer, $j));
        }
        printf("\n");
    }
}
lgo_BufferFree($inBuffer);
lgo_BufferFree($outBuffer);