xref: /onnv-gate/usr/src/lib/libast/common/vmalloc/vmclose.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                   Phong Vo <kpv@research.att.com>                    *
204887Schin *                                                                      *
214887Schin ***********************************************************************/
224887Schin #if defined(_UWIN) && defined(_BLD_ast)
234887Schin 
_STUB_vmclose()244887Schin void _STUB_vmclose(){}
254887Schin 
264887Schin #else
274887Schin 
284887Schin #include	"vmhdr.h"
294887Schin 
304887Schin /*	Close down a region.
314887Schin **
324887Schin **	Written by Kiem-Phong Vo, kpv@research.att.com, 01/16/94.
334887Schin */
344887Schin #if __STD_C
vmclose(Vmalloc_t * vm)354887Schin int vmclose(Vmalloc_t* vm)
364887Schin #else
374887Schin int vmclose(vm)
384887Schin Vmalloc_t*	vm;
394887Schin #endif
404887Schin {
414887Schin 	Seg_t		*seg, *vmseg, *next;
424887Schin 	Vmalloc_t	*v, *last;
434887Schin 	Vmdata_t*	vd = vm->data;
444887Schin 	int		ev = 0;
454887Schin 
464887Schin 	if(vm == Vmheap)
474887Schin 		return -1;
484887Schin 
494887Schin 	if(!(vd->mode&VM_TRUST) && ISLOCK(vd,0))
504887Schin 		return -1;
514887Schin 
524887Schin 	if(vm->disc->exceptf &&
534887Schin 	   (ev = (*vm->disc->exceptf)(vm,VM_CLOSE,NIL(Void_t*),vm->disc)) < 0 )
544887Schin 		return -1;
554887Schin 
564887Schin 	/* make this region inaccessible until it disappears */
574887Schin 	vd->mode &= ~VM_TRUST;
584887Schin 	SETLOCK(vd,0);
594887Schin 
604887Schin 	if((vd->mode&VM_MTPROFILE) && _Vmpfclose)
614887Schin 		(*_Vmpfclose)(vm);
624887Schin 
634887Schin 	/* remove from linked list of regions	*/
644887Schin 	for(last = Vmheap, v = last->next; v; last = v, v = v->next)
654887Schin 	{	if(v == vm)
664887Schin 		{	last->next = v->next;
674887Schin 			break;
684887Schin 		}
694887Schin 	}
704887Schin 
714887Schin 	if(ev == 0)
724887Schin 	{	vmseg = NIL(Seg_t*);
734887Schin 		for(seg = vd->seg; seg; seg = next)
744887Schin 		{	next = seg->next;
754887Schin 			if(seg->extent == seg->size)
764887Schin 				vmseg = seg;
774887Schin 			else	(*vm->disc->memoryf)(vm,seg->addr,seg->extent,0,vm->disc);
784887Schin 		}
794887Schin 		if(vmseg)
804887Schin 			(*vm->disc->memoryf)(vm,vmseg->addr,vmseg->extent,0,vm->disc);
814887Schin 	}
824887Schin 	else	CLRLOCK(vd,0);
834887Schin 
844887Schin 	vmfree(Vmheap,vm);
854887Schin 
864887Schin 	return 0;
874887Schin }
884887Schin 
894887Schin #endif
90