1. safe routines for poking etc.

Hello All,

Here's a safe.e file with safe versions of poke(), mem_copy()
etc. that you can use when debugging. I would have liked
to name them the same as the standard routines,
but this turned out to be impractical, so I stuck
"safe_" on the front of each one. You *can* rename
ex.exe built-in routines, but unfortunately, not Euphoria
global routines defined in .e files such as machine.e.

This code has not been thoroughly tested. In any case
you may need to modify it for your own particular programs.

I did test it on Ralf's latest gfxbeta from Irv's FTP site and it
caught an attempt to poke into unallocated memory. This error
did not show up on my Pentium when I ran his demo, but
my 486 had a Causeway error. Both machines catch the error
when safe.e is included, and poke() is renamed to safe_poke(),
and allocate() is renamed to safe_allocate() everywhere in his
code.

I'll try to insert safe.e directly into this message.
Please ignore any extra '=' signs or 3D's that get inserted.

-- safe.e ------------ cut here --------------------

-- Ensure that memory operations are safe (for debugging)
-- safe versions of peek, poke, mem_copy, mem_set,
-- allocate, allocate_low

include machine.e

constant OK = 1, BAD = 0

sequence safe_address_list

-- add any acceptable areas of low memory here:
safe_address_list = {
    {#A0000, 200*320} -- video memory, start & length
}

function safe_address(atom a)
-- is it ok to read/write address a?
    -- check the list of safe memory blocks:
    for i = 1 to length(safe_address_list) do
        if a >= safe_address_list[i][1] and
            a < safe_address_list[i][1] + safe_address_list[i][2] then
            return OK
        end if
    end for
    return BAD
end function

procedure die(sequence msg)
-- terminate with a message
    puts(1, "\n\n" & msg & "\n\n")
    ? 1/ 0 -- force traceback
end procedure

global function safe_peek(object x)
-- safe version of peek
    integer len
    atom a

    if atom(x) then
        len = 1
        a = x
    else
        len = x[2]
        a = x[1]
    end if
    if safe_address(a) and safe_address(a + len - 1) then
        return peek(x)
    else
        die(sprintf("BAD POKE ADDRESS!!!! %d\n\n", a))
    end if
end function

global procedure safe_poke(atom a, object v)
-- safe version of poke
    integer len

    if atom(v) then
        len = 1
    else
        len = length(v)
    end if
    if safe_address(a) and safe_address(a + len - 1) then
        poke(a, v)
    else
        die(sprintf("BAD POKE ADDRESS!!!! %d\n\n", a))
    end if
end procedure

global procedure safe_mem_copy(atom target, atom source, atom len)
-- safe mem_copy
    if not safe_address(target) or not safe_address(target+len-1) then
        die(sprintf("BAD MEM_COPY TARGET ADDRESS!!!! %d\n\n", target))
    elsif not safe_address(source) or not safe_address(source+len-1) then
        die(sprintf("BAD MEM_COPY SOURCE ADDRESS!!!! %d\n\n", target))
    else
        mem_copy(target, source, len)
    end if
end procedure

global procedure safe_mem_set(atom target, atom value, atom len)
-- safe mem_set
    if safe_address(target) and safe_address(target + len - 1) then
        mem_set(target, value, len)
    else
        die(sprintf("BAD MEM_SET ADDRESS!!!! %d\n\n", target))
    end if
end procedure

global function safe_allocate(integer n)
-- allocate memory block and add it to safe list
    atom a

    a = allocate(n)
    if a = 0 then
        die("OUT OF MEMORY!")
    end if
    safe_address_list = append(safe_address_list, {a, n})
    return a
end function

global function safe_allocate_low(integer n)
-- allocate memory block and add it to safe list
    atom a

    a = allocate_low(n)
    if a = 0 then
        die("OUT OF MEMORY!")
    end if
    safe_address_list = append(safe_address_list, {a, n})
    return a
end function

---------------

Regards,
  Rob Craig
  Rapid Deployment Software

new topic     » topic index » view message » categorize

2. Re: safe routines for poking etc.

Great idea Robert, i knew there were a lot of precision errors, but
when i go to dos under win95, it swaps it's onw memory to a
swap-file and gives me the full 24 meg i have, a lot of memory is not
used for anything .. so the error does not occur. Maybe we can have a
special data type for this kind of stuff in the future, for debugging
i don't care about the speed, but it would be nice if i can guarantee
that whetever my program does wrong, you'll just get an error
message! Something like memory. Like this:

memory video_card
SetMemory(video_card,#A0000,320*200)
SetAcces(video_card, {320,200},{1})             -- one byte per pos
video_card[4][5] = 3

So with SetAcces you set the dimensions and the length of them. If
it's represents more than one byte it is split up in values as the
last sequence passed to it)

So this :

SetAcces(some_addres, {200,100,100},{1,4,5})

Makes memory at some_addres accessable as a 3-dimension table where
each piece is 10 bytes long, the piece is split in 3 parts, one
value of one byte, one value of 4 bytes and one value of 5 bytes.

See the potentional?
Most can be done by a precompiler and quick look-up-tables.
So it should be easy to implend and greatly ease programming in
Euphoria.......
(SVGA can then also be done by writing to it directly.... i
only wonder how fast it would be)

PS Warning to all, it already told ya its a BETA version, it does
what it is supposed to do in most cases...but not all. Be aware of
this when trying the code... okay?

Ralf Nieuwenhuijsen
nieuwen at xs4all.nl

new topic     » goto parent     » topic index » view message » categorize

3. Re: safe routines for poking etc.

Robert Craig wrote:

> You *can* rename ex.exe built-in routines, but unfortunately, not Euphoria
> global routines defined in .e files such as machine.e.

 How do you rename them ?
 Say i want to create a function that gets input for the built-in
routine open ().
 That function needs to call the real buil-in routine open () and
the name of my custom routine needs to be open()..

How do i do that?

BTW Poke ()  is a built-in routine according to your documentation...

Thank you..

Ralf Nieuwenhuijsen
nieuwen at xs4all.nl

new topic     » goto parent     » topic index » view message » categorize

4. Re: safe routines for poking etc.

>> You *can* rename ex.exe built-in routines, but unfortunately, not Euphoria
>> global routines defined in .e files such as machine.e.

Ralf asks:
> How do you rename them ?

If you want to name your own routine with the same name
as one of the routines built-in to ex.exe, you can just
do it, although you'll get a warning about it.

As Ralph observed, there's a problem if your routine
wants to also *call* the original routine as part of
what it does to replace the original routine. e.g.

global function open()
-- my replacement for Euphoria's open()
 .... open()
end function

The above would be taken by Euphoria to be a recursive call,
*not* a call to Euphoria's open().

To get around this you can define:

function real_open()
-- does a real Euphoria open:
 .... open()
end function

global function open()
-- my replacement for Euphoria's open()
 .... real_open()
end function

Unfortunately, there's no easy way to override
a global routine written in Euphoria. Maybe this
will change in the future. It would have been
nice if you could just temporarily plug in safe.e without
changing all of your various calls from xxx() to safe_xxx().

Actually, I could have saved you the trouble of changing
peek, poke, mem_set and mem_copy by using the trick shown above,
but I couldn't use the same trick on allocate, free,
allocate_low or free_low, so it seemed more consistent
to just rename the whole bunch as safe_xxx().

Regards,
  Rob Craig
  Rapid Deployment Software

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu