| 1 | /* $Id$ $Revision$ */ |
| 2 | /* vim:set shiftwidth=4 ts=8: */ |
| 3 | |
| 4 | /************************************************************************* |
| 5 | * Copyright (c) 2011 AT&T Intellectual Property |
| 6 | * All rights reserved. This program and the accompanying materials |
| 7 | * are made available under the terms of the Eclipse Public License v1.0 |
| 8 | * which accompanies this distribution, and is available at |
| 9 | * http://www.eclipse.org/legal/epl-v10.html |
| 10 | * |
| 11 | * Contributors: See CVS logs. Details at http://www.graphviz.org/ |
| 12 | *************************************************************************/ |
| 13 | |
| 14 | #include "vmhdr.h" |
| 15 | |
| 16 | /* Close down a region. |
| 17 | ** |
| 18 | ** Written by Kiem-Phong Vo, kpv@research.att.com, 01/16/94. |
| 19 | */ |
| 20 | int vmclose(Vmalloc_t * vm) |
| 21 | { |
| 22 | reg Seg_t *seg, *vmseg; |
| 23 | reg Vmemory_f memoryf; |
| 24 | reg Vmdata_t *vd = vm->data; |
| 25 | reg Vmalloc_t *v, *last; |
| 26 | |
| 27 | if (vm == Vmheap) |
| 28 | return -1; |
| 29 | |
| 30 | if (!(vd->mode & VM_TRUST) && ISLOCK(vd, 0)) |
| 31 | return -1; |
| 32 | |
| 33 | if (vm->disc->exceptf && |
| 34 | (*vm->disc->exceptf) (vm, VM_CLOSE, NIL(void *), vm->disc) < 0) |
| 35 | return -1; |
| 36 | |
| 37 | /* make this region inaccessible until it disappears */ |
| 38 | vd->mode &= ~VM_TRUST; |
| 39 | SETLOCK(vd, 0); |
| 40 | |
| 41 | if ((vd->mode & VM_MTPROFILE) && _Vmpfclose) |
| 42 | (*_Vmpfclose) (vm); |
| 43 | |
| 44 | /* remove from linked list of regions */ |
| 45 | for (last = Vmheap, v = last->next; v; last = v, v = v->next) { |
| 46 | if (v == vm) { |
| 47 | last->next = v->next; |
| 48 | break; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /* free all non-region segments */ |
| 53 | memoryf = vm->disc->memoryf; |
| 54 | vmseg = NIL(Seg_t *); |
| 55 | for (seg = vd->seg; seg;) { |
| 56 | reg Seg_t *next = seg->next; |
| 57 | if (seg->extent != seg->size) |
| 58 | (void) (*memoryf) (vm, seg->addr, seg->extent, 0, vm->disc); |
| 59 | else |
| 60 | vmseg = seg; |
| 61 | seg = next; |
| 62 | } |
| 63 | |
| 64 | /* this must be done here because even though this region is freed, |
| 65 | there may still be others that share this space. |
| 66 | */ |
| 67 | CLRLOCK(vd, 0); |
| 68 | |
| 69 | /* free the segment that contains the region data */ |
| 70 | if (vmseg) |
| 71 | (void) (*memoryf) (vm, vmseg->addr, vmseg->extent, 0, vm->disc); |
| 72 | |
| 73 | /* free the region itself */ |
| 74 | vmfree(Vmheap, vm); |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |