~ubuntu-branches/ubuntu/hardy/sigscheme/hardy-proposed

« back to all changes in this revision

Viewing changes to src/gcroots/gcroots.c

  • Committer: Bazaar Package Importer
  • Author(s): NIIBE Yutaka
  • Date: 2007-01-29 15:31:24 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070129153124-j5fcqyrwcfbczma7
Tags: 0.7.4-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*===========================================================================
 
2
 *  Filename : gcroots.c
 
3
 *  About    : SigScheme-dependent portable implementation of libgcroots
 
4
 *
 
5
 *  Copyright (C) 2006 YAMAMOTO Kengo <yamaken AT bp.iij4u.or.jp>
 
6
 *  Copyright (c) 2007 SigScheme Project <uim AT freedesktop.org>
 
7
 *
 
8
 *  All rights reserved.
 
9
 *
 
10
 *  Redistribution and use in source and binary forms, with or without
 
11
 *  modification, are permitted provided that the following conditions
 
12
 *  are met:
 
13
 *
 
14
 *  1. Redistributions of source code must retain the above copyright
 
15
 *     notice, this list of conditions and the following disclaimer.
 
16
 *  2. Redistributions in binary form must reproduce the above copyright
 
17
 *     notice, this list of conditions and the following disclaimer in the
 
18
 *     documentation and/or other materials provided with the distribution.
 
19
 *  3. Neither the name of authors nor the names of its contributors
 
20
 *     may be used to endorse or promote products derived from this software
 
21
 *     without specific prior written permission.
 
22
 *
 
23
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
 
24
 *  IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 
25
 *  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
26
 *  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
 
27
 *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
28
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
29
 *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 
30
 *  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 
31
 *  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 
32
 *  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 
33
 *  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
34
===========================================================================*/
 
35
 
 
36
#include <config.h>
 
37
 
 
38
#if HAVE_GETCONTEXT
 
39
#include <ucontext.h>
 
40
#else
 
41
#include <setjmp.h>
 
42
#endif
 
43
 
 
44
#include "sigscheme.h"
 
45
#include "gcroots.h"
 
46
 
 
47
/*=======================================
 
48
  File Local Macro Definitions
 
49
=======================================*/
 
50
 
 
51
/*=======================================
 
52
  File Local Type Definitions
 
53
=======================================*/
 
54
struct _GCROOTS_context {
 
55
    void *stack_base;
 
56
    GCROOTS_mark_proc mark;
 
57
    scm_bool scan_entire_system_stack;
 
58
};
 
59
 
 
60
/*=======================================
 
61
  Variable Definitions
 
62
=======================================*/
 
63
 
 
64
/*=======================================
 
65
  File Local Function Declarations
 
66
=======================================*/
 
67
static void mark_internal(GCROOTS_context *ctx);
 
68
 
 
69
/*=======================================
 
70
  Function Definitions
 
71
=======================================*/
 
72
SCM_EXPORT GCROOTS_context *
 
73
GCROOTS_init(GCROOTS_context_alloc_proc allocator, GCROOTS_mark_proc marker,
 
74
             int scan_entire_system_stack)
 
75
{
 
76
    GCROOTS_context *ctx;
 
77
 
 
78
    SCM_ASSERT(allocator);
 
79
    SCM_ASSERT(marker);
 
80
    /* scan_entire_system_stack is not supported by this implementation */
 
81
    SCM_ASSERT(!scan_entire_system_stack);
 
82
 
 
83
    ctx = (*allocator)(sizeof(GCROOTS_context));
 
84
    if (ctx) {
 
85
        ctx->mark = marker;
 
86
        ctx->scan_entire_system_stack = scan_entire_system_stack;
 
87
        ctx->stack_base = NULL;
 
88
    }
 
89
 
 
90
    return ctx;
 
91
}
 
92
 
 
93
SCM_EXPORT void
 
94
GCROOTS_fin(GCROOTS_context *ctx)
 
95
{
 
96
    /* Nothing to do for this implementation. Caller must free ctx. */
 
97
}
 
98
 
 
99
SCM_EXPORT void *
 
100
GCROOTS_call_with_gc_ready_stack(GCROOTS_context *ctx,
 
101
                                 GCROOTS_user_proc proc, void *arg)
 
102
{
 
103
    void *ret;
 
104
    void *stack_top; /* approx */
 
105
    volatile GCROOTS_user_proc anti_inline_proc;
 
106
 
 
107
    if (!ctx->stack_base)
 
108
        ctx->stack_base = &stack_top;
 
109
 
 
110
    anti_inline_proc = proc;
 
111
    ret = (*anti_inline_proc)(arg);
 
112
 
 
113
    if (ctx->stack_base == &stack_top)
 
114
        ctx->stack_base = NULL;
 
115
 
 
116
    return ret;
 
117
}
 
118
 
 
119
SCM_EXPORT void
 
120
GCROOTS_mark(GCROOTS_context *ctx)
 
121
{
 
122
#if HAVE_GETCONTEXT
 
123
    ucontext_t uctx;
 
124
#else
 
125
    jmp_buf env;
 
126
#endif
 
127
    void (*volatile anti_inline_mark_internal)(GCROOTS_context *);
 
128
 
 
129
    if (ctx->stack_base) {
 
130
#if HAVE_GETCONTEXT
 
131
        getcontext(&uctx);
 
132
#else
 
133
        setjmp(env);
 
134
#endif
 
135
        anti_inline_mark_internal = mark_internal;
 
136
        (*anti_inline_mark_internal)(ctx);
 
137
    }
 
138
}
 
139
 
 
140
static void
 
141
mark_internal(GCROOTS_context *ctx)
 
142
{
 
143
    void *stack_top; /* approx */
 
144
 
 
145
    (*ctx->mark)(ctx->stack_base, &stack_top, scm_false, scm_false);
 
146
}