├── .github └── workflows │ └── build.yml ├── .gitignore ├── .gitmodules ├── LGMP.vcxproj ├── LGMP.vcxproj.filters ├── LICENSE ├── README.md ├── dump ├── CMakeLists.txt └── main.c ├── lgmp.sln ├── lgmp ├── CMakeLists.txt ├── include │ └── lgmp │ │ ├── client.h │ │ ├── host.h │ │ ├── lgmp.h │ │ └── status.h └── src │ ├── client.c │ ├── headers.h │ ├── host.c │ ├── lgmp.h │ └── status.c ├── refresh-copyright ├── relacy-test ├── CMakeLists.txt └── main.cpp ├── test-client ├── CMakeLists.txt └── main.c └── test-host ├── CMakeLists.txt └── main.c /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: build 2 | on: [push, pull_request] 3 | jobs: 4 | lgmp: 5 | runs-on: ubuntu-20.04 6 | strategy: 7 | fail-fast: false 8 | matrix: 9 | cc: [gcc, clang] 10 | build_type: [Release, Debug] 11 | steps: 12 | - uses: actions/checkout@v1 13 | with: 14 | submodules: recursive 15 | - name: Configure LGMP 16 | env: 17 | CC: /usr/bin/${{ matrix.cc }} 18 | run: | 19 | mkdir lgmp/build 20 | cd lgmp/build 21 | cmake -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} .. 22 | - name: Build LGMP 23 | run: | 24 | cd lgmp/build 25 | make -j$(nproc) 26 | 27 | relacy-test: 28 | runs-on: ubuntu-20.04 29 | strategy: 30 | fail-fast: false 31 | matrix: 32 | compiler: 33 | - {cc: gcc, cxx: g++} 34 | - {cc: clang, cxx: clang++} 35 | build_type: [Release, Debug] 36 | exclude: 37 | # This test somehow fails with "longjmp causes uninitialized stack frame" 38 | - compiler: {cc: gcc, cxx: g++} 39 | build_type: Release 40 | steps: 41 | - uses: actions/checkout@v1 42 | with: 43 | submodules: recursive 44 | - name: Configure relacy-test 45 | env: 46 | CC: /usr/bin/${{ matrix.compiler.cc }} 47 | CXX: /usr/bin/${{ matrix.compiler.cxx }} 48 | run: | 49 | mkdir relacy-test/build 50 | cd relacy-test/build 51 | cmake -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} .. 52 | - name: Build relacy-test 53 | run: | 54 | cd relacy-test/build 55 | make -j$(nproc) 56 | - name: Run relacy-test 57 | run: relacy-test/build/relacy-test 58 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.a 2 | *.o 3 | *.exe 4 | */build 5 | .vs/ 6 | Debug/ 7 | x64/ 8 | x86/ 9 | Release/ 10 | Releases/ 11 | *.user 12 | *.sln.docstates 13 | *.suo 14 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "relacy"] 2 | path = relacy 3 | url = https://github.com/dvyukov/relacy.git 4 | -------------------------------------------------------------------------------- /LGMP.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 16.0 23 | {FFCC376C-4D98-4B50-B431-E1BBC9C67E65} 24 | Win32Proj 25 | 10.0.26100.0 26 | 27 | 28 | 29 | StaticLibrary 30 | true 31 | WindowsUserModeDriver10.0 32 | Spectre 33 | Windows10 34 | Universal 35 | <_NT_TARGET_VERSION>0xA000005 36 | 2 37 | 25 38 | 25 39 | 40 | 41 | StaticLibrary 42 | false 43 | WindowsUserModeDriver10.0 44 | Spectre 45 | Windows10 46 | Universal 47 | <_NT_TARGET_VERSION>0xA000005 48 | 2 49 | 25 50 | 25 51 | 52 | 53 | StaticLibrary 54 | true 55 | WindowsUserModeDriver10.0 56 | Spectre 57 | Windows10 58 | Universal 59 | <_NT_TARGET_VERSION>0xA000005 60 | 2 61 | 25 62 | 25 63 | 64 | 65 | StaticLibrary 66 | false 67 | WindowsUserModeDriver10.0 68 | Spectre 69 | Windows10 70 | Universal 71 | <_NT_TARGET_VERSION>0xA000005 72 | 2 73 | 25 74 | 25 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | true 96 | 97 | 98 | true 99 | 100 | 101 | 102 | WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) 103 | MultiThreaded 104 | Level3 105 | ProgramDatabase 106 | Disabled 107 | $(ProjectDir)lgmp\include;%(AdditionalIncludeDirectories) 108 | 109 | 110 | 111 | 112 | MachineX86 113 | true 114 | Windows 115 | 116 | 117 | 118 | 119 | WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) 120 | MultiThreaded 121 | Level3 122 | ProgramDatabase 123 | $(ProjectDir)lgmp\include;%(AdditionalIncludeDirectories) 124 | 125 | 126 | 127 | 128 | MachineX86 129 | true 130 | Windows 131 | true 132 | true 133 | 134 | 135 | 136 | 137 | $(ProjectDir)lgmp\include;%(AdditionalIncludeDirectories) 138 | 139 | 140 | MultiThreaded 141 | 142 | 143 | 144 | 145 | $(ProjectDir)lgmp\include;%(AdditionalIncludeDirectories) 146 | 147 | 148 | MultiThreaded 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | -------------------------------------------------------------------------------- /LGMP.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | Source Files 23 | 24 | 25 | Source Files 26 | 27 | 28 | 29 | 30 | Header Files 31 | 32 | 33 | Header Files 34 | 35 | 36 | Header Files 37 | 38 | 39 | Header Files 40 | 41 | 42 | Header Files 43 | 44 | 45 | Header Files 46 | 47 | 48 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Looking Glass Memory Protocol 2 | 3 | This library provides a message based broadcast system over shared memory to 4 | multiple listeners making full use of atomic locking and is safe to use across 5 | Virtual Machine boundaries as used in the Looking Glass project. 6 | -------------------------------------------------------------------------------- /dump/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(dump) 3 | 4 | include_directories(include) 5 | set(SOURCES main.c) 6 | 7 | get_filename_component(PROJECT_TOP "${PROJECT_SOURCE_DIR}/.." ABSOLUTE) 8 | add_subdirectory("${PROJECT_TOP}/lgmp" "${CMAKE_BINARY_DIR}/lgmp") 9 | 10 | add_executable(dump ${SOURCES}) 11 | target_link_libraries(dump 12 | lgmp 13 | curses 14 | ) 15 | -------------------------------------------------------------------------------- /dump/main.c: -------------------------------------------------------------------------------- 1 | /** 2 | * LGMP - Looking Glass Memory Protocol 3 | * Copyright © 2020-2025 Geoffrey McRae 4 | * https://github.com/gnif/LGMP 5 | * 6 | * This program is free software; you can redistribute it and/or modify it 7 | * under the terms of the GNU General Public License as published by the Free 8 | * Software Foundation; either version 2 of the License, or (at your option) 9 | * any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 | * more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along 17 | * with this program; if not, write to the Free Software Foundation, Inc., 59 18 | * Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 | */ 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | #include 34 | #include "../kvmfr.h" 35 | 36 | #include "../../lgmp/src/headers.h" 37 | 38 | bool running = true; 39 | 40 | static void finish(int sig) 41 | { 42 | running = false; 43 | } 44 | 45 | int main(int argc, char * argv[]) 46 | { 47 | const char * shmFile = NULL; 48 | bool error = false; 49 | 50 | int opt; 51 | while ((opt = getopt(argc, argv, "f:")) != -1) { 52 | switch(opt) 53 | { 54 | case 'f': 55 | shmFile = optarg; 56 | break; 57 | 58 | default: 59 | error = true; 60 | break; 61 | } 62 | } 63 | 64 | if (!shmFile || error) 65 | { 66 | fprintf(stderr, "Invalid usage, expected: -f /dev/shm/file\n"); 67 | exit(EXIT_FAILURE); 68 | } 69 | 70 | int fd; 71 | bool dmabuf = false; 72 | unsigned devSize; 73 | 74 | if (strlen(shmFile) > 8 && memcmp(shmFile, "/dev/kvmfr", 10) == 0) 75 | { 76 | dmabuf = true; 77 | fd = open(shmFile, O_RDWR, (mode_t)0600); 78 | 79 | // get the device size 80 | devSize = ioctl(fd, KVMFR_DMABUF_GETSIZE, 0); 81 | } 82 | else 83 | { 84 | struct stat st; 85 | if (stat(shmFile, &st) != 0) 86 | { 87 | perror("stat of shmFile failed"); 88 | exit(EXIT_FAILURE); 89 | } 90 | devSize = st.st_size; 91 | fd = open(shmFile, O_RDONLY); 92 | } 93 | 94 | if (!fd) 95 | { 96 | perror("open failed"); 97 | exit(EXIT_FAILURE); 98 | } 99 | 100 | void * ram = mmap(0, devSize, PROT_READ, MAP_SHARED, fd, 0); 101 | if (!ram) 102 | { 103 | perror("mmap failed"); 104 | goto out_close; 105 | } 106 | 107 | fprintf(stderr, "Mapped %s - %uMiB\n", shmFile, devSize / 1048576UL); 108 | 109 | signal(SIGINT, finish); 110 | initscr(); 111 | 112 | while(running) 113 | { 114 | erase(); 115 | struct LGMPHeader * header = (struct LGMPHeader *)ram; 116 | 117 | printw( 118 | "LGMPHeader\n" 119 | " magic = %08x\n" 120 | " version = %u\n" 121 | " sessionID = %u\n" 122 | " timestamp = %lu\n" 123 | " numQueues = %u\n" 124 | " udataSize = %u\n", 125 | header->magic, 126 | header->version, 127 | header->sessionID, 128 | atomic_load(&header->timestamp), 129 | header->numQueues, 130 | header->udataSize); 131 | 132 | for(int i = 0; i < header->numQueues; ++i) 133 | { 134 | struct LGMPHeaderQueue * hq = &header->queues[i]; 135 | struct LGMPHeaderMessage *messages = (struct LGMPHeaderMessage *) 136 | (ram + hq->messagesOffset); 137 | 138 | printw( 139 | "LGMPHeaderQueue(%d)\n" 140 | " queueID = 0x%08x\n" 141 | " numMessages = %u\n" 142 | " maxTime = %u\n" 143 | " position = %u\n" 144 | " msgTimeout = %u\n" 145 | " messagesOffset = 0x%08x\n" 146 | " cMsgWPos = %u\n" 147 | " cMsgAvail = %u\n", 148 | i, 149 | hq->queueID, 150 | hq->numMessages, 151 | hq->maxTime, 152 | atomic_load(&hq->position), 153 | atomic_load(&hq->msgTimeout), 154 | hq->messagesOffset, 155 | hq->cMsgWPos, 156 | hq->cMsgAvail); 157 | 158 | for(int i = 0; i < 32; ++i) 159 | printw(" client %-2d = id:%x, timeout:%u\n", 160 | i, 161 | hq->clientID[i], 162 | hq->timeout[i]); 163 | 164 | printw( 165 | " subs = 0x%016lx\n" 166 | " start = %u\n" 167 | " count = %u\n", 168 | atomic_load(&hq->subs), 169 | hq->start, 170 | atomic_load(&hq->count)); 171 | } 172 | 173 | refresh(); 174 | } 175 | 176 | endwin(); 177 | munmap(ram, devSize); 178 | out_close: 179 | close(fd); 180 | out: 181 | return 0; 182 | } 183 | -------------------------------------------------------------------------------- /lgmp.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.33423.256 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LGMP", "LGMP.vcxproj", "{FFCC376C-4D98-4B50-B431-E1BBC9C67E65}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|x64 = Debug|x64 11 | Debug|x86 = Debug|x86 12 | Release|x64 = Release|x64 13 | Release|x86 = Release|x86 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {FFCC376C-4D98-4B50-B431-E1BBC9C67E65}.Debug|x64.ActiveCfg = Debug|x64 17 | {FFCC376C-4D98-4B50-B431-E1BBC9C67E65}.Debug|x64.Build.0 = Debug|x64 18 | {FFCC376C-4D98-4B50-B431-E1BBC9C67E65}.Debug|x86.ActiveCfg = Debug|Win32 19 | {FFCC376C-4D98-4B50-B431-E1BBC9C67E65}.Debug|x86.Build.0 = Debug|Win32 20 | {FFCC376C-4D98-4B50-B431-E1BBC9C67E65}.Release|x64.ActiveCfg = Release|x64 21 | {FFCC376C-4D98-4B50-B431-E1BBC9C67E65}.Release|x64.Build.0 = Release|x64 22 | {FFCC376C-4D98-4B50-B431-E1BBC9C67E65}.Release|x86.ActiveCfg = Release|Win32 23 | {FFCC376C-4D98-4B50-B431-E1BBC9C67E65}.Release|x86.Build.0 = Release|Win32 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {702FA151-6DCA-4AB2-B217-9BABC784F7C5} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /lgmp/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(lgmp LANGUAGES C) 3 | 4 | include_directories(include) 5 | 6 | set(SOURCES 7 | src/host.c 8 | src/client.c 9 | src/status.c 10 | ) 11 | 12 | add_compile_options( 13 | "-Wall" 14 | "-Werror" 15 | "-Wstrict-prototypes" 16 | "-Wfatal-errors" 17 | "-ffast-math" 18 | "-fdata-sections" 19 | "-ffunction-sections" 20 | "$<$:-O0;-g3;-ggdb>" 21 | ) 22 | 23 | add_library(lgmp STATIC ${SOURCES}) 24 | target_include_directories(lgmp 25 | INTERFACE 26 | include 27 | PRIVATE 28 | src 29 | ) 30 | -------------------------------------------------------------------------------- /lgmp/include/lgmp/client.h: -------------------------------------------------------------------------------- 1 | /** 2 | * LGMP - Looking Glass Memory Protocol 3 | * Copyright © 2020-2025 Geoffrey McRae 4 | * https://github.com/gnif/LGMP 5 | * 6 | * This program is free software; you can redistribute it and/or modify it 7 | * under the terms of the GNU General Public License as published by the Free 8 | * Software Foundation; either version 2 of the License, or (at your option) 9 | * any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 | * more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along 17 | * with this program; if not, write to the Free Software Foundation, Inc., 59 18 | * Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 | */ 20 | 21 | #ifndef LGMP_CLIENT_H 22 | #define LGMP_CLIENT_H 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | #include "lgmp.h" 29 | #include "status.h" 30 | 31 | #ifdef __cplusplus 32 | extern "C" { 33 | #endif 34 | 35 | LGMP_STATUS lgmpClientInit(void * mem, const size_t size, PLGMPClient * result); 36 | void lgmpClientFree(PLGMPClient * client); 37 | LGMP_STATUS lgmpClientSessionInit(PLGMPClient client, uint32_t * udataSize, 38 | uint8_t ** udata, uint32_t * clientID); 39 | bool lgmpClientSessionValid(PLGMPClient client); 40 | 41 | LGMP_STATUS lgmpClientSubscribe(PLGMPClient client, uint32_t queueID, 42 | PLGMPClientQueue * result); 43 | LGMP_STATUS lgmpClientUnsubscribe(PLGMPClientQueue * result); 44 | 45 | typedef struct 46 | { 47 | uint32_t udata; 48 | uint32_t size; 49 | void * mem; 50 | } 51 | LGMPMessage, * PLGMPMessage; 52 | 53 | LGMP_STATUS lgmpClientAdvanceToLast(PLGMPClientQueue queue); 54 | LGMP_STATUS lgmpClientProcess(PLGMPClientQueue queue, PLGMPMessage result); 55 | LGMP_STATUS lgmpClientMessageDone(PLGMPClientQueue queue); 56 | 57 | // send data to the host of up to LGMP_MSGS_SIZE in size 58 | // serial is set to the seral number of the message just added if successful 59 | LGMP_STATUS lgmpClientSendData(PLGMPClientQueue queue, const void * data, 60 | uint32_t size, uint32_t * serial); 61 | 62 | // get the last serial processed by the host 63 | // this can be used to determine if data messages have been processed by the host 64 | LGMP_STATUS lgmpClientGetSerial(PLGMPClientQueue queue, uint32_t * serial); 65 | 66 | #ifdef __cplusplus 67 | } 68 | #endif 69 | 70 | #endif 71 | -------------------------------------------------------------------------------- /lgmp/include/lgmp/host.h: -------------------------------------------------------------------------------- 1 | /** 2 | * LGMP - Looking Glass Memory Protocol 3 | * Copyright © 2020-2025 Geoffrey McRae 4 | * https://github.com/gnif/LGMP 5 | * 6 | * This program is free software; you can redistribute it and/or modify it 7 | * under the terms of the GNU General Public License as published by the Free 8 | * Software Foundation; either version 2 of the License, or (at your option) 9 | * any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 | * more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along 17 | * with this program; if not, write to the Free Software Foundation, Inc., 59 18 | * Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 | */ 20 | 21 | #ifndef LGMP_HOST_H 22 | #define LGMP_HOST_H 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | #include "lgmp.h" 29 | #include "status.h" 30 | 31 | #ifdef __cplusplus 32 | extern "C" { 33 | #endif 34 | 35 | LGMP_STATUS lgmpHostInit(void *mem, const uint32_t size, PLGMPHost * result, 36 | uint32_t udataSize, uint8_t * udata); 37 | void lgmpHostFree (PLGMPHost * host); 38 | LGMP_STATUS lgmpHostProcess(PLGMPHost host); 39 | 40 | struct LGMPQueueConfig 41 | { 42 | uint32_t queueID; // application defined queue ID 43 | uint32_t numMessages; // number of messages in the queue 44 | uint32_t subTimeout; // length of time in ms to wait before removing a subscriber 45 | }; 46 | 47 | LGMP_STATUS lgmpHostQueueNew (PLGMPHost host, 48 | const struct LGMPQueueConfig config, PLGMPHostQueue * result); 49 | bool lgmpHostQueueHasSubs(PLGMPHostQueue queue); 50 | uint32_t lgmpHostQueueNewSubs(PLGMPHostQueue queue); 51 | uint32_t lgmpHostQueuePending(PLGMPHostQueue queue); 52 | LGMP_STATUS lgmpHostQueuePost (PLGMPHostQueue queue, uint32_t udata, 53 | PLGMPMemory payload); 54 | LGMP_STATUS lgmpHostReadData(PLGMPHostQueue queue, void * data, size_t * size); 55 | LGMP_STATUS lgmpHostAckData(PLGMPHostQueue queue); 56 | LGMP_STATUS lgmpHostGetClientIDs(PLGMPHostQueue queue, uint32_t clientIDs[32], 57 | unsigned int * count); 58 | 59 | /** 60 | * Allocates some RAM for application use from the shared memory 61 | * 62 | * Note: These allocations are permanant! Calling lgmpHostMemFree only frees 63 | * the LGMPMemory structure, but does not recover the shared memory for later 64 | * use. 65 | */ 66 | size_t lgmpHostMemAvail (PLGMPHost host); 67 | LGMP_STATUS lgmpHostMemAlloc (PLGMPHost host, uint32_t size, 68 | PLGMPMemory * result); 69 | LGMP_STATUS lgmpHostMemAllocAligned(PLGMPHost host, uint32_t size, 70 | uint32_t alignment, PLGMPMemory * result); 71 | void lgmpHostMemFree (PLGMPMemory * mem); 72 | void * lgmpHostMemPtr (PLGMPMemory mem); 73 | 74 | #ifdef __cplusplus 75 | } 76 | #endif 77 | 78 | #endif 79 | -------------------------------------------------------------------------------- /lgmp/include/lgmp/lgmp.h: -------------------------------------------------------------------------------- 1 | /** 2 | * LGMP - Looking Glass Memory Protocol 3 | * Copyright © 2020-2025 Geoffrey McRae 4 | * https://github.com/gnif/LGMP 5 | * 6 | * This program is free software; you can redistribute it and/or modify it 7 | * under the terms of the GNU General Public License as published by the Free 8 | * Software Foundation; either version 2 of the License, or (at your option) 9 | * any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 | * more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along 17 | * with this program; if not, write to the Free Software Foundation, Inc., 59 18 | * Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 | */ 20 | 21 | #ifndef LGMP_LGMP_H 22 | #define LGMP_LGMP_H 23 | 24 | // this MUST match the size defined in `src/headers.h` 25 | #define LGMP_MSGS_SIZE 64 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | typedef struct LGMPHost * PLGMPHost; 32 | typedef struct LGMPClient * PLGMPClient; 33 | typedef struct LGMPHostQueue * PLGMPHostQueue; 34 | typedef struct LGMPClientQueue * PLGMPClientQueue; 35 | typedef struct LGMPMemory * PLGMPMemory; 36 | 37 | #ifdef __cplusplus 38 | } 39 | #endif 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /lgmp/include/lgmp/status.h: -------------------------------------------------------------------------------- 1 | /** 2 | * LGMP - Looking Glass Memory Protocol 3 | * Copyright © 2020-2025 Geoffrey McRae 4 | * https://github.com/gnif/LGMP 5 | * 6 | * This program is free software; you can redistribute it and/or modify it 7 | * under the terms of the GNU General Public License as published by the Free 8 | * Software Foundation; either version 2 of the License, or (at your option) 9 | * any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 | * more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along 17 | * with this program; if not, write to the Free Software Foundation, Inc., 59 18 | * Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 | */ 20 | 21 | #ifndef LGMP_STATUS_H 22 | #define LGMP_STATUS_H 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | typedef enum 29 | { 30 | LGMP_OK, 31 | LGMP_ERR_CLOCK_FAILURE, 32 | LGMP_ERR_INVALID_ARGUMENT, 33 | LGMP_ERR_INVALID_SIZE, 34 | LGMP_ERR_INVALID_ALIGNMENT, 35 | LGMP_ERR_INVALID_SESSION, 36 | LGMP_ERR_NO_MEM, 37 | LGMP_ERR_NO_SHARED_MEM, 38 | LGMP_ERR_HOST_STARTED, 39 | LGMP_ERR_NO_QUEUES, 40 | LGMP_ERR_QUEUE_FULL, 41 | LGMP_ERR_QUEUE_EMPTY, 42 | LGMP_ERR_QUEUE_UNSUBSCRIBED, 43 | LGMP_ERR_QUEUE_TIMEOUT, 44 | LGMP_ERR_INVALID_MAGIC, 45 | LGMP_ERR_INVALID_VERSION, 46 | LGMP_ERR_NO_SUCH_QUEUE, 47 | LGMP_ERR_CORRUPTED 48 | } 49 | LGMP_STATUS; 50 | 51 | const char * lgmpStatusString(LGMP_STATUS status); 52 | 53 | #ifdef __cplusplus 54 | } 55 | #endif 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /lgmp/src/client.c: -------------------------------------------------------------------------------- 1 | /** 2 | * LGMP - Looking Glass Memory Protocol 3 | * Copyright © 2020-2025 Geoffrey McRae 4 | * https://github.com/gnif/LGMP 5 | * 6 | * This program is free software; you can redistribute it and/or modify it 7 | * under the terms of the GNU General Public License as published by the Free 8 | * Software Foundation; either version 2 of the License, or (at your option) 9 | * any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 | * more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along 17 | * with this program; if not, write to the Free Software Foundation, Inc., 59 18 | * Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 | */ 20 | 21 | #include "lgmp/client.h" 22 | 23 | #include "lgmp.h" 24 | #include "headers.h" 25 | 26 | #include 27 | #include 28 | #include 29 | 30 | #define LGMP_HEARTBEAT_TIMEOUT 1000 31 | 32 | struct LGMPClientQueue 33 | { 34 | PLGMPClient client; 35 | unsigned int id; 36 | unsigned int index; 37 | uint32_t position; 38 | 39 | struct LGMPHeader * header; 40 | struct LGMPHeaderQueue * hq; 41 | }; 42 | 43 | struct LGMPClient 44 | { 45 | uint8_t * mem; 46 | struct LGMPHeader * header; 47 | 48 | uint32_t id; 49 | uint32_t sessionID; 50 | uint64_t hosttime; 51 | uint64_t lastHeartbeat; 52 | 53 | struct LGMPClientQueue queues[LGMP_MAX_QUEUES]; 54 | }; 55 | 56 | LGMP_STATUS lgmpClientInit(void * mem, const size_t size, PLGMPClient * result) 57 | { 58 | assert(mem); 59 | assert(size > 0); 60 | assert(result); 61 | 62 | *result = NULL; 63 | if (size < sizeof(struct LGMPHeader)) 64 | return LGMP_ERR_INVALID_SIZE; 65 | 66 | // make sure that lgmpGetClockMS works 67 | if (!lgmpGetClockMS()) 68 | return LGMP_ERR_CLOCK_FAILURE; 69 | 70 | struct LGMPHeader *header = (struct LGMPHeader*)mem; 71 | 72 | *result = calloc(1, sizeof(**result)); 73 | if (!*result) 74 | return LGMP_ERR_NO_MEM; 75 | 76 | PLGMPClient client = *result; 77 | client->mem = (uint8_t*)mem; 78 | client->header = header; 79 | client->hosttime = atomic_load(&header->timestamp); 80 | return LGMP_OK; 81 | } 82 | 83 | void lgmpClientFree(PLGMPClient * client) 84 | { 85 | assert(client); 86 | if (!*client) 87 | return; 88 | 89 | free(*client); 90 | *client = NULL; 91 | } 92 | 93 | LGMP_STATUS lgmpClientSessionInit(PLGMPClient client, uint32_t * udataSize, 94 | uint8_t ** udata, uint32_t * clientID) 95 | { 96 | assert(client); 97 | struct LGMPHeader * header = client->header; 98 | 99 | if (header->magic != LGMP_PROTOCOL_MAGIC) 100 | return LGMP_ERR_INVALID_MAGIC; 101 | 102 | if (header->version != LGMP_PROTOCOL_VERSION) 103 | return LGMP_ERR_INVALID_VERSION; 104 | 105 | uint64_t timestamp = atomic_load(&header->timestamp); 106 | #ifndef LGMP_REALACY 107 | // check the host's timestamp is updating 108 | if (timestamp == client->hosttime) 109 | return LGMP_ERR_INVALID_SESSION; 110 | #endif 111 | 112 | client->id = rand(); 113 | client->sessionID = header->sessionID; 114 | client->hosttime = timestamp; 115 | client->lastHeartbeat = lgmpGetClockMS(); 116 | 117 | if (udataSize) *udataSize = header->udataSize; 118 | if (udata ) *udata = (uint8_t*)&header->udata; 119 | if (clientID ) *clientID = client->id; 120 | 121 | memset(&client->queues, 0, sizeof(client->queues)); 122 | return LGMP_OK; 123 | } 124 | 125 | bool lgmpClientSessionValid(PLGMPClient client) 126 | { 127 | assert(client); 128 | 129 | // check if the host has been restarted 130 | if (client->sessionID != client->header->sessionID) 131 | return false; 132 | 133 | #ifndef LGMP_REALACY 134 | 135 | // check if the heartbeat changed 136 | const uint64_t hosttime = atomic_load(&client->header->timestamp); 137 | const uint64_t now = lgmpGetClockMS(); 138 | if (client->hosttime != hosttime) 139 | { 140 | client->lastHeartbeat = now; 141 | client->hosttime = hosttime; 142 | return true; 143 | } 144 | 145 | // check if the heartbeat timeout has been exceeded 146 | if (now - client->lastHeartbeat > LGMP_HEARTBEAT_TIMEOUT) 147 | return false; 148 | 149 | #endif 150 | 151 | return true; 152 | } 153 | 154 | LGMP_STATUS lgmpClientSubscribe(PLGMPClient client, uint32_t queueID, 155 | PLGMPClientQueue * result) 156 | { 157 | assert(client); 158 | assert(result); 159 | 160 | *result = NULL; 161 | 162 | struct LGMPHeaderQueue *hq = NULL; 163 | uint32_t queueIndex; 164 | for(queueIndex = 0; queueIndex < client->header->numQueues; ++queueIndex) 165 | if (client->header->queues[queueIndex].queueID == queueID) 166 | { 167 | hq = &client->header->queues[queueIndex]; 168 | break; 169 | } 170 | 171 | if (!hq) 172 | return LGMP_ERR_NO_SUCH_QUEUE; 173 | 174 | *result = &client->queues[queueIndex]; 175 | PLGMPClientQueue q = *result; 176 | 177 | // take the queue lock 178 | LGMP_QUEUE_LOCK(hq); 179 | uint64_t subs = atomic_load(&hq->subs); 180 | 181 | // recover subs for reuse that have been flagged as bad and have exceeded the 182 | // queue timeout 183 | if (LGMP_SUBS_ON(subs)) 184 | { 185 | const uint64_t hosttime = atomic_load(&client->header->timestamp); 186 | uint32_t reap = 0; 187 | for(unsigned int id = 0; id < 32; ++id) 188 | { 189 | uint32_t bit = (1U << id); 190 | if ((LGMP_SUBS_BAD(subs) & bit) && hosttime > hq->timeout[id]) 191 | { 192 | reap |= bit; 193 | hq->timeout [id] = 0; 194 | hq->clientID[id] = 0; 195 | } 196 | } 197 | subs = LGMP_SUBS_CLEAR(subs, reap); 198 | } 199 | 200 | // find the next free queue ID 201 | unsigned int id = 0; 202 | while(id < 32 && ((LGMP_SUBS_ON(subs) | LGMP_SUBS_BAD(subs)) & (1U << id))) 203 | ++id; 204 | 205 | // check if full 206 | if (id == 32) 207 | { 208 | LGMP_QUEUE_UNLOCK(hq); 209 | return LGMP_ERR_QUEUE_FULL; //TODO: better return error 210 | } 211 | 212 | hq->timeout [id] = 0; 213 | hq->clientID[id] = client->id; 214 | subs = LGMP_SUBS_SET(subs, 1ULL << id); 215 | atomic_store(&hq->subs, subs); 216 | atomic_fetch_add(&hq->newSubCount, 1); 217 | 218 | q->header = client->header; 219 | q->client = client; 220 | q->index = queueIndex; 221 | q->id = id; 222 | q->position = hq->position; 223 | q->hq = hq; 224 | 225 | LGMP_QUEUE_UNLOCK(hq); 226 | return LGMP_OK; 227 | } 228 | 229 | LGMP_STATUS lgmpClientUnsubscribe(PLGMPClientQueue * result) 230 | { 231 | assert(result); 232 | 233 | if (!*result) 234 | return LGMP_OK; 235 | 236 | PLGMPClientQueue queue = *result; 237 | assert(queue->client); 238 | 239 | struct LGMPHeaderQueue *hq = queue->hq; 240 | const uint32_t bit = 1U << queue->id; 241 | 242 | LGMP_QUEUE_LOCK(hq); 243 | uint64_t subs = atomic_load(&hq->subs); 244 | if (LGMP_SUBS_BAD(subs) & bit) 245 | { 246 | LGMP_QUEUE_UNLOCK(hq); 247 | return LGMP_ERR_QUEUE_TIMEOUT; 248 | } 249 | 250 | // unset the queue id bit 251 | subs = LGMP_SUBS_CLEAR(subs, bit); 252 | atomic_store(&hq->subs, subs); 253 | hq->timeout [queue->id] = 0; 254 | hq->clientID[queue->id] = 0; 255 | LGMP_QUEUE_UNLOCK(hq); 256 | 257 | memset(queue, 0, sizeof(struct LGMPClientQueue)); 258 | *result = NULL; 259 | 260 | return LGMP_OK; 261 | } 262 | 263 | LGMP_STATUS lgmpClientAdvanceToLast(PLGMPClientQueue queue) 264 | { 265 | assert(queue); 266 | 267 | struct LGMPHeaderQueue *hq = queue->hq; 268 | const uint32_t bit = 1U << queue->id; 269 | const uint64_t subs = atomic_load(&hq->subs); 270 | 271 | if (LGMP_SUBS_BAD(subs) & bit || hq->clientID[queue->id] != queue->client->id) 272 | return LGMP_ERR_QUEUE_TIMEOUT; 273 | 274 | if (!(LGMP_SUBS_ON(subs) & bit)) 275 | { 276 | if (lgmpClientSessionValid(queue->client)) 277 | return LGMP_ERR_QUEUE_UNSUBSCRIBED; 278 | else 279 | return LGMP_ERR_INVALID_SESSION; 280 | } 281 | 282 | uint32_t end = atomic_load(&hq->position); 283 | if (end == queue->position) 284 | return LGMP_ERR_QUEUE_EMPTY; 285 | 286 | struct LGMPHeaderMessage *messages = (struct LGMPHeaderMessage *) 287 | (queue->client->mem + hq->messagesOffset); 288 | 289 | uint32_t next = queue->position; 290 | uint32_t last; 291 | bool cleanup = true; 292 | bool locked = false; 293 | while(true) 294 | { 295 | last = next; 296 | if (++next == hq->numMessages) 297 | next = 0; 298 | 299 | if (next == end) 300 | break; 301 | 302 | // turn off the pending bit for our queue 303 | struct LGMPHeaderMessage *msg = &messages[last]; 304 | 305 | // turn off the pending bit for our queue 306 | if (((atomic_fetch_and(&msg->pendingSubs, ~bit) & ~bit) == 0) && cleanup) 307 | { 308 | if (!locked) 309 | { 310 | if (LGMP_QUEUE_TRY_LOCK(hq)) 311 | locked = true; 312 | else 313 | { 314 | cleanup = false; 315 | continue; 316 | } 317 | } 318 | 319 | // someone else may have done this before we got the lock, so check 320 | if (hq->start != last) 321 | { 322 | LGMP_QUEUE_UNLOCK(hq); 323 | cleanup = false; 324 | locked = false; 325 | continue; 326 | } 327 | 328 | // message finished 329 | hq->start = next; 330 | // decrement the count 331 | uint32_t count = atomic_fetch_sub(&hq->count, 1); 332 | 333 | // check for underflow, this should never happen 334 | if (count == 0) 335 | { 336 | atomic_store(&hq->count, 0); 337 | LGMP_QUEUE_UNLOCK(hq); 338 | return LGMP_ERR_CORRUPTED; 339 | } 340 | } 341 | } 342 | 343 | // release the lock if we have it 344 | if (locked) 345 | { 346 | // update the timeout 347 | atomic_store(&hq->msgTimeout, 348 | atomic_load(&queue->header->timestamp) + hq->maxTime); 349 | LGMP_QUEUE_UNLOCK(hq); 350 | } 351 | 352 | queue->position = last; 353 | return LGMP_OK; 354 | } 355 | 356 | LGMP_STATUS lgmpClientProcess(PLGMPClientQueue queue, PLGMPMessage result) 357 | { 358 | assert(queue); 359 | assert(result); 360 | 361 | struct LGMPHeaderQueue *hq = queue->hq; 362 | const uint32_t bit = 1U << queue->id; 363 | const uint64_t subs = atomic_load(&hq->subs); 364 | 365 | if (LGMP_SUBS_BAD(subs) & bit) 366 | return LGMP_ERR_QUEUE_TIMEOUT; 367 | 368 | if (!(LGMP_SUBS_ON(subs) & bit)) 369 | { 370 | if (lgmpClientSessionValid(queue->client)) 371 | return LGMP_ERR_QUEUE_UNSUBSCRIBED; 372 | else 373 | return LGMP_ERR_INVALID_SESSION; 374 | } 375 | 376 | if (atomic_load(&hq->position) == queue->position) 377 | return LGMP_ERR_QUEUE_EMPTY; 378 | 379 | struct LGMPHeaderMessage *messages = (struct LGMPHeaderMessage *) 380 | (queue->client->mem + hq->messagesOffset); 381 | struct LGMPHeaderMessage *msg = &messages[queue->position]; 382 | 383 | result->udata = msg->udata; 384 | result->size = msg->size; 385 | result->mem = queue->client->mem + msg->offset; 386 | 387 | return LGMP_OK; 388 | } 389 | 390 | LGMP_STATUS lgmpClientMessageDone(PLGMPClientQueue queue) 391 | { 392 | assert(queue); 393 | 394 | struct LGMPHeaderQueue *hq = queue->hq; 395 | const uint32_t bit = 1U << queue->id; 396 | const uint64_t subs = atomic_load(&hq->subs); 397 | 398 | if (LGMP_SUBS_BAD(subs) & bit) 399 | return LGMP_ERR_QUEUE_TIMEOUT; 400 | 401 | if (!(LGMP_SUBS_ON(subs) & bit)) 402 | { 403 | if (lgmpClientSessionValid(queue->client)) 404 | return LGMP_ERR_QUEUE_UNSUBSCRIBED; 405 | else 406 | return LGMP_ERR_INVALID_SESSION; 407 | } 408 | 409 | if (atomic_load(&hq->position) == queue->position) 410 | return LGMP_ERR_QUEUE_EMPTY; 411 | 412 | struct LGMPHeaderMessage *messages = (struct LGMPHeaderMessage *) 413 | (queue->client->mem + hq->messagesOffset); 414 | struct LGMPHeaderMessage *msg = &messages[queue->position]; 415 | 416 | // turn off the pending bit for our queue and try to dequeue the message if 417 | // it's finished. 418 | if ((atomic_fetch_and(&msg->pendingSubs, ~bit) & ~bit) == 0 && 419 | LGMP_QUEUE_TRY_LOCK(hq)) 420 | { 421 | // someone else may have done this before we got the lock, so check 422 | if (hq->start != queue->position) 423 | { 424 | LGMP_QUEUE_UNLOCK(hq); 425 | goto done; 426 | } 427 | 428 | // message finished 429 | if (hq->start + 1 == hq->numMessages) 430 | hq->start = 0; 431 | else 432 | ++hq->start; 433 | 434 | // decrement the count and update the timeout 435 | uint32_t count = atomic_fetch_sub(&hq->count, 1); 436 | 437 | // check for underflow, this should never happen 438 | if (count == 0) 439 | { 440 | atomic_store(&hq->count, 0); 441 | LGMP_QUEUE_UNLOCK(hq); 442 | return LGMP_ERR_CORRUPTED; 443 | } 444 | 445 | atomic_store(&hq->msgTimeout, 446 | atomic_load(&queue->header->timestamp) + hq->maxTime); 447 | 448 | LGMP_QUEUE_UNLOCK(hq); 449 | } 450 | 451 | done: 452 | if (++queue->position == hq->numMessages) 453 | queue->position = 0; 454 | 455 | return LGMP_OK; 456 | } 457 | 458 | LGMP_STATUS lgmpClientSendData(PLGMPClientQueue queue, const void * data, 459 | uint32_t size, uint32_t * serial) 460 | { 461 | struct LGMPHeaderQueue *hq = queue->hq; 462 | const uint32_t bit = 1U << queue->id; 463 | const uint64_t subs = atomic_load(&hq->subs); 464 | 465 | if (size > LGMP_MSGS_SIZE) 466 | return LGMP_ERR_INVALID_SIZE; 467 | 468 | if (LGMP_SUBS_BAD(subs) & bit) 469 | return LGMP_ERR_QUEUE_TIMEOUT; 470 | 471 | // if there is no room, just return 472 | if (atomic_load(&hq->cMsgAvail) == 0) 473 | return LGMP_ERR_QUEUE_FULL; 474 | 475 | // lock the client message buffer 476 | LGMP_LOCK(hq->cMsgLock); 477 | 478 | // if there is now now room, unlock and return 479 | if (atomic_load(&hq->cMsgAvail) == 0) 480 | { 481 | LGMP_UNLOCK(hq->cMsgLock); 482 | return LGMP_ERR_QUEUE_FULL; 483 | } 484 | 485 | // get the write position and copy in the data 486 | uint32_t wpos = atomic_load(&hq->cMsgWPos); 487 | hq->cMsgs[wpos].size = size; 488 | memcpy(hq->cMsgs[wpos].data, data, size); 489 | 490 | // advance the write pointer and decrement the available count 491 | if (++wpos == LGMP_MSGS_MAX) 492 | wpos = 0; 493 | atomic_store(&hq->cMsgWPos, wpos); 494 | atomic_fetch_sub(&hq->cMsgAvail, 1); 495 | 496 | // increment the write serial 497 | uint32_t tmp = atomic_fetch_add(&hq->cMsgWSerial, 1); 498 | 499 | // unlock the client message buffer 500 | LGMP_UNLOCK(hq->cMsgLock); 501 | 502 | // return the message serial if it's wanted 503 | if (serial) 504 | *serial = tmp + 1; 505 | 506 | return LGMP_OK; 507 | }; 508 | 509 | LGMP_STATUS lgmpClientGetSerial(PLGMPClientQueue queue, uint32_t * serial) 510 | { 511 | struct LGMPHeaderQueue *hq = queue->hq; 512 | const uint32_t bit = 1U << queue->id; 513 | const uint64_t subs = atomic_load(&hq->subs); 514 | 515 | if (LGMP_SUBS_BAD(subs) & bit) 516 | return LGMP_ERR_QUEUE_TIMEOUT; 517 | 518 | *serial = atomic_load(&hq->cMsgRSerial); 519 | return LGMP_OK; 520 | } 521 | -------------------------------------------------------------------------------- /lgmp/src/headers.h: -------------------------------------------------------------------------------- 1 | /** 2 | * LGMP - Looking Glass Memory Protocol 3 | * Copyright © 2020-2025 Geoffrey McRae 4 | * https://github.com/gnif/LGMP 5 | * 6 | * This program is free software; you can redistribute it and/or modify it 7 | * under the terms of the GNU General Public License as published by the Free 8 | * Software Foundation; either version 2 of the License, or (at your option) 9 | * any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 | * more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along 17 | * with this program; if not, write to the Free Software Foundation, Inc., 59 18 | * Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 | */ 20 | 21 | #ifndef LGMP_PRIVATE_HEADERS_H 22 | #define LGMP_PRIVATE_HEADERS_H 23 | 24 | #include 25 | 26 | #include "lgmp.h" 27 | 28 | #define LGMP_PROTOCOL_MAGIC 0x504d474c 29 | #define LGMP_PROTOCOL_VERSION 7 30 | #define LGMP_MAX_QUEUES 5 31 | 32 | // maximum number of client messages supported 33 | #define LGMP_MSGS_MAX 10 34 | 35 | #ifdef _MSC_VER 36 | #define LGMP_LOCK_INIT(lock) \ 37 | (lock) = 0; 38 | 39 | #define LGMP_LOCK(lock) \ 40 | while (InterlockedCompareExchange((volatile LONG *)&(lock), 1, 0) != 0) {} 41 | 42 | #define LGMP_TRY_LOCK(lock) \ 43 | (InterlockedCompareExchange((volatile LONG *)&(lock), 1, 0) == 0) 44 | 45 | #define LGMP_UNLOCK(lock) \ 46 | InterlockedExchange((volatile LONG *)&(lock), 0) 47 | 48 | #define _Atomic(T) volatile T 49 | 50 | /** 51 | * WARNING: These defines must be used with GREAT CARE. They do not perfectly 52 | * replicate the behaviour of the std C11 methods */ 53 | #define atomic_load(var) *(var) 54 | #define atomic_store(var, v) (*var = v) 55 | #define atomic_fetch_add(var, v) (InterlockedAdd((volatile LONG *)(var), v) - v) 56 | #define atomic_fetch_and(var, v) InterlockedAnd((volatile LONG *)(var), v) 57 | #define atomic_fetch_sub(var, v) (InterlockedAdd((volatile LONG *)(var), -(v)) + v) 58 | #define atomic_exchange(var, v) InterlockedExchange((volatile LONG *)(var), v) 59 | 60 | #else 61 | #include 62 | 63 | /** 64 | * Note: we do not use atomic_flag in order to remain ABI compatible with MSVC 65 | */ 66 | 67 | #define LGMP_LOCK_INIT(lock) \ 68 | atomic_store(&(lock), 0); 69 | 70 | static inline void _LGMP_LOCK(_Atomic(uint32_t) * lock) 71 | { 72 | uint32_t expected = 0; 73 | while (!atomic_compare_exchange_strong(lock, &expected, 1)) { 74 | expected = 0; 75 | } 76 | } 77 | #define LGMP_LOCK(lock) _LGMP_LOCK(&(lock)) 78 | 79 | static inline bool _LGMP_TRY_LOCK(_Atomic(uint32_t) * lock) 80 | { 81 | uint32_t expected = 0; 82 | return atomic_compare_exchange_strong(lock, &expected, 1); 83 | } 84 | #define LGMP_TRY_LOCK(lock) _LGMP_TRY_LOCK(&(lock)) 85 | 86 | #define LGMP_UNLOCK(lock) \ 87 | atomic_store(&(lock), 0); 88 | #endif 89 | 90 | #define LGMP_QUEUE_LOCK(hq) LGMP_LOCK(hq->lock) 91 | #define LGMP_QUEUE_TRY_LOCK(hq) LGMP_TRY_LOCK(hq->lock) 92 | #define LGMP_QUEUE_UNLOCK(hq) LGMP_UNLOCK(hq->lock) 93 | 94 | #define LGMP_SUBS_ON(x) (uint32_t)(x >> 32) 95 | #define LGMP_SUBS_BAD(x) (uint32_t)(x >> 0) 96 | #define LGMP_SUBS_OR_BAD(x, bad) ((x) | (bad)) 97 | #define LGMP_SUBS_CLEAR(x, cl) ((x) & ~((cl) | ((uint64_t)(cl) << 32))) 98 | #define LGMP_SUBS_SET(x, st) ((x) | ((uint64_t)(st) << 32)) 99 | 100 | #ifdef _MSC_VER 101 | //NOTE: the default alignment of MSVC matches that of gcc (for now) 102 | //If this becomes an issue in the future we will need to investigate a 103 | //solution 104 | #define ALIGNED 105 | #else 106 | #define ALIGNED __attribute__((aligned(4))) 107 | #endif 108 | 109 | struct LGMPHeaderMessage 110 | { 111 | uint32_t udata; 112 | uint32_t size; 113 | uint32_t offset; 114 | _Atomic(uint32_t) pendingSubs; 115 | } 116 | ALIGNED; 117 | 118 | struct LGMPClientMessage 119 | { 120 | uint32_t size; 121 | uint8_t data[LGMP_MSGS_SIZE]; 122 | } 123 | ALIGNED; 124 | 125 | struct LGMPHeaderQueue 126 | { 127 | uint32_t queueID; 128 | uint32_t numMessages; 129 | _Atomic(uint32_t) newSubCount; 130 | uint32_t maxTime; 131 | 132 | _Atomic(uint32_t) position; 133 | uint32_t messagesOffset; 134 | uint64_t timeout[32]; 135 | uint32_t clientID[32]; 136 | 137 | /* the lock MUST be held to use the following values */ 138 | _Atomic(uint32_t) lock; 139 | _Atomic(uint64_t) subs; // see LGMP_SUBS_* macros 140 | uint32_t start; 141 | _Atomic(uint64_t) msgTimeout; 142 | _Atomic(uint32_t) count; 143 | 144 | /* messages submitted from the client */ 145 | _Atomic(uint32_t) cMsgLock; 146 | _Atomic(uint32_t) cMsgAvail; 147 | _Atomic(uint32_t) cMsgWPos; 148 | _Atomic(uint32_t) cMsgWSerial; 149 | _Atomic(uint32_t) cMsgRSerial; 150 | struct LGMPClientMessage cMsgs[LGMP_MSGS_MAX]; 151 | } 152 | ALIGNED; 153 | 154 | #ifdef _MSC_VER 155 | // don't warn on zero length arrays 156 | #pragma warning(push) 157 | #pragma warning(disable: 4200) 158 | #endif 159 | 160 | struct LGMPHeader 161 | { 162 | uint32_t magic; 163 | uint32_t version; 164 | uint32_t sessionID; 165 | uint32_t numQueues; 166 | _Atomic(uint64_t) timestamp; 167 | struct LGMPHeaderQueue queues[LGMP_MAX_QUEUES]; 168 | uint32_t udataSize; 169 | uint8_t udata[0]; 170 | } 171 | ALIGNED; 172 | 173 | #ifdef _MSC_VER 174 | #pragma warning(pop) 175 | #endif 176 | 177 | #endif 178 | -------------------------------------------------------------------------------- /lgmp/src/host.c: -------------------------------------------------------------------------------- 1 | /** 2 | * LGMP - Looking Glass Memory Protocol 3 | * Copyright © 2020-2025 Geoffrey McRae 4 | * https://github.com/gnif/LGMP 5 | * 6 | * This program is free software; you can redistribute it and/or modify it 7 | * under the terms of the GNU General Public License as published by the Free 8 | * Software Foundation; either version 2 of the License, or (at your option) 9 | * any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 | * more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along 17 | * with this program; if not, write to the Free Software Foundation, Inc., 59 18 | * Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 | */ 20 | 21 | #include "lgmp/host.h" 22 | 23 | #include "lgmp.h" 24 | #include "headers.h" 25 | 26 | #include 27 | #include 28 | #include 29 | 30 | #define ALIGN(x) ((x + (3)) & ~(3)) 31 | 32 | struct LGMPHostQueue 33 | { 34 | PLGMPHost host; 35 | unsigned int index; 36 | uint32_t position; 37 | uint32_t cMsgPos; 38 | 39 | struct LGMPHeaderQueue * hq; 40 | }; 41 | 42 | struct LGMPHost 43 | { 44 | uint8_t * mem; 45 | size_t size; 46 | uint32_t avail; 47 | uint32_t nextFree; 48 | bool started; 49 | uint32_t sessionID; 50 | uint32_t numQueues; 51 | uint8_t * udata; 52 | uint32_t udataSize; 53 | 54 | struct LGMPHeader * header; 55 | struct LGMPHostQueue queues[LGMP_MAX_QUEUES]; 56 | }; 57 | 58 | static void initHeader(PLGMPHost host) 59 | { 60 | host->header->timestamp = lgmpGetClockMS(); 61 | host->header->version = LGMP_PROTOCOL_VERSION; 62 | host->header->numQueues = host->numQueues; 63 | host->header->udataSize = host->udataSize; 64 | memcpy(host->header->udata, host->udata, host->udataSize); 65 | 66 | // this must be set last to ensure a client doesn't read invalid data before 67 | // we're ready 68 | host->header->magic = LGMP_PROTOCOL_MAGIC; 69 | } 70 | 71 | LGMP_STATUS lgmpHostInit(void *mem, const uint32_t size, PLGMPHost * result, 72 | uint32_t udataSize, uint8_t * udata) 73 | { 74 | assert(mem); 75 | assert(size > 0); 76 | assert(result); 77 | 78 | *result = NULL; 79 | 80 | // make sure that lgmpGetClockMS works 81 | if (!lgmpGetClockMS()) 82 | return LGMP_ERR_CLOCK_FAILURE; 83 | 84 | if (size < sizeof(struct LGMPHeader) + udataSize) 85 | return LGMP_ERR_INVALID_SIZE; 86 | 87 | *result = calloc(1, sizeof(**result)); 88 | if (!*result) 89 | return LGMP_ERR_NO_MEM; 90 | 91 | PLGMPHost host = *result; 92 | host->mem = mem; 93 | host->size = size; 94 | host->avail = size - ALIGN(sizeof(struct LGMPHeader) + udataSize); 95 | host->nextFree = ALIGN(sizeof(struct LGMPHeader) + udataSize); 96 | host->header = (struct LGMPHeader *)mem; 97 | 98 | // take a copy of the user data so we can re-init the header if it gets wiped 99 | // out by a misbehaving process. 100 | host->udata = malloc(udataSize); 101 | if (!host->udata) 102 | { 103 | free(*result); 104 | *result = NULL; 105 | return LGMP_ERR_NO_MEM; 106 | } 107 | memcpy(host->udata, udata, udataSize); 108 | host->udataSize = udataSize; 109 | 110 | // ensure the sessionID changes so that clients can determine if the host was 111 | // restarted. 112 | const uint32_t sessionID = host->header->sessionID; 113 | host->sessionID = rand(); 114 | while(sessionID == host->sessionID) 115 | host->sessionID = rand(); 116 | host->header->sessionID = host->sessionID; 117 | 118 | initHeader(host); 119 | return LGMP_OK; 120 | } 121 | 122 | void lgmpHostFree(PLGMPHost * host) 123 | { 124 | assert(host); 125 | if (!*host) 126 | return; 127 | 128 | free((*host)->udata); 129 | free(*host); 130 | *host = NULL; 131 | } 132 | 133 | LGMP_STATUS lgmpHostQueueNew(PLGMPHost host, const struct LGMPQueueConfig config, 134 | PLGMPHostQueue * result) 135 | { 136 | assert(host); 137 | assert(result); 138 | 139 | *result = NULL; 140 | if (host->started) 141 | return LGMP_ERR_HOST_STARTED; 142 | 143 | if (host->numQueues == LGMP_MAX_QUEUES) 144 | return LGMP_ERR_NO_QUEUES; 145 | 146 | // + 1 for end marker 147 | uint32_t numMessages = config.numMessages + 1; 148 | 149 | const size_t needed = sizeof(struct LGMPHeaderMessage) * numMessages; 150 | if (host->avail < needed) 151 | return LGMP_ERR_NO_SHARED_MEM; 152 | 153 | *result = &host->queues[host->numQueues]; 154 | PLGMPHostQueue queue = *result; 155 | 156 | struct LGMPHeaderQueue * hq = &host->header->queues[host->numQueues++]; 157 | hq->queueID = config.queueID; 158 | hq->numMessages = numMessages; 159 | hq->newSubCount = 0; 160 | LGMP_LOCK_INIT(hq->lock); 161 | hq->subs = 0; 162 | hq->position = 0; 163 | hq->messagesOffset = host->nextFree; 164 | hq->start = 0; 165 | atomic_store(&hq->msgTimeout, 0); 166 | hq->maxTime = config.subTimeout; 167 | hq->count = 0; 168 | 169 | LGMP_LOCK_INIT(hq->cMsgLock); 170 | atomic_store(&hq->cMsgAvail , LGMP_MSGS_MAX); 171 | atomic_store(&hq->cMsgWPos , 0); 172 | atomic_store(&hq->cMsgWSerial, 0); 173 | atomic_store(&hq->cMsgRSerial, 0); 174 | 175 | queue->host = host; 176 | queue->index = host->numQueues; 177 | queue->position = 0; 178 | queue->hq = hq; 179 | 180 | host->avail -= ALIGN(needed); 181 | host->nextFree += ALIGN(needed); 182 | 183 | ++host->header->numQueues; 184 | return LGMP_OK; 185 | } 186 | 187 | bool lgmpHostQueueHasSubs(PLGMPHostQueue queue) 188 | { 189 | assert(queue); 190 | return LGMP_SUBS_ON(atomic_load(&queue->hq->subs)) != 0; 191 | } 192 | 193 | uint32_t lgmpHostQueueNewSubs(PLGMPHostQueue queue) 194 | { 195 | assert(queue); 196 | return atomic_exchange(&queue->hq->newSubCount, 0); 197 | } 198 | 199 | uint32_t lgmpHostQueuePending(PLGMPHostQueue queue) 200 | { 201 | assert(queue); 202 | return atomic_load(&queue->hq->count); 203 | } 204 | 205 | LGMP_STATUS lgmpHostProcess(PLGMPHost host) 206 | { 207 | assert(host); 208 | 209 | // for an unkown reason sometimes when the guest starts the shared memory is 210 | // zeroed by something external after we have initialized it, detect this and 211 | // report it. 212 | if (host->header->magic != LGMP_PROTOCOL_MAGIC) 213 | return LGMP_ERR_CORRUPTED; 214 | 215 | const uint64_t now = lgmpGetClockMS(); 216 | atomic_store(&host->header->timestamp, now); 217 | 218 | // each queue 219 | for(unsigned int i = 0; i < host->numQueues; ++i) 220 | { 221 | struct LGMPHostQueue *queue = &host->queues[i]; 222 | struct LGMPHeaderQueue *hq = queue->hq; 223 | struct LGMPHeaderMessage *messages = (struct LGMPHeaderMessage *) 224 | (host->mem + hq->messagesOffset); 225 | 226 | LGMP_QUEUE_LOCK(hq); 227 | if(!atomic_load(&queue->hq->count)) 228 | { 229 | LGMP_QUEUE_UNLOCK(hq); 230 | continue; 231 | } 232 | 233 | uint64_t subs = atomic_load(&hq->subs); 234 | for(;;) 235 | { 236 | struct LGMPHeaderMessage *msg = &messages[hq->start]; 237 | uint32_t pend = msg->pendingSubs & LGMP_SUBS_ON(subs); 238 | 239 | const uint32_t newBadSubs = pend & ~LGMP_SUBS_BAD(subs); 240 | if (newBadSubs && now > atomic_load(&hq->msgTimeout)) 241 | { 242 | // reset garbage collection timeout for new bad subs 243 | subs = LGMP_SUBS_OR_BAD(subs, newBadSubs); 244 | const uint64_t timeout = now + hq->maxTime; 245 | for(unsigned int id = 0; id < 32; ++id) 246 | if (newBadSubs & (1U << id)) 247 | hq->timeout[id] = timeout; 248 | 249 | // clear the pending subs 250 | msg->pendingSubs = 0; 251 | pend = 0; 252 | } 253 | 254 | // if there are still valid pending subs break out 255 | if (pend & ~LGMP_SUBS_BAD(subs)) 256 | break; 257 | 258 | // message finished 259 | if (hq->start + 1 == hq->numMessages) 260 | hq->start = 0; 261 | else 262 | ++hq->start; 263 | 264 | // decrement the queue count and break out if there are no more messages 265 | if (atomic_fetch_sub(&queue->hq->count, 1) == 1) 266 | break; 267 | 268 | // update the timeout 269 | atomic_store(&hq->msgTimeout, now + hq->maxTime); 270 | } 271 | 272 | atomic_store(&hq->subs, subs); 273 | LGMP_QUEUE_UNLOCK(hq); 274 | } 275 | 276 | return LGMP_OK; 277 | } 278 | 279 | size_t lgmpHostMemAvail(PLGMPHost host) 280 | { 281 | assert(host); 282 | return host->avail; 283 | } 284 | 285 | LGMP_STATUS lgmpHostMemAlloc(PLGMPHost host, uint32_t size, PLGMPMemory *result) 286 | { 287 | return lgmpHostMemAllocAligned(host, size, 4, result); 288 | } 289 | 290 | LGMP_STATUS lgmpHostMemAllocAligned(PLGMPHost host, uint32_t size, 291 | uint32_t alignment, PLGMPMemory *result) 292 | { 293 | assert(host); 294 | assert(result); 295 | 296 | uint32_t nextFree = host->nextFree; 297 | if (alignment > 0) 298 | { 299 | // alignment must be a power of two 300 | if ((alignment & (alignment - 1)) != 0) 301 | return LGMP_ERR_INVALID_ALIGNMENT; 302 | 303 | size = (size + (alignment - 1)) & ~(alignment - 1); 304 | nextFree = (nextFree + (alignment - 1)) & ~(alignment - 1); 305 | } 306 | 307 | if (size > host->avail - (nextFree - host->nextFree)) 308 | return LGMP_ERR_NO_SHARED_MEM; 309 | 310 | *result = calloc(1, sizeof(**result)); 311 | if (!*result) 312 | return LGMP_ERR_NO_MEM; 313 | 314 | PLGMPMemory mem = *result; 315 | mem->host = host; 316 | mem->offset = nextFree; 317 | mem->size = size; 318 | mem->mem = host->mem + nextFree; 319 | 320 | host->avail -= (nextFree - host->nextFree) + size; 321 | host->nextFree = nextFree + size; 322 | 323 | return LGMP_OK; 324 | } 325 | 326 | void lgmpHostMemFree(PLGMPMemory * mem) 327 | { 328 | assert(mem); 329 | if (!*mem) 330 | return; 331 | 332 | free(*mem); 333 | *mem = NULL; 334 | } 335 | 336 | void * lgmpHostMemPtr(PLGMPMemory mem) 337 | { 338 | assert(mem); 339 | return mem->mem; 340 | } 341 | 342 | LGMP_STATUS lgmpHostQueuePost(PLGMPHostQueue queue, uint32_t udata, 343 | PLGMPMemory payload) 344 | { 345 | struct LGMPHeaderQueue *hq = queue->hq; 346 | 347 | LGMP_QUEUE_LOCK(hq); 348 | 349 | // get the subscribers 350 | const uint64_t subs = atomic_load(&hq->subs); 351 | const uint32_t pend = LGMP_SUBS_ON(subs) & ~(LGMP_SUBS_BAD(subs)); 352 | 353 | // if nobody has subscribed there is no point in posting the message 354 | if (!pend) 355 | { 356 | LGMP_QUEUE_UNLOCK(hq); 357 | return LGMP_OK; 358 | } 359 | 360 | // we should never fully fill the buffer 361 | if (atomic_load(&queue->hq->count) == hq->numMessages - 1) 362 | { 363 | LGMP_QUEUE_UNLOCK(hq); 364 | return LGMP_ERR_QUEUE_FULL; 365 | } 366 | 367 | struct LGMPHeaderMessage *messages = (struct LGMPHeaderMessage *) 368 | (queue->host->mem + hq->messagesOffset); 369 | 370 | struct LGMPHeaderMessage *msg = &messages[queue->position]; 371 | 372 | msg->udata = udata; 373 | msg->size = payload->size; 374 | msg->offset = payload->offset; 375 | msg->pendingSubs = pend; 376 | 377 | // increment the queue count, if it were zero update the msgTimeout 378 | if (atomic_fetch_add(&hq->count, 1) == 0) 379 | atomic_store(&hq->msgTimeout, lgmpGetClockMS() + hq->maxTime); 380 | 381 | if (++queue->position == hq->numMessages) 382 | queue->position = 0; 383 | 384 | atomic_store(&hq->position, queue->position); 385 | 386 | LGMP_QUEUE_UNLOCK(hq); 387 | return LGMP_OK; 388 | } 389 | 390 | LGMP_STATUS lgmpHostReadData(PLGMPHostQueue queue, void * data, size_t * size) 391 | { 392 | struct LGMPHeaderQueue *hq = queue->hq; 393 | 394 | if (atomic_load(&hq->cMsgAvail) == LGMP_MSGS_MAX) 395 | return LGMP_ERR_QUEUE_EMPTY; 396 | 397 | // lock the client message buffer 398 | LGMP_LOCK(hq->cMsgLock); 399 | 400 | struct LGMPClientMessage * msg = &hq->cMsgs[queue->cMsgPos]; 401 | if (++queue->cMsgPos == LGMP_MSGS_MAX) 402 | queue->cMsgPos = 0; 403 | 404 | memcpy(data, msg->data, msg->size); 405 | *size = msg->size; 406 | 407 | atomic_fetch_add(&hq->cMsgAvail, 1); 408 | LGMP_UNLOCK(hq->cMsgLock); 409 | return LGMP_OK; 410 | } 411 | 412 | LGMP_STATUS lgmpHostAckData(PLGMPHostQueue queue) 413 | { 414 | struct LGMPHeaderQueue *hq = queue->hq; 415 | atomic_fetch_add(&hq->cMsgRSerial, 1); 416 | return LGMP_OK; 417 | } 418 | 419 | LGMP_STATUS lgmpHostGetClientIDs(PLGMPHostQueue queue, uint32_t clientIDs[32], 420 | unsigned int * count) 421 | { 422 | assert(queue); 423 | assert(count); 424 | 425 | struct LGMPHeaderQueue *hq = queue->hq; 426 | 427 | LGMP_QUEUE_LOCK(hq); 428 | const uint64_t now = lgmpGetClockMS(); 429 | 430 | uint64_t subs = atomic_load(&hq->subs); 431 | *count = 0; 432 | for(int i = 0; i < 32; ++i) 433 | { 434 | const uint32_t bit = 1U << i; 435 | if (!(LGMP_SUBS_ON(subs) & bit) || 436 | ((LGMP_SUBS_BAD(subs) & bit) && now > hq->timeout[i])) 437 | continue; 438 | 439 | clientIDs[(*count)++] = hq->clientID[i]; 440 | } 441 | LGMP_QUEUE_UNLOCK(hq); 442 | return LGMP_OK; 443 | } 444 | -------------------------------------------------------------------------------- /lgmp/src/lgmp.h: -------------------------------------------------------------------------------- 1 | /** 2 | * LGMP - Looking Glass Memory Protocol 3 | * Copyright © 2020-2025 Geoffrey McRae 4 | * https://github.com/gnif/LGMP 5 | * 6 | * This program is free software; you can redistribute it and/or modify it 7 | * under the terms of the GNU General Public License as published by the Free 8 | * Software Foundation; either version 2 of the License, or (at your option) 9 | * any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 | * more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along 17 | * with this program; if not, write to the Free Software Foundation, Inc., 59 18 | * Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 | */ 20 | 21 | #ifndef LGMP_PRIVATE_LGMP_H 22 | #define LGMP_PRIVATE_LGMP_H 23 | 24 | #include "lgmp/lgmp.h" 25 | #include 26 | 27 | #if defined(_WIN32) 28 | #include 29 | #else 30 | #include 31 | #endif 32 | 33 | struct LGMPMemory 34 | { 35 | PLGMPHost host; 36 | unsigned int offset; 37 | uint32_t size; 38 | void *mem; 39 | }; 40 | 41 | // returns a milliseond resolution monotonic counter 42 | inline static uint64_t lgmpGetClockMS(void) 43 | { 44 | #if defined(_WIN32) 45 | static LARGE_INTEGER freq = { 0 }; 46 | static LARGE_INTEGER start = { 0 }; 47 | if (!freq.QuadPart) 48 | { 49 | QueryPerformanceFrequency(&freq); 50 | QueryPerformanceCounter(&start); 51 | if (freq.QuadPart == 0) 52 | abort(); 53 | } 54 | 55 | LARGE_INTEGER time; 56 | QueryPerformanceCounter(&time); 57 | return (((time.QuadPart - start.QuadPart) * 1000ULL) / freq.QuadPart) + 1; 58 | #else 59 | struct timespec tsnow; 60 | if (clock_gettime(CLOCK_MONOTONIC, &tsnow) != 0) 61 | return 0; 62 | return (uint64_t)tsnow.tv_sec * 1000ULL + (uint64_t)tsnow.tv_nsec / 1000000ULL; 63 | #endif 64 | } 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /lgmp/src/status.c: -------------------------------------------------------------------------------- 1 | /** 2 | * LGMP - Looking Glass Memory Protocol 3 | * Copyright © 2020-2025 Geoffrey McRae 4 | * https://github.com/gnif/LGMP 5 | * 6 | * This program is free software; you can redistribute it and/or modify it 7 | * under the terms of the GNU General Public License as published by the Free 8 | * Software Foundation; either version 2 of the License, or (at your option) 9 | * any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 | * more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along 17 | * with this program; if not, write to the Free Software Foundation, Inc., 59 18 | * Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 | */ 20 | 21 | #include "lgmp/status.h" 22 | 23 | const char * lgmpStatusString(LGMP_STATUS status) 24 | { 25 | switch(status) 26 | { 27 | case LGMP_OK : return "LGMP_OK"; 28 | case LGMP_ERR_CLOCK_FAILURE : return "LGMP_CLOCK_FAILURE"; 29 | case LGMP_ERR_INVALID_ARGUMENT : return "LGMP_ERR_INVALID_ARGUMENT"; 30 | case LGMP_ERR_INVALID_SIZE : return "LGMP_ERR_INVALID_SIZE"; 31 | case LGMP_ERR_INVALID_ALIGNMENT : return "LGMP_ERR_INVALID_ALIGNMENT"; 32 | case LGMP_ERR_INVALID_SESSION : return "LGMP_ERR_INVALID_SESSION"; 33 | case LGMP_ERR_NO_MEM : return "LGMP_ERR_NO_MEM"; 34 | case LGMP_ERR_NO_SHARED_MEM : return "LGMP_ERR_NO_SHARED_MEM"; 35 | case LGMP_ERR_HOST_STARTED : return "LGMP_ERR_HOST_STARTED"; 36 | case LGMP_ERR_NO_QUEUES : return "LGMP_ERR_NO_QUEUES"; 37 | case LGMP_ERR_QUEUE_FULL : return "LGMP_ERR_QUEUE_FULL"; 38 | case LGMP_ERR_QUEUE_EMPTY : return "LGMP_ERR_QUEUE_EMPTY"; 39 | case LGMP_ERR_QUEUE_UNSUBSCRIBED: return "LGMP_ERR_QUEUE_UNSUBSCRIBED"; 40 | case LGMP_ERR_QUEUE_TIMEOUT : return "LGMP_ERR_QUEUE_TIMEOUT"; 41 | case LGMP_ERR_INVALID_MAGIC : return "LGMP_ERR_INVALID_MAGIC"; 42 | case LGMP_ERR_INVALID_VERSION : return "LGMP_ERR_INVALID_VERSION"; 43 | case LGMP_ERR_NO_SUCH_QUEUE : return "LGMP_ERR_NO_SUCH_QUEUE"; 44 | case LGMP_ERR_CORRUPTED : return "LGMP_ERR_CORRUPTED"; 45 | } 46 | return "Invalid status!"; 47 | } 48 | -------------------------------------------------------------------------------- /refresh-copyright: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import datetime 4 | import fnmatch 5 | import os 6 | import re 7 | import subprocess 8 | from textwrap import wrap 9 | 10 | PROJECT = os.path.dirname(__file__) 11 | EXTENSIONS = ('.c', '.cpp', '.h', '.nsi', '.rc') 12 | START_YEAR = 2020 13 | CURRENT_YEAR = datetime.date.today().year 14 | 15 | reignore = re.compile('^vendor/|.*/shader/|.*/d3d12.h$') 16 | recopyright = re.compile(r'\A/\*.*?\*/\s+', re.DOTALL) 17 | 18 | project_name = 'LGMP - Looking Glass Memory Protocol' 19 | copyright = f'Copyright © {START_YEAR}-{CURRENT_YEAR} Geoffrey McRae ' 20 | project_url = 'https://github.com/gnif/LGMP' 21 | header = [project_name, copyright, project_url] 22 | 23 | paragraphs = ['''\ 24 | This program is free software; you can redistribute it and/or modify it 25 | under the terms of the GNU General Public License as published by the Free 26 | Software Foundation; either version 2 of the License, or (at your option) 27 | any later version.''', 28 | '''\ 29 | This program is distributed in the hope that it will be useful, but WITHOUT 30 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 31 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 32 | more details.''', 33 | '''\ 34 | You should have received a copy of the GNU General Public License along 35 | with this program; if not, write to the Free Software Foundation, Inc., 59 36 | Temple Place, Suite 330, Boston, MA 02111-1307 USA'''] 37 | 38 | 39 | def make_comment_block(): 40 | lines = ['/**'] 41 | lines += [' * ' + line for line in header] 42 | 43 | for paragraph in paragraphs: 44 | lines.append(' *') 45 | lines += wrap(paragraph, width=78, initial_indent=' * ', subsequent_indent=' * ') 46 | 47 | lines.append(' */') 48 | return '\n'.join(lines) + '\n\n' 49 | 50 | 51 | def gen_c_literal(): 52 | lines = [''] + header 53 | for paragraph in paragraphs: 54 | lines.append('') 55 | lines += wrap(paragraph, width=79) 56 | lines.append('') 57 | return '\n'.join(f' "{line}\\n"' for line in lines) + '\n' 58 | 59 | 60 | def update_c_style(file, copyright): 61 | print(f'Updating copyright for {file}...') 62 | with open(file, encoding='utf-8') as f: 63 | data = recopyright.sub('', f.read()) 64 | with open(file, 'w', encoding='utf-8') as f: 65 | f.write(copyright) 66 | f.write(data) 67 | 68 | def appstring_license(): 69 | lines = [] 70 | for paragraph in paragraphs: 71 | paragraph = wrap(paragraph, width=75) 72 | for line in paragraph[:-1]: 73 | lines.append(f' "{line} "') 74 | lines.append(f' "{paragraph[-1]}\\n"') 75 | lines.append(r' "\n"') 76 | lines.pop() 77 | lines[-1] = f'{lines[-1][:-3]}";' 78 | return lines 79 | 80 | def main(): 81 | comment_block = make_comment_block() 82 | files = subprocess.check_output(['git', '-C', PROJECT, 'ls-files', '-z']).decode('utf-8').split('\0') 83 | for file in files: 84 | if reignore.match(file): 85 | continue 86 | if file.endswith(EXTENSIONS): 87 | update_c_style(os.path.join(PROJECT, file), comment_block) 88 | 89 | if __name__ == '__main__': 90 | main() -------------------------------------------------------------------------------- /relacy-test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(relacy-test) 3 | 4 | include_directories(include) 5 | set(SOURCES main.cpp) 6 | 7 | get_filename_component(PROJECT_TOP "${PROJECT_SOURCE_DIR}/.." ABSOLUTE) 8 | add_subdirectory("${PROJECT_TOP}/lgmp" "${CMAKE_BINARY_DIR}/lgmp") 9 | target_compile_definitions(lgmp PRIVATE -DLGMP_REALACY) 10 | 11 | add_executable(relacy-test ${SOURCES}) 12 | target_link_libraries(relacy-test 13 | lgmp 14 | ) 15 | target_include_directories(relacy-test 16 | PUBLIC 17 | "${PROJECT_TOP}/relacy" 18 | ) 19 | -------------------------------------------------------------------------------- /relacy-test/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * LGMP - Looking Glass Memory Protocol 3 | * Copyright © 2020-2025 Geoffrey McRae 4 | * https://github.com/gnif/LGMP 5 | * 6 | * This program is free software; you can redistribute it and/or modify it 7 | * under the terms of the GNU General Public License as published by the Free 8 | * Software Foundation; either version 2 of the License, or (at your option) 9 | * any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 | * more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along 17 | * with this program; if not, write to the Free Software Foundation, Inc., 59 18 | * Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 | */ 20 | 21 | #include 22 | #include 23 | #include 24 | 25 | class CLGMPQueueTest 26 | { 27 | public: 28 | bool initHost(void * mem, const size_t memSize) 29 | { 30 | LGMP_STATUS status; 31 | 32 | uint8_t data[32]; 33 | memset(data, 0xaa, sizeof(data)); 34 | if ((status = lgmpHostInit(mem, memSize, &m_host, sizeof(data), data)) 35 | != LGMP_OK) 36 | { 37 | printf("lgmpHostInit: %s\n", lgmpStatusString(status)); 38 | return false; 39 | } 40 | 41 | const struct LGMPQueueConfig conf = 42 | { 43 | .queueID = 0, 44 | .numMessages = 10, 45 | .subTimeout = 10000 46 | }; 47 | 48 | if ((status = lgmpHostQueueNew(m_host, conf, &m_hqueue)) != LGMP_OK) 49 | { 50 | printf("lgmpHostQueueNew: %s\n", lgmpStatusString(status)); 51 | return false; 52 | } 53 | 54 | if ((status = lgmpHostMemAlloc(m_host, 1024, &m_mem)) != LGMP_OK) 55 | { 56 | printf("lgmpHostMemAlloc: %s\n", lgmpStatusString(status)); 57 | return false; 58 | } 59 | 60 | return true; 61 | } 62 | 63 | bool initClient(void * mem, const size_t memSize) 64 | { 65 | LGMP_STATUS status; 66 | 67 | uint32_t dataSize; 68 | uint8_t * data; 69 | 70 | if((status = lgmpClientInit(mem, memSize, &m_client)) 71 | != LGMP_OK) 72 | { 73 | printf("lgmpClientInit: %s\n", lgmpStatusString(status)); 74 | return false; 75 | } 76 | 77 | uint32_t clientId; 78 | if((status = lgmpClientSessionInit(m_client, &dataSize, &data, &clientId)) 79 | != LGMP_OK) 80 | { 81 | printf("lgmpClientSessionInit: %s\n", lgmpStatusString(status)); 82 | return false; 83 | } 84 | 85 | if ((status = lgmpClientSubscribe(m_client, 0, &m_cqueue)) != LGMP_OK) 86 | { 87 | printf("lgmpClientSubscribe: %s\n", lgmpStatusString(status)); 88 | return false; 89 | } 90 | 91 | return true; 92 | } 93 | 94 | void fini() 95 | { 96 | lgmpClientUnsubscribe(&m_cqueue); 97 | lgmpClientFree (&m_client); 98 | lgmpHostFree (&m_host ); 99 | } 100 | 101 | bool hostProcess() 102 | { 103 | LGMP_STATUS status; 104 | if ((status = lgmpHostProcess(m_host)) != LGMP_OK) 105 | { 106 | printf("lgmpHostProcess: %s\n", lgmpStatusString(status)); 107 | return false; 108 | } 109 | return true; 110 | } 111 | 112 | LGMP_STATUS enqueue() 113 | { 114 | return lgmpHostQueuePost(m_hqueue, 0, m_mem); 115 | } 116 | 117 | LGMP_STATUS peek() 118 | { 119 | LGMPMessage msg; 120 | return lgmpClientProcess(m_cqueue, &msg); 121 | } 122 | 123 | LGMP_STATUS dequeue() 124 | { 125 | return lgmpClientMessageDone(m_cqueue); 126 | } 127 | 128 | private: 129 | PLGMPHost m_host = NULL; 130 | PLGMPHostQueue m_hqueue = NULL; 131 | PLGMPMemory m_mem = NULL; 132 | 133 | PLGMPClient m_client = NULL; 134 | PLGMPClientQueue m_cqueue = NULL; 135 | }; 136 | 137 | struct QueueTest : rl::test_suite 138 | { 139 | CLGMPQueueTest q; 140 | const size_t memSize = 10 * 1024 * 1024; 141 | void * mem; 142 | int pending = 0; 143 | 144 | void before() 145 | { 146 | RL_ASSERT((mem = malloc(memSize)) != NULL); 147 | RL_ASSERT(q.initHost (mem, memSize)); 148 | RL_ASSERT(q.initClient(mem, memSize)); 149 | } 150 | 151 | void after() 152 | { 153 | q.fini(); 154 | free(mem); 155 | } 156 | 157 | void thread(unsigned thread_index) 158 | { 159 | if (thread_index < 30) 160 | { 161 | RL_ASSERT(q.hostProcess()); 162 | LGMP_STATUS status = q.enqueue(); 163 | 164 | if (pending == 10) 165 | RL_ASSERT(status == LGMP_ERR_QUEUE_FULL); 166 | else 167 | { 168 | RL_ASSERT(status == LGMP_OK); 169 | ++pending; 170 | } 171 | } 172 | else 173 | { 174 | LGMP_STATUS status = q.peek(); 175 | if (pending == 0) 176 | { 177 | RL_ASSERT(status == LGMP_ERR_QUEUE_EMPTY); 178 | RL_ASSERT(q.dequeue() == LGMP_ERR_QUEUE_EMPTY); 179 | } 180 | else 181 | { 182 | RL_ASSERT(status == LGMP_OK); 183 | RL_ASSERT(q.dequeue() == LGMP_OK); 184 | --pending; 185 | } 186 | } 187 | } 188 | }; 189 | 190 | int main() 191 | { 192 | rl::simulate(); 193 | } 194 | -------------------------------------------------------------------------------- /test-client/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(test-client) 3 | 4 | include_directories(include) 5 | set(SOURCES main.c) 6 | 7 | get_filename_component(PROJECT_TOP "${PROJECT_SOURCE_DIR}/.." ABSOLUTE) 8 | add_subdirectory("${PROJECT_TOP}/lgmp" "${CMAKE_BINARY_DIR}/lgmp") 9 | 10 | add_executable(test-client ${SOURCES}) 11 | target_link_libraries(test-client 12 | lgmp 13 | ) 14 | -------------------------------------------------------------------------------- /test-client/main.c: -------------------------------------------------------------------------------- 1 | /** 2 | * LGMP - Looking Glass Memory Protocol 3 | * Copyright © 2020-2025 Geoffrey McRae 4 | * https://github.com/gnif/LGMP 5 | * 6 | * This program is free software; you can redistribute it and/or modify it 7 | * under the terms of the GNU General Public License as published by the Free 8 | * Software Foundation; either version 2 of the License, or (at your option) 9 | * any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 | * more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along 17 | * with this program; if not, write to the Free Software Foundation, Inc., 59 18 | * Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 | */ 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include 31 | #include 32 | #include "../kvmfr.h" 33 | 34 | #include "lgmp/client.h" 35 | #include "../../lgmp/src/lgmp.h" 36 | 37 | void * ram; 38 | 39 | int main(int argc, char * argv[]) 40 | { 41 | unsigned int delay = 50; 42 | const char * shmFile = NULL; 43 | bool error = false; 44 | 45 | int opt; 46 | while ((opt = getopt(argc, argv, "f:d:")) != -1) { 47 | switch(opt) 48 | { 49 | case 'f': 50 | shmFile = optarg; 51 | break; 52 | 53 | case 'd': 54 | delay = atoi(optarg) * 1000; 55 | break; 56 | 57 | default: 58 | error = true; 59 | break; 60 | } 61 | } 62 | 63 | srand(lgmpGetClockMS()); 64 | 65 | if (!shmFile || error) 66 | { 67 | fprintf(stderr, "Invalid usage, expected: -f /dev/shm/file -d N\n"); 68 | exit(EXIT_FAILURE); 69 | } 70 | 71 | int fd; 72 | bool dmabuf = false; 73 | unsigned devSize; 74 | 75 | if (strlen(shmFile) > 8 && memcmp(shmFile, "/dev/kvmfr", 10) == 0) 76 | { 77 | dmabuf = true; 78 | fd = open(shmFile, O_RDWR, (mode_t)0600); 79 | 80 | // get the device size 81 | devSize = ioctl(fd, KVMFR_DMABUF_GETSIZE, 0); 82 | } 83 | else 84 | { 85 | struct stat st; 86 | if (stat(shmFile, &st) != 0) 87 | { 88 | perror("stat of shmFile failed"); 89 | exit(EXIT_FAILURE); 90 | } 91 | devSize = st.st_size; 92 | fd = open(shmFile, O_RDWR); 93 | } 94 | 95 | if (!fd) 96 | { 97 | perror("open failed"); 98 | exit(EXIT_FAILURE); 99 | } 100 | 101 | void * ram = mmap(0, devSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 102 | if (!ram) 103 | { 104 | perror("mmap failed"); 105 | goto out_close; 106 | } 107 | 108 | PLGMPClient client; 109 | LGMP_STATUS status; 110 | while((status = lgmpClientInit(ram, devSize, &client)) 111 | != LGMP_OK) 112 | { 113 | printf("lgmpClientInit %s\n", lgmpStatusString(status)); 114 | goto out_unmap; 115 | } 116 | 117 | uint32_t udataSize; 118 | uint8_t * udata; 119 | uint32_t clientID; 120 | while((status = lgmpClientSessionInit(client, &udataSize, &udata, &clientID)) 121 | != LGMP_OK) 122 | { 123 | usleep(100000); 124 | printf("lgmpClientSessionInit: %s\n", lgmpStatusString(status)); 125 | } 126 | 127 | printf("Session valid, clientID: %x\n", clientID); 128 | 129 | PLGMPClientQueue queue; 130 | while((status = lgmpClientSubscribe(client, 0, &queue)) != LGMP_OK) 131 | { 132 | if (status == LGMP_ERR_NO_SUCH_QUEUE) 133 | usleep(250000); 134 | else 135 | { 136 | printf("lgmpClientSubscrbe: %s\n", lgmpStatusString(status)); 137 | goto out_lgmpclient; 138 | } 139 | } 140 | 141 | uint32_t serial; 142 | bool dataDone = true; 143 | 144 | #if 0 145 | uint8_t data[32]; 146 | for(int i = 0; i < 20; ++i) 147 | { 148 | if ((status = lgmpClientSendData(queue, data, sizeof(data), &serial)) != LGMP_OK) 149 | { 150 | if (status == LGMP_ERR_QUEUE_FULL) 151 | { 152 | --i; 153 | continue; 154 | } 155 | 156 | printf("lgmpClientSendData: %s\n", lgmpStatusString(status)); 157 | goto out_lgmpclient; 158 | } 159 | } 160 | #endif 161 | 162 | uint32_t lastCount = 0; 163 | unsigned int msgCount = 0; 164 | while(lgmpClientSessionValid(client)) 165 | { 166 | LGMPMessage msg; 167 | if((status = lgmpClientProcess(queue, &msg)) != LGMP_OK) 168 | { 169 | if (status == LGMP_ERR_QUEUE_EMPTY) 170 | { 171 | usleep(1); 172 | continue; 173 | } 174 | else 175 | { 176 | printf("lgmpClientProcess: %s\n", lgmpStatusString(status)); 177 | goto out_unsub; 178 | } 179 | } 180 | 181 | printf("message %u\n", ++msgCount); 182 | 183 | if (delay) 184 | printf("Got %4u: %s\n", msg.udata, (char *)msg.mem); 185 | 186 | #if 0 187 | if (!lastCount) 188 | lastCount = msg.udata; 189 | else 190 | { 191 | if (lastCount != msg.udata - 1) 192 | { 193 | printf("MISSED MESSAGE\n"); 194 | goto out_unsub; 195 | } 196 | lastCount = msg.udata; 197 | } 198 | #endif 199 | 200 | if (delay) 201 | usleep(delay); 202 | 203 | status = lgmpClientMessageDone(queue); 204 | if (status != LGMP_OK) 205 | printf("lgmpClientMessageDone: %s\n", lgmpStatusString(status)); 206 | 207 | if (!dataDone) 208 | { 209 | uint32_t hostSerial; 210 | lgmpClientGetSerial(queue, &hostSerial); 211 | printf("serial %u - hostSerial %u\n", serial, hostSerial); 212 | if (hostSerial >= serial) 213 | { 214 | dataDone = true; 215 | printf("data done\n"); 216 | goto out_unsub; 217 | } 218 | } 219 | } 220 | 221 | printf("Shutdown\n"); 222 | 223 | out_unsub: 224 | if (lgmpClientSessionValid(client)) 225 | lgmpClientUnsubscribe(&queue); 226 | out_lgmpclient: 227 | lgmpClientFree(&client); 228 | out_unmap: 229 | munmap(ram, devSize); 230 | out_close: 231 | close(fd); 232 | out: 233 | return 0; 234 | } 235 | -------------------------------------------------------------------------------- /test-host/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(test-host) 3 | 4 | include_directories(include) 5 | set(SOURCES main.c) 6 | 7 | get_filename_component(PROJECT_TOP "${PROJECT_SOURCE_DIR}/.." ABSOLUTE) 8 | add_subdirectory("${PROJECT_TOP}/lgmp" "${CMAKE_BINARY_DIR}/lgmp") 9 | 10 | add_executable(test-host ${SOURCES}) 11 | target_link_libraries(test-host 12 | lgmp 13 | ) 14 | -------------------------------------------------------------------------------- /test-host/main.c: -------------------------------------------------------------------------------- 1 | /** 2 | * LGMP - Looking Glass Memory Protocol 3 | * Copyright © 2020-2025 Geoffrey McRae 4 | * https://github.com/gnif/LGMP 5 | * 6 | * This program is free software; you can redistribute it and/or modify it 7 | * under the terms of the GNU General Public License as published by the Free 8 | * Software Foundation; either version 2 of the License, or (at your option) 9 | * any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 | * more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along 17 | * with this program; if not, write to the Free Software Foundation, Inc., 59 18 | * Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 | */ 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | #include "lgmp/host.h" 30 | 31 | void * ram; 32 | #define SHARED_FILE "/dev/shm/lgmp-test" 33 | #define RAM_SIZE (10*1048576) 34 | 35 | int main(int argc, char * argv[]) 36 | { 37 | int fd = open(SHARED_FILE, O_RDWR | O_CREAT, (mode_t)0600); 38 | if (fd < 0) 39 | { 40 | perror("open failed"); 41 | return -1; 42 | } 43 | 44 | if (ftruncate(fd, RAM_SIZE) != 0) 45 | { 46 | perror("ftruncate failed"); 47 | goto out_close; 48 | } 49 | 50 | ram = mmap(0, RAM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 51 | if (!ram) 52 | { 53 | perror("mmap failed"); 54 | goto out_close; 55 | } 56 | 57 | PLGMPHost host; 58 | LGMP_STATUS status; 59 | 60 | uint8_t udata[32]; 61 | memset(udata, 0xaa, sizeof(udata)); 62 | 63 | if ((status = lgmpHostInit(ram, RAM_SIZE, &host, sizeof(udata), udata)) 64 | != LGMP_OK) 65 | { 66 | printf("lgmpHostInit failed: %s\n", lgmpStatusString(status)); 67 | goto out_unmap; 68 | } 69 | 70 | const struct LGMPQueueConfig conf = 71 | { 72 | .queueID = 0, 73 | .numMessages = 10, 74 | .subTimeout = 1000 75 | }; 76 | 77 | PLGMPHostQueue queue; 78 | if ((status = lgmpHostQueueNew(host, conf, &queue)) != LGMP_OK) 79 | { 80 | printf("lgmpHostQueueNew failed: %s\n", lgmpStatusString(status)); 81 | goto out_lgmphost; 82 | } 83 | 84 | PLGMPMemory mem[10] = { 0 }; 85 | for(int i = 0; i < 10; ++i) 86 | { 87 | if ((status = lgmpHostMemAlloc(host, 1024, &mem[i])) != LGMP_OK) 88 | { 89 | printf("lgmpHostAlloc failed: %s\n", lgmpStatusString(status)); 90 | goto out_lgmphost; 91 | } 92 | } 93 | 94 | 95 | sprintf(lgmpHostMemPtr(mem[0]), "This is a test from the host application"); 96 | sprintf(lgmpHostMemPtr(mem[1]), "With multiple buffers"); 97 | sprintf(lgmpHostMemPtr(mem[2]), "Containing text"); 98 | sprintf(lgmpHostMemPtr(mem[3]), "That might or might not be"); 99 | sprintf(lgmpHostMemPtr(mem[4]), "interesting."); 100 | sprintf(lgmpHostMemPtr(mem[5]), "This is buffer number 6"); 101 | sprintf(lgmpHostMemPtr(mem[6]), "Now number 7"); 102 | sprintf(lgmpHostMemPtr(mem[7]), "And now number 8"); 103 | sprintf(lgmpHostMemPtr(mem[8]), "Second last buffer"); 104 | sprintf(lgmpHostMemPtr(mem[9]), "It's over!"); 105 | 106 | uint32_t time = 0; 107 | uint32_t count = 0; 108 | int pendingAck = 0; 109 | 110 | while(true) 111 | { 112 | ++time; 113 | 114 | if((status = lgmpHostQueuePost(queue, count, mem[count % 10])) != LGMP_ERR_QUEUE_FULL) 115 | ++count; 116 | 117 | if (time % 100 == 0) 118 | { 119 | if (lgmpHostProcess(host) != LGMP_OK) 120 | { 121 | printf("lgmpHostQueuePost Failed: %s\n", lgmpStatusString(status)); 122 | break; 123 | } 124 | 125 | uint8_t buffer[LGMP_MSGS_SIZE]; 126 | size_t size; 127 | while((status = lgmpHostReadData(queue, buffer, &size)) == LGMP_OK) 128 | { 129 | printf("Read a client message of %d in size\n", size); 130 | ++pendingAck; 131 | } 132 | 133 | if (status != LGMP_ERR_QUEUE_EMPTY) 134 | { 135 | printf("lgmpHostReadData Failed: %s\n", lgmpStatusString(status)); 136 | break; 137 | } 138 | 139 | uint32_t newSubs; 140 | if ((newSubs = lgmpHostQueueNewSubs(queue)) > 0) 141 | printf("newSubs: %u\n", newSubs); 142 | } 143 | 144 | if(pendingAck) 145 | { 146 | --pendingAck; 147 | lgmpHostAckData(queue); 148 | } 149 | 150 | uint32_t clientIDs[32]; 151 | unsigned int count; 152 | lgmpHostGetClientIDs(queue, clientIDs, &count); 153 | for(int i = 0; i < count; ++i) 154 | printf("Client %x\n", clientIDs[i]); 155 | 156 | usleep(1000); 157 | } 158 | 159 | for(int i = 0; i < 10; ++i) 160 | lgmpHostMemFree(&mem[i]); 161 | 162 | out_lgmphost: 163 | lgmpHostFree(&host); 164 | out_unmap: 165 | munmap(ram, RAM_SIZE); 166 | out_close: 167 | close(fd); 168 | out: 169 | return 0; 170 | } 171 | --------------------------------------------------------------------------------