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 | |
| 17 | #include <unistd.h> |
| 18 | |
| 19 | #include "jemalloc.h" |
| 20 | |
| 21 | void* je_pvalloc(size_t bytes) { |
| 22 | size_t pagesize = sysconf(_SC_PAGESIZE); |
| 23 | return je_memalign(pagesize, (bytes + pagesize - 1) & ~(pagesize - 1)); |
| 24 | } |
| 25 | |
| 26 | #ifdef je_memalign |
| 27 | #undef je_memalign |
| 28 | #endif |
| 29 | |
| 30 | // The man page for memalign says it fails if boundary is not a power of 2, |
| 31 | // but this is not true. Both glibc and dlmalloc round up to the next power |
| 32 | // of 2, so we'll do the same. |
| 33 | void* je_memalign_round_up_boundary(size_t boundary, size_t size) { |
| 34 | unsigned int power_of_2 = static_cast<unsigned int>(boundary); |
| 35 | if (power_of_2 != 0) { |
| 36 | power_of_2 = 1UL << (sizeof(unsigned int)*8 - 1 - __builtin_clz(power_of_2)); |
| 37 | if (power_of_2 != boundary) { |
| 38 | boundary = power_of_2 << 1; |
| 39 | } |
| 40 | } else { |
| 41 | boundary = 1; |
| 42 | } |
| 43 | return je_memalign(boundary, size); |
| 44 | } |