aboutsummaryrefslogtreecommitdiffstats
path: root/erts/emulator/beam/beam_catches.c
blob: 72bd3583e1a95a690690b1d0d23d5dc0c2220f8d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * %CopyrightBegin%
 *
 * Copyright Ericsson AB 2000-2011. All Rights Reserved.
 *
 * The contents of this file are subject to the Erlang Public License,
 * Version 1.1, (the "License"); you may not use this file except in
 * compliance with the License. You should have received a copy of the
 * Erlang Public License along with this software. If not, it can be
 * retrieved online at http://www.erlang.org/.
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 *
 * %CopyrightEnd%
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "sys.h"
#include "beam_catches.h"
#include "global.h"

/* R14B04 has about 380 catches when starting erlang */
#define DEFAULT_TABSIZE (1024)
typedef struct {
    BeamInstr *cp;
    unsigned cdr;
} beam_catch_t;

#ifdef DEBUG
#  define IF_DEBUG(x) x
#else
#  define IF_DEBUG(x)
#endif

struct bc_code_ix { /*SVERK A better name maybe... */
    int free_list;
    unsigned high_mark;
    unsigned tabsize;
    beam_catch_t *beam_catches;

    IF_DEBUG(int is_prepared;)
};

static struct bc_code_ix bccix[ERTS_NUM_CODE_IX];

void beam_catches_init(void)
{
    int i;

    bccix[0].tabsize   = DEFAULT_TABSIZE;
    bccix[0].free_list = -1;
    bccix[0].high_mark = 0;
    bccix[0].beam_catches = erts_alloc(ERTS_ALC_T_CODE,
				     sizeof(beam_catch_t)*DEFAULT_TABSIZE);
    IF_DEBUG(bccix[0].is_prepared = 0);
    for (i=1; i<ERTS_NUM_CODE_IX; i++) {
	bccix[i] = bccix[i-1];
    }
     /* For initial load: */
    IF_DEBUG(bccix[erts_loader_code_ix()].is_prepared = 1);
}


static void gc_old_vec(beam_catch_t* vec)
{
    int i;
    for (i=0; i<ERTS_NUM_CODE_IX; i++) {
	if (bccix[i].beam_catches == vec) {
	    return;
	}
    }
    erts_free(ERTS_ALC_T_CODE, vec);
}


void beam_catches_start_load(void)
{
    ErtsCodeIndex dst = erts_loader_code_ix();
    ErtsCodeIndex src = erts_active_code_ix();
    beam_catch_t* prev_vec = bccix[dst].beam_catches;

    ASSERT(!bccix[src].is_prepared && !bccix[dst].is_prepared);

    bccix[dst] = bccix[src];
    gc_old_vec(prev_vec);
    IF_DEBUG(bccix[dst].is_prepared = 1);
}

void beam_catches_end_load(int commit)
{
    IF_DEBUG(bccix[erts_loader_code_ix()].is_prepared = 0);
}

unsigned beam_catches_cons(BeamInstr *cp, unsigned cdr)
{
    int i;
    struct bc_code_ix* p = &bccix[erts_loader_code_ix()];

    ASSERT(p->is_prepared);
    /*
     * Allocate from free_list while it is non-empty.
     * If free_list is empty, allocate at high_mark.
     *
     * This avoids the need to initialise the free list in
     * beam_catches_init(), which would cost O(TABSIZ) time.
     */
    if (p->free_list >= 0) {
	i = p->free_list;
	p->free_list = p->beam_catches[i].cdr;
    }
    else {
	if (p->high_mark >= p->tabsize) {
	    /* No free slots and table is full: realloc table */
	    beam_catch_t* prev_vec = p->beam_catches;
	    unsigned newsize = p->tabsize*2;

	    p->beam_catches = erts_alloc(ERTS_ALC_T_CODE,
					 newsize*sizeof(beam_catch_t));
	    sys_memcpy(p->beam_catches, prev_vec,
		       p->tabsize*sizeof(beam_catch_t));
	    gc_old_vec(prev_vec);
	    p->tabsize = newsize;
	}
	i = p->high_mark++;
    }

    p->beam_catches[i].cp  = cp;
    p->beam_catches[i].cdr = cdr;

    return i;
}

BeamInstr *beam_catches_car(unsigned i)
{
    struct bc_code_ix* p = &bccix[erts_active_code_ix()];

    if (i >= p->tabsize ) {
	erl_exit(1, "beam_catches_delmod: index %#x is out of range\r\n", i);
    }
    return p->beam_catches[i].cp;
}

void beam_catches_delmod(unsigned head, BeamInstr *code, unsigned code_bytes,
			 ErtsCodeIndex code_ix)
{
    struct bc_code_ix* p = &bccix[code_ix];
    unsigned i, cdr;

    ASSERT((code_ix == erts_active_code_ix()) != bccix[erts_loader_code_ix()].is_prepared);
    for(i = head; i != (unsigned)-1;) {
	if (i >= p->tabsize) {
	    erl_exit(1, "beam_catches_delmod: index %#x is out of range\r\n", i);
	}
	if( (char*)p->beam_catches[i].cp - (char*)code >= code_bytes ) {
	    erl_exit(1,
		    "beam_catches_delmod: item %#x has cp %#lx which is not "
		    "in module's range [%#lx,%#lx[\r\n",
		    i, (long)p->beam_catches[i].cp,
		    (long)code, (long)((char*)code + code_bytes));
	}
	p->beam_catches[i].cp = 0;
	cdr = p->beam_catches[i].cdr;
	p->beam_catches[i].cdr = p->free_list;
	p->free_list = i;
	i = cdr;
    }
}