aboutsummaryrefslogtreecommitdiffstats
path: root/src/gmalloc.c
diff options
context:
space:
mode:
authorYAMAMOTO Mitsuharu2007-03-28 08:16:05 +0000
committerYAMAMOTO Mitsuharu2007-03-28 08:16:05 +0000
commit2f213514dfe35d154c853df85a0169a7bb851f3e (patch)
tree1ccfeb6f24fe559d9736427eb34f6f5e619eb1bb /src/gmalloc.c
parente43c7935f3dc404c05506c193fd2609cebe4223f (diff)
downloademacs-2f213514dfe35d154c853df85a0169a7bb851f3e.tar.gz
emacs-2f213514dfe35d154c853df85a0169a7bb851f3e.zip
[HAVE_GTK_AND_PTHREAD]: Define USE_PTHREAD.
[USE_PTHREAD]: Include pthread.h. (malloc_init_once_control, _malloc_mutex) [USE_PTHREAD]: New variables. (malloc_initialize_1): New function created from __malloc_initialize. (__malloc_initialize): Use it. (LOCK, UNLOCK): New macros to make malloc etc. thread safe. (_malloc_internal, _free_internal, _realloc_internal): Use them.
Diffstat (limited to 'src/gmalloc.c')
-rw-r--r--src/gmalloc.c78
1 files changed, 65 insertions, 13 deletions
diff --git a/src/gmalloc.c b/src/gmalloc.c
index 3f32617fc64..50535d4940c 100644
--- a/src/gmalloc.c
+++ b/src/gmalloc.c
@@ -1,6 +1,9 @@
1/* This file is no longer automatically generated from libc. */ 1/* This file is no longer automatically generated from libc. */
2 2
3#define _MALLOC_INTERNAL 3#define _MALLOC_INTERNAL
4#ifdef HAVE_GTK_AND_PTHREAD
5#define USE_PTHREAD
6#endif
4 7
5/* The malloc headers and source files from the C library follow here. */ 8/* The malloc headers and source files from the C library follow here. */
6 9
@@ -73,6 +76,10 @@ Fifth Floor, Boston, MA 02110-1301, USA.
73#include <unistd.h> 76#include <unistd.h>
74#endif 77#endif
75 78
79#ifdef USE_PTHREAD
80#include <pthread.h>
81#endif
82
76#endif /* _MALLOC_INTERNAL. */ 83#endif /* _MALLOC_INTERNAL. */
77 84
78 85
@@ -229,6 +236,15 @@ extern __ptr_t _malloc_internal PP ((__malloc_size_t __size));
229extern __ptr_t _realloc_internal PP ((__ptr_t __ptr, __malloc_size_t __size)); 236extern __ptr_t _realloc_internal PP ((__ptr_t __ptr, __malloc_size_t __size));
230extern void _free_internal PP ((__ptr_t __ptr)); 237extern void _free_internal PP ((__ptr_t __ptr));
231 238
239#ifdef USE_PTHREAD
240extern pthread_mutex_t _malloc_mutex;
241#define LOCK() pthread_mutex_lock (&_malloc_mutex)
242#define UNLOCK() pthread_mutex_unlock (&_malloc_mutex)
243#else
244#define LOCK()
245#define UNLOCK()
246#endif
247
232#endif /* _MALLOC_INTERNAL. */ 248#endif /* _MALLOC_INTERNAL. */
233 249
234/* Given an address in the middle of a malloc'd object, 250/* Given an address in the middle of a malloc'd object,
@@ -536,13 +552,14 @@ register_heapinfo ()
536 _heapinfo[block + blocks].busy.info.size = -blocks; 552 _heapinfo[block + blocks].busy.info.size = -blocks;
537} 553}
538 554
539/* Set everything up and remember that we have. */ 555#ifdef USE_PTHREAD
540int 556static pthread_once_t malloc_init_once_control = PTHREAD_ONCE_INIT;
541__malloc_initialize () 557pthread_mutex_t _malloc_mutex;
542{ 558#endif
543 if (__malloc_initialized)
544 return 0;
545 559
560static void
561malloc_initialize_1 ()
562{
546#ifdef GC_MCHECK 563#ifdef GC_MCHECK
547 mcheck (NULL); 564 mcheck (NULL);
548#endif 565#endif
@@ -550,10 +567,21 @@ __malloc_initialize ()
550 if (__malloc_initialize_hook) 567 if (__malloc_initialize_hook)
551 (*__malloc_initialize_hook) (); 568 (*__malloc_initialize_hook) ();
552 569
570#ifdef USE_PTHREAD
571 {
572 pthread_mutexattr_t attr;
573
574 pthread_mutexattr_init (&attr);
575 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
576 pthread_mutex_init (&_malloc_mutex, &attr);
577 pthread_mutexattr_destroy (&attr);
578 }
579#endif
580
553 heapsize = HEAP / BLOCKSIZE; 581 heapsize = HEAP / BLOCKSIZE;
554 _heapinfo = (malloc_info *) align (heapsize * sizeof (malloc_info)); 582 _heapinfo = (malloc_info *) align (heapsize * sizeof (malloc_info));
555 if (_heapinfo == NULL) 583 if (_heapinfo == NULL)
556 return 0; 584 return;
557 memset (_heapinfo, 0, heapsize * sizeof (malloc_info)); 585 memset (_heapinfo, 0, heapsize * sizeof (malloc_info));
558 _heapinfo[0].free.size = 0; 586 _heapinfo[0].free.size = 0;
559 _heapinfo[0].free.next = _heapinfo[0].free.prev = 0; 587 _heapinfo[0].free.next = _heapinfo[0].free.prev = 0;
@@ -565,7 +593,23 @@ __malloc_initialize ()
565 593
566 __malloc_initialized = 1; 594 __malloc_initialized = 1;
567 PROTECT_MALLOC_STATE (1); 595 PROTECT_MALLOC_STATE (1);
568 return 1; 596 return;
597}
598
599/* Set everything up and remember that we have. */
600int
601__malloc_initialize ()
602{
603#ifdef USE_PTHREAD
604 pthread_once (&malloc_init_once_control, malloc_initialize_1);
605#else
606 if (__malloc_initialized)
607 return 0;
608
609 malloc_initialize_1 ();
610#endif
611
612 return __malloc_initialized;
569} 613}
570 614
571static int morecore_recursing; 615static int morecore_recursing;
@@ -708,6 +752,7 @@ _malloc_internal (size)
708 return NULL; 752 return NULL;
709#endif 753#endif
710 754
755 LOCK ();
711 PROTECT_MALLOC_STATE (0); 756 PROTECT_MALLOC_STATE (0);
712 757
713 if (size < sizeof (struct list)) 758 if (size < sizeof (struct list))
@@ -765,7 +810,7 @@ _malloc_internal (size)
765 if (result == NULL) 810 if (result == NULL)
766 { 811 {
767 PROTECT_MALLOC_STATE (1); 812 PROTECT_MALLOC_STATE (1);
768 return NULL; 813 goto out;
769 } 814 }
770 815
771 /* Link all fragments but the first into the free list. */ 816 /* Link all fragments but the first into the free list. */
@@ -831,7 +876,7 @@ _malloc_internal (size)
831 } 876 }
832 result = morecore (wantblocks * BLOCKSIZE); 877 result = morecore (wantblocks * BLOCKSIZE);
833 if (result == NULL) 878 if (result == NULL)
834 return NULL; 879 goto out;
835 block = BLOCK (result); 880 block = BLOCK (result);
836 /* Put the new block at the end of the free list. */ 881 /* Put the new block at the end of the free list. */
837 _heapinfo[block].free.size = wantblocks; 882 _heapinfo[block].free.size = wantblocks;
@@ -886,6 +931,8 @@ _malloc_internal (size)
886 } 931 }
887 932
888 PROTECT_MALLOC_STATE (1); 933 PROTECT_MALLOC_STATE (1);
934 out:
935 UNLOCK ();
889 return result; 936 return result;
890} 937}
891 938
@@ -996,6 +1043,7 @@ _free_internal (ptr)
996 if (ptr == NULL) 1043 if (ptr == NULL)
997 return; 1044 return;
998 1045
1046 LOCK ();
999 PROTECT_MALLOC_STATE (0); 1047 PROTECT_MALLOC_STATE (0);
1000 1048
1001 for (l = _aligned_blocks; l != NULL; l = l->next) 1049 for (l = _aligned_blocks; l != NULL; l = l->next)
@@ -1221,6 +1269,7 @@ _free_internal (ptr)
1221 } 1269 }
1222 1270
1223 PROTECT_MALLOC_STATE (1); 1271 PROTECT_MALLOC_STATE (1);
1272 UNLOCK ();
1224} 1273}
1225 1274
1226/* Return memory to the heap. */ 1275/* Return memory to the heap. */
@@ -1384,6 +1433,7 @@ _realloc_internal (ptr, size)
1384 1433
1385 block = BLOCK (ptr); 1434 block = BLOCK (ptr);
1386 1435
1436 LOCK ();
1387 PROTECT_MALLOC_STATE (0); 1437 PROTECT_MALLOC_STATE (0);
1388 1438
1389 type = _heapinfo[block].busy.type; 1439 type = _heapinfo[block].busy.type;
@@ -1398,7 +1448,7 @@ _realloc_internal (ptr, size)
1398 { 1448 {
1399 memcpy (result, ptr, size); 1449 memcpy (result, ptr, size);
1400 _free_internal (ptr); 1450 _free_internal (ptr);
1401 return result; 1451 goto out;
1402 } 1452 }
1403 } 1453 }
1404 1454
@@ -1451,7 +1501,7 @@ _realloc_internal (ptr, size)
1451 (void) _malloc_internal (blocks * BLOCKSIZE); 1501 (void) _malloc_internal (blocks * BLOCKSIZE);
1452 _free_internal (previous); 1502 _free_internal (previous);
1453 } 1503 }
1454 return NULL; 1504 goto out;
1455 } 1505 }
1456 if (ptr != result) 1506 if (ptr != result)
1457 memmove (result, ptr, blocks * BLOCKSIZE); 1507 memmove (result, ptr, blocks * BLOCKSIZE);
@@ -1471,7 +1521,7 @@ _realloc_internal (ptr, size)
1471 and copy the lesser of the new size and the old. */ 1521 and copy the lesser of the new size and the old. */
1472 result = _malloc_internal (size); 1522 result = _malloc_internal (size);
1473 if (result == NULL) 1523 if (result == NULL)
1474 return NULL; 1524 goto out;
1475 memcpy (result, ptr, min (size, (__malloc_size_t) 1 << type)); 1525 memcpy (result, ptr, min (size, (__malloc_size_t) 1 << type));
1476 _free_internal (ptr); 1526 _free_internal (ptr);
1477 } 1527 }
@@ -1479,6 +1529,8 @@ _realloc_internal (ptr, size)
1479 } 1529 }
1480 1530
1481 PROTECT_MALLOC_STATE (1); 1531 PROTECT_MALLOC_STATE (1);
1532 out:
1533 UNLOCK ();
1482 return result; 1534 return result;
1483} 1535}
1484 1536