]>
git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/memutils.h
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 // memutils - memory-related low-level utilities for easier living
25 #include <Security/utilities.h>
34 // Encapsulate these very sharp tools in a separate namespace
36 namespace LowLevelMemoryUtilities
41 // The default system alignment.
42 // @@@ We should really get this from somewhere... probably from utility_config.h.
44 static const size_t systemAlignment
= 4;
45 typedef UInt32 PointerInt
;
49 // Get the local alignment for a type.
52 inline size_t alignof() { struct { char c
; T t
; } s
; return sizeof(s
) - sizeof(T
); }
56 // Round up a size or pointer to an alignment boundary.
57 // Alignment must be a power of two; default is default alignment.
59 inline size_t alignUp(size_t size
, size_t alignment
= systemAlignment
)
61 return ((size
- 1) & ~(alignment
- 1)) + alignment
;
64 inline void *alignUp(void *p
, size_t alignment
= systemAlignment
)
66 return reinterpret_cast<void *>(alignUp(PointerInt(p
), alignment
));
69 inline const void *alignUp(const void *p
, size_t alignment
= systemAlignment
)
71 return reinterpret_cast<const void *>(alignUp(PointerInt(p
), alignment
));
75 inline const T
*increment(const void *p
, ptrdiff_t offset
)
76 { return reinterpret_cast<const T
*>(PointerInt(p
) + offset
); }
79 inline T
*increment(void *p
, ptrdiff_t offset
)
80 { return reinterpret_cast<T
*>(PointerInt(p
) + offset
); }
82 inline const void *increment(const void *p
, ptrdiff_t offset
)
83 { return increment
<const void>(p
, offset
); }
85 inline void *increment(void *p
, ptrdiff_t offset
)
86 { return increment
<void>(p
, offset
); }
89 inline const T
*increment(const void *p
, ptrdiff_t offset
, size_t alignment
)
90 { return increment
<const T
>(alignUp(p
, alignment
), offset
); }
93 inline T
*increment(void *p
, ptrdiff_t offset
, size_t alignment
)
94 { return increment
<T
>(alignUp(p
, alignment
), offset
); }
96 inline const void *increment(const void *p
, ptrdiff_t offset
, size_t alignment
)
97 { return increment
<const void>(p
, offset
, alignment
); }
99 inline void *increment(void *p
, ptrdiff_t offset
, size_t alignment
)
100 { return increment
<void>(p
, offset
, alignment
); }
102 inline ptrdiff_t difference(const void *p1
, const void *p2
)
103 { return PointerInt(p1
) - PointerInt(p2
); }
106 } // end namespace LowLevelMemoryUtilities
108 } // end namespace Security