@@ -112,6 +112,59 @@ static inline void vgc_set_cache_idx(int idx) { _vgc_cache_idx = idx; }
112112 static inline void * vgc_get_sp (void ) { return __builtin_frame_address (0 ); }
113113#endif
114114
115+ // ============================================================
116+ // Stack bounds
117+ // ============================================================
118+ #if defined(__APPLE__ )
119+ #include <pthread.h>
120+ static inline int vgc_get_stack_bounds (uintptr_t * lo , uintptr_t * hi ) {
121+ void * stack_hi = pthread_get_stackaddr_np (pthread_self ());
122+ size_t stack_size = pthread_get_stacksize_np (pthread_self ());
123+ if (stack_hi == NULL || stack_size == 0 ) return 0 ;
124+ * hi = (uintptr_t )stack_hi ;
125+ * lo = * hi - stack_size ;
126+ return 1 ;
127+ }
128+ #elif defined(__linux__ ) || defined(__ANDROID__ )
129+ #include <pthread.h>
130+ static inline int vgc_get_stack_bounds (uintptr_t * lo , uintptr_t * hi ) {
131+ pthread_attr_t attr ;
132+ if (pthread_getattr_np (pthread_self (), & attr ) != 0 ) return 0 ;
133+ void * stack_lo = NULL ;
134+ size_t stack_size = 0 ;
135+ int ok = pthread_attr_getstack (& attr , & stack_lo , & stack_size ) == 0 && stack_lo != NULL && stack_size != 0 ;
136+ pthread_attr_destroy (& attr );
137+ if (!ok ) return 0 ;
138+ * lo = (uintptr_t )stack_lo ;
139+ * hi = * lo + stack_size ;
140+ return 1 ;
141+ }
142+ #elif defined(__FreeBSD__ ) || defined(__DragonFly__ ) || defined(__NetBSD__ ) || defined(__OpenBSD__ )
143+ #include <pthread.h>
144+ static inline int vgc_get_stack_bounds (uintptr_t * lo , uintptr_t * hi ) {
145+ pthread_attr_t attr ;
146+ if (pthread_attr_init (& attr ) != 0 ) return 0 ;
147+ if (pthread_attr_get_np (pthread_self (), & attr ) != 0 ) {
148+ pthread_attr_destroy (& attr );
149+ return 0 ;
150+ }
151+ void * stack_lo = NULL ;
152+ size_t stack_size = 0 ;
153+ int ok = pthread_attr_getstack (& attr , & stack_lo , & stack_size ) == 0 && stack_lo != NULL && stack_size != 0 ;
154+ pthread_attr_destroy (& attr );
155+ if (!ok ) return 0 ;
156+ * lo = (uintptr_t )stack_lo ;
157+ * hi = * lo + stack_size ;
158+ return 1 ;
159+ }
160+ #else
161+ static inline int vgc_get_stack_bounds (uintptr_t * lo , uintptr_t * hi ) {
162+ (void )lo ;
163+ (void )hi ;
164+ return 0 ;
165+ }
166+ #endif
167+
115168// ============================================================
116169// Bitmap operations (for mark/alloc bitmaps)
117170// ============================================================
0 commit comments