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