OLD | NEW |
1 // Copyright (c) 2005, Google Inc. | 1 // Copyright (c) 2005, 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 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 | 107 |
108 // Structure for discovering alignment | 108 // Structure for discovering alignment |
109 union MemoryAligner { | 109 union MemoryAligner { |
110 void* p; | 110 void* p; |
111 double d; | 111 double d; |
112 size_t s; | 112 size_t s; |
113 } CACHELINE_ALIGNED; | 113 } CACHELINE_ALIGNED; |
114 | 114 |
115 static SpinLock spinlock(SpinLock::LINKER_INITIALIZED); | 115 static SpinLock spinlock(SpinLock::LINKER_INITIALIZED); |
116 | 116 |
117 #if defined(HAVE_MMAP) || defined(MADV_FREE) | 117 #ifdef HAVE_GETPAGESIZE |
118 // Page size is initialized on demand (only needed for mmap-based allocators) | |
119 static size_t pagesize = 0; | 118 static size_t pagesize = 0; |
120 #endif | 119 #endif |
121 | 120 |
122 // The current system allocator | 121 // The current system allocator |
123 SysAllocator* sys_alloc = NULL; | 122 SysAllocator* sys_alloc = NULL; |
124 | 123 |
125 // Configuration parameters. | 124 // Configuration parameters. |
126 DEFINE_int32(malloc_devmem_start, | 125 DEFINE_int32(malloc_devmem_start, |
127 EnvToInt("TCMALLOC_DEVMEM_START", 0), | 126 EnvToInt("TCMALLOC_DEVMEM_START", 0), |
128 "Physical memory starting location in MB for /dev/mem allocation." | 127 "Physical memory starting location in MB for /dev/mem allocation." |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 const char* names_[kMaxAllocators]; | 187 const char* names_[kMaxAllocators]; |
189 }; | 188 }; |
190 static char default_space[sizeof(DefaultSysAllocator)]; | 189 static char default_space[sizeof(DefaultSysAllocator)]; |
191 static const char sbrk_name[] = "SbrkSysAllocator"; | 190 static const char sbrk_name[] = "SbrkSysAllocator"; |
192 static const char mmap_name[] = "MmapSysAllocator"; | 191 static const char mmap_name[] = "MmapSysAllocator"; |
193 | 192 |
194 | 193 |
195 void* SbrkSysAllocator::Alloc(size_t size, size_t *actual_size, | 194 void* SbrkSysAllocator::Alloc(size_t size, size_t *actual_size, |
196 size_t alignment) { | 195 size_t alignment) { |
197 #ifndef HAVE_SBRK | 196 #ifndef HAVE_SBRK |
198 failed_ = true; | |
199 return NULL; | 197 return NULL; |
200 #else | 198 #else |
201 // Check if we should use sbrk allocation. | 199 // Check if we should use sbrk allocation. |
202 // FLAGS_malloc_skip_sbrk starts out as false (its uninitialized | 200 // FLAGS_malloc_skip_sbrk starts out as false (its uninitialized |
203 // state) and eventually gets initialized to the specified value. Note | 201 // state) and eventually gets initialized to the specified value. Note |
204 // that this code runs for a while before the flags are initialized. | 202 // that this code runs for a while before the flags are initialized. |
205 // That means that even if this flag is set to true, some (initial) | 203 // That means that even if this flag is set to true, some (initial) |
206 // memory will be allocated with sbrk before the flag takes effect. | 204 // memory will be allocated with sbrk before the flag takes effect. |
207 if (FLAGS_malloc_skip_sbrk) { | 205 if (FLAGS_malloc_skip_sbrk) { |
208 return NULL; | 206 return NULL; |
209 } | 207 } |
210 | 208 |
211 // sbrk will release memory if passed a negative number, so we do | 209 // sbrk will release memory if passed a negative number, so we do |
212 // a strict check here | 210 // a strict check here |
213 if (static_cast<ptrdiff_t>(size + alignment) < 0) return NULL; | 211 if (static_cast<std::ptrdiff_t>(size + alignment) < 0) return NULL; |
214 | 212 |
215 // This doesn't overflow because TCMalloc_SystemAlloc has already | 213 // This doesn't overflow because TCMalloc_SystemAlloc has already |
216 // tested for overflow at the alignment boundary. | 214 // tested for overflow at the alignment boundary. |
217 size = ((size + alignment - 1) / alignment) * alignment; | 215 size = ((size + alignment - 1) / alignment) * alignment; |
218 | 216 |
219 // "actual_size" indicates that the bytes from the returned pointer | 217 // "actual_size" indicates that the bytes from the returned pointer |
220 // p up to and including (p + actual_size - 1) have been allocated. | 218 // p up to and including (p + actual_size - 1) have been allocated. |
221 if (actual_size) { | 219 if (actual_size) { |
222 *actual_size = size; | 220 *actual_size = size; |
223 } | 221 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 if ((ptr & (alignment-1)) != 0) { | 258 if ((ptr & (alignment-1)) != 0) { |
261 ptr += alignment - (ptr & (alignment-1)); | 259 ptr += alignment - (ptr & (alignment-1)); |
262 } | 260 } |
263 return reinterpret_cast<void*>(ptr); | 261 return reinterpret_cast<void*>(ptr); |
264 #endif // HAVE_SBRK | 262 #endif // HAVE_SBRK |
265 } | 263 } |
266 | 264 |
267 void* MmapSysAllocator::Alloc(size_t size, size_t *actual_size, | 265 void* MmapSysAllocator::Alloc(size_t size, size_t *actual_size, |
268 size_t alignment) { | 266 size_t alignment) { |
269 #ifndef HAVE_MMAP | 267 #ifndef HAVE_MMAP |
270 failed_ = true; | |
271 return NULL; | 268 return NULL; |
272 #else | 269 #else |
273 // Check if we should use mmap allocation. | 270 // Check if we should use mmap allocation. |
274 // FLAGS_malloc_skip_mmap starts out as false (its uninitialized | 271 // FLAGS_malloc_skip_mmap starts out as false (its uninitialized |
275 // state) and eventually gets initialized to the specified value. Note | 272 // state) and eventually gets initialized to the specified value. Note |
276 // that this code runs for a while before the flags are initialized. | 273 // that this code runs for a while before the flags are initialized. |
277 // Chances are we never get here before the flags are initialized since | 274 // Chances are we never get here before the flags are initialized since |
278 // sbrk is used until the heap is exhausted (before mmap is used). | 275 // sbrk is used until the heap is exhausted (before mmap is used). |
279 if (FLAGS_malloc_skip_mmap) { | 276 if (FLAGS_malloc_skip_mmap) { |
280 return NULL; | 277 return NULL; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 } | 326 } |
330 | 327 |
331 ptr += adjust; | 328 ptr += adjust; |
332 return reinterpret_cast<void*>(ptr); | 329 return reinterpret_cast<void*>(ptr); |
333 #endif // HAVE_MMAP | 330 #endif // HAVE_MMAP |
334 } | 331 } |
335 | 332 |
336 void* DevMemSysAllocator::Alloc(size_t size, size_t *actual_size, | 333 void* DevMemSysAllocator::Alloc(size_t size, size_t *actual_size, |
337 size_t alignment) { | 334 size_t alignment) { |
338 #ifndef HAVE_MMAP | 335 #ifndef HAVE_MMAP |
339 failed_ = true; | |
340 return NULL; | 336 return NULL; |
341 #else | 337 #else |
342 static bool initialized = false; | 338 static bool initialized = false; |
343 static off_t physmem_base; // next physical memory address to allocate | 339 static off_t physmem_base; // next physical memory address to allocate |
344 static off_t physmem_limit; // maximum physical address allowed | 340 static off_t physmem_limit; // maximum physical address allowed |
345 static int physmem_fd; // file descriptor for /dev/mem | 341 static int physmem_fd; // file descriptor for /dev/mem |
346 | 342 |
347 // Check if we should use /dev/mem allocation. Note that it may take | 343 // Check if we should use /dev/mem allocation. Note that it may take |
348 // a while to get this flag initialized, so meanwhile we fall back to | 344 // a while to get this flag initialized, so meanwhile we fall back to |
349 // the next allocator. (It looks like 7MB gets allocated before | 345 // the next allocator. (It looks like 7MB gets allocated before |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
486 CheckAddressBits<kAddressBits>( | 482 CheckAddressBits<kAddressBits>( |
487 reinterpret_cast<uintptr_t>(result) + *actual_size - 1); | 483 reinterpret_cast<uintptr_t>(result) + *actual_size - 1); |
488 } else { | 484 } else { |
489 CheckAddressBits<kAddressBits>( | 485 CheckAddressBits<kAddressBits>( |
490 reinterpret_cast<uintptr_t>(result) + size - 1); | 486 reinterpret_cast<uintptr_t>(result) + size - 1); |
491 } | 487 } |
492 } | 488 } |
493 return result; | 489 return result; |
494 } | 490 } |
495 | 491 |
| 492 size_t TCMalloc_SystemAddGuard(void* start, size_t size) { |
| 493 #ifdef HAVE_GETPAGESIZE |
| 494 if (pagesize == 0) |
| 495 pagesize = getpagesize(); |
| 496 |
| 497 if (size < pagesize || (reinterpret_cast<size_t>(start) % pagesize) != 0) |
| 498 return 0; |
| 499 |
| 500 if (!mprotect(start, pagesize, PROT_NONE)) |
| 501 return pagesize; |
| 502 #endif |
| 503 |
| 504 return 0; |
| 505 } |
| 506 |
496 void TCMalloc_SystemRelease(void* start, size_t length) { | 507 void TCMalloc_SystemRelease(void* start, size_t length) { |
497 #ifdef MADV_FREE | 508 #ifdef MADV_FREE |
498 if (FLAGS_malloc_devmem_start) { | 509 if (FLAGS_malloc_devmem_start) { |
499 // It's not safe to use MADV_FREE/MADV_DONTNEED if we've been | 510 // It's not safe to use MADV_FREE/MADV_DONTNEED if we've been |
500 // mapping /dev/mem for heap memory. | 511 // mapping /dev/mem for heap memory. |
501 return; | 512 return; |
502 } | 513 } |
503 if (pagesize == 0) pagesize = getpagesize(); | 514 if (pagesize == 0) pagesize = getpagesize(); |
504 const size_t pagemask = pagesize - 1; | 515 const size_t pagemask = pagesize - 1; |
505 | 516 |
(...skipping 15 matching lines...) Expand all Loading... |
521 // Note -- ignoring most return codes, because if this fails it | 532 // Note -- ignoring most return codes, because if this fails it |
522 // doesn't matter... | 533 // doesn't matter... |
523 while (madvise(reinterpret_cast<char*>(new_start), new_end - new_start, | 534 while (madvise(reinterpret_cast<char*>(new_start), new_end - new_start, |
524 MADV_FREE) == -1 && | 535 MADV_FREE) == -1 && |
525 errno == EAGAIN) { | 536 errno == EAGAIN) { |
526 // NOP | 537 // NOP |
527 } | 538 } |
528 } | 539 } |
529 #endif | 540 #endif |
530 } | 541 } |
| 542 |
| 543 void TCMalloc_SystemCommit(void* start, size_t length) { |
| 544 // Nothing to do here. TCMalloc_SystemRelease does not alter pages |
| 545 // such that they need to be re-committed before they can be used by the |
| 546 // application. |
| 547 } |
OLD | NEW |