1b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org/* 2b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. 3b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * 4b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * Use of this source code is governed by a BSD-style license 5b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * that can be found in the LICENSE file in the root of the source 6b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * tree. An additional intellectual property rights grant can be found 7b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * in the file PATENTS. All contributing project authors may 8b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * be found in the AUTHORS file in the root of the source tree. 9b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org */ 10b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org 11b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_ALIGNED_MALLOC_H_ 12b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_ALIGNED_MALLOC_H_ 13b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org 14b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// The functions declared here 15b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// 1) Allocates block of aligned memory. 16b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// 2) Re-calculates a pointer such that it is aligned to a higher or equal 17b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// address. 18b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// Note: alignment must be a power of two. The alignment is in bytes. 19b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org 20b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#include <stddef.h> 21b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org 22b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgnamespace webrtc { 23b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org 24b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// Returns a pointer to the first boundry of |alignment| bytes following the 25b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// address of |ptr|. 26b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// Note that there is no guarantee that the memory in question is available. 27b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// |ptr| has no requirements other than it can't be NULL. 28b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgvoid* GetRightAlign(const void* ptr, size_t alignment); 29b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org 30b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// Allocates memory of |size| bytes aligned on an |alignment| boundry. 31b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// The return value is a pointer to the memory. Note that the memory must 32b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// be de-allocated using AlignedFree. 33b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgvoid* AlignedMalloc(size_t size, size_t alignment); 34b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// De-allocates memory created using the AlignedMalloc() API. 35b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgvoid AlignedFree(void* mem_block); 36b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org 37b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// Templated versions to facilitate usage of aligned malloc without casting 38b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// to and from void*. 39b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgtemplate<typename T> 40b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgT* GetRightAlign(const T* ptr, size_t alignment) { 41b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org return reinterpret_cast<T*>(GetRightAlign(reinterpret_cast<const void*>(ptr), 42b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org alignment)); 43b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org} 44b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgtemplate<typename T> 45b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgT* AlignedMalloc(size_t size, size_t alignment) { 46b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org return reinterpret_cast<T*>(AlignedMalloc(size, alignment)); 47b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org} 48b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org 491d2f5dcbb683b08a2c9ea229334c6baf8b377158andrew@webrtc.org// Deleter for use with scoped_ptr. E.g., use as 501d2f5dcbb683b08a2c9ea229334c6baf8b377158andrew@webrtc.org// scoped_ptr<Foo, AlignedFreeDeleter> foo; 511d2f5dcbb683b08a2c9ea229334c6baf8b377158andrew@webrtc.orgstruct AlignedFreeDeleter { 521d2f5dcbb683b08a2c9ea229334c6baf8b377158andrew@webrtc.org inline void operator()(void* ptr) const { 531d2f5dcbb683b08a2c9ea229334c6baf8b377158andrew@webrtc.org AlignedFree(ptr); 541d2f5dcbb683b08a2c9ea229334c6baf8b377158andrew@webrtc.org } 55b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}; 56b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org 57b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org} // namespace webrtc 58b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org 59b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_ALIGNED_MALLOC_H_ 60