cppcore
A small, portable C++ core library
Loading...
Searching...
No Matches
MemUtils.h
Go to the documentation of this file.
1/*-----------------------------------------------------------------------------------------------
2The MIT License (MIT)
3
4Copyright (c) 2014-2025 Kim Kulling
5
6Permission is hereby granted, free of charge, to any person obtaining a copy of
7this software and associated documentation files (the "Software"), to deal in
8the Software without restriction, including without limitation the rights to
9use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10the Software, and to permit persons to whom the Software is furnished to do so,
11subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22-----------------------------------------------------------------------------------------------*/
23#pragma once
24
26#include <cinttypes>
27
28namespace cppcore {
29
30#define ALIGN_MASK(value, mask) (((value) + (mask)) & ((~0) & (~(mask))))
31
35template<class T>
36inline size_t align(size_t n) {
37 return (n + sizeof(T) - 1) & ~(sizeof(T) - 1);
38}
39
40//-------------------------------------------------------------------------------------------------
45//-------------------------------------------------------------------------------------------------
47public:
50 static void clearMemory(void *buffer, size_t size);
51
56 static bool isAligned(const void *ptr, size_t align);
57
63 static const void *alignPtr(void *ptr, size_t extra, size_t align);
64
66 MemUtils() = default;
67
69 ~MemUtils() = default;
70};
71
72inline bool MemUtils::isAligned(const void *ptr, size_t align) {
73 union {
74 const void *mPtr;
75 uintptr_t mAddr;
76 } unaligned;
77
78 unaligned.mPtr = ptr;
79 return 0 == (unaligned.mAddr & (align - 1));
80}
81
82inline const void *MemUtils::alignPtr(void *ptr, size_t extra, size_t align) {
83 if (align == 0) {
84 return nullptr;
85 }
86 union {
87 const void *mPtr;
88 uintptr_t mAddr;
89 } unaligned;
90 unaligned.mPtr = ptr;
91 uintptr_t un = unaligned.mAddr + extra; // space for header
92 const uintptr_t mask = align - 1;
93 uintptr_t aligned = ALIGN_MASK(un, mask);
94 unaligned.mAddr = aligned;
95
96 return unaligned.mPtr;
97}
98
99} // Namespace cppcore
#define DLL_CPPCORE_EXPORT
#define ALIGN_MASK(value, mask)
Definition MemUtils.h:30
~MemUtils()=default
The class destructor.
static bool isAligned(const void *ptr, size_t align)
Will return true, if the pointer fits into the alignment.
Definition MemUtils.h:72
static void clearMemory(void *buffer, size_t size)
Will clear the given buffer with zero.
Definition MemUtils.cpp:27
static const void * alignPtr(void *ptr, size_t extra, size_t align)
Will align the given pointer.
Definition MemUtils.h:82
MemUtils()=default
The default class constructor.
size_t align(size_t n)
Will calculate the alignment for the given size.
Definition MemUtils.h:36