| 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 | |
| 109 // We don't respond to allocation requests smaller than big_page_size_ unless | 104 // We don't respond to allocation requests smaller than big_page_size_ unless |
| 110 // the caller is willing to take more than they asked for. | 105 // the caller is willing to take more than they asked for. |
| 111 if (actual_size == NULL && size < big_page_size_) { | 106 if (actual_size == NULL && size < big_page_size_) { |
| 112 return NULL; | 107 return NULL; |
| 113 } | 108 } |
| 114 | 109 |
| 115 // Enforce huge page alignment. Be careful to deal with overflow. | 110 // Enforce huge page alignment. Be careful to deal with overflow. |
| 116 if (alignment < big_page_size_) alignment = big_page_size_; | 111 if (alignment < big_page_size_) alignment = big_page_size_; |
| 117 size_t aligned_size = ((size + alignment - 1) / alignment) * alignment; | 112 size_t aligned_size = ((size + alignment - 1) / alignment) * alignment; |
| 118 if (aligned_size < size) { | 113 if (aligned_size < size) { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 } | 154 } |
| 160 | 155 |
| 161 // Note: size + extra does not overflow since: | 156 // Note: size + extra does not overflow since: |
| 162 // size + alignment < (1<<NBITS). | 157 // size + alignment < (1<<NBITS). |
| 163 // and extra <= alignment | 158 // and extra <= alignment |
| 164 // therefore size + extra < (1<<NBITS) | 159 // therefore size + extra < (1<<NBITS) |
| 165 void *result = mmap(0, size + extra, PROT_WRITE|PROT_READ, | 160 void *result = mmap(0, size + extra, PROT_WRITE|PROT_READ, |
| 166 MAP_SHARED, hugetlb_fd_, hugetlb_base_); | 161 MAP_SHARED, hugetlb_fd_, hugetlb_base_); |
| 167 if (result == reinterpret_cast<void*>(MAP_FAILED)) { | 162 if (result == reinterpret_cast<void*>(MAP_FAILED)) { |
| 168 if (!FLAGS_memfs_malloc_ignore_mmap_fail) { | 163 if (!FLAGS_memfs_malloc_ignore_mmap_fail) { |
| 169 TCMalloc_MESSAGE(__FILE__, __LINE__, "mmap of size %"PRIuS" failed: %s\n", | 164 TCMalloc_MESSAGE(__FILE__, __LINE__, "mmap failed: %s\n", |
| 170 size + extra, strerror(errno)); | 165 strerror(errno)); |
| 171 failed_ = true; | 166 failed_ = true; |
| 172 if (FLAGS_memfs_malloc_abort_on_fail) { | 167 if (FLAGS_memfs_malloc_abort_on_fail) { |
| 173 CRASH("memfs_malloc_abort_on_fail is set\n"); | 168 CRASH("memfs_malloc_abort_on_fail is set\n"); |
| 174 } | 169 } |
| 175 } | 170 } |
| 176 return NULL; | 171 return NULL; |
| 177 } | 172 } |
| 178 uintptr_t ptr = reinterpret_cast<uintptr_t>(result); | 173 uintptr_t ptr = reinterpret_cast<uintptr_t>(result); |
| 179 | 174 |
| 180 // Adjust the return memory so it is aligned | 175 // Adjust the return memory so it is aligned |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 | 220 |
| 226 SysAllocator *alloc = new HugetlbSysAllocator(hugetlb_fd, page_size); | 221 SysAllocator *alloc = new HugetlbSysAllocator(hugetlb_fd, page_size); |
| 227 // Register ourselves with tcmalloc | 222 // Register ourselves with tcmalloc |
| 228 RegisterSystemAllocator(alloc, 0); | 223 RegisterSystemAllocator(alloc, 0); |
| 229 } | 224 } |
| 230 } | 225 } |
| 231 | 226 |
| 232 REGISTER_MODULE_INITIALIZER(memfs_malloc, { InitSystemAllocator(); }); | 227 REGISTER_MODULE_INITIALIZER(memfs_malloc, { InitSystemAllocator(); }); |
| 233 | 228 |
| 234 #endif /* ifdef __linux */ | 229 #endif /* ifdef __linux */ |
| OLD | NEW |