xref: /illumos-gate/usr/src/lib/cfgadm_plugins/ac/common/mema_test_subr.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
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) 1996-1998 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #include <stddef.h>
28 #include <stdlib.h>
29 #include <assert.h>
30 #include <sys/param.h>
31 #include <memory.h>
32 #include <config_admin.h>
33 #include "mema_test.h"
34 
35 void *
mtest_allocate_buf(mtest_handle_t handle,size_t size)36 mtest_allocate_buf(
37 	mtest_handle_t handle,
38 	size_t size)
39 {
40 	struct mtest_alloc_ent *new_ent;
41 
42 	new_ent =
43 	    (struct mtest_alloc_ent *)malloc(sizeof (struct mtest_alloc_ent));
44 	if (new_ent == NULL)
45 		return (NULL);
46 
47 	new_ent->buf = malloc(size);
48 	if (new_ent->buf == NULL) {
49 		free((void *)new_ent);
50 		return (NULL);
51 	}
52 	/* TODO: probably not thread safe? */
53 	new_ent->next = handle->alloc_list;
54 	handle->alloc_list = new_ent;
55 
56 	return (new_ent->buf);
57 }
58 
59 /* This routine dedicated to George Cameron */
60 void
mtest_deallocate_buf(mtest_handle_t handle,void * buf)61 mtest_deallocate_buf(
62 	mtest_handle_t handle,
63 	void *buf)
64 {
65 	struct mtest_alloc_ent **p, *p1;
66 
67 	p = &handle->alloc_list;
68 	while ((*p) != NULL && (*p)->buf != buf)
69 		p = &(*p)->next;
70 	assert((*p) != NULL);
71 	p1 = *p;
72 	*p = (*p)->next;
73 	free(p1->buf);
74 	free((void *)p1);
75 }
76 
77 void
mtest_deallocate_buf_all(mtest_handle_t handle)78 mtest_deallocate_buf_all(mtest_handle_t handle)
79 {
80 	struct mtest_alloc_ent *p1;
81 
82 	while ((p1 = handle->alloc_list) != NULL) {
83 		handle->alloc_list = p1->next;
84 		free(p1->buf);
85 		free((void *)p1);
86 	}
87 }
88 
89 void
mtest_message(mtest_handle_t handle,const char * msg)90 mtest_message(mtest_handle_t handle, const char *msg)
91 {
92 	if (handle->msgp != NULL && handle->msgp->message_routine != NULL) {
93 		(*handle->msgp->message_routine)(handle->msgp->appdata_ptr,
94 		    msg);
95 	}
96 }
97