| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* GATE PROJECT LICENSE: | ||
| 2 | +----------------------------------------------------------------------------+ | ||
| 3 | | Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> | | ||
| 4 | | All rights reserved. | | ||
| 5 | | | | ||
| 6 | | Redistribution and use in source and binary forms, with or without | | ||
| 7 | | modification, are permitted provided that the following conditions are met:| | ||
| 8 | | | | ||
| 9 | | 1. Redistributions of source code must retain the above copyright notice, | | ||
| 10 | | this list of conditions and the following disclaimer. | | ||
| 11 | | 2. Redistributions in binary form must reproduce the above copyright | | ||
| 12 | | notice, this list of conditions and the following disclaimer in the | | ||
| 13 | | documentation and/or other materials provided with the distribution. | | ||
| 14 | | | | ||
| 15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| | ||
| 16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | ||
| 17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | ||
| 18 | | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | | ||
| 19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | ||
| 20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | ||
| 21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | ||
| 22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | ||
| 23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | ||
| 24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | ||
| 25 | | THE POSSIBILITY OF SUCH DAMAGE. | | ||
| 26 | +----------------------------------------------------------------------------+ | ||
| 27 | */ | ||
| 28 | |||
| 29 | #include "gate/io/ipcsharedmem.hpp" | ||
| 30 | #include "gate/gatetypes.hpp" | ||
| 31 | #include "gate/exceptions.hpp" | ||
| 32 | #include "gate/memalloc.hpp" | ||
| 33 | #include "gate/debugging.h" | ||
| 34 | |||
| 35 | namespace gate | ||
| 36 | { | ||
| 37 | namespace io | ||
| 38 | { | ||
| 39 | |||
| 40 | 1 | IpcSharedMem::IpcSharedMem(size_t memSize, enumint_t flagBits) | |
| 41 | 1 | : size(memSize), flags(flagBits), attachedmem(NULL) | |
| 42 | { | ||
| 43 | 1 | gate_string_t ipc_id = GATE_STRING_INIT_EMPTY; | |
| 44 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | result_t result = gate_ipcsharedmem_create(&this->impl, memSize, flagBits, &ipc_id); |
| 45 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_ERROR(result); |
| 46 | 2 | String id(String::createFrom(ipc_id)); | |
| 47 | 1 | this->uid.swap(id); | |
| 48 | 1 | } | |
| 49 | |||
| 50 | ✗ | IpcSharedMem::IpcSharedMem(String const& text_uid) | |
| 51 | ✗ | : uid(text_uid), size(0), flags(0), attachedmem(NULL) | |
| 52 | { | ||
| 53 | ✗ | result_t result = gate_ipcsharedmem_open(&this->impl, text_uid.c_impl(), &this->size, &this->flags); | |
| 54 | ✗ | GATEXX_CHECK_ERROR(result); | |
| 55 | ✗ | } | |
| 56 | |||
| 57 | 1 | IpcSharedMem::~IpcSharedMem() noexcept | |
| 58 | { | ||
| 59 | 1 | this->detach(); | |
| 60 | 1 | result_t result = gate_ipcsharedmem_destroy(this->impl); | |
| 61 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATE_DEBUG_TRACE_FAILED(result, "Failed to destroy IPC shared memory"); |
| 62 | 1 | } | |
| 63 | |||
| 64 | ✗ | String const& IpcSharedMem::getUid() const | |
| 65 | { | ||
| 66 | ✗ | return this->uid; | |
| 67 | } | ||
| 68 | |||
| 69 | 1 | size_t IpcSharedMem::getSize() const | |
| 70 | { | ||
| 71 | 1 | return this->size; | |
| 72 | } | ||
| 73 | |||
| 74 | ✗ | enumint_t IpcSharedMem::getFlags() const | |
| 75 | { | ||
| 76 | ✗ | return this->flags; | |
| 77 | } | ||
| 78 | |||
| 79 | 1 | void* IpcSharedMem::getMemoryPointer() const | |
| 80 | { | ||
| 81 | 1 | return this->attachedmem; | |
| 82 | } | ||
| 83 | |||
| 84 | |||
| 85 | |||
| 86 | 1 | void* IpcSharedMem::attach() | |
| 87 | { | ||
| 88 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (this->attachedmem != NULL) |
| 89 | { | ||
| 90 | ✗ | GATEXX_RAISE_ERROR(results::InvalidState); | |
| 91 | } | ||
| 92 | 1 | void* ptr = NULL; | |
| 93 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | result_t result = gate_ipcsharedmem_attach(this->impl, &ptr); |
| 94 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_EXCEPTION(result); |
| 95 | 1 | this->attachedmem = ptr; | |
| 96 | 1 | return ptr; | |
| 97 | } | ||
| 98 | |||
| 99 | 2 | void IpcSharedMem::detach() | |
| 100 | { | ||
| 101 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (this->attachedmem != NULL) |
| 102 | { | ||
| 103 | 1 | void* ptr = this->attachedmem; | |
| 104 | 1 | this->attachedmem = NULL; | |
| 105 | 1 | result_t result = gate_ipcsharedmem_detach(this->impl, ptr); | |
| 106 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATE_DEBUG_TRACE_FAILED(result, "Failed to detach IPC shared memory"); |
| 107 | } | ||
| 108 | 2 | } | |
| 109 | |||
| 110 | } // end of namespace io | ||
| 111 | } // end of namespace gate | ||
| 112 |