xref: /titanic_41/usr/src/lib/efcode/engine/resource.c (revision 70025d765b044c6d8594bb965a2247a61e991a99)
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 fc_resource_t *
37 find_resource(fc_resource_t **head, void *ptr, int (cmp)(void *, void *))
38 {
39 	fc_resource_t *f, *prev, *r = *head;
40 
41 	f = NULL;
42 	prev = NULL;
43 	while (r) {
44 		if (r->data == NULL) {
45 			fc_resource_t *dead;
46 
47 			if (prev)
48 				prev->next = r->next;
49 			else {
50 				*head = r->next;
51 			}
52 			dead = r;
53 			r = r->next;
54 			FREE(dead);
55 		} else {
56 			if (cmp(ptr, r->data)) {
57 				f = r;
58 				break;
59 			}
60 			prev = r;
61 			r = r->next;
62 		}
63 	}
64 	return (f);
65 }
66 
67 void *
68 add_resource(fc_resource_t **head, void *ptr, int (cmp)(void *, void *))
69 {
70 	fc_resource_t *r;
71 
72 	r = find_resource(head, ptr, cmp);
73 	if (r == NULL) {
74 		r = MALLOC(sizeof (fc_resource_t));
75 		r->data = ptr;
76 		r->next = *head;
77 		*head = r;
78 		return (r->data);
79 	}
80 	log_message(MSG_ERROR, "add_resource: Duplicate entry: %p\n", ptr);
81 	return (NULL);
82 }
83 
84 void
85 free_resource(fc_resource_t **head, void *ptr,  int (cmp)(void *, void *))
86 {
87 	fc_resource_t *r;
88 
89 	if ((r = find_resource(head, ptr, cmp)) != NULL)
90 		r->data = NULL;
91 	else
92 		log_message(MSG_ERROR, "free_resource: No such Entry: %p\n",
93 		    ptr);
94 }
95 
96 #ifdef DEBUG
97 
98 static int
99 dump_print(void *s, void *d)
100 {
101 	log_message(MSG_DEBUG, "Buffer: %p\n", d);
102 	return (0);
103 }
104 
105 void
106 dump_resources(fcode_env_t *env)
107 {
108 	fc_resource_t **head;
109 
110 	head = (fc_resource_t **) POP(DS);
111 	(void) find_resource(head, NULL, dump_print);
112 }
113 
114 void
115 propbufs(fcode_env_t *env)
116 {
117 	PUSH(DS, (fstack_t) &env->propbufs);
118 }
119 
120 #pragma init(_init)
121 
122 static void
123 _init(void)
124 {
125 	fcode_env_t *env = initial_env;
126 
127 	NOTICE;
128 	ASSERT(env);
129 
130 	FORTH(0,		"propbufs",		propbufs);
131 	FORTH(0,		"dump-resource",	dump_resources);
132 }
133 
134 #endif
135