xref: /titanic_44/usr/src/lib/efcode/engine/init.c (revision 749f21d359d8fbd020c974a1a5227316221bfc9c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1999 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 
33 #include <fcode/private.h>
34 #include <fcode/log.h>
35 
36 fcode_env_t *initial_env = 0;
37 int dict_size = 0x100000;	/* 1Mb, hopefully big enough... */
38 int stack_size = 0x200;
39 
40 void *
41 safe_malloc(size_t n, char *f, int l)
42 {
43 	void *p;
44 
45 	p = malloc((size_t) n);
46 #if defined(__sparcv9)
47 	/*
48 	 * For Ultrasparc, we must force addresses to be less than 4Gb,
49 	 * since Fcode assumes that addresses can be stored in 32 bits.
50 	 * To get around this would require turning all addresses into
51 	 * cookies, which is a lot of work.
52 	 */
53 	if (((uint64_t)p) >= 0x100000000) {
54 		log_message(MSG_WARN, "Malloc returned address > 4Gb\n");
55 	}
56 #endif	/* __sparcv9 */
57 	if (p) {
58 		memset(p, 0, (size_t) n);
59 	} else
60 		log_message(MSG_ERROR, "%s:%d:Malloc(%llx) failed\n", f, l,
61 		    (uint64_t)n);
62 	return (p);
63 }
64 
65 void *
66 safe_realloc(void *p, size_t n, char *f, int l)
67 {
68 	void *newp;
69 
70 	if ((newp = safe_malloc(n, f, l)) == NULL) {
71 		log_message(MSG_ERROR, "%s:%d:realloc(%p, %x) failed\n", f, l,
72 		    p, n);
73 		safe_free(p, f, l);
74 		return (NULL);
75 	}
76 	if (p) {
77 		memcpy(newp, p, n);
78 		safe_free(p, f, l);
79 	}
80 	return (newp);
81 }
82 
83 void
84 safe_free(void *p, char *f, int l)
85 {
86 	if (p) {
87 		free(p);
88 	}
89 }
90 
91 char *
92 safe_strdup(char *s, char *f, int l)
93 {
94 	char *p = strdup(s);
95 
96 	return (p);
97 }
98 
99 #pragma init(_init)
100 
101 static void
102 _init(void)
103 {
104 	int i;
105 	acf_t f_error_addr;
106 	fcode_env_t *env;
107 
108 	NOTICE;
109 
110 	fcode_impl_count = 0;
111 	env = MALLOC(sizeof (fcode_env_t));
112 	env->table = MALLOC((MAX_FCODE + 1) * sizeof (fcode_token));
113 	env->base = MALLOC(dict_size);
114 	env->here = env->base;
115 	env->ds = env->ds0 = MALLOC(stack_size * sizeof (fstack_t));
116 	env->rs = env->rs0 = MALLOC(stack_size * sizeof (fstack_t));
117 	env->order = MALLOC(MAX_ORDER * sizeof (token_t));
118 	env->input = MALLOC(sizeof (input_typ));
119 	env->num_base = 0x10;
120 
121 	/* Setup the initial forth environment */
122 	do_forth(env);
123 	do_definitions(env);
124 	install_handlers(env);
125 
126 	initial_env = env;
127 
128 	/*
129 	 * Need to define this early because it is the default for
130 	 * all unimpl, FCODE functions
131 	 */
132 	P1275(0x0fc, IMMEDIATE,	"ferror",		f_error);
133 	f_error_addr = LINK_TO_ACF(env->lastlink);
134 	for (i = 0; i <= MAX_FCODE; i++) {
135 		DEBUGF(ANY, env->table[i].usage = 0);
136 		SET_TOKEN(i, IMMEDIATE, "ferror", f_error_addr);
137 	}
138 	fcode_impl_count = 0;
139 }
140