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) 1994, by Sun Microsytems, Inc.
24 */
25
26/*
27 * Function to allocate memory in target process (used by combinations).
28 */
29
30#ifndef DEBUG
31#define	NDEBUG	1
32#endif
33
34#include <assert.h>
35#include "tnfctl_int.h"
36#include "prb_internals.h"
37#include "dbg.h"
38
39
40/*
41 * _tnfctl_targmem_alloc() - allocates memory in the target process.
42 */
43tnfctl_errcode_t
44_tnfctl_targmem_alloc(tnfctl_handle_t *hndl, size_t size, uintptr_t *addr_p)
45{
46	int			miscstat;
47	tnf_memseg_t		memseg;
48
49	assert(hndl->memseg_p != NULL);
50	*addr_p = 0;
51
52	/* read the memseg block from the target process */
53	miscstat = hndl->p_read(hndl->proc_p, hndl->memseg_p, &memseg,
54		sizeof (memseg));
55	if (miscstat)
56		return (TNFCTL_ERR_INTERNAL);
57
58	/* if there is memory left, allocate it */
59	if ((memseg.min_p + memseg.i_reqsz) <= (memseg.max_p - size)) {
60		memseg.max_p -= size;
61
62		miscstat = hndl->p_write(hndl->proc_p, hndl->memseg_p,
63			&memseg, sizeof (memseg));
64		if (miscstat)
65			return (TNFCTL_ERR_INTERNAL);
66
67		*addr_p = (uintptr_t) memseg.max_p;
68
69		DBG_TNF_PROBE_2(_tnfctl_targmem_alloc_1, "libtnfctl",
70			"sunw%verbosity 3",
71			tnf_long, size_allocated, size,
72			tnf_opaque, at_location, *addr_p);
73
74		return (TNFCTL_ERR_NONE);
75	} else {
76		return (TNFCTL_ERR_INTERNAL);
77	}
78}
79