2 * Copyright (C) 2011 Carl Hetherington <carl@carlh.net>
3 * Copyright (C) 2013 John Emmas <john@creativepost.co.uk>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "pbd/pthread_utils.h"
29 int (*pbd_alloc_allowed) () = 0;
31 /** Thread-local key whose value is set to 1 if malloc checking is disabled
32 * for this thread, 0 otherwise.
35 static pthread_key_t disabled;
37 static pthread_once_t once;
42 (void) pthread_key_create (&disabled, NULL);
45 /** This is our malloc which overrides the system one */
46 void* malloc (size_t s)
48 static void * (*real_malloc) (size_t) = NULL;
50 /* find the system malloc */
51 real_malloc = dlsym (RTLD_NEXT, "malloc");
54 (void) pthread_once (&once, make_key);
56 if (pthread_getspecific (disabled) == NULL && pbd_alloc_allowed && !pbd_alloc_allowed ()) {
57 /* pbd_alloc_allowed says that this malloc is not permitted */
61 /* Pass through to the system malloc */
62 return real_malloc (s);
66 suspend_rt_malloc_checks ()
68 (void) pthread_once (&once, make_key);
69 pthread_setspecific (disabled, (void *) 1);
73 resume_rt_malloc_checks ()
75 (void) pthread_once (&once, make_key);
76 pthread_setspecific (disabled, (void *) 0);