mirror of
https://github.com/klzgrad/naiveproxy.git
synced 2024-12-01 01:36:09 +03:00
58 lines
1.3 KiB
C
58 lines
1.3 KiB
C
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a BSD-style license that can be
|
||
|
// found in the LICENSE file.
|
||
|
|
||
|
#include <type_traits>
|
||
|
|
||
|
// A runtime sized aligned allocation can be created:
|
||
|
//
|
||
|
// float* my_array = static_cast<float*>(AlignedAlloc(size, alignment));
|
||
|
//
|
||
|
// // ... later, to release the memory:
|
||
|
// AlignedFree(my_array);
|
||
|
//
|
||
|
// Or using unique_ptr:
|
||
|
//
|
||
|
// std::unique_ptr<float, AlignedFreeDeleter> my_array(
|
||
|
// static_cast<float*>(AlignedAlloc(size, alignment)));
|
||
|
|
||
|
#ifndef BASE_MEMORY_ALIGNED_MEMORY_H_
|
||
|
#define BASE_MEMORY_ALIGNED_MEMORY_H_
|
||
|
|
||
|
#include <stddef.h>
|
||
|
#include <stdint.h>
|
||
|
|
||
|
#include "base/base_export.h"
|
||
|
#include "base/compiler_specific.h"
|
||
|
|
||
|
#if defined(COMPILER_MSVC)
|
||
|
#include <malloc.h>
|
||
|
#else
|
||
|
#include <stdlib.h>
|
||
|
#endif
|
||
|
|
||
|
namespace base {
|
||
|
|
||
|
// This can be replaced with std::aligned_malloc when we have C++17.
|
||
|
BASE_EXPORT void* AlignedAlloc(size_t size, size_t alignment);
|
||
|
|
||
|
inline void AlignedFree(void* ptr) {
|
||
|
#if defined(COMPILER_MSVC)
|
||
|
_aligned_free(ptr);
|
||
|
#else
|
||
|
free(ptr);
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
// Deleter for use with unique_ptr. E.g., use as
|
||
|
// std::unique_ptr<Foo, base::AlignedFreeDeleter> foo;
|
||
|
struct AlignedFreeDeleter {
|
||
|
inline void operator()(void* ptr) const {
|
||
|
AlignedFree(ptr);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
} // namespace base
|
||
|
|
||
|
#endif // BASE_MEMORY_ALIGNED_MEMORY_H_
|