(This has been split off from the static code analysis thread since it's
a different topic.)
I've completed the first cut of an overhaul of the use of dynamic memory
allocation in the source tree with an eye toward safety and an eventual
place to do a clean shutdown (closing the database, removing locks, etc.).
What was done:
Malloc(), calloc(), realloc(), strdup() and strndup() have been made
fail-safe through counterparts named dt_*() which exit cleanly through a
common function. The new versions are in the utilities.h header as
short static inline functions that, on failure, call an error function
in utilities.c. This arrangement seems subjectively faster than a
straight-up function call and doesn't pepper copies of the same static
strings all over the object files. If any functions have been missed,
please point them out.
Calls to free() have been changed to dt_free(), which is currently a
pass-through to free().
dt_alloc_align() and dt_free_align() have been given the same treatment
and renamed to dt_malloc_aligned() and dt_free_aligned() for name
consistency with the other functions.
What was not done:
Calls to these functions' glib equivalents are still intact. What I've
been able to get from the documentation so far is that there's nothing
special about them other than terminating on failure, but I want to make
sure there are no other ramifications before proceeding. The glib
functions do a clean exit if allocation fails.
I may see about adding something to catch calls to malloc() et al and
cause a compilation error or at least a hard runtime failure.
No effort has been made to do any kind of clean shutdown. I need some
advice from the learned on how to approach that.
There are a number of standalone programs that allocate memory but don't
include any of the other common dt headers. Those will be dealt with
separately to avoid having to link anything additional.
These changes have not been run through PVS-Studio to see how much
smaller the report is (and to catch any mistakes in my own work).
If anyone wants to give it a try, the current code is in the
"malloc-overhaul" branch of my fork at
https://github.com/markfeit/darktable.git. So far all seems good, but
I'm going to dogfood it for a bit longer before submitting a pull request.
--Mark
___________________________________________________________________________
darktable developer mailing list
to unsubscribe send a mail to darktable-dev+unsubscr...@lists.darktable.org