-
Notifications
You must be signed in to change notification settings - Fork 172
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
Make libsamplerate work with other libs/frameworks such as JUCE. #67
Conversation
Hello fellow JUCEr! Let me know if you need a hand with anything. Totally open to testing if you need it. |
C++ is not C. C is not a strict subset of C++. Adding these casts makes this code incorrect for a C compiler. There may be a way to keep it correct C and improve it for C++ but this is not it. |
Thank you very much for the reply. I really appreciate you've taken the time to look into it.
I might have missed something but I've checked both calloc for C, C++ which suggests the only concern might be:
I guess it's possible to make something that is more explicit but sure is more redundant. (though it should keep the C code intact):
|
The correct solution is to provide a wrapper function that warps all the |
The function might look something like:
|
Yes. I'll re-commit / force-push my branch for you to review. update: It seems decltype is C++11 so I'm not sure it's the right way for broader compatibility. Thank you. |
Any C++ fix you supply should be compatible with the broadest possible range of C++ compilers and standards. |
@erikd Can you provide a source for this claim? IMO casts (the C-style casts used here) are fully valid in C so this should be fine
@talaviram Your approach is the wrong way round. If you were to go this way use a define like A way I'd go with would be to slim down the config.h. Some of the stuff seems to be for tests only -> move to tests. Endianess can be detected with some if-defing (see e.g. Boost). The clipping stuff (CPU_CLIPS_POSITIVE, ...) is sketchy and plain wrong on cross-compilers. I'd do manual clipping there: Check if the float value is outside |
@Flamefire Saying it adding the casts is "incorrect for C" was probably overstating the case, but with the casts, and without the inclusion of the header file that defines the function that returns a pointer, the C compiler will infer that the functions returns an |
Do you mean |
I like to avoid all un-necessary casts in my code. This is an un-necessary (for C) cast. |
That's a good question. the rationale for |
Ok understand. I don't understand the need to compile the C code in C++ though (@talaviram ) If this would really be required I'd prefer the cast over the #ifdef code proposed though. It is less error-prone than repeating the code 2 times guarded with an untested #ifdef |
The previous code which is 5 lines is pretty fooloproof, but to address you concern, how about this?
with a comment of why it is like that. |
Since there are 8 places where calloc needs to be modified to support this. addition to common.h:
so all calls would simply be like: |
Please don't. You are repeating the type as 2 different ways. I propose:
Or Any way: Can you explain why exactly this is required? Why would you compile C code with a C++ compiler? This sounds wrong. The whole approach there doesn't look right (especially maintaining cryptography manually is a big red flag). Juce modules should at least support C code natively There is |
Thank you for your feedback it is indeed cleaner. But what would you do if you provide a single value in cases like the following?
JUCE module format cannot include 'c' file. You can see this limitation also here: You can ask Jules why also other JUCE libs uses this approach: With current module format we provide pre-compiled lib but with the current permissive license of libsamplerate having it as a submodule is cleaner and cross-platform friendly. |
Completely forgot about flexible array members... I guess use your solution then or provide 2 macros where the other looks like However I still think this is the wrong approach. As @erikd wrote C is not a subset of C++ and if "JUCE module format cannot include 'c' file." then this is a missing feature of JUCE. I seriously doubt the correctness of the used approach. Even in C++ mono-builds (including multiple C++ files in 1) can change semantic. Consider the typedefs (as explained in your second link) or anonymous namespaces (TU local definitions which might conflict). The suggested approach of putting each src in a different namespace is also wrong and will not work.
Then it should be real submodules ala CMake Speaking of config.h: My suggestion would be to change |
4678ae1
to
e4f0618
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the ZERO_ALLOC
changes are fine, but I would prefer to avoid having the same #ifdef
in nearly every source file.
examples/audio_out.c
Outdated
#include <config.h> | ||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having #ifdef` everywhere is a pain in the neck and almost certain to go wrong sooner of later.
Instead, I would suggest that you replace all existing inlcudes of config.h
with:
#include "src-config.h"
and then create that file which would just be:
#ifdef HAVE_CONFIG_H
#include "config.h>"
#endif
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about my suggestion of including it as "config.h"
? You can then place a file named config.h next to the cpp c files including them and be done. No big changes required and guaranteed to work as the current dir is the first to search that include for
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
config.h
is a generated file, generated by autotools. It can't be generated and part of the repo
the cpp files
There are no cpp file in this repo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The author wants to include files of this repo in another. There he could add a config.h if he wants to avoid configuring this project.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about my suggestion of including it as
"config.h"
?
For my specific use case it'll be ok, I can already just put my config.h and set it as a search path.
But HAVE_CONFIG_H
is common and I think it's better to not make it mandatory.
This is common practice in many libs -
https://github.com/renesas-rcar/flac_decoder/blob/master/lib/src/libFLAC/crc.c#L45
(and many more)
d550e66
to
62c13c1
Compare
Is there anything else needed for this PR that I am missing? |
I would really just like the title of the first commit comment improved. Maybe something like: "Make allocation more C++ friendly". |
…ther libs/using the code directly.
Thanks. I've changed the commit comment. |
@talaviram I just got reminded of this by something I (re)saw: Compiling this code as C++ is invalid C++ due to the flexible array members being an extension to C++ supported by only some compilers and it is undefined behavior due to missing constructors of objects allocated by It might work, but be aware that it can break at anytime. So I'd strongly suggest to NOT compile this as C++. C++ is not a superset of C |
I'm going to make libsamplerate as a JUCE module.
I've made minor "cosmetic" changes that will be very welcomed to keep using the libsamplerate origin git as a submodule of this module.
Explained are the 2 commits:
Allow compilation under cpp:
HAVE_CONFIG_H
config.h
. with the JUCE module for example I generate all the defs with macros that are already exists in JUCE itself. here is an example of such use.https://github.com/WeAreROLI/JUCE/blob/master/modules/juce_audio_formats/codecs/juce_FlacAudioFormat.cpp