* include/gc.h (GC_is_heap_ptr): New API function declaration.
* misc.c (GC_is_heap_ptr): New function.
* tests/test.c (run_one_test): Add GC_is_heap_ptr tests.
/* GC_free. */
GC_API void * GC_CALL GC_base(void * /* displaced_pointer */);
+/* Return TRUE if and only if the argument points to somewhere in GC */
+/* heap. Primary use is as a fast alternative to GC_base to check */
+/* whether the pointed object is allocated by GC or not. It is assumed */
+/* that the collector is already initialized. */
+GC_API int GC_CALL GC_is_heap_ptr(const void *);
+
/* Given a pointer to the base of an object, return its size in bytes. */
/* The returned size may be slightly larger than what was originally */
/* requested. */
return((void *)r);
}
+/* Return TRUE if and only if p points to somewhere in GC heap. */
+GC_API int GC_CALL GC_is_heap_ptr(const void *p)
+{
+ bottom_index *bi;
+
+ GC_ASSERT(GC_is_initialized);
+ GET_BI(p, bi);
+ return HDR_FROM_BI(bi, p) != 0;
+}
/* Return the size of an object, given a pointer to its base. */
/* (For small objects this also happens to work from interior pointers, */
GC_printf("GC_base(heap ptr) produced incorrect result\n");
FAIL;
}
+ if (!GC_is_heap_ptr(x)) {
+ GC_printf("GC_is_heap_ptr(heap_ptr) produced incorrect result\n");
+ FAIL;
+ }
+ if (GC_is_heap_ptr(&x)) {
+ GC_printf("GC_is_heap_ptr(&local_var) produced incorrect result\n");
+ FAIL;
+ }
+ if (GC_is_heap_ptr(&fail_count) || GC_is_heap_ptr(NULL)) {
+ GC_printf("GC_is_heap_ptr(&global_var) produced incorrect result\n");
+ FAIL;
+ }
(void)GC_PRE_INCR(x, 0);
(void)GC_POST_INCR(x);
(void)GC_POST_DECR(x);