(root)/
m4-1.4.19/
tests/
mmap-anon-util.h
       1  /* Some auxiliary stuff for using mmap & friends.
       2     Copyright (C) 2002-2021  Bruno Haible <bruno@clisp.org>
       3  
       4     This program is free software: you can redistribute it and/or modify
       5     it under the terms of the GNU General Public License as published by
       6     the Free Software Foundation; either version 2 of the License, or
       7     (at your option) any later version.
       8  
       9     This program is distributed in the hope that it will be useful,
      10     but WITHOUT ANY WARRANTY; without even the implied warranty of
      11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12     GNU General Public License for more details.
      13  
      14     You should have received a copy of the GNU General Public License
      15     along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
      16  
      17  #if defined _WIN32 && !defined __CYGWIN__
      18  
      19  /* ------------------------ Windows ------------------------ */
      20  
      21  # define WIN32_LEAN_AND_MEAN /* avoid including junk */
      22  # include <windows.h>
      23  # include <winerror.h>
      24  # define PROT_NONE PAGE_NOACCESS
      25  # define PROT_READ PAGE_READONLY
      26  # define PROT_READ_WRITE PAGE_READWRITE
      27  
      28  static void *
      29  mmap_zeromap (void *map_addr_hint, size_t map_len)
      30  {
      31    if (VirtualAlloc ((void *)((uintptr_t) map_addr_hint & -0x10000),
      32                      (((uintptr_t) map_addr_hint + map_len - 1) | 0xffff) + 1
      33                      - ((uintptr_t) map_addr_hint & -0x10000),
      34                      MEM_RESERVE, PAGE_NOACCESS)
      35        && VirtualAlloc (map_addr_hint, map_len, MEM_COMMIT, PAGE_READWRITE))
      36      return map_addr_hint;
      37    else
      38      return (void *)(-1);
      39  }
      40  
      41  int
      42  munmap (void *addr, size_t len)
      43  {
      44    if (VirtualFree (addr, len, MEM_DECOMMIT))
      45      return 0;
      46    else
      47      return -1;
      48  }
      49  
      50  int
      51  mprotect (void *addr, size_t len, int prot)
      52  {
      53    DWORD oldprot;
      54  
      55    if (VirtualProtect (addr, len, prot, &oldprot))
      56      return 0;
      57    else
      58      return -1;
      59  }
      60  
      61  #else
      62  
      63  /* ------------------------ Unix ------------------------ */
      64  
      65  # include <sys/types.h>
      66  # include <sys/mman.h>
      67  # include <fcntl.h>
      68  
      69  # ifndef PROT_NONE
      70  #  define PROT_NONE 0
      71  # endif
      72  # define PROT_READ_WRITE  (PROT_READ|PROT_WRITE)
      73  
      74  # if HAVE_MAP_ANONYMOUS
      75  #  define zero_fd -1
      76  #  define map_flags MAP_ANONYMOUS | MAP_PRIVATE
      77  # else
      78  #  ifndef MAP_FILE
      79  #   define MAP_FILE 0
      80  #  endif
      81  static int zero_fd;
      82  #  define map_flags MAP_FILE | MAP_PRIVATE
      83  # endif
      84  
      85  static void *
      86  mmap_zeromap (void *map_addr_hint, size_t map_len)
      87  {
      88  # ifdef __hpux
      89    /* HP-UX 10 mmap() often fails when given a hint.  So give the OS complete
      90       freedom about the address range.  */
      91    return (void *) mmap ((void *) 0,    map_len, PROT_READ_WRITE, map_flags, zero_fd, 0);
      92  # else
      93    return (void *) mmap (map_addr_hint, map_len, PROT_READ_WRITE, map_flags, zero_fd, 0);
      94  # endif
      95  }
      96  
      97  #endif