Malloc/Free Patch

Christophe Massiot massiot at via.ecp.fr
Mon Feb 12 13:33:29 CET 2007


Hello,

On Sun, Feb 11, 2007, Adam Charrett wrote:
> I would just like to say that I have been using libdvbpsi for the last year and think its great, the only library that fulfilled my needs and works!I've been using libdvbpsi in my application, dvbstreamer, and have been finding that there was a huge number of malloc/frees which seemed to be causing some memory fragmentation, so I modified libdvbpsi to only allocate more memory if it didn't already have a free section to use.I've include the patch here, I hope you consider it helpful. On average it reduce the malloc/free count from several million (when running continuously for a day or 2) to 2.ThanksAdam Charrett

I am grateful for the idea. However your patch has two critical issues
which make it impossible to merge it into the SVN trunk :

> + * List of sections available for reuse.
> + ****************************************************************************/
> +static dvbpsi_psi_section_t * sp_free_sections = NULL;

1. We can't use a static variable in libdvbpsi because it must be
reentrant. If two threads call the libdvbpsi patched with your code, it
is likely to lead to a major heap corruption within a few seconds. A
solution would be to use mutexes but this would bring latency and
portability issues, so I think the best is to keep one cache per
libdvbpsi instances, and put the chained list in the dvbpsi_decoder
structure.

> +void dvbpsi_FreePSISectionsCache(void)
> +{
> +   dvbpsi_DeletePSISections(sp_free_sections);
> +   sp_free_sections = NULL;
> +}

2. Also we want to keep source-level API compatibility as far as
possible. With this change you must modify all applications using
libdvbpsi to call dvbpsi_FreePSISectionsCache() when exiting, otherwise
creating a memory leak. dvbpsi_FreePSISectionsCache should get called
automatically when the dvbpsi handle is released.

-- 
Christophe Massiot.

-- 
This is the libdvbpsi-devel mailing-list, see http://developers.videolan.org/
To unsubscribe, go to: http://developers.videolan.org/lists.html



More information about the libdvbpsi-devel mailing list