Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(20)

Side by Side Diff: base/memory/aligned_memory.cc

Issue 1039823002: Add base::LazyInstance (Closed) Base URL: https://chromium.googlesource.com/chromium/mini_chromium@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/memory/aligned_memory.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/memory/aligned_memory.h"
6
7 #include "base/logging.h"
8
9 #if defined(OS_ANDROID)
10 #include <malloc.h>
11 #endif
12
13 namespace base {
14
15 void* AlignedAlloc(size_t size, size_t alignment) {
16 DCHECK_GT(size, 0U);
17 DCHECK_EQ(alignment & (alignment - 1), 0U);
18 DCHECK_EQ(alignment % sizeof(void*), 0U);
19 void* ptr = NULL;
20 #if defined(COMPILER_MSVC)
21 ptr = _aligned_malloc(size, alignment);
22 // Android technically supports posix_memalign(), but does not expose it in
23 // the current version of the library headers used by Chrome. Luckily,
24 // memalign() on Android returns pointers which can safely be used with
25 // free(), so we can use it instead. Issue filed to document this:
26 // http://code.google.com/p/android/issues/detail?id=35391
27 #elif defined(OS_ANDROID)
28 ptr = memalign(alignment, size);
29 #else
30 if (posix_memalign(&ptr, alignment, size))
31 ptr = NULL;
32 #endif
33 // Since aligned allocations may fail for non-memory related reasons, force a
34 // crash if we encounter a failed allocation; maintaining consistent behavior
35 // with a normal allocation failure in Chrome.
36 if (!ptr) {
37 CHECK(false);
38 }
39 // Sanity check alignment just to be safe.
40 DCHECK_EQ(reinterpret_cast<uintptr_t>(ptr) & (alignment - 1), 0U);
41 return ptr;
42 }
43
44 } // namespace base
OLDNEW
« no previous file with comments | « base/memory/aligned_memory.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698