美文网首页
webrtc malloc 内存对齐

webrtc malloc 内存对齐

作者: gykimo | 来源:发表于2021-05-07 17:06 被阅读0次

代码分析

如最后一节的代码展示,AlignedMalloc是优化后的分配内存接口,参数和系统malloc一样,但是AlignedMalloc返回的参数是经过对齐的,这样CPU访问内存时可能会快一些。

AlignedMalloc的实现原理也比较简单,先malloc内存memory_pointer,然后将最小的对齐的地址aligned_pos返回给使用者。

当AlignedFree释放内存aligned_pos时,需要转换到memory_pointer。所以AlignedMalloc实际在aligned_pos前面header_pos处存放了实际的内存地址memory_pointer,这样就可以转换到实际malloc分配的地址。

源代码

https://webrtc.googlesource.com/src/+/refs/heads/master/rtc_base/memory/aligned_malloc.h
https://webrtc.googlesource.com/src/+/refs/heads/master/rtc_base/memory/aligned_malloc.cc

/*
 *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */
#include "rtc_base/memory/aligned_malloc.h"
#include <stdlib.h>  // for free, malloc
#include <string.h>  // for memcpy
#include "rtc_base/checks.h"
#ifdef _WIN32
#include <windows.h>
#else
#include <stdint.h>
#endif
// Reference on memory alignment:
// http://stackoverflow.com/questions/227897/solve-the-memory-alignment-in-c-interview-question-that-stumped-me
namespace webrtc {
uintptr_t GetRightAlign(uintptr_t start_pos, size_t alignment) {
  // The pointer should be aligned with |alignment| bytes. The - 1 guarantees
  // that it is aligned towards the closest higher (right) address.
  return (start_pos + alignment - 1) & ~(alignment - 1);
}
// Alignment must be an integer power of two.
bool ValidAlignment(size_t alignment) {
  if (!alignment) {
    return false;
  }
  return (alignment & (alignment - 1)) == 0;
}
void* GetRightAlign(const void* pointer, size_t alignment) {
  if (!pointer) {
    return NULL;
  }
  if (!ValidAlignment(alignment)) {
    return NULL;
  }
  uintptr_t start_pos = reinterpret_cast<uintptr_t>(pointer);
  return reinterpret_cast<void*>(GetRightAlign(start_pos, alignment));
}
void* AlignedMalloc(size_t size, size_t alignment) {
  if (size == 0) {
    return NULL;
  }
  if (!ValidAlignment(alignment)) {
    return NULL;
  }
  // The memory is aligned towards the lowest address that so only
  // alignment - 1 bytes needs to be allocated.
  // A pointer to the start of the memory must be stored so that it can be
  // retreived for deletion, ergo the sizeof(uintptr_t).
  void* memory_pointer = malloc(size + sizeof(uintptr_t) + alignment - 1);
  RTC_CHECK(memory_pointer) << "Couldn't allocate memory in AlignedMalloc";
  // Aligning after the sizeof(uintptr_t) bytes will leave room for the header
  // in the same memory block.
  uintptr_t align_start_pos = reinterpret_cast<uintptr_t>(memory_pointer);
  align_start_pos += sizeof(uintptr_t);
  uintptr_t aligned_pos = GetRightAlign(align_start_pos, alignment);
  void* aligned_pointer = reinterpret_cast<void*>(aligned_pos);
  // Store the address to the beginning of the memory just before the aligned
  // memory.
  uintptr_t header_pos = aligned_pos - sizeof(uintptr_t);
  void* header_pointer = reinterpret_cast<void*>(header_pos);
  uintptr_t memory_start = reinterpret_cast<uintptr_t>(memory_pointer);
  memcpy(header_pointer, &memory_start, sizeof(uintptr_t));
  return aligned_pointer;
}
void AlignedFree(void* mem_block) {
  if (mem_block == NULL) {
    return;
  }
  uintptr_t aligned_pos = reinterpret_cast<uintptr_t>(mem_block);
  uintptr_t header_pos = aligned_pos - sizeof(uintptr_t);
  // Read out the address of the AlignedMemory struct from the header.
  uintptr_t memory_start_pos = *reinterpret_cast<uintptr_t*>(header_pos);
  void* memory_start = reinterpret_cast<void*>(memory_start_pos);
  free(memory_start);
}
}  // namespace webrtc

相关文章

  • webrtc malloc 内存对齐

    代码分析 如最后一节的代码展示,AlignedMalloc是优化后的分配内存接口,参数和系统malloc一样,但是...

  • api

    posix_memalign() 内存申请方式posix_memalign 内存对齐:malloc申请到的内存在3...

  • 目录

    优秀的开源框架,肯定有特别的代码优化技巧。快速汲取营养。 内存管理 webrtc内存对齐:https://www....

  • iOS底层原理 - 内存对齐&& malloc理解

    1.回顾之前 ​ 前面我们讲过alloc的一些底层探索中,在分配内存的时候有涉及到内存对齐的概念。instance...

  • 2.iOS底层学习之内存对齐

    学习了内存对齐之后的疑问?? 1.为啥要内存对齐?2.内存对齐的规则?3.内存对齐实例分析。 内存对齐的目的 上网...

  • 内存对齐

    本次主要讨论三个问题: 什么是内存对齐 内存对齐的好处 如何对齐 内存对齐 内存对齐是一种提高内存访问速度的策略。...

  • 结构体内存对齐

    对象内存对齐 探讨的问题 1.什么是内存对齐?2.为什么要做内存对齐?3.结构体内存对齐规则4.源码内存对齐算法 ...

  • Per-Class allocator(1)

    小型内存池设计 内存池设计思路 降低malloc调用次数为了减少malloc的次数,考虑能否先统一malloc一大...

  • 虚幻 Unreal Engine 4内存管理

    不受内存管理的内存 malloc & free new & deletenew与malloc的区别在于,new在分...

  • C语言的malloc

    为什么C语言要有malloc malloc就是memory allocate动态分配内存,malloc的出现时为了...

网友评论

      本文标题:webrtc malloc 内存对齐

      本文链接:https://www.haomeiwen.com/subject/chssdltx.html