xref: /onnv-gate/usr/src/lib/cfgadm_plugins/ac/common/mema_test_subr.c (revision 2305:7954d746a1b5)
1*2305Sstevel /*
2*2305Sstevel  * CDDL HEADER START
3*2305Sstevel  *
4*2305Sstevel  * The contents of this file are subject to the terms of the
5*2305Sstevel  * Common Development and Distribution License, Version 1.0 only
6*2305Sstevel  * (the "License").  You may not use this file except in compliance
7*2305Sstevel  * with the License.
8*2305Sstevel  *
9*2305Sstevel  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*2305Sstevel  * or http://www.opensolaris.org/os/licensing.
11*2305Sstevel  * See the License for the specific language governing permissions
12*2305Sstevel  * and limitations under the License.
13*2305Sstevel  *
14*2305Sstevel  * When distributing Covered Code, include this CDDL HEADER in each
15*2305Sstevel  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*2305Sstevel  * If applicable, add the following below this CDDL HEADER, with the
17*2305Sstevel  * fields enclosed by brackets "[]" replaced with your own identifying
18*2305Sstevel  * information: Portions Copyright [yyyy] [name of copyright owner]
19*2305Sstevel  *
20*2305Sstevel  * CDDL HEADER END
21*2305Sstevel  */
22*2305Sstevel /*
23*2305Sstevel  * Copyright (c) 1996-1998 by Sun Microsystems, Inc.
24*2305Sstevel  * All rights reserved.
25*2305Sstevel  */
26*2305Sstevel 
27*2305Sstevel #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*2305Sstevel 
29*2305Sstevel #include <stddef.h>
30*2305Sstevel #include <stdlib.h>
31*2305Sstevel #include <assert.h>
32*2305Sstevel #include <sys/param.h>
33*2305Sstevel #include <memory.h>
34*2305Sstevel #include <config_admin.h>
35*2305Sstevel #include "mema_test.h"
36*2305Sstevel 
37*2305Sstevel void *
mtest_allocate_buf(mtest_handle_t handle,size_t size)38*2305Sstevel mtest_allocate_buf(
39*2305Sstevel 	mtest_handle_t handle,
40*2305Sstevel 	size_t size)
41*2305Sstevel {
42*2305Sstevel 	struct mtest_alloc_ent *new_ent;
43*2305Sstevel 
44*2305Sstevel 	new_ent =
45*2305Sstevel 	    (struct mtest_alloc_ent *)malloc(sizeof (struct mtest_alloc_ent));
46*2305Sstevel 	if (new_ent == NULL)
47*2305Sstevel 		return (NULL);
48*2305Sstevel 
49*2305Sstevel 	new_ent->buf = malloc(size);
50*2305Sstevel 	if (new_ent->buf == NULL) {
51*2305Sstevel 		free((void *)new_ent);
52*2305Sstevel 		return (NULL);
53*2305Sstevel 	}
54*2305Sstevel 	/* TODO: probably not thread safe? */
55*2305Sstevel 	new_ent->next = handle->alloc_list;
56*2305Sstevel 	handle->alloc_list = new_ent;
57*2305Sstevel 
58*2305Sstevel 	return (new_ent->buf);
59*2305Sstevel }
60*2305Sstevel 
61*2305Sstevel /* This routine dedicated to George Cameron */
62*2305Sstevel void
mtest_deallocate_buf(mtest_handle_t handle,void * buf)63*2305Sstevel mtest_deallocate_buf(
64*2305Sstevel 	mtest_handle_t handle,
65*2305Sstevel 	void *buf)
66*2305Sstevel {
67*2305Sstevel 	struct mtest_alloc_ent **p, *p1;
68*2305Sstevel 
69*2305Sstevel 	p = &handle->alloc_list;
70*2305Sstevel 	while ((*p) != NULL && (*p)->buf != buf)
71*2305Sstevel 		p = &(*p)->next;
72*2305Sstevel 	assert((*p) != NULL);
73*2305Sstevel 	p1 = *p;
74*2305Sstevel 	*p = (*p)->next;
75*2305Sstevel 	free(p1->buf);
76*2305Sstevel 	free((void *)p1);
77*2305Sstevel }
78*2305Sstevel 
79*2305Sstevel void
mtest_deallocate_buf_all(mtest_handle_t handle)80*2305Sstevel mtest_deallocate_buf_all(mtest_handle_t handle)
81*2305Sstevel {
82*2305Sstevel 	struct mtest_alloc_ent *p1;
83*2305Sstevel 
84*2305Sstevel 	while ((p1 = handle->alloc_list) != NULL) {
85*2305Sstevel 		handle->alloc_list = p1->next;
86*2305Sstevel 		free(p1->buf);
87*2305Sstevel 		free((void *)p1);
88*2305Sstevel 	}
89*2305Sstevel }
90*2305Sstevel 
91*2305Sstevel void
mtest_message(mtest_handle_t handle,const char * msg)92*2305Sstevel mtest_message(mtest_handle_t handle, const char *msg)
93*2305Sstevel {
94*2305Sstevel 	if (handle->msgp != NULL && handle->msgp->message_routine != NULL) {
95*2305Sstevel 		(*handle->msgp->message_routine)(handle->msgp->appdata_ptr,
96*2305Sstevel 		    msg);
97*2305Sstevel 	}
98*2305Sstevel }
99