-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Util: Add header for aligned memory allocation.
- Loading branch information
Showing
3 changed files
with
78 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** @file | ||
@brief Header defining an aligned memory allocator. | ||
@date 2015 | ||
@author | ||
Sensics, Inc. | ||
<http://sensics.com/osvr> | ||
*/ | ||
|
||
/* | ||
// Copyright 2015 Sensics, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
*/ | ||
|
||
#ifndef INCLUDED_AlignedMemory_h_GUID_552DA92F_420A_4ADB_BCD2_493486A1C7A1 | ||
#define INCLUDED_AlignedMemory_h_GUID_552DA92F_420A_4ADB_BCD2_493486A1C7A1 | ||
|
||
// Internal Includes | ||
// - none | ||
|
||
// Library/third-party includes | ||
// - none | ||
|
||
// Standard includes | ||
#include <memory> | ||
|
||
#define OSVR_ALIGN_SIZE 16 | ||
|
||
namespace osvr { | ||
namespace util { | ||
/// @brief Aligned allocation function, gives a pointer to a block of | ||
/// memory aligned to a memory boundary. | ||
/// | ||
/// Used in header-only C++ wrappers over C APIs. | ||
inline void* aligned_alloc(size_t bytes) { | ||
// Allocate a memory buffer with enough space to store a pointer to the | ||
// original buffer. | ||
size_t space = bytes + OSVR_ALIGN_SIZE; | ||
void* buffer = malloc(space + sizeof(void*)); | ||
void* aligned = (void**)buffer + 1; | ||
|
||
// After this call the 'aligned' pointer will be aligned to a boundary. | ||
// If there is not enough space to align the pointer, it stays unaligned. | ||
std::align(OSVR_ALIGN_SIZE, bytes, aligned, space); | ||
|
||
// Store the buffer pointer for the delete call. | ||
((void **)aligned)[-1] = buffer; | ||
|
||
return aligned; | ||
} | ||
|
||
/// @brief Aligned deallocation function, uses the pointer to the original | ||
/// memory block to deallocate it. | ||
/// | ||
/// Used in header-only C++ wrappers over C APIs. | ||
inline void aligned_free(void* p) { | ||
// Null-pointer should be a no-op. | ||
if (p) | ||
free(((void **)p)[-1]); | ||
} | ||
} // namespace util | ||
} // namespace osvr | ||
#endif // INCLUDED_AlignedMemory_h_GUID_552DA92F_420A_4ADB_BCD2_493486A1C7A1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters