1.1 --- a/libexec/lib/src/segment.cc Fri Mar 03 19:29:33 2023 +0100
1.2 +++ b/libexec/lib/src/segment.cc Sat Mar 04 12:42:09 2023 +0100
1.3 @@ -1,7 +1,7 @@
1.4 /*
1.5 * Program memory segment support.
1.6 *
1.7 - * Copyright (C) 2022 Paul Boddie <paul@boddie.org.uk>
1.8 + * Copyright (C) 2022, 2023 Paul Boddie <paul@boddie.org.uk>
1.9 *
1.10 * This program is free software; you can redistribute it and/or
1.11 * modify it under the terms of the GNU General Public License as
1.12 @@ -21,6 +21,7 @@
1.13
1.14 #include <l4/sys/err.h>
1.15
1.16 +#include <ipc/cap_alloc.h>
1.17 #include <ipc/mem_ipc.h>
1.18 #include <mem/memory_utils.h>
1.19
1.20 @@ -31,10 +32,25 @@
1.21
1.22
1.23
1.24 -/* Obligatory destructor. */
1.25 +/* Free the allocated resources. */
1.26
1.27 Segment::~Segment()
1.28 {
1.29 + if (_allocated)
1.30 + {
1.31 + if (_attached)
1.32 + ipc_detach_dataspace(_buf);
1.33 +
1.34 + /* Also unmap the dataspace, not apparently done by the underlying
1.35 + functions. */
1.36 +
1.37 + if (l4_is_valid_cap(_ds))
1.38 + ipc_cap_free_um(_ds);
1.39 + }
1.40 + else if (client_opened(_file))
1.41 + {
1.42 + client_close(_file);
1.43 + }
1.44 }
1.45
1.46 /* Common initialisation. */
1.47 @@ -74,6 +90,9 @@
1.48
1.49 long Segment::allocate(bool attach)
1.50 {
1.51 + _allocated = true;
1.52 + _attached = attach;
1.53 +
1.54 if (attach)
1.55 return ipc_allocate_align(_region_allocated_size,
1.56 L4RE_RM_F_SEARCH_ADDR | L4RE_RM_F_RW,
1.57 @@ -113,6 +132,7 @@
1.58 return err;
1.59 }
1.60
1.61 + _file = file;
1.62 _ds = file->ref;
1.63 return L4_EOK;
1.64 }