xref: /titanic_50/usr/src/lib/libctf/common/ctf_subr.c (revision f3e7f55e73a39377d55a030f124cc86b3b66a9cc)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <ctf_impl.h>
287c478bd9Sstevel@tonic-gate #include <libctf.h>
297c478bd9Sstevel@tonic-gate #include <sys/mman.h>
307c478bd9Sstevel@tonic-gate #include <stdarg.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate void *
ctf_data_alloc(size_t size)337c478bd9Sstevel@tonic-gate ctf_data_alloc(size_t size)
347c478bd9Sstevel@tonic-gate {
357c478bd9Sstevel@tonic-gate 	return (mmap(NULL, size, PROT_READ | PROT_WRITE,
367c478bd9Sstevel@tonic-gate 	    MAP_PRIVATE | MAP_ANON, -1, 0));
377c478bd9Sstevel@tonic-gate }
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate void
ctf_data_free(void * buf,size_t size)407c478bd9Sstevel@tonic-gate ctf_data_free(void *buf, size_t size)
417c478bd9Sstevel@tonic-gate {
427c478bd9Sstevel@tonic-gate 	(void) munmap(buf, size);
437c478bd9Sstevel@tonic-gate }
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate void
ctf_data_protect(void * buf,size_t size)467c478bd9Sstevel@tonic-gate ctf_data_protect(void *buf, size_t size)
477c478bd9Sstevel@tonic-gate {
487c478bd9Sstevel@tonic-gate 	(void) mprotect(buf, size, PROT_READ);
497c478bd9Sstevel@tonic-gate }
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate void *
ctf_alloc(size_t size)527c478bd9Sstevel@tonic-gate ctf_alloc(size_t size)
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate 	return (malloc(size));
557c478bd9Sstevel@tonic-gate }
567c478bd9Sstevel@tonic-gate 
57*f3e7f55eSRobert Mustacchi void *
mergeq_alloc(size_t size)58*f3e7f55eSRobert Mustacchi mergeq_alloc(size_t size)
59*f3e7f55eSRobert Mustacchi {
60*f3e7f55eSRobert Mustacchi 	return (malloc(size));
61*f3e7f55eSRobert Mustacchi }
62*f3e7f55eSRobert Mustacchi 
63*f3e7f55eSRobert Mustacchi void *
workq_alloc(size_t size)64*f3e7f55eSRobert Mustacchi workq_alloc(size_t size)
65*f3e7f55eSRobert Mustacchi {
66*f3e7f55eSRobert Mustacchi 	return (malloc(size));
67*f3e7f55eSRobert Mustacchi }
68*f3e7f55eSRobert Mustacchi 
697c478bd9Sstevel@tonic-gate /*ARGSUSED*/
707c478bd9Sstevel@tonic-gate void
ctf_free(void * buf,size_t size)717c478bd9Sstevel@tonic-gate ctf_free(void *buf, size_t size)
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate 	free(buf);
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate 
76*f3e7f55eSRobert Mustacchi /*ARGSUSED*/
77*f3e7f55eSRobert Mustacchi void
mergeq_free(void * buf,size_t size)78*f3e7f55eSRobert Mustacchi mergeq_free(void *buf, size_t size)
79*f3e7f55eSRobert Mustacchi {
80*f3e7f55eSRobert Mustacchi 	free(buf);
81*f3e7f55eSRobert Mustacchi }
82*f3e7f55eSRobert Mustacchi 
83*f3e7f55eSRobert Mustacchi /*ARGSUSED*/
84*f3e7f55eSRobert Mustacchi void
workq_free(void * buf,size_t size)85*f3e7f55eSRobert Mustacchi workq_free(void *buf, size_t size)
86*f3e7f55eSRobert Mustacchi {
87*f3e7f55eSRobert Mustacchi 	free(buf);
88*f3e7f55eSRobert Mustacchi }
89*f3e7f55eSRobert Mustacchi 
907c478bd9Sstevel@tonic-gate const char *
ctf_strerror(int err)917c478bd9Sstevel@tonic-gate ctf_strerror(int err)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	return (strerror(err));
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
977c478bd9Sstevel@tonic-gate void
ctf_dprintf(const char * format,...)987c478bd9Sstevel@tonic-gate ctf_dprintf(const char *format, ...)
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate 	if (_libctf_debug) {
1017c478bd9Sstevel@tonic-gate 		va_list alist;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 		va_start(alist, format);
1047c478bd9Sstevel@tonic-gate 		(void) fputs("libctf DEBUG: ", stderr);
1057c478bd9Sstevel@tonic-gate 		(void) vfprintf(stderr, format, alist);
1067c478bd9Sstevel@tonic-gate 		va_end(alist);
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate }
109