aboutsummaryrefslogtreecommitdiffstats
path: root/src/gmalloc.c
diff options
context:
space:
mode:
authorMiles Bader2007-08-13 13:48:35 +0000
committerMiles Bader2007-08-13 13:48:35 +0000
commitb2e6b10fe2d40020a75ab0025af98a4abf339cd2 (patch)
treedb265e5ea93cdc13f8e3b54ed5c7ad2869d50ec9 /src/gmalloc.c
parent905350bef3ebc514a418658dd155c1d062664b56 (diff)
parent37cc095b6a175fb5a2fb18fa029eaf3aa3b3fa53 (diff)
downloademacs-b2e6b10fe2d40020a75ab0025af98a4abf339cd2.tar.gz
emacs-b2e6b10fe2d40020a75ab0025af98a4abf339cd2.zip
Merge from emacs--devo--0
Patches applied: * emacs--devo--0 (patch 846-851) - Update from CVS - Merge from emacs--rel--22 * emacs--rel--22 (patch 88-92) - Update from CVS - Merge from gnus--rel--5.10 * gnus--rel--5.10 (patch 242-244) - Update from CVS Revision: emacs@sv.gnu.org/emacs--unicode--0--patch-246
Diffstat (limited to 'src/gmalloc.c')
-rw-r--r--src/gmalloc.c115
1 files changed, 98 insertions, 17 deletions
diff --git a/src/gmalloc.c b/src/gmalloc.c
index cf79b9159f2..ccc08e1ff68 100644
--- a/src/gmalloc.c
+++ b/src/gmalloc.c
@@ -129,6 +129,8 @@ extern FREE_RETURN_TYPE free PP ((__ptr_t __ptr));
129#if ! (defined (_MALLOC_INTERNAL) && __DJGPP__ - 0 == 1) /* Avoid conflict. */ 129#if ! (defined (_MALLOC_INTERNAL) && __DJGPP__ - 0 == 1) /* Avoid conflict. */
130extern __ptr_t memalign PP ((__malloc_size_t __alignment, 130extern __ptr_t memalign PP ((__malloc_size_t __alignment,
131 __malloc_size_t __size)); 131 __malloc_size_t __size));
132extern int posix_memalign PP ((__ptr_t *, __malloc_size_t,
133 __malloc_size_t size));
132#endif 134#endif
133 135
134/* Allocate SIZE bytes on a page boundary. */ 136/* Allocate SIZE bytes on a page boundary. */
@@ -136,6 +138,10 @@ extern __ptr_t memalign PP ((__malloc_size_t __alignment,
136extern __ptr_t valloc PP ((__malloc_size_t __size)); 138extern __ptr_t valloc PP ((__malloc_size_t __size));
137#endif 139#endif
138 140
141#ifdef USE_PTHREAD
142/* Set up mutexes and make malloc etc. thread-safe. */
143extern void malloc_enable_thread PP ((void));
144#endif
139 145
140#ifdef _MALLOC_INTERNAL 146#ifdef _MALLOC_INTERNAL
141 147
@@ -242,10 +248,27 @@ extern void _free_internal_nolock PP ((__ptr_t __ptr));
242 248
243#ifdef USE_PTHREAD 249#ifdef USE_PTHREAD
244extern pthread_mutex_t _malloc_mutex, _aligned_blocks_mutex; 250extern pthread_mutex_t _malloc_mutex, _aligned_blocks_mutex;
245#define LOCK() pthread_mutex_lock (&_malloc_mutex) 251extern int _malloc_thread_enabled_p;
246#define UNLOCK() pthread_mutex_unlock (&_malloc_mutex) 252#define LOCK() \
247#define LOCK_ALIGNED_BLOCKS() pthread_mutex_lock (&_aligned_blocks_mutex) 253 do { \
248#define UNLOCK_ALIGNED_BLOCKS() pthread_mutex_unlock (&_aligned_blocks_mutex) 254 if (_malloc_thread_enabled_p) \
255 pthread_mutex_lock (&_malloc_mutex); \
256 } while (0)
257#define UNLOCK() \
258 do { \
259 if (_malloc_thread_enabled_p) \
260 pthread_mutex_unlock (&_malloc_mutex); \
261 } while (0)
262#define LOCK_ALIGNED_BLOCKS() \
263 do { \
264 if (_malloc_thread_enabled_p) \
265 pthread_mutex_lock (&_aligned_blocks_mutex); \
266 } while (0)
267#define UNLOCK_ALIGNED_BLOCKS() \
268 do { \
269 if (_malloc_thread_enabled_p) \
270 pthread_mutex_unlock (&_aligned_blocks_mutex); \
271 } while (0)
249#else 272#else
250#define LOCK() 273#define LOCK()
251#define UNLOCK() 274#define UNLOCK()
@@ -563,6 +586,47 @@ register_heapinfo ()
563#ifdef USE_PTHREAD 586#ifdef USE_PTHREAD
564pthread_mutex_t _malloc_mutex = PTHREAD_MUTEX_INITIALIZER; 587pthread_mutex_t _malloc_mutex = PTHREAD_MUTEX_INITIALIZER;
565pthread_mutex_t _aligned_blocks_mutex = PTHREAD_MUTEX_INITIALIZER; 588pthread_mutex_t _aligned_blocks_mutex = PTHREAD_MUTEX_INITIALIZER;
589int _malloc_thread_enabled_p;
590
591static void
592malloc_atfork_handler_prepare ()
593{
594 LOCK ();
595 LOCK_ALIGNED_BLOCKS ();
596}
597
598static void
599malloc_atfork_handler_parent ()
600{
601 UNLOCK_ALIGNED_BLOCKS ();
602 UNLOCK ();
603}
604
605static void
606malloc_atfork_handler_child ()
607{
608 UNLOCK_ALIGNED_BLOCKS ();
609 UNLOCK ();
610}
611
612/* Set up mutexes and make malloc etc. thread-safe. */
613void
614malloc_enable_thread ()
615{
616 if (_malloc_thread_enabled_p)
617 return;
618
619 /* Some pthread implementations call malloc for statically
620 initialized mutexes when they are used first. To avoid such a
621 situation, we initialize mutexes here while their use is
622 disabled in malloc etc. */
623 pthread_mutex_init (&_malloc_mutex, NULL);
624 pthread_mutex_init (&_aligned_blocks_mutex, NULL);
625 pthread_atfork (malloc_atfork_handler_prepare,
626 malloc_atfork_handler_parent,
627 malloc_atfork_handler_child);
628 _malloc_thread_enabled_p = 1;
629}
566#endif 630#endif
567 631
568static void 632static void
@@ -575,19 +639,6 @@ malloc_initialize_1 ()
575 if (__malloc_initialize_hook) 639 if (__malloc_initialize_hook)
576 (*__malloc_initialize_hook) (); 640 (*__malloc_initialize_hook) ();
577 641
578 /* We don't use recursive mutex because pthread_mutexattr_init may
579 call malloc internally. */
580#if 0 /* defined (USE_PTHREAD) */
581 {
582 pthread_mutexattr_t attr;
583
584 pthread_mutexattr_init (&attr);
585 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
586 pthread_mutex_init (&_malloc_mutex, &attr);
587 pthread_mutexattr_destroy (&attr);
588 }
589#endif
590
591 heapsize = HEAP / BLOCKSIZE; 642 heapsize = HEAP / BLOCKSIZE;
592 _heapinfo = (malloc_info *) align (heapsize * sizeof (malloc_info)); 643 _heapinfo = (malloc_info *) align (heapsize * sizeof (malloc_info));
593 if (_heapinfo == NULL) 644 if (_heapinfo == NULL)
@@ -1808,6 +1859,36 @@ memalign (alignment, size)
1808 return result; 1859 return result;
1809} 1860}
1810 1861
1862#ifndef ENOMEM
1863#define ENOMEM 12
1864#endif
1865
1866#ifndef EINVAL
1867#define EINVAL 22
1868#endif
1869
1870int
1871posix_memalign (memptr, alignment, size)
1872 __ptr_t *memptr;
1873 __malloc_size_t alignment;
1874 __malloc_size_t size;
1875{
1876 __ptr_t mem;
1877
1878 if (alignment == 0
1879 || alignment % sizeof (__ptr_t) != 0
1880 || (alignment & (alignment - 1)) != 0)
1881 return EINVAL;
1882
1883 mem = memalign (alignment, size);
1884 if (mem == NULL)
1885 return ENOMEM;
1886
1887 *memptr = mem;
1888
1889 return 0;
1890}
1891
1811#endif /* Not DJGPP v1 */ 1892#endif /* Not DJGPP v1 */
1812/* Allocate memory on a page boundary. 1893/* Allocate memory on a page boundary.
1813 Copyright (C) 1991, 92, 93, 94, 96 Free Software Foundation, Inc. 1894 Copyright (C) 1991, 92, 93, 94, 96 Free Software Foundation, Inc.