public inbox for ecos-discuss@sourceware.org
 help / color / mirror / Atom feed
* [ECOS] fopen/fclose
@ 2002-03-22  8:31 Jurica Baricevic
  2002-03-25  3:42 ` Andrew Lunn
  0 siblings, 1 reply; 7+ messages in thread
From: Jurica Baricevic @ 2002-03-22  8:31 UTC (permalink / raw)
  To: Ecos

Hi!

I have a quite simple question regarding memory allocation/deallocation in
fopen/fclose :-)

It seems that fopen() creates stream with new operator (fopen.cxx:133):
   stream = new Cyg_StdioStream(....);

while its pair fclose() destroys it with the following combination
(fclose.cxx:101):
   stream->~Cyg_StdioStream();
   free(stream);

I wonder what could be the reason for not using delete operator in fclose()
instead?


Thanks,
Jura



-- 
Before posting, please read the FAQ: http://sources.redhat.com/fom/ecos
and search the list archive: http://sources.redhat.com/ml/ecos-discuss

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [ECOS] fopen/fclose
  2002-03-22  8:31 [ECOS] fopen/fclose Jurica Baricevic
@ 2002-03-25  3:42 ` Andrew Lunn
  2002-03-25  8:46   ` Jurica Baricevic
  0 siblings, 1 reply; 7+ messages in thread
From: Andrew Lunn @ 2002-03-25  3:42 UTC (permalink / raw)
  To: Jurica Baricevic; +Cc: Ecos

On Fri, Mar 22, 2002 at 05:01:07PM +0100, Jurica Baricevic wrote:
> Hi!
> 
> I have a quite simple question regarding memory allocation/deallocation in
> fopen/fclose :-)
> 
> It seems that fopen() creates stream with new operator (fopen.cxx:133):
>    stream = new Cyg_StdioStream(....);
> 
> while its pair fclose() destroys it with the following combination
> (fclose.cxx:101):
>    stream->~Cyg_StdioStream();
>    free(stream);
> 
> I wonder what could be the reason for not using delete operator in fclose()
> instead?

Have a look at infra/current/src/delete.cxx.
CYGFUN_INFRA_EMPTY_DELETE_FUNCTIONS is enabled by default since most
eCos programs don't do dynamic stuff with memory. They create what
they need at startup and keep it forever. Hence the delete operator is
not needed.

FILE operations are the exception to this.

     Andrew

-- 
Before posting, please read the FAQ: http://sources.redhat.com/fom/ecos
and search the list archive: http://sources.redhat.com/ml/ecos-discuss

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: [ECOS] fopen/fclose
  2002-03-25  3:42 ` Andrew Lunn
@ 2002-03-25  8:46   ` Jurica Baricevic
  2002-03-25  8:50     ` Andrew Lunn
  0 siblings, 1 reply; 7+ messages in thread
From: Jurica Baricevic @ 2002-03-25  8:46 UTC (permalink / raw)
  To: Andrew Lunn, Ecos

Andrew Lunn wrote (Monday, March 25, 2002 09:33):
> On Fri, Mar 22, 2002 at 05:01:07PM +0100, Jurica Baricevic wrote:
> > Hi!
> >
> > I have a quite simple question regarding memory
> allocation/deallocation in
> > fopen/fclose :-)
> >
> > It seems that fopen() creates stream with new operator (fopen.cxx:133):
> >    stream = new Cyg_StdioStream(....);
> >
> > while its pair fclose() destroys it with the following combination
> > (fclose.cxx:101):
> >    stream->~Cyg_StdioStream();
> >    free(stream);
> >
> > I wonder what could be the reason for not using delete operator
> in fclose()
> > instead?
>
> Have a look at infra/current/src/delete.cxx.
> CYGFUN_INFRA_EMPTY_DELETE_FUNCTIONS is enabled by default since most
> eCos programs don't do dynamic stuff with memory. They create what
> they need at startup and keep it forever. Hence the delete operator is
> not needed.
>
> FILE operations are the exception to this.
>

