Line data Source code
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 :
11 : #include "webrtc/system_wrappers/include/aligned_malloc.h"
12 :
13 : #include <memory.h>
14 : #include <stdlib.h>
15 :
16 : #ifdef _WIN32
17 : #include <windows.h>
18 : #else
19 : #include <stdint.h>
20 : #endif
21 :
22 : #include "webrtc/typedefs.h"
23 :
24 : // Reference on memory alignment:
25 : // http://stackoverflow.com/questions/227897/solve-the-memory-alignment-in-c-interview-question-that-stumped-me
26 : namespace webrtc {
27 :
28 0 : uintptr_t GetRightAlign(uintptr_t start_pos, size_t alignment) {
29 : // The pointer should be aligned with |alignment| bytes. The - 1 guarantees
30 : // that it is aligned towards the closest higher (right) address.
31 0 : return (start_pos + alignment - 1) & ~(alignment - 1);
32 : }
33 :
34 : // Alignment must be an integer power of two.
35 0 : bool ValidAlignment(size_t alignment) {
36 0 : if (!alignment) {
37 0 : return false;
38 : }
39 0 : return (alignment & (alignment - 1)) == 0;
40 : }
41 :
42 0 : void* GetRightAlign(const void* pointer, size_t alignment) {
43 0 : if (!pointer) {
44 0 : return NULL;
45 : }
46 0 : if (!ValidAlignment(alignment)) {
47 0 : return NULL;
48 : }
49 0 : uintptr_t start_pos = reinterpret_cast<uintptr_t>(pointer);
50 0 : return reinterpret_cast<void*>(GetRightAlign(start_pos, alignment));
51 : }
52 :
53 0 : void* AlignedMalloc(size_t size, size_t alignment) {
54 0 : if (size == 0) {
55 0 : return NULL;
56 : }
57 0 : if (!ValidAlignment(alignment)) {
58 0 : return NULL;
59 : }
60 :
61 : // The memory is aligned towards the lowest address that so only
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
64 : // retreived for deletion, ergo the sizeof(uintptr_t).
65 0 : void* memory_pointer = malloc(size + sizeof(uintptr_t) + alignment - 1);
66 0 : if (memory_pointer == NULL) {
67 0 : return NULL;
68 : }
69 :
70 : // Aligning after the sizeof(uintptr_t) bytes will leave room for the header
71 : // in the same memory block.
72 0 : uintptr_t align_start_pos = reinterpret_cast<uintptr_t>(memory_pointer);
73 0 : align_start_pos += sizeof(uintptr_t);
74 0 : uintptr_t aligned_pos = GetRightAlign(align_start_pos, alignment);
75 0 : void* aligned_pointer = reinterpret_cast<void*>(aligned_pos);
76 :
77 : // Store the address to the beginning of the memory just before the aligned
78 : // memory.
79 0 : uintptr_t header_pos = aligned_pos - sizeof(uintptr_t);
80 0 : void* header_pointer = reinterpret_cast<void*>(header_pos);
81 0 : uintptr_t memory_start = reinterpret_cast<uintptr_t>(memory_pointer);
82 0 : memcpy(header_pointer, &memory_start, sizeof(uintptr_t));
83 :
84 0 : return aligned_pointer;
85 : }
86 :
87 0 : void AlignedFree(void* mem_block) {
88 0 : if (mem_block == NULL) {
89 0 : return;
90 : }
91 0 : uintptr_t aligned_pos = reinterpret_cast<uintptr_t>(mem_block);
92 0 : uintptr_t header_pos = aligned_pos - sizeof(uintptr_t);
93 :
94 : // Read out the address of the AlignedMemory struct from the header.
95 0 : uintptr_t memory_start_pos = *reinterpret_cast<uintptr_t*>(header_pos);
96 0 : void* memory_start = reinterpret_cast<void*>(memory_start_pos);
97 0 : free(memory_start);
98 : }
99 :
100 : } // namespace webrtc
|