From: ivmai Date: Sat, 9 Apr 2011 10:50:54 +0000 (+0000) Subject: 2011-04-09 Ivan Maidanski X-Git-Tag: gc7_2alpha6~75 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=00ee2f7451eed8bd1cceda8001df1722067c9f17;p=gc 2011-04-09 Ivan Maidanski * dbg_mlc.c (GC_register_finalizer_no_order): Remove redundant declaration. * dbg_mlc.c (GC_debug_malloc_replacement, GC_debug_realloc_replacement): Rename RA to GC_DBG_RA. * malloc.c (GC_debug_malloc_replacement): Ditto. * mallocx.c (GC_debug_realloc_replacement): Ditto. * dbg_mlc.c (GC_store_debug_info): Move proto from dbg_mlc.h. * malloc.c (GC_strdup, GC_strndup, GC_wcsdup): Move to mallocx.c. * malloc.c: Include errno.h only REDIRECT_MALLOC; remove redundant includes of string.h. * mallocx.c: Include string.h (for GC_strdup). * include/private/dbg_mlc.h (GC_store_debug_info): Move declaration to dbg_mlc.c. * include/private/gc_locks.h (UNCOND_LOCK, UNCOND_UNLOCK): Remove redundant trailing ';'. * include/private/gc_priv.h (START_WORLD, COND_DUMP): Ditto. * include/private/gc_locks.h (LOCK, UNLOCK): Place opening '{' properly. * include/private/gc_priv.h (GC_DBG_RA): Move from dbg_mlc.c, malloc.c, mallocx.c. --- diff --git a/ChangeLog b/ChangeLog index db572e12..46cdbc55 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,26 @@ +2011-04-09 Ivan Maidanski + + * dbg_mlc.c (GC_register_finalizer_no_order): Remove redundant + declaration. + * dbg_mlc.c (GC_debug_malloc_replacement, + GC_debug_realloc_replacement): Rename RA to GC_DBG_RA. + * malloc.c (GC_debug_malloc_replacement): Ditto. + * mallocx.c (GC_debug_realloc_replacement): Ditto. + * dbg_mlc.c (GC_store_debug_info): Move proto from dbg_mlc.h. + * malloc.c (GC_strdup, GC_strndup, GC_wcsdup): Move to mallocx.c. + * malloc.c: Include errno.h only REDIRECT_MALLOC; remove redundant + includes of string.h. + * mallocx.c: Include string.h (for GC_strdup). + * include/private/dbg_mlc.h (GC_store_debug_info): Move declaration + to dbg_mlc.c. + * include/private/gc_locks.h (UNCOND_LOCK, UNCOND_UNLOCK): Remove + redundant trailing ';'. + * include/private/gc_priv.h (START_WORLD, COND_DUMP): Ditto. + * include/private/gc_locks.h (LOCK, UNLOCK): Place opening '{' + properly. + * include/private/gc_priv.h (GC_DBG_RA): Move from dbg_mlc.c, + malloc.c, mallocx.c. + 2011-04-07 Ivan Maidanski * alloc.c (GC_check_heap, GC_print_all_smashed): Move the diff --git a/dbg_mlc.c b/dbg_mlc.c index bc14d08c..feaf51ad 100644 --- a/dbg_mlc.c +++ b/dbg_mlc.c @@ -24,10 +24,6 @@ GC_INNER void GC_default_print_heap_obj_proc(ptr_t p); -GC_API void GC_CALL GC_register_finalizer_no_order(void * obj, - GC_finalization_proc fn, void * cd, - GC_finalization_proc *ofn, void * *ocd); - #ifndef SHORT_DBG_HDRS /* Check whether object with base pointer p has debugging info */ /* p is assumed to point to a legitimate object in our part */ @@ -58,7 +54,7 @@ GC_API void GC_CALL GC_register_finalizer_no_order(void * obj, } return(FALSE); } -#endif +#endif /* !SHORT_DBG_HDRS */ #ifdef KEEP_BACK_PTRS @@ -459,16 +455,16 @@ STATIC void GC_debug_print_heap_obj_proc(ptr_t p) GC_INNER void GC_start_debugging(void) { -# ifndef SHORT_DBG_HDRS - GC_check_heap = GC_check_heap_proc; - GC_print_all_smashed = GC_print_all_smashed_proc; -# else - GC_check_heap = GC_do_nothing; - GC_print_all_smashed = GC_do_nothing; -# endif - GC_print_heap_obj = GC_debug_print_heap_obj_proc; - GC_debugging_started = TRUE; - GC_register_displacement((word)sizeof(oh)); +# ifndef SHORT_DBG_HDRS + GC_check_heap = GC_check_heap_proc; + GC_print_all_smashed = GC_print_all_smashed_proc; +# else + GC_check_heap = GC_do_nothing; + GC_print_all_smashed = GC_do_nothing; +# endif + GC_print_heap_obj = GC_debug_print_heap_obj_proc; + GC_debugging_started = TRUE; + GC_register_displacement((word)sizeof(oh)); } size_t GC_debug_header_size = sizeof(oh); @@ -1165,18 +1161,12 @@ GC_API void GC_CALL GC_debug_register_finalizer_ignore_self store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd); } -#ifdef GC_ADD_CALLER -# define RA GC_RETURN_ADDR, -#else -# define RA -#endif - GC_API void * GC_CALL GC_debug_malloc_replacement(size_t lb) { - return GC_debug_malloc(lb, RA "unknown", 0); + return GC_debug_malloc(lb, GC_DBG_RA "unknown", 0); } GC_API void * GC_CALL GC_debug_realloc_replacement(void *p, size_t lb) { - return GC_debug_realloc(p, lb, RA "unknown", 0); + return GC_debug_realloc(p, lb, GC_DBG_RA "unknown", 0); } diff --git a/gcj_mlc.c b/gcj_mlc.c index 6c160c49..7afa6bf2 100644 --- a/gcj_mlc.c +++ b/gcj_mlc.c @@ -199,7 +199,12 @@ static void maybe_finalize(void) return((void *) op); } -GC_INNER void GC_start_debugging(void); +GC_INNER void GC_start_debugging(void); /* defined in dbg_mlc.c */ + +/* Store debugging info into p. Return displaced pointer. */ +/* Assumes we don't hold allocation lock. */ +GC_INNER ptr_t GC_store_debug_info(ptr_t p, word sz, const char *str, + word integer); /* Similar to GC_gcj_malloc, but add debug info. This is allocated */ /* with GC_gcj_debug_kind. */ @@ -209,7 +214,7 @@ GC_API void * GC_CALL GC_debug_gcj_malloc(size_t lb, void * result; DCL_LOCK_STATE; - /* We're careful to avoid extra calls, which could */ + /* We're careful to avoid extra calls, which could */ /* confuse the backtrace. */ LOCK(); maybe_finalize(); diff --git a/include/private/dbg_mlc.h b/include/private/dbg_mlc.h index 45dc1812..a6f2ef6a 100644 --- a/include/private/dbg_mlc.h +++ b/include/private/dbg_mlc.h @@ -160,9 +160,4 @@ typedef struct { # define GC_HAS_DEBUG_INFO(p) GC_has_other_debug_info(p) #endif -/* Store debugging info into p. Return displaced pointer. */ -/* Assumes we don't hold allocation lock. */ -GC_INNER ptr_t GC_store_debug_info(ptr_t p, word sz, const char *str, - word integer); - #endif /* _DBG_MLC_H */ diff --git a/include/private/gc_locks.h b/include/private/gc_locks.h index 3b0d1c3a..f5119b89 100644 --- a/include/private/gc_locks.h +++ b/include/private/gc_locks.h @@ -64,8 +64,8 @@ { GC_ASSERT(I_HOLD_LOCK()); UNSET_LOCK_HOLDER(); \ LeaveCriticalSection(&GC_allocate_ml); } # else -# define UNCOND_LOCK() EnterCriticalSection(&GC_allocate_ml); -# define UNCOND_UNLOCK() LeaveCriticalSection(&GC_allocate_ml); +# define UNCOND_LOCK() EnterCriticalSection(&GC_allocate_ml) +# define UNCOND_UNLOCK() LeaveCriticalSection(&GC_allocate_ml) # endif /* !GC_ASSERTIONS */ # define SET_LOCK_HOLDER() GC_lock_holder = GetCurrentThreadId() # define UNSET_LOCK_HOLDER() GC_lock_holder = NO_THREAD @@ -125,18 +125,17 @@ /* GC_call_with_alloc_lock. */ # ifdef GC_ASSERTIONS # define UNCOND_LOCK() \ - { if (AO_test_and_set_acquire(&GC_allocate_lock) == AO_TS_SET) \ - GC_lock(); \ - SET_LOCK_HOLDER(); } + { if (AO_test_and_set_acquire(&GC_allocate_lock) == AO_TS_SET) \ + GC_lock(); \ + SET_LOCK_HOLDER(); } # define UNCOND_UNLOCK() \ - { GC_ASSERT(I_HOLD_LOCK()); UNSET_LOCK_HOLDER(); \ - AO_CLEAR(&GC_allocate_lock); } + { GC_ASSERT(I_HOLD_LOCK()); UNSET_LOCK_HOLDER(); \ + AO_CLEAR(&GC_allocate_lock); } # else # define UNCOND_LOCK() \ - { if (AO_test_and_set_acquire(&GC_allocate_lock) == AO_TS_SET) \ - GC_lock(); } -# define UNCOND_UNLOCK() \ - AO_CLEAR(&GC_allocate_lock) + { if (AO_test_and_set_acquire(&GC_allocate_lock) == AO_TS_SET) \ + GC_lock(); } +# define UNCOND_UNLOCK() AO_CLEAR(&GC_allocate_lock) # endif /* !GC_ASSERTIONS */ # else /* THREAD_LOCAL_ALLOC || USE_PTHREAD_LOCKS */ # ifndef USE_PTHREAD_LOCKS @@ -147,18 +146,17 @@ # include GC_EXTERN pthread_mutex_t GC_allocate_ml; # ifdef GC_ASSERTIONS -# define UNCOND_LOCK() \ - { GC_lock(); \ - SET_LOCK_HOLDER(); } +# define UNCOND_LOCK() { GC_lock(); SET_LOCK_HOLDER(); } # define UNCOND_UNLOCK() \ { GC_ASSERT(I_HOLD_LOCK()); UNSET_LOCK_HOLDER(); \ pthread_mutex_unlock(&GC_allocate_ml); } # else /* !GC_ASSERTIONS */ # if defined(NO_PTHREAD_TRYLOCK) -# define UNCOND_LOCK() GC_lock(); +# define UNCOND_LOCK() GC_lock() # else /* !defined(NO_PTHREAD_TRYLOCK) */ # define UNCOND_LOCK() \ - { if (0 != pthread_mutex_trylock(&GC_allocate_ml)) GC_lock(); } + { if (0 != pthread_mutex_trylock(&GC_allocate_ml)) \ + GC_lock(); } # endif # define UNCOND_UNLOCK() pthread_mutex_unlock(&GC_allocate_ml) # endif /* !GC_ASSERTIONS */ @@ -201,8 +199,8 @@ #if defined(UNCOND_LOCK) && !defined(LOCK) /* At least two thread running; need to lock. */ -# define LOCK() if (GC_need_to_lock) { UNCOND_LOCK(); } -# define UNLOCK() if (GC_need_to_lock) { UNCOND_UNLOCK(); } +# define LOCK() { if (GC_need_to_lock) UNCOND_LOCK(); } +# define UNLOCK() { if (GC_need_to_lock) UNCOND_UNLOCK(); } #endif # ifndef ENTER_GC diff --git a/include/private/gc_priv.h b/include/private/gc_priv.h index 89195327..7f3f5081 100644 --- a/include/private/gc_priv.h +++ b/include/private/gc_priv.h @@ -399,7 +399,7 @@ typedef char * ptr_t; /* A generic pointer to which we can add */ # define START_WORLD() \ PCR_ThCtl_SetExclusiveMode(PCR_ThCtl_ExclusiveMode_null, \ PCR_allSigsBlocked, \ - PCR_waitForever); + PCR_waitForever) # else # if defined(GC_WIN32_THREADS) || defined(GC_PTHREADS) GC_INNER void GC_stop_world(void); @@ -1709,6 +1709,12 @@ GC_INNER ptr_t GC_allocobj(size_t sz, int kind); GC_INNER void * GC_clear_stack(void *); /* in misc.c, behaves like identity. */ +#ifdef GC_ADD_CALLER +# define GC_DBG_RA GC_RETURN_ADDR, +#else +# define GC_DBG_RA /* empty */ +#endif + /* We make the GC_clear_stack() call a tail one, hoping to get more of */ /* the stack. */ #define GENERAL_MALLOC(lb,k) \ @@ -1805,9 +1811,9 @@ GC_EXTERN GC_bool GC_have_errors; /* We saw a smashed or leaked object. */ #ifndef NO_DEBUGGING GC_EXTERN GC_bool GC_dump_regularly; /* Generate regular debugging dumps. */ -# define COND_DUMP if (GC_dump_regularly) GC_dump(); +# define COND_DUMP if (GC_dump_regularly) GC_dump() #else -# define COND_DUMP +# define COND_DUMP /* empty */ #endif #ifdef KEEP_BACK_PTRS diff --git a/malloc.c b/malloc.c index 026e3f9c..781d65ab 100644 --- a/malloc.c +++ b/malloc.c @@ -17,9 +17,6 @@ #include #include -#ifndef MSWINCE -# include -#endif GC_INNER void GC_extend_size_map(size_t); /* in misc.c */ @@ -232,65 +229,6 @@ GC_API void * GC_CALL GC_generic_malloc(size_t lb, int k) } } -/* provide a version of strdup() that uses the collector to allocate the - copy of the string */ -GC_API char * GC_CALL GC_strdup(const char *s) -{ - char *copy; - size_t lb; - if (s == NULL) return NULL; - lb = strlen(s) + 1; - if ((copy = GC_malloc_atomic(lb)) == NULL) { -# ifndef MSWINCE - errno = ENOMEM; -# endif - return NULL; - } -# ifndef MSWINCE - strcpy(copy, s); -# else - /* strcpy() is deprecated in WinCE */ - memcpy(copy, s, lb); -# endif - return copy; -} - -GC_API char * GC_CALL GC_strndup(const char *str, size_t size) -{ - char *copy; - size_t len = strlen(str); /* str is expected to be non-NULL */ - if (len > size) - len = size; - copy = GC_malloc_atomic(len + 1); - if (copy == NULL) { -# ifndef MSWINCE - errno = ENOMEM; -# endif - return NULL; - } - BCOPY(str, copy, len); - copy[len] = '\0'; - return copy; -} - -#ifdef GC_REQUIRE_WCSDUP -# include /* for wcslen() */ - - GC_API wchar_t * GC_CALL GC_wcsdup(const wchar_t *str) - { - size_t lb = (wcslen(str) + 1) * sizeof(wchar_t); - wchar_t *copy = GC_malloc_atomic(lb); - if (copy == NULL) { -# ifndef MSWINCE - errno = ENOMEM; -# endif - return NULL; - } - BCOPY(str, copy, lb); - return copy; - } -#endif /* GC_REQUIRE_WCSDUP */ - /* Allocate lb bytes of composite (pointerful) data */ #ifdef THREAD_LOCAL_ALLOC GC_INNER void * GC_core_malloc(size_t lb) @@ -326,19 +264,19 @@ GC_API char * GC_CALL GC_strndup(const char *str, size_t size) } } -# ifdef REDIRECT_MALLOC +#ifdef REDIRECT_MALLOC + +# ifndef MSWINCE +# include +# endif /* Avoid unnecessary nested procedure calls here, by #defining some */ /* malloc replacements. Otherwise we end up saving a */ /* meaningless return address in the object. It also speeds things up, */ /* but it is admittedly quite ugly. */ -# ifdef GC_ADD_CALLER -# define RA GC_RETURN_ADDR, -# else -# define RA -# endif + # define GC_debug_malloc_replacement(lb) \ - GC_debug_malloc(lb, RA "unknown", 0) + GC_debug_malloc(lb, GC_DBG_RA "unknown", 0) void * malloc(size_t lb) { @@ -381,7 +319,7 @@ void * malloc(size_t lb) WARN("Failed to find ld.so text mapping: Expect crash\n", 0); } } -#endif +#endif /* GC_LINUX_THREADS */ void * calloc(size_t n, size_t lb) { @@ -408,7 +346,6 @@ void * calloc(size_t n, size_t lb) } #ifndef strdup -# include char *strdup(const char *s) { size_t lb = strlen(s) + 1; @@ -427,7 +364,6 @@ void * calloc(size_t n, size_t lb) #ifndef strndup /* This is similar to strdup(). */ -# include char *strndup(const char *str, size_t size) { char *copy; @@ -447,7 +383,7 @@ void * calloc(size_t n, size_t lb) #undef GC_debug_malloc_replacement -# endif /* REDIRECT_MALLOC */ +#endif /* REDIRECT_MALLOC */ /* Explicitly deallocate an object p. */ GC_API void GC_CALL GC_free(void * p) diff --git a/mallocx.c b/mallocx.c index 8d6939ba..82088c5b 100644 --- a/mallocx.c +++ b/mallocx.c @@ -24,6 +24,7 @@ */ #include +#include #ifdef MSWINCE # ifndef WIN32_LEAN_AND_MEAN @@ -152,13 +153,9 @@ GC_API void * GC_CALL GC_realloc(void * p, size_t lb) # ifdef REDIRECT_REALLOC /* As with malloc, avoid two levels of extra calls here. */ -# ifdef GC_ADD_CALLER -# define RA GC_RETURN_ADDR, -# else -# define RA -# endif + # define GC_debug_realloc_replacement(p, lb) \ - GC_debug_realloc(p, lb, RA "unknown", 0) + GC_debug_realloc(p, lb, GC_DBG_RA "unknown", 0) void * realloc(void * p, size_t lb) { @@ -598,3 +595,62 @@ GC_API int GC_CALL GC_posix_memalign(void **memptr, size_t align, size_t lb) } } #endif /* ATOMIC_UNCOLLECTABLE */ + +/* provide a version of strdup() that uses the collector to allocate the + copy of the string */ +GC_API char * GC_CALL GC_strdup(const char *s) +{ + char *copy; + size_t lb; + if (s == NULL) return NULL; + lb = strlen(s) + 1; + if ((copy = GC_malloc_atomic(lb)) == NULL) { +# ifndef MSWINCE + errno = ENOMEM; +# endif + return NULL; + } +# ifndef MSWINCE + strcpy(copy, s); +# else + /* strcpy() is deprecated in WinCE */ + memcpy(copy, s, lb); +# endif + return copy; +} + +GC_API char * GC_CALL GC_strndup(const char *str, size_t size) +{ + char *copy; + size_t len = strlen(str); /* str is expected to be non-NULL */ + if (len > size) + len = size; + copy = GC_malloc_atomic(len + 1); + if (copy == NULL) { +# ifndef MSWINCE + errno = ENOMEM; +# endif + return NULL; + } + BCOPY(str, copy, len); + copy[len] = '\0'; + return copy; +} + +#ifdef GC_REQUIRE_WCSDUP +# include /* for wcslen() */ + + GC_API wchar_t * GC_CALL GC_wcsdup(const wchar_t *str) + { + size_t lb = (wcslen(str) + 1) * sizeof(wchar_t); + wchar_t *copy = GC_malloc_atomic(lb); + if (copy == NULL) { +# ifndef MSWINCE + errno = ENOMEM; +# endif + return NULL; + } + BCOPY(str, copy, lb); + return copy; + } +#endif /* GC_REQUIRE_WCSDUP */