-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Fix operator new #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix operator new #108
Conversation
…plies correct realization for all required operator new and operator delete overloads. Please use it.
Hmm. I agree that implementing things by the spec makes sense, but currently Arduino always compiles with Because of the extra complexity for users and the extra runtime overhead, I think that enabling exceptions within the Arduino environment is not going to happen. Also, it does not seem reasonable for all user code to use the nothrow syntax Of course, when someone is compiling the Arduino core without Now, you suggest that with Looking at the C++ 2003 spec (ISO/IEC 14882:2003) Section 3.7.3.1 describes what "allocation functions" (e.g.,
This shows that an allocation function that is not declared to throw an exception is allowed to return NULL. A bit further, section 5.3.4 describes how the "new expression" should work. In particular, paragraph 13 describes what happens when the allocation function returns NULL:
This means that the code generated for a "new expression" is required to handle the "return NULL" case when the It occurs to me that this support is mostly intended for when a class supplies a static Furthermore section 18.4.1.1 specifies the
Note that calling Now, with all the above in mind, let's get back to Given all of the above, I'd suggest the following:
How does this sound? IIUC, this would solve your specific problem, since you can then enable exceptions and link against uclibc++ without problems, right? |
I absolutely agree that exceptions should not be enabled by default in arduino IDE. And I'm not sure that it is possible to enable them: I don't remember well, but there were missing parts which would not allow to build program with exceptions for AVR. So my suggestion was for the case when -fno-exceptions has been specified. P.S. My intention was not to enable exceptions. I just don't know how to handle std::bad_alloc in most cases. And many programmers who I know have the same oppinion. std::bad_alloc means that my program can't work at all and should be terminated. So I wanted this termination to be automatical. Make it crash in safe and predictable place, rather when zero pointer is accessed. So idea in explicetely calling nothrow version of new is to additionally remind a programmer to check returned value. P.P.S. I've used dinamic memory allocation only on initialization stage. So termination of embedded program if it can't be started correctly was exactly what I needed. |
Ah, if you look at it like that, it actually seems you can still conform Also, you make a good point that terminating the program is better than Looking at your pullrequest, you suggest to remove So, if we want to provide a Perhaps something like this:
I guess it still make sense to define Would this allow you to use uclibc++ in the way you intend to? |
One more thing: I guess it would make sense to also copy the nothrow versions from uclibc++, to allow people to use nothrow when they know what they're doing and want to check the |
I agree with you. Intergating uclibc++ and maintaining it would require too big efforts.
This logic is already implemented in uclibc++. std::__throw_bad_alloc is defined here: You can replace std::terminate() with call to abort(), but I like having std::terminate more, because user can set his own terminate handler, which would do something more appropriate, than spinning in endless loop. Possible variants:
Yes, this would be good.
I'm fine even with slightly hacked arduino IDE. Or I can remove new and delete implementation from arduino port of uclibc++. |
I saw the Additionally, we could define a weak version of I also just noticed that uclibc++'s So:
Would that work? |
@kibergus, any comments on my latest proposal? :-) |
Sorry for slow reaction: I couldn't answer your letter immediately and then forgot about it. I think, tha your suggestions are very good. Thank you. |
Ok, now only to implement it :-) If you want to have a crack at a pullrequest for this (against the ide-1.5.x branch, I think would be best), feel free. If not, I'll probably do it once I find some time (not for a few weeks at least, though). |
Corrected board definitons (discussed with Henrik)
Last bit not implemented by a4496b9 Squash and rebase of arduino/Arduino#108
If still interesting, I ported the PR to https://github.com/arduino/ArduinoCore-avr . If someone wants to adopt it, please open a PR using this link https://github.com/arduino/ArduinoCore-avr/compare/pr_108?expand=1 , thanks. |
@facchinm, I think the changes proposed in comments are still interesting, not so much the PR itself. I've opened an issue in the AVR repo about this to keep track. |
Fix arduino#1485 A better implementation may be desirable as discussed in arduino#108
Implementation of operator new in arduino contradicts C++ standard.
If malloc returned NULL it MUST throw exception or cal std::terminate in case if exceptions are disabled.
If you want operator new to return NULL just use non throw version of it which is not provided by arduino. Beside that, there should be operator new[] and placement variants of the operators.
There is correct realization in uclibc++ and I suggest using it instead of current implementation. To achieve it gnu ABI specific functions must be moved to separate header (see #107). After that new.h and new.cpp must be deleted and uclibc++ should be used instead.
If you have strong objections against uclibc++ but are still concerned about quality of the library, I can separate implementation of operator new from uclibc++ and provide patches for integrating it to arduino. But this is not a right way because it would tear standard library implementation to peaces.