]>
git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_utilities/lib/memutils.h
2 * Copyright (c) 2000-2004,2011,2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 // memutils - memory-related low-level utilities for easier living
31 #include <security_utilities/utilities.h>
32 #include <sys/types.h>
38 // Encapsulate these very sharp tools in a separate (ugly-named) namespace
41 namespace LowLevelMemoryUtilities
{
45 // The default system alignment.
47 static const size_t systemAlignment
= 4;
51 // Get the local alignment for a type, as used by the acting compiler.
54 inline int alignof_template() { struct { char c
; T t
; } s
; return sizeof(s
) - sizeof(T
); }
58 // Get the local offset of a field in a (struct or class) type, as layed out
59 // by the acting compiler.
60 // NB: "offsetof" is a standard-defined macro. Don't use that.
62 template <class Type
, class Field
>
63 inline size_t fieldOffsetOf(Field (Type::*field
))
65 Type
*object
= 0; // we don't REALLY need this, but it's easier to read
66 return uintptr_t(&(object
->*field
)) - uintptr_t(object
);
71 // Round up a size or pointer to an alignment boundary.
72 // Alignment must be a power of two; default is default alignment.
74 inline size_t alignUp(size_t size
, size_t alignment
= systemAlignment
)
76 return ((size
- 1) & ~(alignment
- 1)) + alignment
;
79 inline void *alignUp(void *p
, size_t alignment
= systemAlignment
)
81 return reinterpret_cast<void *>(alignUp(uintptr_t(p
), alignment
));
84 inline const void *alignUp(const void *p
, size_t alignment
= systemAlignment
)
86 return reinterpret_cast<const void *>(alignUp(uintptr_t(p
), alignment
));
90 inline const T
*increment(const void *p
, ptrdiff_t offset
)
91 { return reinterpret_cast<const T
*>(uintptr_t(p
) + offset
); }
94 inline T
*increment(void *p
, ptrdiff_t offset
)
95 { return reinterpret_cast<T
*>(uintptr_t(p
) + offset
); }
97 inline const void *increment(const void *p
, ptrdiff_t offset
)
98 { return increment
<const void>(p
, offset
); }
100 inline void *increment(void *p
, ptrdiff_t offset
)
101 { return increment
<void>(p
, offset
); }
104 inline const T
*increment(const void *p
, ptrdiff_t offset
, size_t alignment
)
105 { return increment
<const T
>(alignUp(p
, alignment
), offset
); }
108 inline T
*increment(void *p
, ptrdiff_t offset
, size_t alignment
)
109 { return increment
<T
>(alignUp(p
, alignment
), offset
); }
111 inline const void *increment(const void *p
, ptrdiff_t offset
, size_t alignment
)
112 { return increment
<const void>(p
, offset
, alignment
); }
114 inline void *increment(void *p
, ptrdiff_t offset
, size_t alignment
)
115 { return increment
<void>(p
, offset
, alignment
); }
117 inline ptrdiff_t difference(const void *p1
, const void *p2
)
118 { return uintptr_t(p1
) - uintptr_t(p2
); }
121 } // end namespace LowLevelMemoryUtilities
122 } // end namespace Security