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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stddef.h>
30 #include <stdlib.h>
31 #include <assert.h>
32 #include <sys/param.h>
33 #include <memory.h>
34 #include <config_admin.h>
35 #include "mema_test.h"
36 
37 void *
38 mtest_allocate_buf(
39 	mtest_handle_t handle,
40 	size_t size)
41 {
42 	struct mtest_alloc_ent *new_ent;
43 
44 	new_ent =
45 	    (struct mtest_alloc_ent *)malloc(sizeof (struct mtest_alloc_ent));
46 	if (new_ent == NULL)
47 		return (NULL);
48 
49 	new_ent->buf = malloc(size);
50 	if (new_ent->buf == NULL) {
51 		free((void *)new_ent);
52 		return (NULL);
53 	}
54 	/* TODO: probably not thread safe? */
55 	new_ent->next = handle->alloc_list;
56 	handle->alloc_list = new_ent;
57 
58 	return (new_ent->buf);
59 }
60 
61 /* This routine dedicated to George Cameron */
62 void
63 mtest_deallocate_buf(
64 	mtest_handle_t handle,
65 	void *buf)
66 {
67 	struct mtest_alloc_ent **p, *p1;
68 
69 	p = &handle->alloc_list;
70 	while ((*p) != NULL && (*p)->buf != buf)
71 		p = &(*p)->next;
72 	assert((*p) != NULL);
73 	p1 = *p;
74 	*p = (*p)->next;
75 	free(p1->buf);
76 	free((void *)p1);
77 }
78 
79 void
80 mtest_deallocate_buf_all(mtest_handle_t handle)
81 {
82 	struct mtest_alloc_ent *p1;
83 
84 	while ((p1 = handle->alloc_list) != NULL) {
85 		handle->alloc_list = p1->next;
86 		free(p1->buf);
87 		free((void *)p1);
88 	}
89 }
90 
91 void
92 mtest_message(mtest_handle_t handle, const char *msg)
93 {
94 	if (handle->msgp != NULL && handle->msgp->message_routine != NULL) {
95 		(*handle->msgp->message_routine)(handle->msgp->appdata_ptr,
96 		    msg);
97 	}
98 }
99