niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "system_wrappers/include/aligned_malloc.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | #include <memory.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 14 | #include <stdlib.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 15 | |
kwiberg | 77eab70 | 2016-09-28 17:42:01 -0700 | [diff] [blame] | 16 | #ifdef _WIN32 |
henrike@webrtc.org | 28625c1 | 2012-10-02 15:38:35 +0000 | [diff] [blame] | 17 | #include <windows.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 18 | #else |
henrike@webrtc.org | 28625c1 | 2012-10-02 15:38:35 +0000 | [diff] [blame] | 19 | #include <stdint.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | #endif |
| 21 | |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 22 | #include "typedefs.h" // NOLINT(build/include) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | |
henrike@webrtc.org | 28625c1 | 2012-10-02 15:38:35 +0000 | [diff] [blame] | 24 | // Reference on memory alignment: |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 25 | // http://stackoverflow.com/questions/227897/solve-the-memory-alignment-in-c-interview-question-that-stumped-me |
henrike@webrtc.org | 28625c1 | 2012-10-02 15:38:35 +0000 | [diff] [blame] | 26 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 27 | |
henrike@webrtc.org | 46d4073 | 2012-10-03 16:50:37 +0000 | [diff] [blame] | 28 | uintptr_t GetRightAlign(uintptr_t start_pos, size_t alignment) { |
henrike@webrtc.org | 28625c1 | 2012-10-02 15:38:35 +0000 | [diff] [blame] | 29 | // The pointer should be aligned with |alignment| bytes. The - 1 guarantees |
| 30 | // that it is aligned towards the closest higher (right) address. |
henrike@webrtc.org | 46d4073 | 2012-10-03 16:50:37 +0000 | [diff] [blame] | 31 | return (start_pos + alignment - 1) & ~(alignment - 1); |
henrike@webrtc.org | cd9adf7 | 2012-09-29 03:49:36 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | // Alignment must be an integer power of two. |
henrike@webrtc.org | 28625c1 | 2012-10-02 15:38:35 +0000 | [diff] [blame] | 35 | bool ValidAlignment(size_t alignment) { |
| 36 | if (!alignment) { |
| 37 | return false; |
| 38 | } |
| 39 | return (alignment & (alignment - 1)) == 0; |
henrike@webrtc.org | cd9adf7 | 2012-09-29 03:49:36 +0000 | [diff] [blame] | 40 | } |
| 41 | |
henrike@webrtc.org | 0ed9c69 | 2012-10-08 20:20:22 +0000 | [diff] [blame] | 42 | void* GetRightAlign(const void* pointer, size_t alignment) { |
| 43 | if (!pointer) { |
henrike@webrtc.org | 28625c1 | 2012-10-02 15:38:35 +0000 | [diff] [blame] | 44 | return NULL; |
| 45 | } |
| 46 | if (!ValidAlignment(alignment)) { |
| 47 | return NULL; |
| 48 | } |
henrike@webrtc.org | 0ed9c69 | 2012-10-08 20:20:22 +0000 | [diff] [blame] | 49 | uintptr_t start_pos = reinterpret_cast<uintptr_t>(pointer); |
henrike@webrtc.org | 46d4073 | 2012-10-03 16:50:37 +0000 | [diff] [blame] | 50 | return reinterpret_cast<void*>(GetRightAlign(start_pos, alignment)); |
henrike@webrtc.org | cd9adf7 | 2012-09-29 03:49:36 +0000 | [diff] [blame] | 51 | } |
| 52 | |
henrike@webrtc.org | 28625c1 | 2012-10-02 15:38:35 +0000 | [diff] [blame] | 53 | void* AlignedMalloc(size_t size, size_t alignment) { |
| 54 | if (size == 0) { |
| 55 | return NULL; |
| 56 | } |
| 57 | if (!ValidAlignment(alignment)) { |
| 58 | return NULL; |
| 59 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 60 | |
henrike@webrtc.org | 28625c1 | 2012-10-02 15:38:35 +0000 | [diff] [blame] | 61 | // The memory is aligned towards the lowest address that so only |
| 62 | // alignment - 1 bytes needs to be allocated. |
henrike@webrtc.org | 0ed9c69 | 2012-10-08 20:20:22 +0000 | [diff] [blame] | 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). |
| 65 | void* memory_pointer = malloc(size + sizeof(uintptr_t) + alignment - 1); |
| 66 | if (memory_pointer == NULL) { |
henrike@webrtc.org | 28625c1 | 2012-10-02 15:38:35 +0000 | [diff] [blame] | 67 | return NULL; |
| 68 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 69 | |
henrike@webrtc.org | 0ed9c69 | 2012-10-08 20:20:22 +0000 | [diff] [blame] | 70 | // Aligning after the sizeof(uintptr_t) bytes will leave room for the header |
henrike@webrtc.org | 28625c1 | 2012-10-02 15:38:35 +0000 | [diff] [blame] | 71 | // in the same memory block. |
henrike@webrtc.org | 0ed9c69 | 2012-10-08 20:20:22 +0000 | [diff] [blame] | 72 | uintptr_t align_start_pos = reinterpret_cast<uintptr_t>(memory_pointer); |
henrike@webrtc.org | 46d4073 | 2012-10-03 16:50:37 +0000 | [diff] [blame] | 73 | align_start_pos += sizeof(uintptr_t); |
| 74 | uintptr_t aligned_pos = GetRightAlign(align_start_pos, alignment); |
henrike@webrtc.org | 0ed9c69 | 2012-10-08 20:20:22 +0000 | [diff] [blame] | 75 | void* aligned_pointer = reinterpret_cast<void*>(aligned_pos); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 76 | |
henrike@webrtc.org | 0ed9c69 | 2012-10-08 20:20:22 +0000 | [diff] [blame] | 77 | // Store the address to the beginning of the memory just before the aligned |
| 78 | // memory. |
| 79 | uintptr_t header_pos = aligned_pos - sizeof(uintptr_t); |
| 80 | void* header_pointer = reinterpret_cast<void*>(header_pos); |
| 81 | uintptr_t memory_start = reinterpret_cast<uintptr_t>(memory_pointer); |
| 82 | memcpy(header_pointer, &memory_start, sizeof(uintptr_t)); |
| 83 | |
| 84 | return aligned_pointer; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 85 | } |
| 86 | |
henrike@webrtc.org | 46d4073 | 2012-10-03 16:50:37 +0000 | [diff] [blame] | 87 | void AlignedFree(void* mem_block) { |
| 88 | if (mem_block == NULL) { |
henrike@webrtc.org | 28625c1 | 2012-10-02 15:38:35 +0000 | [diff] [blame] | 89 | return; |
| 90 | } |
henrike@webrtc.org | 46d4073 | 2012-10-03 16:50:37 +0000 | [diff] [blame] | 91 | uintptr_t aligned_pos = reinterpret_cast<uintptr_t>(mem_block); |
| 92 | uintptr_t header_pos = aligned_pos - sizeof(uintptr_t); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 93 | |
henrike@webrtc.org | 28625c1 | 2012-10-02 15:38:35 +0000 | [diff] [blame] | 94 | // Read out the address of the AlignedMemory struct from the header. |
henrike@webrtc.org | 0ed9c69 | 2012-10-08 20:20:22 +0000 | [diff] [blame] | 95 | uintptr_t memory_start_pos = *reinterpret_cast<uintptr_t*>(header_pos); |
| 96 | void* memory_start = reinterpret_cast<void*>(memory_start_pos); |
| 97 | free(memory_start); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 98 | } |
henrike@webrtc.org | 28625c1 | 2012-10-02 15:38:35 +0000 | [diff] [blame] | 99 | |
henrike@webrtc.org | cd9adf7 | 2012-09-29 03:49:36 +0000 | [diff] [blame] | 100 | } // namespace webrtc |