1da2e3ebdSchin /***********************************************************************
2da2e3ebdSchin *                                                                      *
3da2e3ebdSchin *               This software is part of the ast package               *
4*b30d1939SAndy Fiddaman *          Copyright (c) 1985-2012 AT&T Intellectual Property          *
5da2e3ebdSchin *                      and is licensed under the                       *
6*b30d1939SAndy Fiddaman *                 Eclipse Public License, Version 1.0                  *
77c2fbfb3SApril Chin *                    by AT&T Intellectual Property                     *
8da2e3ebdSchin *                                                                      *
9da2e3ebdSchin *                A copy of the License is available at                 *
10*b30d1939SAndy Fiddaman *          http://www.eclipse.org/org/documents/epl-v10.html           *
11*b30d1939SAndy Fiddaman *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
12da2e3ebdSchin *                                                                      *
13da2e3ebdSchin *              Information and Software Systems Research               *
14da2e3ebdSchin *                            AT&T Research                             *
15da2e3ebdSchin *                           Florham Park NJ                            *
16da2e3ebdSchin *                                                                      *
17da2e3ebdSchin *                 Glenn Fowler <gsf@research.att.com>                  *
18da2e3ebdSchin *                  David Korn <dgk@research.att.com>                   *
19da2e3ebdSchin *                   Phong Vo <kpv@research.att.com>                    *
20da2e3ebdSchin *                                                                      *
21da2e3ebdSchin ***********************************************************************/
22da2e3ebdSchin #if defined(_UWIN) && defined(_BLD_ast)
23da2e3ebdSchin 
_STUB_vmclose()24da2e3ebdSchin void _STUB_vmclose(){}
25da2e3ebdSchin 
26da2e3ebdSchin #else
27da2e3ebdSchin 
28da2e3ebdSchin #include	"vmhdr.h"
29da2e3ebdSchin 
30da2e3ebdSchin /*	Close down a region.
31da2e3ebdSchin **
32da2e3ebdSchin **	Written by Kiem-Phong Vo, kpv@research.att.com, 01/16/94.
33da2e3ebdSchin */
34da2e3ebdSchin #if __STD_C
vmclose(Vmalloc_t * vm)35da2e3ebdSchin int vmclose(Vmalloc_t* vm)
36da2e3ebdSchin #else
37da2e3ebdSchin int vmclose(vm)
38da2e3ebdSchin Vmalloc_t*	vm;
39da2e3ebdSchin #endif
40da2e3ebdSchin {
41da2e3ebdSchin 	Seg_t		*seg, *vmseg, *next;
42da2e3ebdSchin 	Vmalloc_t	*v, *last;
43da2e3ebdSchin 	Vmdata_t*	vd = vm->data;
44*b30d1939SAndy Fiddaman 	Vmdisc_t*	disc = vm->disc;
45*b30d1939SAndy Fiddaman 	int		mode, rv = 0;
46da2e3ebdSchin 
47*b30d1939SAndy Fiddaman 	if(vm == Vmheap) /* the heap is never freed */
48da2e3ebdSchin 		return -1;
49da2e3ebdSchin 
50*b30d1939SAndy Fiddaman 	if(vm->disc->exceptf && /* announcing closing event */
51*b30d1939SAndy Fiddaman 	   (rv = (*vm->disc->exceptf)(vm,VM_CLOSE,(Void_t*)1,vm->disc)) < 0 )
52da2e3ebdSchin 		return -1;
53da2e3ebdSchin 
54*b30d1939SAndy Fiddaman 	mode = vd->mode; /* remember this in case it gets destroyed below */
55da2e3ebdSchin 
56*b30d1939SAndy Fiddaman 	if((mode&VM_MTPROFILE) && _Vmpfclose)
57da2e3ebdSchin 		(*_Vmpfclose)(vm);
58da2e3ebdSchin 
59*b30d1939SAndy Fiddaman 	/* remove from linked list of regions */
60*b30d1939SAndy Fiddaman 	_vmlock(NIL(Vmalloc_t*), 1);
61da2e3ebdSchin 	for(last = Vmheap, v = last->next; v; last = v, v = v->next)
62da2e3ebdSchin 	{	if(v == vm)
63da2e3ebdSchin 		{	last->next = v->next;
64da2e3ebdSchin 			break;
65da2e3ebdSchin 		}
66da2e3ebdSchin 	}
67*b30d1939SAndy Fiddaman 	_vmlock(NIL(Vmalloc_t*), 0);
68da2e3ebdSchin 
69*b30d1939SAndy Fiddaman 	if(rv == 0) /* deallocate memory obtained from the system */
70*b30d1939SAndy Fiddaman 	{	/* lock-free because alzheimer can cause deadlocks :) */
71*b30d1939SAndy Fiddaman 		vmseg = NIL(Seg_t*);
72da2e3ebdSchin 		for(seg = vd->seg; seg; seg = next)
73da2e3ebdSchin 		{	next = seg->next;
74*b30d1939SAndy Fiddaman 			if(seg->extent == seg->size) /* root segment */
75*b30d1939SAndy Fiddaman 				vmseg = seg; /* don't free this yet */
76*b30d1939SAndy Fiddaman 			else	(*disc->memoryf)(vm,seg->addr,seg->extent,0,disc);
77da2e3ebdSchin 		}
78*b30d1939SAndy Fiddaman 		if(vmseg) /* now safe to free root segment */
79*b30d1939SAndy Fiddaman 			(*disc->memoryf)(vm,vmseg->addr,vmseg->extent,0,disc);
80da2e3ebdSchin 	}
81da2e3ebdSchin 
82*b30d1939SAndy Fiddaman 	if(disc->exceptf) /* finalizing closing */
83*b30d1939SAndy Fiddaman 		(void)(*disc->exceptf)(vm, VM_ENDCLOSE, (Void_t*)0, disc);
84*b30d1939SAndy Fiddaman 
85*b30d1939SAndy Fiddaman 	if(!(mode & VM_MEMORYF) )
86*b30d1939SAndy Fiddaman 		vmfree(Vmheap,vm);
87da2e3ebdSchin 
88da2e3ebdSchin 	return 0;
89da2e3ebdSchin }
90da2e3ebdSchin 
91da2e3ebdSchin #endif
92