Chromium Code Reviews

Side by Side Diff: webrtc/system_wrappers/source/aligned_malloc.cc

Issue 2685783014: Replace NULL with nullptr in all C++ files. (Closed)
Patch Set: Fixing android. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 23 matching lines...)
34 // Alignment must be an integer power of two. 34 // Alignment must be an integer power of two.
35 bool ValidAlignment(size_t alignment) { 35 bool ValidAlignment(size_t alignment) {
36 if (!alignment) { 36 if (!alignment) {
37 return false; 37 return false;
38 } 38 }
39 return (alignment & (alignment - 1)) == 0; 39 return (alignment & (alignment - 1)) == 0;
40 } 40 }
41 41
42 void* GetRightAlign(const void* pointer, size_t alignment) { 42 void* GetRightAlign(const void* pointer, size_t alignment) {
43 if (!pointer) { 43 if (!pointer) {
44 return NULL; 44 return nullptr;
45 } 45 }
46 if (!ValidAlignment(alignment)) { 46 if (!ValidAlignment(alignment)) {
47 return NULL; 47 return nullptr;
48 } 48 }
49 uintptr_t start_pos = reinterpret_cast<uintptr_t>(pointer); 49 uintptr_t start_pos = reinterpret_cast<uintptr_t>(pointer);
50 return reinterpret_cast<void*>(GetRightAlign(start_pos, alignment)); 50 return reinterpret_cast<void*>(GetRightAlign(start_pos, alignment));
51 } 51 }
52 52
53 void* AlignedMalloc(size_t size, size_t alignment) { 53 void* AlignedMalloc(size_t size, size_t alignment) {
54 if (size == 0) { 54 if (size == 0) {
55 return NULL; 55 return nullptr;
56 } 56 }
57 if (!ValidAlignment(alignment)) { 57 if (!ValidAlignment(alignment)) {
58 return NULL; 58 return nullptr;
59 } 59 }
60 60
61 // The memory is aligned towards the lowest address that so only 61 // The memory is aligned towards the lowest address that so only
62 // alignment - 1 bytes needs to be allocated. 62 // alignment - 1 bytes needs to be allocated.
63 // A pointer to the start of the memory must be stored so that it can be 63 // A pointer to the start of the memory must be stored so that it can be
64 // retreived for deletion, ergo the sizeof(uintptr_t). 64 // retreived for deletion, ergo the sizeof(uintptr_t).
65 void* memory_pointer = malloc(size + sizeof(uintptr_t) + alignment - 1); 65 void* memory_pointer = malloc(size + sizeof(uintptr_t) + alignment - 1);
66 if (memory_pointer == NULL) { 66 if (memory_pointer == nullptr) {
67 return NULL; 67 return nullptr;
68 } 68 }
69 69
70 // Aligning after the sizeof(uintptr_t) bytes will leave room for the header 70 // Aligning after the sizeof(uintptr_t) bytes will leave room for the header
71 // in the same memory block. 71 // in the same memory block.
72 uintptr_t align_start_pos = reinterpret_cast<uintptr_t>(memory_pointer); 72 uintptr_t align_start_pos = reinterpret_cast<uintptr_t>(memory_pointer);
73 align_start_pos += sizeof(uintptr_t); 73 align_start_pos += sizeof(uintptr_t);
74 uintptr_t aligned_pos = GetRightAlign(align_start_pos, alignment); 74 uintptr_t aligned_pos = GetRightAlign(align_start_pos, alignment);
75 void* aligned_pointer = reinterpret_cast<void*>(aligned_pos); 75 void* aligned_pointer = reinterpret_cast<void*>(aligned_pos);
76 76
77 // Store the address to the beginning of the memory just before the aligned 77 // Store the address to the beginning of the memory just before the aligned
78 // memory. 78 // memory.
79 uintptr_t header_pos = aligned_pos - sizeof(uintptr_t); 79 uintptr_t header_pos = aligned_pos - sizeof(uintptr_t);
80 void* header_pointer = reinterpret_cast<void*>(header_pos); 80 void* header_pointer = reinterpret_cast<void*>(header_pos);
81 uintptr_t memory_start = reinterpret_cast<uintptr_t>(memory_pointer); 81 uintptr_t memory_start = reinterpret_cast<uintptr_t>(memory_pointer);
82 memcpy(header_pointer, &memory_start, sizeof(uintptr_t)); 82 memcpy(header_pointer, &memory_start, sizeof(uintptr_t));
83 83
84 return aligned_pointer; 84 return aligned_pointer;
85 } 85 }
86 86
87 void AlignedFree(void* mem_block) { 87 void AlignedFree(void* mem_block) {
88 if (mem_block == NULL) { 88 if (mem_block == nullptr) {
89 return; 89 return;
90 } 90 }
91 uintptr_t aligned_pos = reinterpret_cast<uintptr_t>(mem_block); 91 uintptr_t aligned_pos = reinterpret_cast<uintptr_t>(mem_block);
92 uintptr_t header_pos = aligned_pos - sizeof(uintptr_t); 92 uintptr_t header_pos = aligned_pos - sizeof(uintptr_t);
93 93
94 // Read out the address of the AlignedMemory struct from the header. 94 // Read out the address of the AlignedMemory struct from the header.
95 uintptr_t memory_start_pos = *reinterpret_cast<uintptr_t*>(header_pos); 95 uintptr_t memory_start_pos = *reinterpret_cast<uintptr_t*>(header_pos);
96 void* memory_start = reinterpret_cast<void*>(memory_start_pos); 96 void* memory_start = reinterpret_cast<void*>(memory_start_pos);
97 free(memory_start); 97 free(memory_start);
98 } 98 }
99 99
100 } // namespace webrtc 100 } // namespace webrtc
OLDNEW

Powered by Google App Engine