Import from 1.9a8 tarball
[mozilla-nspr.git] / nsprpub / pr / src / misc / prthinfo.c
blob76c448884616cdd48ffa4458464b2801f465424f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is the Netscape Portable Runtime (NSPR).
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "prlog.h"
39 #include "prthread.h"
40 #ifdef XP_MAC
41 #include "pprthred.h"
42 #else
43 #include "private/pprthred.h"
44 #endif
45 #include "primpl.h"
47 PR_IMPLEMENT(PRWord *)
48 PR_GetGCRegisters(PRThread *t, int isCurrent, int *np)
50 return _MD_HomeGCRegisters(t, isCurrent, np);
53 PR_IMPLEMENT(PRStatus)
54 PR_ThreadScanStackPointers(PRThread* t,
55 PRScanStackFun scanFun, void* scanClosure)
57 PRThread* current = PR_GetCurrentThread();
58 PRWord *sp, *esp, *p0;
59 int n;
60 void **ptd;
61 PRStatus status;
62 PRUint32 index;
63 int stack_end;
66 ** Store the thread's registers in the thread structure so the GC
67 ** can scan them. Then scan them.
69 p0 = _MD_HomeGCRegisters(t, t == current, &n);
70 status = scanFun(t, (void**)p0, n, scanClosure);
71 if (status != PR_SUCCESS)
72 return status;
74 /* Scan the C stack for pointers into the GC heap */
75 #if defined(XP_PC) && defined(WIN16)
77 ** Under WIN16, the stack of the current thread is always mapped into
78 ** the "task stack" (at SS:xxxx). So, if t is the current thread, scan
79 ** the "task stack". Otherwise, scan the "cached stack" of the inactive
80 ** thread...
82 if (t == current) {
83 sp = (PRWord*) &stack_end;
84 esp = (PRWord*) _pr_top_of_task_stack;
86 PR_ASSERT(sp <= esp);
87 } else {
88 sp = (PRWord*) PR_GetSP(t);
89 esp = (PRWord*) t->stack->stackTop;
91 PR_ASSERT((t->stack->stackSize == 0) ||
92 ((sp > (PRWord*)t->stack->stackBottom) &&
93 (sp <= (PRWord*)t->stack->stackTop)));
95 #else /* ! WIN16 */
96 #ifdef HAVE_STACK_GROWING_UP
97 if (t == current) {
98 esp = (PRWord*) &stack_end;
99 } else {
100 esp = (PRWord*) PR_GetSP(t);
102 sp = (PRWord*) t->stack->stackTop;
103 if (t->stack->stackSize) {
104 PR_ASSERT((esp > (PRWord*)t->stack->stackTop) &&
105 (esp < (PRWord*)t->stack->stackBottom));
107 #else /* ! HAVE_STACK_GROWING_UP */
108 if (t == current) {
109 sp = (PRWord*) &stack_end;
110 } else {
111 sp = (PRWord*) PR_GetSP(t);
113 esp = (PRWord*) t->stack->stackTop;
114 if (t->stack->stackSize) {
115 PR_ASSERT((sp > (PRWord*)t->stack->stackBottom) &&
116 (sp < (PRWord*)t->stack->stackTop));
118 #endif /* ! HAVE_STACK_GROWING_UP */
119 #endif /* ! WIN16 */
121 #if defined(WIN16)
123 prword_t scan;
124 prword_t limit;
126 scan = (prword_t) sp;
127 limit = (prword_t) esp;
128 while (scan < limit) {
129 prword_t *test;
131 test = *((prword_t **)scan);
132 status = scanFun(t, (void**)&test, 1, scanClosure);
133 if (status != PR_SUCCESS)
134 return status;
135 scan += sizeof(char);
138 #else
139 if (sp < esp) {
140 status = scanFun(t, (void**)sp, esp - sp, scanClosure);
141 if (status != PR_SUCCESS)
142 return status;
144 #endif
147 ** Mark all of the per-thread-data items attached to this thread
149 ** The execution environment better be accounted for otherwise it
150 ** will be collected
152 status = scanFun(t, (void**)&t->environment, 1, scanClosure);
153 if (status != PR_SUCCESS)
154 return status;
156 #ifndef GC_LEAK_DETECTOR
157 /* if thread is not allocated on stack, this is redundant. */
158 ptd = t->privateData;
159 for (index = 0; index < t->tpdLength; index++, ptd++) {
160 status = scanFun(t, (void**)ptd, 1, scanClosure);
161 if (status != PR_SUCCESS)
162 return status;
164 #endif
166 return PR_SUCCESS;
169 /* transducer for PR_EnumerateThreads */
170 typedef struct PRScanStackData {
171 PRScanStackFun scanFun;
172 void* scanClosure;
173 } PRScanStackData;
175 static PRStatus PR_CALLBACK
176 pr_ScanStack(PRThread* t, int i, void* arg)
178 #if defined(XP_MAC)
179 #pragma unused (i)
180 #endif
181 PRScanStackData* data = (PRScanStackData*)arg;
182 return PR_ThreadScanStackPointers(t, data->scanFun, data->scanClosure);
185 PR_IMPLEMENT(PRStatus)
186 PR_ScanStackPointers(PRScanStackFun scanFun, void* scanClosure)
188 PRScanStackData data;
189 data.scanFun = scanFun;
190 data.scanClosure = scanClosure;
191 return PR_EnumerateThreads(pr_ScanStack, &data);
194 PR_IMPLEMENT(PRUword)
195 PR_GetStackSpaceLeft(PRThread* t)
197 PRThread *current = PR_GetCurrentThread();
198 PRWord *sp, *esp;
199 int stack_end;
201 #if defined(WIN16)
203 ** Under WIN16, the stack of the current thread is always mapped into
204 ** the "task stack" (at SS:xxxx). So, if t is the current thread, scan
205 ** the "task stack". Otherwise, scan the "cached stack" of the inactive
206 ** thread...
208 if (t == current) {
209 sp = (PRWord*) &stack_end;
210 esp = (PRWord*) _pr_top_of_task_stack;
212 PR_ASSERT(sp <= esp);
213 } else {
214 sp = (PRWord*) PR_GetSP(t);
215 esp = (PRWord*) t->stack->stackTop;
217 PR_ASSERT((t->stack->stackSize == 0) ||
218 ((sp > (PRWord*)t->stack->stackBottom) &&
219 (sp <= (PRWord*)t->stack->stackTop)));
221 #else /* ! WIN16 */
222 #ifdef HAVE_STACK_GROWING_UP
223 if (t == current) {
224 esp = (PRWord*) &stack_end;
225 } else {
226 esp = (PRWord*) PR_GetSP(t);
228 sp = (PRWord*) t->stack->stackTop;
229 if (t->stack->stackSize) {
230 PR_ASSERT((esp > (PRWord*)t->stack->stackTop) &&
231 (esp < (PRWord*)t->stack->stackBottom));
233 #else /* ! HAVE_STACK_GROWING_UP */
234 if (t == current) {
235 sp = (PRWord*) &stack_end;
236 } else {
237 sp = (PRWord*) PR_GetSP(t);
239 esp = (PRWord*) t->stack->stackTop;
240 if (t->stack->stackSize) {
241 PR_ASSERT((sp > (PRWord*)t->stack->stackBottom) &&
242 (sp < (PRWord*)t->stack->stackTop));
244 #endif /* ! HAVE_STACK_GROWING_UP */
245 #endif /* ! WIN16 */
246 return (PRUword)t->stack->stackSize - ((PRWord)esp - (PRWord)sp);