On creating pointers

new topic     » topic index » view thread      » older message » newer message

This is a multipart message in MIME format

--43318315-POCO-21400222

How is this as a starter for working on pointers?

antoine

--43318315-POCO-21400222

--           CHANGE .e into .ex,.exw,.exu to run the testpart    =
             .

--                   CREATE YOUR OWN POINTERS and DATA_AREA      =
             .
--              and  PBR or PBV through the provided routines    =
             .
--      Sorry, NO garbage-collection performed                   =
             .
--      Sorry, DATA is `stanamic`=3D> static-dynamic under your own=
 control IE  .
--      data-sizes initialised may be shrunk (but not expanded)=
 due to the    .
--      way I wrote this piece of code            a.j.m.tammer=
 2002/03/24     .
--      The routines operate correctly on single-level sequences=
 only         .

--          INDEX of ROUTINES with SHORT SYNTAX and EXPLANATION  =
             .

-- pointer=3Dset_pointer(object len)=3D> sets pointer to area of=
 (len)bytes       .
--     object can be any formula resulting in an atom OR an atom =
             .
--                                      updates pointer_addr. &=
 data_start    .
--                                      stores len in POINTER+4  =
             .
--                             returns: pointer_to_pointer       =
             .
-- s_data=3Dget_val(object POINTER)=3D>returns data as a sequence s  =
             .
-- s_info=3Dget_ref(atom POINTER)=3D>returns sequence=
 {data_start,data_len}       .
-- store(atom POINTER sequence data)=3D>stores data at data_start=
 in POINTER    .
--                                                               =
             .
-- Usage: POINTER=3Dmy_pointer                                     =
             .
--        DATA=3Dmy_data
--        s_data=3Dget_val(POINTER)                                =
             .
--        s_info=3Dget_ref(POINTER)                                =
             .
--        store(POINTER,DATA)                                    =
             .
-- on it's way: modify(POINTER,DATA,NEW_DATA) akind of=
 store(get_val())       .
-- as well as checks for PointersLeft and DataSpaceLeft          =
             .

-- Right now the code doesn't check whether the amount of bytes=
 you want to   .
-- assign to a SET pointer actually fits in the initially=
 assigned fieldsize  .
-- I'm working on a bit of code that will allow fieldsizes upto=
 2^15-1, while .
-- checking if new-size > initial-size and issueing a warning if=
 TRUE         .
-- Size-Integer's bits 1-15 being actual-size, 16-30 being=
 initial-size       .

include machine1.e -- a version of machine.e that initialises=
 alloc.mem to 0's.
                   -- modified allocate() & allocate_low() at=
 bottom of file  .
                   -- does work with machine.e as well           =
             .
--                                                    SETTING UP=
 GLOBALS NEEDED

global atom P_BASE P_BASE=3Dallocate(1048576) -- RE: lines after=
 include        .
global integer P_SIZE P_SIZE=3D8
global atom P_TAB P_TAB=3DP_BASE-P_SIZE
global integer P_MAX P_MAX=3D1024
global integer P_LEFT P_LEFT=3DP_MAX
global integer DTA_AREA_START DTA_AREA_START=3DP_BASE+8192
global atom DTA_START DTA_START=3DDTA_AREA_START
global atom POINTER POINTER=3D0
global sequence DATA DATA=3D{}
--                                                           =
 DEFINING ROUTINES

global function set_pointer(object initial_data_size)
  P_TAB+=3DP_SIZE
  poke4(P_TAB+4,initial_data_size)
  poke4(P_TAB,DTA_START)
  DTA_START+=3Dinitial_data_size
  return P_TAB
end function

global function get_val(object POINTER)
  return peek({peek4u(POINTER),peek4u(POINTER+4)})
end function

global function get_ref(object POINTER)
  return {peek4u(POINTER),peek4u(POINTER+4)}
end function

global procedure store(object POINTER, sequence data)
  poke(peek4u(POINTER),data)
  poke4(POINTER+4,length(data))
end procedure
--=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--                   TESTING VALIDITY OF ROUTINES                =
             .
atom p_var1 p_var1=3D0
atom p_var2 p_var2=3D0

sequence testdata1, testdata2,empty1,empty2
testdata1=3D"This is an example"
testdata2=3D"Another example"
empty1=3D{} empty2=3D{}

p_var1=3Dset_pointer(length(testdata1))
p_var2=3Dset_pointer(length(testdata2))

POINTER=3Dp_var
DATA=3Dtestdata1
store(POINTER,DATA)
empty1=3Dget_val(POINTER)
printf(1,"%s\n\n",{empty1})

POINTER=3Dp_var2
DATA=3Dtestdata2
store(POINTER,DATA)
empty2=3Dget_ref(POINTER)
printf(1,"%d %d \n\n",{empty2[1],empty2[2]})

? p_var
? peek4u(p_var)
? peek4u(p_var+4)
printf(1,"%s\n",32)
? p_var2
? peek4u(p_var2)
? peek4u(p_var2+4)

free(P_BASE)

? 1/0


-- MODIFIED allocate() & allocate_low()                          =
             .

global function allocate(positive_int n)
-- Allocate n bytes of memory and return the address.
-- Free the memory using free() below.
    atom IsAllocated IsAllocated=3D0
    IsAllocated=3Dmachine_func(M_ALLOC, n)
    if IsAllocated then
      mem_set(IsAllocated,0,n)
    end if
    return IsAllocated
end function

global function allocate_low(positive_int n)
-- Allocate n bytes of memory and return the address.
-- Free the memory using free() below.
    atom IsAllocated IsAllocated=3D0
    IsAllocated=3Dmachine_func(M_ALLOC_LOW, n)
    if IsAllocated then
      mem_set(IsAllocated,0,n)
    end if
    return IsAllocated
end function


--43318315-POCO-21400222--

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu