Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Christopher Ferris | 03eebcb | 2014-06-13 13:57:51 -0700 | [diff] [blame] | 17 | #include <sys/param.h> |
Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 18 | #include <unistd.h> |
| 19 | |
| 20 | #include "jemalloc.h" |
Christopher Ferris | 03eebcb | 2014-06-13 13:57:51 -0700 | [diff] [blame] | 21 | #include "private/bionic_macros.h" |
Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 22 | |
| 23 | void* je_pvalloc(size_t bytes) { |
Elliott Hughes | 91570ce | 2014-07-10 12:34:23 -0700 | [diff] [blame^] | 24 | size_t pagesize = getpagesize(); |
Christopher Ferris | 03eebcb | 2014-06-13 13:57:51 -0700 | [diff] [blame] | 25 | size_t size = BIONIC_ALIGN(bytes, pagesize); |
| 26 | if (size < bytes) { |
| 27 | return NULL; |
| 28 | } |
| 29 | return je_memalign(pagesize, size); |
Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | #ifdef je_memalign |
| 33 | #undef je_memalign |
| 34 | #endif |
| 35 | |
| 36 | // The man page for memalign says it fails if boundary is not a power of 2, |
| 37 | // but this is not true. Both glibc and dlmalloc round up to the next power |
| 38 | // of 2, so we'll do the same. |
| 39 | void* je_memalign_round_up_boundary(size_t boundary, size_t size) { |
Christopher Ferris | 03eebcb | 2014-06-13 13:57:51 -0700 | [diff] [blame] | 40 | if (boundary != 0) { |
| 41 | if (!powerof2(boundary)) { |
| 42 | boundary = BIONIC_ROUND_UP_POWER_OF_2(boundary); |
Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 43 | } |
| 44 | } else { |
| 45 | boundary = 1; |
| 46 | } |
| 47 | return je_memalign(boundary, size); |
| 48 | } |