LCOV - code coverage report
Current view: top level - source/lib/sysdep/os/unix - uvm.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 17 51 33.3 %
Date: 2023-01-19 00:18:29 Functions: 5 12 41.7 %

          Line data    Source code
       1             : /* Copyright (C) 2011 Wildfire Games.
       2             :  *
       3             :  * Permission is hereby granted, free of charge, to any person obtaining
       4             :  * a copy of this software and associated documentation files (the
       5             :  * "Software"), to deal in the Software without restriction, including
       6             :  * without limitation the rights to use, copy, modify, merge, publish,
       7             :  * distribute, sublicense, and/or sell copies of the Software, and to
       8             :  * permit persons to whom the Software is furnished to do so, subject to
       9             :  * the following conditions:
      10             :  *
      11             :  * The above copyright notice and this permission notice shall be included
      12             :  * in all copies or substantial portions of the Software.
      13             :  *
      14             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
      15             :  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
      16             :  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
      17             :  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
      18             :  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
      19             :  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      20             :  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
      21             :  */
      22             : 
      23             : #include "precompiled.h"
      24             : #include "lib/sysdep/vm.h"
      25             : 
      26             : #include "lib/alignment.h"
      27             : 
      28             : // "anonymous" effectively means mapping /dev/zero, but is more efficient.
      29             : // MAP_ANONYMOUS is not in SUSv3, but is a very common extension.
      30             : // unfortunately, MacOS X only defines MAP_ANON, which Solaris says is
      31             : // deprecated. workaround there: define MAP_ANONYMOUS in terms of MAP_ANON.
      32             : #ifndef MAP_ANONYMOUS
      33             : # define MAP_ANONYMOUS MAP_ANON
      34             : #endif
      35             : 
      36             : static const int mmap_flags = MAP_PRIVATE|MAP_ANONYMOUS;
      37             : 
      38             : namespace vm {
      39             : 
      40           1 : void* ReserveAddressSpace(size_t size, size_t UNUSED(commitSize), PageType UNUSED(pageType), int UNUSED(prot))
      41             : {
      42           1 :     errno = 0;
      43           1 :     void* p = mmap(0, size, PROT_NONE, mmap_flags|MAP_NORESERVE, -1, 0);
      44           1 :     if(p == MAP_FAILED)
      45           0 :         return 0;
      46           1 :     return p;
      47             : }
      48             : 
      49           1 : void ReleaseAddressSpace(void* p, size_t size)
      50             : {
      51           1 :     ENSURE(size != 0);
      52             : 
      53           1 :     errno = 0;
      54           1 :     if(munmap(p, size) != 0)
      55           0 :         DEBUG_WARN_ERR(StatusFromErrno());
      56           1 : }
      57             : 
      58             : 
      59           1 : bool Commit(uintptr_t address, size_t size, PageType UNUSED(pageType), int prot)
      60             : {
      61           1 :     if(prot == PROT_NONE)   // would be understood as a request to decommit
      62             :     {
      63           0 :         DEBUG_WARN_ERR(ERR::INVALID_PARAM);
      64           0 :         return false;
      65             :     }
      66             : 
      67           1 :     errno = 0;
      68           1 :     if(mmap((void*)address, size, prot, mmap_flags|MAP_FIXED, -1, 0) == MAP_FAILED)
      69           0 :         return false;
      70             : 
      71           1 :     if(prot != (PROT_READ|PROT_WRITE))
      72           0 :         (void)Protect(address, size, prot);
      73             : 
      74           1 :     return true;
      75             : }
      76             : 
      77           0 : bool Decommit(uintptr_t address, size_t size)
      78             : {
      79           0 :     errno = 0;
      80           0 :     if(mmap((void*)address, size, PROT_NONE, mmap_flags|MAP_NORESERVE|MAP_FIXED, -1, 0) == MAP_FAILED)
      81           0 :         return false;
      82           0 :     return true;
      83             : }
      84             : 
      85             : 
      86           0 : bool Protect(uintptr_t address, size_t size, int prot)
      87             : {
      88           0 :     errno = 0;
      89           0 :     if(mprotect((void*)address, size, prot) != 0)
      90             :     {
      91           0 :         DEBUG_WARN_ERR(ERR::FAIL);
      92           0 :         return false;
      93             :     }
      94           0 :     return true;
      95             : }
      96             : 
      97             : 
      98           0 : void* Allocate(size_t size, PageType pageType, int prot)
      99             : {
     100           0 :     void* p = ReserveAddressSpace(size);
     101           0 :     if(!p)
     102           0 :         return 0;
     103             : 
     104           0 :     if(!Commit(uintptr_t(p), size, pageType, prot))
     105             :     {
     106           0 :         ReleaseAddressSpace(p, size);
     107           0 :         return 0;
     108             :     }
     109             : 
     110           0 :     return p;
     111             : }
     112             : 
     113           0 : void Free(void* p, size_t size)
     114             : {
     115             :     // (only the Windows implementation distinguishes between Free and ReleaseAddressSpace)
     116           0 :     vm::ReleaseAddressSpace(p, size);
     117           0 : }
     118             : 
     119             : 
     120           0 : void BeginOnDemandCommits()
     121             : {
     122             :     // not yet implemented, but possible with a signal handler
     123           0 : }
     124             : 
     125           0 : void EndOnDemandCommits()
     126             : {
     127             :     // not yet implemented, but possible with a signal handler
     128           0 : }
     129             : 
     130             : 
     131           0 : void DumpStatistics()
     132             : {
     133             :     // we haven't collected any statistics
     134           0 : }
     135             : 
     136           3 : }   // namespace vm

Generated by: LCOV version 1.13