| OLD | NEW |
| 1 // Copyright (c) 2007, Google Inc. | 1 // Copyright (c) 2007, Google Inc. |
| 2 // All rights reserved. | 2 // All rights reserved. |
| 3 // | 3 // |
| 4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
| 6 // met: | 6 // met: |
| 7 // | 7 // |
| 8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
| 9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
| 10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 void HugetlbSysAllocator::DumpStats(TCMalloc_Printer* printer) { | 94 void HugetlbSysAllocator::DumpStats(TCMalloc_Printer* printer) { |
| 95 printer->printf("HugetlbSysAllocator: failed_=%d allocated=%"PRId64"\n", | 95 printer->printf("HugetlbSysAllocator: failed_=%d allocated=%"PRId64"\n", |
| 96 failed_, static_cast<int64_t>(hugetlb_base_)); | 96 failed_, static_cast<int64_t>(hugetlb_base_)); |
| 97 } | 97 } |
| 98 | 98 |
| 99 // No locking needed here since we assume that tcmalloc calls | 99 // No locking needed here since we assume that tcmalloc calls |
| 100 // us with an internal lock held (see tcmalloc/system-alloc.cc). | 100 // us with an internal lock held (see tcmalloc/system-alloc.cc). |
| 101 void* HugetlbSysAllocator::Alloc(size_t size, size_t *actual_size, | 101 void* HugetlbSysAllocator::Alloc(size_t size, size_t *actual_size, |
| 102 size_t alignment) { | 102 size_t alignment) { |
| 103 | 103 |
| 104 // don't go any further if we haven't opened the backing file |
| 105 if (hugetlb_fd_ == -1) { |
| 106 return NULL; |
| 107 } |
| 108 |
| 104 // We don't respond to allocation requests smaller than big_page_size_ unless | 109 // We don't respond to allocation requests smaller than big_page_size_ unless |
| 105 // the caller is willing to take more than they asked for. | 110 // the caller is willing to take more than they asked for. |
| 106 if (actual_size == NULL && size < big_page_size_) { | 111 if (actual_size == NULL && size < big_page_size_) { |
| 107 return NULL; | 112 return NULL; |
| 108 } | 113 } |
| 109 | 114 |
| 110 // Enforce huge page alignment. Be careful to deal with overflow. | 115 // Enforce huge page alignment. Be careful to deal with overflow. |
| 111 if (alignment < big_page_size_) alignment = big_page_size_; | 116 if (alignment < big_page_size_) alignment = big_page_size_; |
| 112 size_t aligned_size = ((size + alignment - 1) / alignment) * alignment; | 117 size_t aligned_size = ((size + alignment - 1) / alignment) * alignment; |
| 113 if (aligned_size < size) { | 118 if (aligned_size < size) { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 } | 159 } |
| 155 | 160 |
| 156 // Note: size + extra does not overflow since: | 161 // Note: size + extra does not overflow since: |
| 157 // size + alignment < (1<<NBITS). | 162 // size + alignment < (1<<NBITS). |
| 158 // and extra <= alignment | 163 // and extra <= alignment |
| 159 // therefore size + extra < (1<<NBITS) | 164 // therefore size + extra < (1<<NBITS) |
| 160 void *result = mmap(0, size + extra, PROT_WRITE|PROT_READ, | 165 void *result = mmap(0, size + extra, PROT_WRITE|PROT_READ, |
| 161 MAP_SHARED, hugetlb_fd_, hugetlb_base_); | 166 MAP_SHARED, hugetlb_fd_, hugetlb_base_); |
| 162 if (result == reinterpret_cast<void*>(MAP_FAILED)) { | 167 if (result == reinterpret_cast<void*>(MAP_FAILED)) { |
| 163 if (!FLAGS_memfs_malloc_ignore_mmap_fail) { | 168 if (!FLAGS_memfs_malloc_ignore_mmap_fail) { |
| 164 TCMalloc_MESSAGE(__FILE__, __LINE__, "mmap failed: %s\n", | 169 TCMalloc_MESSAGE(__FILE__, __LINE__, "mmap of size %"PRIuS" failed: %s\n", |
| 165 strerror(errno)); | 170 size + extra, strerror(errno)); |
| 166 failed_ = true; | 171 failed_ = true; |
| 167 if (FLAGS_memfs_malloc_abort_on_fail) { | 172 if (FLAGS_memfs_malloc_abort_on_fail) { |
| 168 CRASH("memfs_malloc_abort_on_fail is set\n"); | 173 CRASH("memfs_malloc_abort_on_fail is set\n"); |
| 169 } | 174 } |
| 170 } | 175 } |
| 171 return NULL; | 176 return NULL; |
| 172 } | 177 } |
| 173 uintptr_t ptr = reinterpret_cast<uintptr_t>(result); | 178 uintptr_t ptr = reinterpret_cast<uintptr_t>(result); |
| 174 | 179 |
| 175 // Adjust the return memory so it is aligned | 180 // Adjust the return memory so it is aligned |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 | 225 |
| 221 SysAllocator *alloc = new HugetlbSysAllocator(hugetlb_fd, page_size); | 226 SysAllocator *alloc = new HugetlbSysAllocator(hugetlb_fd, page_size); |
| 222 // Register ourselves with tcmalloc | 227 // Register ourselves with tcmalloc |
| 223 RegisterSystemAllocator(alloc, 0); | 228 RegisterSystemAllocator(alloc, 0); |
| 224 } | 229 } |
| 225 } | 230 } |
| 226 | 231 |
| 227 REGISTER_MODULE_INITIALIZER(memfs_malloc, { InitSystemAllocator(); }); | 232 REGISTER_MODULE_INITIALIZER(memfs_malloc, { InitSystemAllocator(); }); |
| 228 | 233 |
| 229 #endif /* ifdef __linux */ | 234 #endif /* ifdef __linux */ |
| OLD | NEW |