Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2006-01

Ansistrings DLL/DSO hack (memory manager)

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

L

Trustmaster sends me this trick/hack (as shown below) to avoid using CMEM in a dll/dso in order to get ansistrings working through a dll/dso.

Comments? Find anything wrong with it?
i.e. the ability to use strings in a DLL/DSO without ever using Pchars.

Trustmaster's trick/hack:

Show quoted text 49 lines
Hi Lars, I've just read an article how to solve the memory management problem with FPC without using CMEM unit. It is done by exporting memory manager in the library and using it everywhere simply this way: // psplib.pp {$H+} {$MODE OBJFPC} library psplib; { ... } procedure GetMemMan (out MemMan : TMemoryManager); stdcall; export; begin GetMemoryManager (MemMan) end; { ... } exports { ... } GetMemMan name 'GetSharedMemoryManager'; { ... } end. // pspunit.pp {$H+} {$MODE OBJFPC} unit pspunit; interface { ... } implementation { ... } procedure GetMemMan (out MemMan : TMemoryManager); stdcall; external 'psplib' name 'GetSharedMemoryManager'; var MemMan : TMemoryManager; { ... } initialization GetMemMan(MemMan); SetMemoryManager (MemMan); { ... } end. // pspapp.pp {$H+} {$MODE OBJFPC} program pspapp; uses pspunit; { ... } begin { ... } end.

Lars' comments:

Seems to make a simple hello world project with strings in it work as a DLL/DSO for now without requiring Cmem, but further testing will need to be done. I will throw up a PasForum clone using this tactic, and see what happens with this sort of set up. When
using no shared memory manager and using strings, PasForum would run but random access violations would occur every so often. I assume this is why the PSP4UM has been running without issue for several while now, because it does work, sort of, to not use a shared memory manager - it's just risky and can cause access violations, and obviously not worth trying for too long.

L505

Trustmaster sends me a trick/hack (as shown below) to avoid using CMEM in a dll/dso in order to get ansistrings working through a dll/dso.

Comments? Find anything that could possibly be wrong with this trick we pull off? i.e. the ability to use strings in a DLL/DSO without ever using Pchars, nor CMEM!

The trick/hack:

Show quoted text 49 lines
Hi Lars, I've just read an article how to solve the memory management problem with FPC without using CMEM unit. It is done by exporting memory manager in the library and using it everywhere simply this way: // psplib.pp {$H+} {$MODE OBJFPC} library psplib; { ... } procedure GetMemMan (out MemMan : TMemoryManager); stdcall; export; begin GetMemoryManager (MemMan) end; { ... } exports { ... } GetMemMan name 'GetSharedMemoryManager'; { ... } end. // pspunit.pp {$H+} {$MODE OBJFPC} unit pspunit; interface { ... } implementation { ... } procedure GetMemMan (out MemMan : TMemoryManager); stdcall; external 'psplib' name 'GetSharedMemoryManager'; var MemMan : TMemoryManager; { ... } initialization GetMemMan(MemMan); SetMemoryManager (MemMan); { ... } end. // pspapp.pp {$H+} {$MODE OBJFPC} program pspapp; uses pspunit; { ... } begin { ... } end.

Lars' comments:

Seems to make a simple hello world project with strings in it work as a DLL/DSO for now without requiring Cmem, but further testing will need to be done. I will throw up a PasForum clone (code named Bubble2) using this tactic, and see what happens with this
sort of set up.

When using no shared memory manager and using strings, PasForum (code named Bubble) would
run but random access violations would occur every so often. I assume this is why the PSP4UM (psp.furtopia.org) has been running without issue for several while now, because it does work,
sort of - but obviously not recommended.

L505
I will throw up a PasForum clone (code named Bubble2) using this tactic, and see what happens with this sort of set up.

Here it is:(BUBBLE2)
http://z505.com/cgi-bin/Bubble2/index.psp
Forgive the colors, I haven't played with the CSS file for more than 2 seconds :-)

And the old one which used NO memory manager at all:(BUBBLE1) http://z505.com/cgi-bin/Bubble/index.psp

And the one that does not use a DLL/DSO so is not worth even discussing, is the original PasForum so I won't mention the link to there.

Jonas Maebe
to L505: Trustmaster sends me a trick/hack (as shown below) to avoid using CMEM in a dll/dso in…
L505 wrote 8 lines
Trustmaster sends me a trick/hack (as shown below) to avoid using CMEM in a dll/dso in order to get ansistrings working through a dll/dso. Comments? Find anything that could possibly be wrong with this trick we pull off? i.e. the ability to use strings in a DLL/DSO without ever using Pchars, nor CMEM!

It's exactly the same principle as using cmem, just that you are using another memory manager than the one of the standard C library. It has the same advantages (can return and accept ansistrings to/from FPC-compiled programs and libararies) and problems (must not return and accept ansistrings to/from programs not compiled with FPC). Well, another advantage is of course for OS'es where the interface to libc changes all the time (although I doubt the interface to malloc/free etc will change anytime soon).

Jonas

L505
to Jonas Maebe

Sorry for the double posting, the emails took a long time to get through.

Show quoted text 10 lines
It's exactly the same principle as using cmem, just that you are using another memory manager than the one of the standard C library. It has the same advantages (can return and accept ansistrings to/from FPC-compiled programs and libararies) and problems (must not return and accept ansistrings to/from programs not compiled with FPC). Well, another advantage is of course for OS'es where the interface to libc changes all the time (although I doubt the interface to malloc/free etc will change anytime soon).

And this case we use FPC mem manager rather than relying on our operating system friend - just makes me feel better to be able to utilize FPC mem manager if I can!