]> git.saurik.com Git - apple/ld64.git/blob - src/ld/code-sign-blobs/memutils.h
ld64-133.3.tar.gz
[apple/ld64.git] / src / ld / code-sign-blobs / memutils.h
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 //
26 // memutils - memory-related low-level utilities for easier living
27 //
28 #ifndef _H_MEMUTILS
29 #define _H_MEMUTILS
30
31 //#include <security_utilities/utilities.h>
32 #include <sys/types.h>
33 #include <stdlib.h>
34 #include <algorithm>
35
36
37 //
38 // Encapsulate these very sharp tools in a separate (ugly-named) namespace
39 //
40 namespace Security {
41 namespace LowLevelMemoryUtilities {
42
43
44 //
45 // The default system alignment.
46 //
47 static const size_t systemAlignment = 4;
48
49
50 //
51 // Get the local alignment for a type, as used by the acting compiler.
52 //
53 template <class T>
54 inline size_t alignof() { struct { char c; T t; } s; return sizeof(s) - sizeof(T); }
55
56
57 //
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.
61 //
62 template <class Type, class Field>
63 inline size_t fieldOffsetOf(Field (Type::*field))
64 {
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);
67 }
68
69
70 //
71 // Round up a size or pointer to an alignment boundary.
72 // Alignment must be a power of two; default is default alignment.
73 //
74 inline size_t alignUp(size_t size, size_t alignment = systemAlignment)
75 {
76 return ((size - 1) & ~(alignment - 1)) + alignment;
77 }
78
79 inline void *alignUp(void *p, size_t alignment = systemAlignment)
80 {
81 return reinterpret_cast<void *>(alignUp(uintptr_t(p), alignment));
82 }
83
84 inline const void *alignUp(const void *p, size_t alignment = systemAlignment)
85 {
86 return reinterpret_cast<const void *>(alignUp(uintptr_t(p), alignment));
87 }
88
89 template <class T>
90 inline const T *increment(const void *p, ptrdiff_t offset)
91 { return reinterpret_cast<const T *>(uintptr_t(p) + offset); }
92
93 template <class T>
94 inline T *increment(void *p, ptrdiff_t offset)
95 { return reinterpret_cast<T *>(uintptr_t(p) + offset); }
96
97 inline const void *increment(const void *p, ptrdiff_t offset)
98 { return increment<const void>(p, offset); }
99
100 inline void *increment(void *p, ptrdiff_t offset)
101 { return increment<void>(p, offset); }
102
103 template <class T>
104 inline const T *increment(const void *p, ptrdiff_t offset, size_t alignment)
105 { return increment<const T>(alignUp(p, alignment), offset); }
106
107 template <class T>
108 inline T *increment(void *p, ptrdiff_t offset, size_t alignment)
109 { return increment<T>(alignUp(p, alignment), offset); }
110
111 inline const void *increment(const void *p, ptrdiff_t offset, size_t alignment)
112 { return increment<const void>(p, offset, alignment); }
113
114 inline void *increment(void *p, ptrdiff_t offset, size_t alignment)
115 { return increment<void>(p, offset, alignment); }
116
117 inline ptrdiff_t difference(const void *p1, const void *p2)
118 { return uintptr_t(p1) - uintptr_t(p2); }
119
120
121 } // end namespace LowLevelMemoryUtilities
122 } // end namespace Security
123
124 #endif //_H_MEMUTILS