Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions include/cppcore/Memory/MemUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#pragma once

#include <cppcore/CPPCoreCommon.h>
#include <string.h>
#include <cinttypes>

namespace cppcore {

Expand All @@ -42,15 +44,27 @@ inline size_t align(size_t n) {
class DLL_CPPCORE_EXPORT MemUtils {
public:
/// @brief Will clear the given buffer with zero.
/// @param buffer [inout] The buffer to clear.
/// @param[inout] buffer The buffer to clear.
static void clearMemory(void *buffer, size_t size);

/// @brief Will return true, if the pointer fits into the alignment.
/// @param[in] ptr The pointer to check.
/// @param[in] align The alignment to check for.
/// @return true if aligned, false if not.
static bool isAligned(const void *ptr, size_t align);

/// @brief Will align the given pointer.
/// @param[in] ptr The pointer to align.
/// @param[in] extra Space for headers / meta information.
/// @param[in] align The alignment to check for.
/// @return The aligned pointer.
static const void *alignPtr(void *ptr, size_t extra, size_t align);

MemUtils() = delete;
~MemUtils() = delete;
/// @brief The default class constructor.
MemUtils() = default;

/// @brief The class destructor.
~MemUtils() = default;
};

inline bool MemUtils::isAligned(const void *ptr, size_t align) {
Expand All @@ -64,6 +78,9 @@ inline bool MemUtils::isAligned(const void *ptr, size_t align) {
}

inline const void *MemUtils::alignPtr(void *ptr, size_t extra, size_t align) {
if (align == 0) {
return nullptr;
}
union {
const void *mPtr;
uintptr_t mAddr;
Expand Down