Thank you very much Andrew. This clarifies the non-conventional pair
new/free.
However, I don't understand why FILE operations wouldn't use delete when
CYGFUN_INFRA_EMPTY_DELETE_FUNCTIONS is disabled (... #ifdef
CYGFUN_INFRA_EMPTY_DELETE_FUNCTIONS ... #else...#endif ...)?
Specifically, I need to overload default new/delete operators and this might
not be possible if 'fopen()' uses new, while 'fclose()' does not call
delete.


Best regards and thanks,
Jura



-- 
Before posting, please read the FAQ: http://sources.redhat.com/fom/ecos
and search the list archive: http://sources.redhat.com/ml/ecos-discuss

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [ECOS] fopen/fclose
  2002-03-25  8:46   ` Jurica Baricevic
@ 2002-03-25  8:50     ` Andrew Lunn
  2002-03-25  9:19       ` Jurica Baricevic
  0 siblings, 1 reply; 7+ messages in thread
From: Andrew Lunn @ 2002-03-25  8:50 UTC (permalink / raw)
  To: Jurica Baricevic; +Cc: Andrew Lunn, Ecos

> Thank you very much Andrew. This clarifies the non-conventional pair
> new/free.
> However, I don't understand why FILE operations wouldn't use delete when
> CYGFUN_INFRA_EMPTY_DELETE_FUNCTIONS is disabled (... #ifdef
> CYGFUN_INFRA_EMPTY_DELETE_FUNCTIONS ... #else...#endif ...)?
> Specifically, I need to overload default new/delete operators and this might
> not be possible if 'fopen()' uses new, while 'fclose()' does not call
> delete.

How about...

Because know one has yet needed to overload new/delete for FILE
operations. 

Just a guess. Jifl is the correct person to answer this question i
guess.

You are obviously delving around the insides of eCos and not using the
C API. The C API does not require what you want and so its not
strictly needed. You can always add it and send a patch to Jifl for
inclusion.

        Andrew

-- 
Before posting, please read the FAQ: http://sources.redhat.com/fom/ecos
and search the list archive: http://sources.redhat.com/ml/ecos-discuss

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: [ECOS] fopen/fclose
  2002-03-25  8:50     ` Andrew Lunn
@ 2002-03-25  9:19       ` Jurica Baricevic
  2002-03-25 11:20         ` Andrew Lunn
  2002-04-03 19:34         ` Jonathan Larmour
  0 siblings, 2 replies; 7+ messages in thread
From: Jurica Baricevic @ 2002-03-25  9:19 UTC (permalink / raw)
  To: Andrew Lunn; +Cc: Ecos

Andrew Lunn wrote (Monday, March 25, 2002 13:57):
> > However, I don't understand why FILE operations wouldn't use delete when
> > CYGFUN_INFRA_EMPTY_DELETE_FUNCTIONS is disabled (... #ifdef
> > CYGFUN_INFRA_EMPTY_DELETE_FUNCTIONS ... #else...#endif ...)?
> > Specifically, I need to overload default new/delete operators
> and this might
> > not be possible if 'fopen()' uses new, while 'fclose()' does not call
> > delete.
>
> How about...
>
> Because know one has yet needed to overload new/delete for FILE
> operations.
>

Maybe I did not express myself correctly :-)
What I need is to modify C++ built-in (global) 'new' & 'delete' operators.
Since this is quite normal in C++ (egg. for memory leaks & overwrites
detection) I believe that having 'delete' in fclose()
is quite reasonable in case CYGFUN_INFRA_EMPTY_DELETE_FUNCTIONS is disabled.


> Just a guess. Jifl is the correct person to answer this question i
> guess.
>

Then I would appreciate Jonathan's opinion on trivial patch at the end of
mail.


> You are obviously delving around the insides of eCos and not using the
> C API. The C API does not require what you want and so its not
> strictly needed. You can always add it and send a patch to Jifl for
> inclusion.

Thanks for your help.


Best regards,
Jura

Here is the patch that fixed my problem. I hope that it can be safely
applied to the eCos CVS.
====================================================================

--- packages/language/c/libc/stdio/current/src/common/fclose.cxx  Fri Aug 25
20:33:40 2000
+++ packages/language/c/libc/stdio/current/src/common/fclose.cxx Mon Mar 25
14:53:18 2002
@@ -96,12 +96,16 @@ fclose( FILE *stream )
         return EOF;
     }

+#ifdef CYGFUN_INFRA_EMPTY_DELETE_FUNCTIONS
     // FIXME: should use delete?
     // Explicitly call destructor - this flushes the output too
     real_stream->~Cyg_StdioStream();

     // and free it
     free(real_stream);
+#else
+    delete real_stream;
+#endif // CYGFUN_INFRA_EMPTY_DELETE_FUNCTIONS

     // and mark the stream available for use
     Cyg_libc_stdio_files::set_file_stream(i, NULL);



-- 
Before posting, please read the FAQ: http://sources.redhat.com/fom/ecos
and search the list archive: http://sources.redhat.com/ml/ecos-discuss

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [ECOS] fopen/fclose
  2002-03-25  9:19       ` Jurica Baricevic
@ 2002-03-25 11:20         ` Andrew Lunn
  2002-04-03 19:34         ` Jonathan Larmour
  1 sibling, 0 replies; 7+ messages in thread
From: Andrew Lunn @ 2002-03-25 11:20 UTC (permalink / raw)
  To: Jurica Baricevic; +Cc: Ecos

On Mon, Mar 25, 2002 at 05:09:51PM +0100, Jurica Baricevic wrote:
> Andrew Lunn wrote (Monday, March 25, 2002 13:57):
> > > However, I don't understand why FILE operations wouldn't use delete when
> > > CYGFUN_INFRA_EMPTY_DELETE_FUNCTIONS is disabled (... #ifdef
> > > CYGFUN_INFRA_EMPTY_DELETE_FUNCTIONS ... #else...#endif ...)?
> > > Specifically, I need to overload default new/delete operators
> > and this might
> > > not be possible if 'fopen()' uses new, while 'fclose()' does not call
> > > delete.
> >
> > How about...
> >
> > Because know one has yet needed to overload new/delete for FILE
> > operations.
> >
> 
> Maybe I did not express myself correctly :-)
> What I need is to modify C++ built-in (global) 'new' & 'delete' operators.

Oh. Yes, you did express is correctly, i just read it wrongly :-(

    Andrew

-- 
Before posting, please read the FAQ: http://sources.redhat.com/fom/ecos
and search the list archive: http://sources.redhat.com/ml/ecos-discuss

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [ECOS] fopen/fclose
  2002-03-25  9:19       ` Jurica Baricevic
  2002-03-25 11:20         ` Andrew Lunn
@ 2002-04-03 19:34         ` Jonathan Larmour
  1 sibling, 0 replies; 7+ messages in thread
From: Jonathan Larmour @ 2002-04-03 19:34 UTC (permalink / raw)
  To: Jurica Baricevic; +Cc: Ecos

Jurica Baricevic wrote:
> 
> Here is the patch that fixed my problem. I hope that it can be safely
> applied to the eCos CVS.

Yep. I've included it.

Jifl

> ====================================================================
> 
> --- packages/language/c/libc/stdio/current/src/common/fclose.cxx  Fri Aug 25
> 20:33:40 2000
> +++ packages/language/c/libc/stdio/current/src/common/fclose.cxx Mon Mar 25
> 14:53:18 2002
> @@ -96,12 +96,16 @@ fclose( FILE *stream )
>          return EOF;
>      }
> 
> +#ifdef CYGFUN_INFRA_EMPTY_DELETE_FUNCTIONS
>      // FIXME: should use delete?
>      // Explicitly call destructor - this flushes the output too
>      real_stream->~Cyg_StdioStream();
> 
>      // and free it
>      free(real_stream);
> +#else
> +    delete real_stream;
> +#endif // CYGFUN_INFRA_EMPTY_DELETE_FUNCTIONS
> 
>      // and mark the stream available for use
>      Cyg_libc_stdio_files::set_file_stream(i, NULL);
> 
> --
> Before posting, please read the FAQ: http://sources.redhat.com/fom/ecos
> and search the list archive: http://sources.redhat.com/ml/ecos-discuss

-- 
Red Hat, Rustat House, Clifton Road, Cambridge, UK. Tel: +44 (1223) 271062
Maybe this world is another planet's Hell -Aldous Huxley || Opinions==mine

-- 
Before posting, please read the FAQ: http://sources.redhat.com/fom/ecos
and search the list archive: http://sources.redhat.com/ml/ecos-discuss

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2002-04-04  3:34 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-03-22  8:31 [ECOS] fopen/fclose Jurica Baricevic
2002-03-25  3:42 ` Andrew Lunn
2002-03-25  8:46   ` Jurica Baricevic
2002-03-25  8:50     ` Andrew Lunn
2002-03-25  9:19       ` Jurica Baricevic
2002-03-25 11:20         ` Andrew Lunn
2002-04-03 19:34         ` Jonathan Larmour

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).