1/* mremap.c -- version of mremap for gold.  */
2
3/* Copyright 2009 Free Software Foundation, Inc.
4   Written by Ian Lance Taylor <iant@google.com>.
5
6   This file is part of gold.
7
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 3 of the License, or
11   (at your option) any later version.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with this program; if not, write to the Free Software
20   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21   MA 02110-1301, USA.  */
22
23#include "config.h"
24#include "ansidecl.h"
25
26#include <string.h>
27#include <sys/types.h>
28#include <sys/mman.h>
29
30/* This file implements mremap for systems which don't have it.  The
31   gold code requires support for mmap.  However, there are systems
32   which have mmap but not mremap.  This is not a general replacement
33   for mremap; it only supports the features which are required for
34   gold.  In particular, we assume that the MREMAP_MAYMOVE flag is
35   set.  */
36
37/* Some BSD systems still use MAP_ANON instead of MAP_ANONYMOUS.  */
38
39#ifndef MAP_ANONYMOUS
40# define MAP_ANONYMOUS MAP_ANON
41#endif
42
43extern void *mremap (void *, size_t, size_t, int, ...);
44
45void *
46mremap (void *old_address, size_t old_size, size_t new_size,
47	int flags ATTRIBUTE_UNUSED, ...)
48{
49  void *ret;
50
51  ret = mmap (0, new_size, PROT_READ | PROT_WRITE,
52	      MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
53  if (ret == MAP_FAILED)
54    return ret;
55  memcpy (ret, old_address,
56	  old_size < new_size ? old_size : new_size);
57  (void) munmap (old_address, old_size);
58  return ret;
59}
60