diff options
author | raf <none@none> | 2007-03-20 19:30:49 -0700 |
---|---|---|
committer | raf <none@none> | 2007-03-20 19:30:49 -0700 |
commit | 1d53067866b073ea6710000ba4dd448441361988 (patch) | |
tree | fbac45aa59a72e34fdd5e5fccc91c5bb1d0f0df5 /usr/src/lib/watchmalloc | |
parent | 84f43ade33b1b00348bdcdfaca9f71f6a0c786eb (diff) | |
download | illumos-gate-1d53067866b073ea6710000ba4dd448441361988.tar.gz |
6533630 all malloc interposition libraries need to be made fork-safe
--HG--
rename : usr/src/lib/libmapmalloc/common/malloc_debug.c => deleted_files/usr/src/lib/libmapmalloc/common/malloc_debug.c
Diffstat (limited to 'usr/src/lib/watchmalloc')
-rw-r--r-- | usr/src/lib/watchmalloc/Makefile.com | 4 | ||||
-rw-r--r-- | usr/src/lib/watchmalloc/common/mallint.h | 11 | ||||
-rw-r--r-- | usr/src/lib/watchmalloc/common/malloc.c | 52 |
3 files changed, 40 insertions, 27 deletions
diff --git a/usr/src/lib/watchmalloc/Makefile.com b/usr/src/lib/watchmalloc/Makefile.com index 4c7eb9f66d..58f9e072aa 100644 --- a/usr/src/lib/watchmalloc/Makefile.com +++ b/usr/src/lib/watchmalloc/Makefile.com @@ -19,7 +19,7 @@ # CDDL HEADER END # # -# Copyright 2006 Sun Microsystems, Inc. All rights reserved. +# Copyright 2007 Sun Microsystems, Inc. All rights reserved. # Use is subject to license terms. # # ident "%Z%%M% %I% %E% SMI" @@ -39,7 +39,7 @@ LIBS = $(DYNLIB) LDLIBS += -lc CFLAGS += $(CCVERBOSE) CFLAGS64 += $(CCVERBOSE) -CPPFLAGS += -D_REENTRANT -I../common +CPPFLAGS += -I../common -I../../common/inc -D_REENTRANT DYNFLAGS += $(ZINTERPOSE) .KEEP_STATE: diff --git a/usr/src/lib/watchmalloc/common/mallint.h b/usr/src/lib/watchmalloc/common/mallint.h index 4d9b309135..f40a4d34cc 100644 --- a/usr/src/lib/watchmalloc/common/mallint.h +++ b/usr/src/lib/watchmalloc/common/mallint.h @@ -20,7 +20,7 @@ */ /* - * Copyright 2006 Sun Microsystems, Inc. All rights reserved. + * Copyright 2007 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ @@ -29,10 +29,6 @@ #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.2 */ -#ifndef _REENTRANT -#define _REENTRANT -#endif - #include <stdlib.h> #include <unistd.h> #include <fcntl.h> @@ -40,6 +36,7 @@ #include <errno.h> #include <memory.h> #include <thread.h> +#include <pthread.h> #include <synch.h> #include <procfs.h> #include <limits.h> @@ -140,7 +137,3 @@ typedef struct _t_ { #define MAX_GETCORE (size_t)(SSIZE_MAX & ~(ALIGN - 1)) /* round down ALIGN */ #define MAX_MALLOC (size_t)(SIZE_MAX - CORESIZE - 3 * ALIGN) /* overflow chk */ #define MAX_ALIGN (1 + (size_t)SSIZE_MAX) - -/* where are these *really* declared? */ -extern int _mutex_lock(mutex_t *mp); -extern int _mutex_unlock(mutex_t *mp); diff --git a/usr/src/lib/watchmalloc/common/malloc.c b/usr/src/lib/watchmalloc/common/malloc.c index 2e7dfbadbd..c62459c007 100644 --- a/usr/src/lib/watchmalloc/common/malloc.c +++ b/usr/src/lib/watchmalloc/common/malloc.c @@ -20,7 +20,7 @@ */ /* - * Copyright 2006 Sun Microsystems, Inc. All rights reserved. + * Copyright 2007 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ @@ -58,6 +58,7 @@ * Otherwise, it is always 0. */ +#include <c_synonyms.h> #include "mallint.h" static mutex_t __watch_malloc_lock = DEFAULTMUTEX; @@ -164,9 +165,9 @@ void * malloc(size_t size) { void *ret; - _mutex_lock(&__watch_malloc_lock); + (void) mutex_lock(&__watch_malloc_lock); ret = malloc_unlocked(size); - _mutex_unlock(&__watch_malloc_lock); + (void) mutex_unlock(&__watch_malloc_lock); return (ret); } @@ -299,10 +300,10 @@ realloc(void *old, size_t size) } /* pointer to the block */ - _mutex_lock(&__watch_malloc_lock); + (void) mutex_lock(&__watch_malloc_lock); if (old == NULL) { new = malloc_unlocked(size); - _mutex_unlock(&__watch_malloc_lock); + (void) mutex_unlock(&__watch_malloc_lock); return (new); } @@ -318,7 +319,7 @@ realloc(void *old, size_t size) if (!ISBIT0(ts)) { /* XXX; complain here! */ protect(tp); - _mutex_unlock(&__watch_malloc_lock); + (void) mutex_unlock(&__watch_malloc_lock); errno = EINVAL; return (NULL); } @@ -327,7 +328,7 @@ realloc(void *old, size_t size) if (size == SIZE(tp)) { /* nothing to do */ SIZE(tp) = ts; protect(tp); - _mutex_unlock(&__watch_malloc_lock); + (void) mutex_unlock(&__watch_malloc_lock); return (old); } @@ -336,7 +337,7 @@ realloc(void *old, size_t size) if (size == 0) { SETOLD01(SIZE(tp), ts); free_unlocked(old); - _mutex_unlock(&__watch_malloc_lock); + (void) mutex_unlock(&__watch_malloc_lock); return (NULL); } goto call_malloc; @@ -394,7 +395,7 @@ chop_big: /* the previous block may be free */ SETOLD01(SIZE(tp), ts); protect(tp); - _mutex_unlock(&__watch_malloc_lock); + (void) mutex_unlock(&__watch_malloc_lock); return (old); } @@ -406,7 +407,7 @@ call_malloc: /* call malloc to get a new block */ ts = size; (void) memcpy(new, old, ts); free_unlocked(old); - _mutex_unlock(&__watch_malloc_lock); + (void) mutex_unlock(&__watch_malloc_lock); return (new); } @@ -432,7 +433,7 @@ call_malloc: /* call malloc to get a new block */ if (size < SIZE(tp)) /* case 1. */ { SETOLD01(SIZE(tp), ts); protect(tp); - _mutex_unlock(&__watch_malloc_lock); + (void) mutex_unlock(&__watch_malloc_lock); return (old); } else if (size < MINSIZE) /* case 2. */ { size = MINSIZE; @@ -461,7 +462,7 @@ call_malloc: /* call malloc to get a new block */ } SETOLD01(SIZE(tp), ts); protect(tp); - _mutex_unlock(&__watch_malloc_lock); + (void) mutex_unlock(&__watch_malloc_lock); /* malloc() sets errno */ return (NULL); } @@ -1075,9 +1076,9 @@ t_splay(TREE *tp) void free(void *old) { - _mutex_lock(&__watch_malloc_lock); + (void) mutex_lock(&__watch_malloc_lock); free_unlocked(old); - _mutex_unlock(&__watch_malloc_lock); + (void) mutex_unlock(&__watch_malloc_lock); } @@ -1168,7 +1169,7 @@ memalign(size_t align, size_t nbytes) /* malloc sets errno */ return (NULL); } - _mutex_lock(&__watch_malloc_lock); + (void) mutex_lock(&__watch_malloc_lock); /* * get size of the entire block (overhead and all) @@ -1242,7 +1243,7 @@ memalign(size_t align, size_t nbytes) } copy_pattern(LIVEPAT, aligned_blk); protect(aligned_blk); - _mutex_unlock(&__watch_malloc_lock); + (void) mutex_unlock(&__watch_malloc_lock); return (DATA(aligned_blk)); } @@ -1440,3 +1441,22 @@ unprotect(TREE *tp) ctl.prwatch.pr_wflags = 0; /* clear the watched area */ (void) write(ctlfd, &ctl, sizeof (ctl)); } + +static void +malloc_prepare() +{ + (void) mutex_lock(&__watch_malloc_lock); +} + +static void +malloc_release() +{ + (void) mutex_unlock(&__watch_malloc_lock); +} + +#pragma init(malloc_init) +static void +malloc_init(void) +{ + (void) pthread_atfork(malloc_prepare, malloc_release, malloc_release); +} |