Page 1 of 1

(share) with (pack) memory

PostPosted: Sun Jan 07, 2018 9:44 am
by hds1
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

Re: (share) with (pack) memory

PostPosted: Tue Jan 09, 2018 5:28 pm
by Lutz
Perhaps you forgot to unpack when reading the information back.

Code: Select all
> (set 'flash (share))
4419780608
> (share flash (pack (dup "u" 10) (sequence 1 10)))
"\001\000\002\000\003\000\004\000\005\000\006\000\007\000\b\000\t\000\n\000"
> (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/develo ... nprogress/

Re: (share) with (pack) memory

PostPosted: Thu Jan 11, 2018 12:52 pm
by hds1
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%04X\n", flash_prom[0],flash_prom[1]);
}

Re: (share) with (pack) memory

PostPosted: Sat Jan 13, 2018 3:26 pm
by Lutz
In this changed example from newlisp-10.7.4/qa-specific-tests/qa-share share is used by a spawned process:
Code: Select all
(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:
Code: Select all
(pack (dup "u" 100) flashMemory)

the correct syntax would be:
Code: Select all
(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)'.