Comments (8)
Vaguely sounds like microsoft's oh-so-very-safe crt apis? I'm not strongly objecting but do we really need this?
from sdl.
Hard no from me.
from sdl.
Fair enough. It would probably also make language bindings harder.
from sdl.
The convention used in e.g. GLib and libdbus is that destroy/free/unref functions take the pointer like they do in SDL2, but for authors of C code it's extremely convenient to have a macro that implements this pattern (shown here as pseudocode using GLib's naming convention):
void mylib_thing_free (MylibThing *thing);
void mylib_clear_thing(MylibThing **thingp)
{
MylibThing *thing = *thingp;
*thingp = NULL;
if (thing) {
mylib_thing_free(thing);
}
}
int do_stuff (...)
{
MylibThing *thing = NULL;
int ret = 0;
... do something that might allocate it ...
... do something that might set ret = -1 and 'goto out' ...
out:
mylib_clear_thing(&thing);
return ret;
}
For example GLib has this for GObject subclasses as the pair g_object_unref()
/g_clear_object()
, and generically for whatever type of pointer as g_clear_pointer(&thing, thing_free)
. Recent libdbus has dbus_clear_connection(&conn)
, dbus_clear_message(&msg)
and so on following the same pattern.
CPython Py_CLEAR()
is similar, although it hides the extra &
inside a macro.
This is particularly convenient for users of compilers implementing gcc-compatible __attribute__((cleanup))
. SDL can't rely on this itself, the same way GLib can't, because SDL and GLib are portable to compilers like MSVC that don't implement that extension; but there are plenty of GLib-dependent projects like Flatpak that only care about gcc-compatible compilers, and similarly I'm sure there are plenty of SDL-dependent projects that only care about gcc-compatible compilers.
from sdl.
Hmmmmm, we do know the type of the pointer in SDL3 now, we could potentially implement SDL_ClearObject(void **foo)
from sdl.
Actually, no, that wouldn't work well. You'd have to cast your argument to (void **)
, never mind.
from sdl.
It would have to be SDL_ClearObject(void *pointer_to_pointer)
and give up compile-time type-safety, because passing a SDL_Surface **
where a void **
is expected violates ISO C "strict aliasing" and compilers will complain accordingly.
It might be better to have the equivalent of what libdbus does: SDL_ClearSurface(SDL_Surface **)
and so on. libdbus has this macro:
/* Implementation for dbus_clear_message() etc. This is not API,
* do not use it directly.
*
* We're using a specific type (T ** and T *) instead of void ** and
* void * partly for type-safety, partly to be strict-aliasing-compliant,
* and partly to keep C++ compilers happy. This code is inlined into
* users of libdbus, so we can't rely on it having dbus' own compiler
* settings. */
#define _dbus_clear_pointer_impl(T, pointer_to_pointer, destroy) \
do { \
T **_pp = (pointer_to_pointer); \
T *_value = *_pp; \
\
*_pp = NULL; \
\
if (_value != NULL) \
destroy (_value); \
} while (0)
/* Not (destroy) (_value) in case destroy() is a function-like macro */
which is used like this:
static inline void
dbus_clear_connection (DBusConnection **pointer_to_connection)
{
_dbus_clear_pointer_impl (DBusConnection, pointer_to_connection,
dbus_connection_unref);
}
The implementation was https://gitlab.freedesktop.org/dbus/dbus/-/commit/e13f29cae788ee0d0d16352c9be2d9e14a5a0b3d, © 2017 Collabora Ltd. and I'm happy to license it under the Zlib license if you want.
from sdl.
Can we use _Generic
macros here?
Only problem is it is C11+.
from sdl.
Related Issues (20)
- [SDL3][audio] time to drop hardcoded channel maps for good? HOT 6
- Android `SDL_GetPreferredLocales` memory corruption HOT 1
- prerelease-3.x tags HOT 1
- Example build failing HOT 4
- Android safe area problems
- Android `setOrientationBis` issues HOT 6
- Automatic cleanup after 1 second in `SDL_FreeLater` can easily cause use after free bugs HOT 5
- Static compile on macOS with Swift with CMake HOT 10
- define of SDL_GetKeyboardState cannot avoid discarded-qualifiers warning HOT 4
- [Question - SDL2] Opt-out of "forcing X11" mech with GLEW&Co? HOT 4
- SDL3: SDL_GetDisplayBounds fails for 0th display HOT 1
- New temporary memory management can't work with threads HOT 1
- `SDL_GetWindowSizeInPixels` may fail HOT 1
- SDL_RenderTexture9Grid enhancement / freature request HOT 5
- Issue with FPS Capping at Monitor Refresh Rate Even When VSync is Disabled HOT 3
- Occasional segfault in testautomation: mouse_warpMouseInWindow HOT 7
- [SDL3] opengl issue HOT 2
- testautomation --filter render_testBlendModes fails with vulkan renderer HOT 1
- [SDL3] testffmpeg render issue HOT 1
- Window content does not exactly fit window size HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sdl.