]> git.saurik.com Git - apple/ld64.git/blob - src/ld/code-sign-blobs/memutils.h
ld64-302.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 <typename T>
54 unsigned long myalignof() {
55 struct { char c; T t; } s;
56 return sizeof(s) - sizeof(T);
57 }
58
59
60 //
61 // Get the local offset of a field in a (struct or class) type, as layed out
62 // by the acting compiler.
63 // NB: "offsetof" is a standard-defined macro. Don't use that.
64 //
65 template <class Type, class Field>
66 inline size_t fieldOffsetOf(Field (Type::*field))
67 {
68 Type *object = 0; // we don't REALLY need this, but it's easier to read
69 return uintptr_t(&(object->*field)) - uintptr_t(object);
70 }
71
72
73 //
74 // Round up a size or pointer to an alignment boundary.
75 // Alignment must be a power of two; default is default alignment.
76 //
77 inline size_t alignUp(size_t size, size_t alignment = systemAlignment)
78 {
79 return ((size - 1) & ~(alignment - 1)) + alignment;
80 }
81
82 inline void *alignUp(void *p, size_t alignment = systemAlignment)
83 {
84 return reinterpret_cast<void *>(alignUp(uintptr_t(p), alignment));
85 }
86
87 inline const void *alignUp(const void *p, size_t alignment = systemAlignment)
88 {
89 return reinterpret_cast<const void *>(alignUp(uintptr_t(p), alignment));
90 }
91
92 template <class T>
93 inline const T *increment(const void *p, ptrdiff_t offset)
94 { return reinterpret_cast<const T *>(uintptr_t(p) + offset); }
95
96 template <class T>
97 inline T *increment(void *p, ptrdiff_t offset)
98 { return reinterpret_cast<T *>(uintptr_t(p) + offset); }
99
100 inline const void *increment(const void *p, ptrdiff_t offset)
101 { return increment<const void>(p, offset); }
102
103 inline void *increment(void *p, ptrdiff_t offset)
104 { return increment<void>(p, offset); }
105
106 template <class T>
107 inline const T *increment(const void *p, ptrdiff_t offset, size_t alignment)
108 { return increment<const T>(alignUp(p, alignment), offset); }
109
110 template <class T>
111 inline T *increment(void *p, ptrdiff_t offset, size_t alignment)
112 { return increment<T>(alignUp(p, alignment), offset); }
113
114 inline const void *increment(const void *p, ptrdiff_t offset, size_t alignment)
115 { return increment<const void>(p, offset, alignment); }
116
117 inline void *increment(void *p, ptrdiff_t offset, size_t alignment)
118 { return increment<void>(p, offset, alignment); }
119
120 inline ptrdiff_t difference(const void *p1, const void *p2)
121 { return uintptr_t(p1) - uintptr_t(p2); }
122
123
124 } // end namespace LowLevelMemoryUtilities
125 } // end namespace Security
126
127 #endif //_H_MEMUTILS