15afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui/*
25afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui * Copyright (C) 2014 The Android Open Source Project
35afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui * All rights reserved.
45afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui *
55afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui * Redistribution and use in source and binary forms, with or without
65afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui * modification, are permitted provided that the following conditions
75afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui * are met:
85afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui *  * Redistributions of source code must retain the above copyright
95afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui *    notice, this list of conditions and the following disclaimer.
105afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui *  * Redistributions in binary form must reproduce the above copyright
115afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui *    notice, this list of conditions and the following disclaimer in
125afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui *    the documentation and/or other materials provided with the
135afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui *    distribution.
145afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui *
155afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
165afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
175afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
185afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
195afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
205afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
215afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
225afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
235afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
245afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
255afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
265afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui * SUCH DAMAGE.
275afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui */
285afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui
295afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui#include <errno.h>
305afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui#include <sys/mman.h>
315afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui
325afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui#include "private/ErrnoRestorer.h"
335afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui
345afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cuiint posix_madvise(void* addr, size_t len, int advice) {
355afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui  ErrnoRestorer errno_restorer;
365afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui
375afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui  // Don't call madvise() on POSIX_MADV_DONTNEED, it will make the space not available.
385afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui  if (advice == POSIX_MADV_DONTNEED) {
395afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui    return 0;
405afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui  }
415afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui  return (madvise(addr, len, advice) == 0 ? 0 : errno);
425afae64a1bac56638c6348f0c8f5e9d61b654029Yabin Cui}
43