Skip to content

Commit

Permalink
Change GC_warn_proc API to accept const C string
Browse files Browse the repository at this point in the history
* docs/debugging.md (Other Signals): Document GC_set_warn_proc
better.
* gc.man (.TH): Update date.
* gc.man (GC_set_warn_proc): Add const qualifier to char*.
* include/gc/gc.h (GC_warn_proc, GC_ignore_warn_proc): Likewise.
* misc.c (GC_default_warn_proc, GC_ignore_warn_proc): Likewise.
* tests/gctest.c (warn_proc): Likewise.
* include/gc/gc.h (GC_warn_proc): Update comment (document the
change).
* include/private/gc_priv.h (WARN): Remove no-const cast.
* misc.c [CPPCHECK] (GC_default_warn_proc): Remove GC_noop1_ptr()
call.
* tests/gctest.c [CPPCHECK] (warn_proc): Likewise.
  • Loading branch information
ivmai committed Jun 14, 2024
1 parent ff4bceb commit 4f73bae
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 21 deletions.
3 changes: 2 additions & 1 deletion docs/debugging.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ to be permanent, the warning indicates a memory leak.
introducing a bounded leak by ignoring them. If the data structures being
allocated are intended to be permanent, then it is also safe to ignore them.
The warnings can be turned off by calling `GC_set_warn_proc` with
a procedure that ignores these warnings (e.g. by doing absolutely nothing).
a procedure that ignores these warnings (e.g. by doing absolutely nothing
like `GC_ignore_warn_proc` does).

## The Collector References a Bad Address in GC_malloc

Expand Down
4 changes: 2 additions & 2 deletions gc.man
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH BDWGC 3 "10 Jun 2024"
.TH BDWGC 3 "14 Jun 2024"
.SH NAME
GC_malloc, GC_malloc_atomic, GC_free, GC_realloc, GC_enable_incremental,
GC_register_finalizer, GC_malloc_ignore_off_page,
Expand All @@ -21,7 +21,7 @@ void * GC_malloc_ignore_off_page(size_t size);
.br
void * GC_malloc_atomic_ignore_off_page(size_t size);
.br
void GC_set_warn_proc(void (*proc)(char *, GC_uintptr_t));
void GC_set_warn_proc(void (*proc)(const char *, GC_uintptr_t));
.br
.sp
cc ... -lgc
Expand Down
9 changes: 5 additions & 4 deletions include/gc/gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -1536,16 +1536,17 @@ GC_API void GC_CALL GC_noop1_ptr(volatile void *);
/* p may not be a NULL pointer. msg is printf format string (arg must */
/* match the format). Both the setter and the getter acquire the */
/* allocator lock (in the reader mode in case of the getter) to avoid */
/* data race. In GC v7.1 and before, the setter returned the old */
/* warn_proc value. */
typedef void (GC_CALLBACK * GC_warn_proc)(char * /* msg */,
/* data race. In GC v7.1 and before: the setter returned the value of */
/* old warn_proc. In GC v8.2.x and before: msg pointer type had no */
/* const qualifier. */
typedef void (GC_CALLBACK * GC_warn_proc)(const char * /* msg */,
GC_uintptr_t /* arg */);
GC_API void GC_CALL GC_set_warn_proc(GC_warn_proc /* p */) GC_ATTR_NONNULL(1);
GC_API GC_warn_proc GC_CALL GC_get_warn_proc(void);

/* GC_ignore_warn_proc may be used as an argument for GC_set_warn_proc */
/* to suppress all warnings (unless statistics printing is turned on). */
GC_API void GC_CALLBACK GC_ignore_warn_proc(char *, GC_uintptr_t);
GC_API void GC_CALLBACK GC_ignore_warn_proc(const char *, GC_uintptr_t);

/* Change file descriptor of GC log. Unavailable on some targets. */
GC_API void GC_CALL GC_set_log_fd(int);
Expand Down
4 changes: 1 addition & 3 deletions include/private/gc_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -752,9 +752,7 @@ EXTERN_C_BEGIN
/* The argument (if any) format specifier should be: */
/* "%s", "%p", "%"WARN_PRIdPTR or "%"WARN_PRIuPTR. */
#define WARN(msg, arg) \
(*GC_current_warn_proc)((/* no const */ char *) \
(word)("GC Warning: " msg), \
(GC_uintptr_t)(arg))
GC_current_warn_proc("GC Warning: " msg, (GC_uintptr_t)(arg))
GC_EXTERN GC_warn_proc GC_current_warn_proc;

/* Print format type macro for decimal signed_word value passed WARN(). */
Expand Down
9 changes: 2 additions & 7 deletions misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1998,21 +1998,16 @@ void GC_err_puts(const char *s)
(void)WRITE(GC_stderr, s, strlen(s)); /* ignore errors */
}

STATIC void GC_CALLBACK GC_default_warn_proc(char *msg, GC_uintptr_t arg)
STATIC void GC_CALLBACK GC_default_warn_proc(const char *msg, GC_uintptr_t arg)
{
/* TODO: Add assertion on arg comply with msg (format). */
# if defined(CPPCHECK)
/* Workaround "parameter can be declared as pointer to const" */
/* cppcheck warning. */
GC_noop1_ptr(msg);
# endif
GC_warn_printf(msg, arg);
}

GC_INNER GC_warn_proc GC_current_warn_proc = GC_default_warn_proc;

/* This is recommended for production code (release). */
GC_API void GC_CALLBACK GC_ignore_warn_proc(char *msg, GC_uintptr_t arg)
GC_API void GC_CALLBACK GC_ignore_warn_proc(const char *msg, GC_uintptr_t arg)
{
if (GC_print_stats) {
/* Don't ignore warnings if stats printing is on. */
Expand Down
5 changes: 1 addition & 4 deletions tests/gctest.c
Original file line number Diff line number Diff line change
Expand Up @@ -2226,11 +2226,8 @@ void SetMinimumStack(long minSize)
#define cMinStackSpace (512L * 1024L)
#endif

static void GC_CALLBACK warn_proc(char *msg, GC_uintptr_t arg)
static void GC_CALLBACK warn_proc(const char *msg, GC_uintptr_t arg)
{
# if defined(CPPCHECK)
GC_noop1_ptr(msg);
# endif
GC_printf(msg, arg);
/*FAIL;*/
}
Expand Down

0 comments on commit 4f73bae

Please sign in to comment.