newLISP Fan Club

Forum => newLISP in the real world => Topic started by: hds1 on January 07, 2018, 01:44:21 AM

Title: (share) with (pack) memory
Post by: hds1 on January 07, 2018, 01:44:21 AM
it seems not possible to use (share) with packed elements.

i.e.

(set 'flash (share))

(share flash (pack (dup "u" 10) (sequence 1 10)))



According to the docs (share) should work with any newlisp expression.



When forking processes and calling C-libs from these processes it would be nice to be able to set the (share) in the parent process without the neccessity to repack it in the child process.



Or do i miss something obvious here ?

Regards

Heiko
Title: Re: (share) with (pack) memory
Post by: Lutz on January 09, 2018, 09:28:55 AM
Perhaps you forgot to unpack when reading the information back.



> (set 'flash (share))
4419780608
> (share flash (pack (dup "u" 10) (sequence 1 10)))
"0100020003000400050006000700b00t00n00"
> (unpack (dup "u" 10) (share flash))
(1 2 3 4 5 6 7 8 9 10)
>


The above works for me on macOS, Linux, FreeBSD and Windows.



Ps: also included a qa-specific-tests/qa-share in the newlisp-10.7.4.tgz source distribution here: http://www.newlisp.org/downloads/development/inprogress/
Title: Re: (share) with (pack) memory
Post by: hds1 on January 11, 2018, 04:52:44 AM
hm, i meant reading the (shared memory) from a C-lib.

i.e.

Parent <-> (share mem) <-> Forked child (calling the C-lib)

Parent:

(set 'flashMemory (share))

(share flashMemory (rand 0xFFFF 100))



Forked Child:

(emulavr_load_flash (pack (dup "u" 100) flashMemory))



C-lib prints rubbish numbers:

ubyte * emulavr_load_flash(ubyte * flash_prom) {

  printf("EMULAVR FLASH intro: 0x%04X 0x%04Xn", flash_prom[0],flash_prom[1]);

}
Title: Re: (share) with (pack) memory
Post by: Lutz on January 13, 2018, 07:26:59 AM
In this changed example from newlisp-10.7.4/qa-specific-tests/qa-share share is used by a spawned process:

(set 'flash (share))
(share flash (pack (dup "u" 10) (sequence 1 10)))

(define (get-data adr)
    (unpack (dup "u" 10) (share adr)))

(spawn 'X (get-data flash))
(sync 10)
(println X " = " (sequence 1 10))
(if (= X (sequence 1 10))
    (println "test passed SUCCESSFUL")
    (println "test passed with error"))


spawn is based on fork internally. There is an example newlisp-10.7.4/example/prodcons.lsp that uses fork directly.



If your example tries to retrieve packed shared memory, it's wrong:

(pack (dup "u" 100) flashMemory)

the correct syntax would be:

(unpack (dup "u" 100) (share flashMemory))

if you pack for storing, you have too unpack, and the content cannot be accessed directly via  'flashMemory' but using '(share flashMemory)'.