Using Newlib in a library

This forum is for general developer support questions.
User avatar
broadblues
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 600
Joined: Sat Jun 18, 2011 2:40 am
Location: Portsmouth, UK
Contact:

Re: Using Newlib in a library

Post by broadblues »

Olrick wrote:I was thinking about the cleaning instructions generated in libExpunge() by IDLTool:

Code: Select all

        
        libBase->IExec->Remove((struct Node *)libBase);
        libBase->IExec->DeleteLibrary((struct Library *)libBase);
I thought it was the cleanup of the LibraryBase I get in libInit(), was I wrong ?
And is there anything to do with the segList I get from libInit() ?

I guess these two resources got allocated and must be freed in case of failure.
Don't call those outside of the lib_expunge() vector, if it needs calling ramlib (the process that deals with library opening / closing etc) will call it.

Your init function should just setup library resuorces.

Return the base on success.

On failure cleanup the resources then return NULL.

Your expunge function should cleanup those resources then Remove() and DeleteLibrary() (was FreeMem() for 68k code) .

If you use the same function to cleanup in the initcode as the expunge code (which is sensible) then make sure it NULLs any pointers and test them for NULL so that it can be called twice without double freeing and crashing.

Don't worry about the seglist it's not yours to release.
User avatar
Olrick
Posts: 70
Joined: Mon Mar 07, 2011 3:24 pm
Location: Rennes, France

Re: Using Newlib in a library

Post by Olrick »

Thank you for the detailed advices.
I'll keep it simple as you described it and make some tests.
In fact I shoudn't spend so much time about a case which will hardly happen, but I like to do the things right the first time sothat I don't have to come back at it later.

Once again, a big thank you to both of you.
God grant me the Serenity to accept the things I cannot change; Courage to change the things I can; and Wisdom to know the difference.
User avatar
Olrick
Posts: 70
Joined: Mon Mar 07, 2011 3:24 pm
Location: Rennes, France

Re: Using Newlib in a library

Post by Olrick »

Ok, I've done it and, surprise, I can use newlib !

just a note about something I noticed in my logs:
- the first time I open a new build of my sample library, libInit() is called, then Obtain() is called twice,
- when I close the library, only one call of Release() is done,
- when I open the library the second time, libInit() is not called and Obtain() is called only once.
- when I overrwrite the library file with a new one, libExpunge() is called and then my missed Release()
I guess OS4 kind of caches the library in case it would be reused

Code: Select all

DEBUG init.c:148: libInit()
DEBUG init.c:171: NewlibBase before opening: 0x0
DEBUG init.c:177: NewlibBase after opening: 0x6ffab258
2016-04-14 21:48:11.660 DEBUG init.c:182: INewlib activated
Function oltkc::Obtain not implemented
Function oltkc::Obtain not implemented
Function oltkc::Release not implemented

Function oltkc::Obtain not implemented
Function oltkc::Release not implemented

2016-04-14 21:49:24.206 DEBUG init.c:121: libExpunge()
DEBUG init.c:101: Cleaning Newlib
Function oltkc::Release not implemented
God grant me the Serenity to accept the things I cannot change; Courage to change the things I can; and Wisdom to know the difference.
User avatar
broadblues
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 600
Joined: Sat Jun 18, 2011 2:40 am
Location: Portsmouth, UK
Contact:

Re: Using Newlib in a library

Post by broadblues »

When you call OpenLibrary() ramlib searches for the library in list of libraries in memory.

If not found it looks in LIBS: CLASSES: and a few other places (can never remeber the exact list) and if found loads it inot memory and calls the init() function.

Then it calls the libopen function and returns the result.

When you close it it's not automatically expunged but remains in memeory till the next program opens it or until reswources are scarce and the various libraries expunge vector are called to free up space.

This is why Avail Flush used to be (mis)used to flush a lib from memeory whilst developing. (There is tool called expunge in the SDK now for this, that can expunge a specific library (if not not open))

In OS4 there is special hook that will watch the location of the open library on disk and expunge the copy memory if it's updated and the library is not currently in use.

In certain special cases you may want a library expunged as soon as the last copy is closed, and there is flag that you can set in libClose() for that (though I can't remeber what it is off hand without docs infront of me).
User avatar
Olrick
Posts: 70
Joined: Mon Mar 07, 2011 3:24 pm
Location: Rennes, France

Re: Using Newlib in a library

Post by Olrick »

Thanks for the explanation.
I'll put it in the wiki too.
God grant me the Serenity to accept the things I cannot change; Courage to change the things I can; and Wisdom to know the difference.
User avatar
Olrick
Posts: 70
Joined: Mon Mar 07, 2011 3:24 pm
Location: Rennes, France

Re: Using Newlib in a library

Post by Olrick »

Hi again
I'm writing a simple step by step howto for the wiki and I'm wondering about a few things.

- In libInit I get a struct Interface *exec
May I just keep it or should I Obtain it ?

- the Utility interface sample in libInit stores the pointers in the libBase
What are the risks if I keep the pointers in globals like for Newlib ?
I guess using globals is bad, but it would allow to minimize the code change between the direct access out of the library for development and debugging, and the access through the library for release.
God grant me the Serenity to accept the things I cannot change; Courage to change the things I can; and Wisdom to know the difference.
User avatar
broadblues
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 600
Joined: Sat Jun 18, 2011 2:40 am
Location: Portsmouth, UK
Contact:

Re: Using Newlib in a library

Post by broadblues »

Olrick wrote:Hi again
I'm writing a simple step by step howto for the wiki and I'm wondering about a few things.

- In libInit I get a struct Interface *exec
May I just keep it or should I Obtain it ?
Just copy it to a global IExec variable or to libbase->lb_IExec if you prefer.

Exec isn't going away, so no need to Obtain the interface to prevent that.
- the Utility interface sample in libInit stores the pointers in the libBase
What are the risks if I keep the pointers in globals like for Newlib ?
I think this is just a matter of 'style'. It's often considered good style to collect globals into a context variable and libBase is a convenient place to use.
I guess using globals is bad, but it would allow to minimize the code change between the direct access out of the library for development and debugging, and the access through the library for release.
That's didn't quite compute... what do you mean here?
User avatar
Olrick
Posts: 70
Joined: Mon Mar 07, 2011 3:24 pm
Location: Rennes, France

Re: Using Newlib in a library

Post by Olrick »

broadblues wrote:
I guess using globals is bad, but it would allow to minimize the code change between the direct access out of the library for development and debugging, and the access through the library for release.
That's didn't quite compute... what do you mean here?
I developped my toolkit as regular sources, compiled with libauto, then it's full of IExec-> and IIntuition-> methods calls. If I put IExec and IIntuition in my libBase, I have to pass these interfaces or libBase as arguments down to the functions needing them.
If it is accceptable tu use global interfaces in the library, I wouldn't need to extend all the function calls to pass the interfaces.

As I will write my choice in the wiki, I prefer to check that it's a good practice first.
God grant me the Serenity to accept the things I cannot change; Courage to change the things I can; and Wisdom to know the difference.
User avatar
Olrick
Posts: 70
Joined: Mon Mar 07, 2011 3:24 pm
Location: Rennes, France

Re: Using Newlib in a library

Post by Olrick »

Hi
I just released my article on the wiki:
http://wiki.amigaos.net/wiki/How_to_cre ... S4_library
Don't hesitate to correct my approximated english and my technical errors and gaps.
I hope it will be of some help for newbies.
God grant me the Serenity to accept the things I cannot change; Courage to change the things I can; and Wisdom to know the difference.
Post Reply