2022-02-11 13:53:21 +01:00
|
|
|
/* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
* Copyright 2011-2022 Blender Foundation */
|
2015-02-14 17:29:47 +05:00
|
|
|
|
|
|
|
|
#ifndef __UTIL_ALIGNED_MALLOC_H__
|
|
|
|
|
#define __UTIL_ALIGNED_MALLOC_H__
|
|
|
|
|
|
2021-10-24 14:19:19 +02:00
|
|
|
#include "util/types.h"
|
2015-02-19 22:17:42 +05:00
|
|
|
|
2015-02-14 17:29:47 +05:00
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
|
2018-01-18 21:06:35 +01:00
|
|
|
/* Minimum alignment needed by all CPU native data types (SSE, AVX). */
|
|
|
|
|
#define MIN_ALIGNMENT_CPU_DATA_TYPES 16
|
|
|
|
|
|
2015-02-14 17:29:47 +05:00
|
|
|
/* Allocate block of size bytes at least aligned to a given value. */
|
2015-02-19 22:17:42 +05:00
|
|
|
void *util_aligned_malloc(size_t size, int alignment);
|
2015-02-14 17:29:47 +05:00
|
|
|
|
|
|
|
|
/* Free memory allocated by util_aligned_malloc. */
|
|
|
|
|
void util_aligned_free(void *ptr);
|
|
|
|
|
|
2019-05-02 12:40:24 +02:00
|
|
|
/* Aligned new operator. */
|
2019-05-14 15:05:24 +02:00
|
|
|
template<typename T, typename... Args> T *util_aligned_new(Args... args)
|
2019-05-02 12:40:24 +02:00
|
|
|
{
|
|
|
|
|
void *mem = util_aligned_malloc(sizeof(T), alignof(T));
|
2019-05-14 15:05:24 +02:00
|
|
|
return new (mem) T(args...);
|
2019-05-02 12:40:24 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-03 17:42:08 +02:00
|
|
|
template<typename T> void util_aligned_delete(T *t)
|
|
|
|
|
{
|
|
|
|
|
if (t) {
|
|
|
|
|
t->~T();
|
|
|
|
|
util_aligned_free(t);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-14 17:29:47 +05:00
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|
|
|
|
|
|
#endif /* __UTIL_ALIGNED_MALLOC_H__ */
|