diff options
author | ivmai <ivmai> | 2011-04-09 10:50:54 +0000 |
---|---|---|
committer | Ivan Maidanski <ivmai@mail.ru> | 2011-07-26 21:06:57 +0400 |
commit | 00ee2f7451eed8bd1cceda8001df1722067c9f17 (patch) | |
tree | 59f3abf96c4cd02aee4cfb9475c22407a73f5eaa /mallocx.c | |
parent | be457cfd5dde0cc1a1db497a70c15b7bd38153b8 (diff) | |
download | bdwgc-00ee2f7451eed8bd1cceda8001df1722067c9f17.tar.gz |
2011-04-09 Ivan Maidanski <ivmai@mail.ru>
* 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.
Diffstat (limited to 'mallocx.c')
-rw-r--r-- | mallocx.c | 68 |
1 files changed, 62 insertions, 6 deletions
@@ -24,6 +24,7 @@ */ #include <stdio.h> +#include <string.h> #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 <wchar.h> /* 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 */ |