blob: 896ccef16697d41f6322197671bbdcefeaeab8f6 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
Daniel Micay4200e262015-11-03 05:14:08 -05002 * Copyright (C) 2015 The Android Open Source Project
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
Daniel Micay4200e262015-11-03 05:14:08 -050028
Daniel Micayc22a7de2015-11-07 10:40:26 -050029#include <errno.h>
Daniel Micay4200e262015-11-03 05:14:08 -050030#include <sys/mman.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080031#include <stdarg.h>
Daniel Micayc22a7de2015-11-07 10:40:26 -050032#include <stdint.h>
33#include <unistd.h>
34
35#include "private/bionic_macros.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080036
Daniel Micay4200e262015-11-03 05:14:08 -050037extern "C" void* ___mremap(void*, size_t, size_t, int, void*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080038
Daniel Micay4200e262015-11-03 05:14:08 -050039void* mremap(void* old_address, size_t old_size, size_t new_size, int flags, ...) {
Daniel Micayc22a7de2015-11-07 10:40:26 -050040 // prevent allocations large enough for `end - start` to overflow
Dan Alberta613d0d2017-10-05 16:39:33 -070041 size_t rounded = __BIONIC_ALIGN(new_size, PAGE_SIZE);
Daniel Micayc22a7de2015-11-07 10:40:26 -050042 if (rounded < new_size || rounded > PTRDIFF_MAX) {
43 errno = ENOMEM;
44 return MAP_FAILED;
45 }
46
Daniel Micay4200e262015-11-03 05:14:08 -050047 void* new_address = nullptr;
48 // The optional argument is only valid if the MREMAP_FIXED flag is set,
49 // so we assume it's not present otherwise.
50 if ((flags & MREMAP_FIXED) != 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080051 va_list ap;
Daniel Micay4200e262015-11-03 05:14:08 -050052 va_start(ap, flags);
53 new_address = va_arg(ap, void*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080054 va_end(ap);
Daniel Micay4200e262015-11-03 05:14:08 -050055 }
56 return ___mremap(old_address, old_size, new_size, flags, new_address);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080057}