xref: /onnv-gate/usr/src/lib/efcode/engine/resource.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright (c) 1999 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <stdlib.h>
31*0Sstevel@tonic-gate #include <string.h>
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <fcode/private.h>
34*0Sstevel@tonic-gate #include <fcode/log.h>
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate fc_resource_t *
find_resource(fc_resource_t ** head,void * ptr,int (cmp)(void *,void *))37*0Sstevel@tonic-gate find_resource(fc_resource_t **head, void *ptr, int (cmp)(void *, void *))
38*0Sstevel@tonic-gate {
39*0Sstevel@tonic-gate 	fc_resource_t *f, *prev, *r = *head;
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate 	f = NULL;
42*0Sstevel@tonic-gate 	prev = NULL;
43*0Sstevel@tonic-gate 	while (r) {
44*0Sstevel@tonic-gate 		if (r->data == NULL) {
45*0Sstevel@tonic-gate 			fc_resource_t *dead;
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate 			if (prev)
48*0Sstevel@tonic-gate 				prev->next = r->next;
49*0Sstevel@tonic-gate 			else {
50*0Sstevel@tonic-gate 				*head = r->next;
51*0Sstevel@tonic-gate 			}
52*0Sstevel@tonic-gate 			dead = r;
53*0Sstevel@tonic-gate 			r = r->next;
54*0Sstevel@tonic-gate 			FREE(dead);
55*0Sstevel@tonic-gate 		} else {
56*0Sstevel@tonic-gate 			if (cmp(ptr, r->data)) {
57*0Sstevel@tonic-gate 				f = r;
58*0Sstevel@tonic-gate 				break;
59*0Sstevel@tonic-gate 			}
60*0Sstevel@tonic-gate 			prev = r;
61*0Sstevel@tonic-gate 			r = r->next;
62*0Sstevel@tonic-gate 		}
63*0Sstevel@tonic-gate 	}
64*0Sstevel@tonic-gate 	return (f);
65*0Sstevel@tonic-gate }
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate void *
add_resource(fc_resource_t ** head,void * ptr,int (cmp)(void *,void *))68*0Sstevel@tonic-gate add_resource(fc_resource_t **head, void *ptr, int (cmp)(void *, void *))
69*0Sstevel@tonic-gate {
70*0Sstevel@tonic-gate 	fc_resource_t *r;
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 	r = find_resource(head, ptr, cmp);
73*0Sstevel@tonic-gate 	if (r == NULL) {
74*0Sstevel@tonic-gate 		r = MALLOC(sizeof (fc_resource_t));
75*0Sstevel@tonic-gate 		r->data = ptr;
76*0Sstevel@tonic-gate 		r->next = *head;
77*0Sstevel@tonic-gate 		*head = r;
78*0Sstevel@tonic-gate 		return (r->data);
79*0Sstevel@tonic-gate 	}
80*0Sstevel@tonic-gate 	log_message(MSG_ERROR, "add_resource: Duplicate entry: %p\n", ptr);
81*0Sstevel@tonic-gate 	return (NULL);
82*0Sstevel@tonic-gate }
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate void
free_resource(fc_resource_t ** head,void * ptr,int (cmp)(void *,void *))85*0Sstevel@tonic-gate free_resource(fc_resource_t **head, void *ptr,  int (cmp)(void *, void *))
86*0Sstevel@tonic-gate {
87*0Sstevel@tonic-gate 	fc_resource_t *r;
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate 	if ((r = find_resource(head, ptr, cmp)) != NULL)
90*0Sstevel@tonic-gate 		r->data = NULL;
91*0Sstevel@tonic-gate 	else
92*0Sstevel@tonic-gate 		log_message(MSG_ERROR, "free_resource: No such Entry: %p\n",
93*0Sstevel@tonic-gate 		    ptr);
94*0Sstevel@tonic-gate }
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate #ifdef DEBUG
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate static int
dump_print(void * s,void * d)99*0Sstevel@tonic-gate dump_print(void *s, void *d)
100*0Sstevel@tonic-gate {
101*0Sstevel@tonic-gate 	log_message(MSG_DEBUG, "Buffer: %p\n", d);
102*0Sstevel@tonic-gate 	return (0);
103*0Sstevel@tonic-gate }
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate void
dump_resources(fcode_env_t * env)106*0Sstevel@tonic-gate dump_resources(fcode_env_t *env)
107*0Sstevel@tonic-gate {
108*0Sstevel@tonic-gate 	fc_resource_t **head;
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 	head = (fc_resource_t **) POP(DS);
111*0Sstevel@tonic-gate 	(void) find_resource(head, NULL, dump_print);
112*0Sstevel@tonic-gate }
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate void
propbufs(fcode_env_t * env)115*0Sstevel@tonic-gate propbufs(fcode_env_t *env)
116*0Sstevel@tonic-gate {
117*0Sstevel@tonic-gate 	PUSH(DS, (fstack_t) &env->propbufs);
118*0Sstevel@tonic-gate }
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate #pragma init(_init)
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate static void
_init(void)123*0Sstevel@tonic-gate _init(void)
124*0Sstevel@tonic-gate {
125*0Sstevel@tonic-gate 	fcode_env_t *env = initial_env;
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 	NOTICE;
128*0Sstevel@tonic-gate 	ASSERT(env);
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate 	FORTH(0,		"propbufs",		propbufs);
131*0Sstevel@tonic-gate 	FORTH(0,		"dump-resource",	dump_resources);
132*0Sstevel@tonic-gate }
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate #endif
135