]> git.saurik.com Git - apple/security.git/blame - cdsa/cdsa_utilities/memutils.h
Security-163.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / memutils.h
CommitLineData
bac41a7b
A
1/*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
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
8 * using this file.
9 *
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.
16 */
17
18
19//
20// memutils - memory-related low-level utilities for easier living
21//
22#ifndef _H_MEMUTILS
23#define _H_MEMUTILS
24
25#include <Security/utilities.h>
26#include <stdlib.h>
27#include <algorithm>
28
29
bac41a7b
A
30namespace Security
31{
32
33//
34// Encapsulate these very sharp tools in a separate namespace
35//
36namespace LowLevelMemoryUtilities
37{
38
39
40//
41// The default system alignment.
42// @@@ We should really get this from somewhere... probably from utility_config.h.
43//
44static const size_t systemAlignment = 4;
45typedef UInt32 PointerInt;
46
47
48//
49// Get the local alignment for a type.
50//
51template <class T>
52inline size_t alignof() { struct { char c; T t; } s; return sizeof(s) - sizeof(T); }
53
54
55//
56// Round up a size or pointer to an alignment boundary.
57// Alignment must be a power of two; default is default alignment.
58//
59inline size_t alignUp(size_t size, size_t alignment = systemAlignment)
60{
61 return ((size - 1) & ~(alignment - 1)) + alignment;
62}
63
64inline void *alignUp(void *p, size_t alignment = systemAlignment)
65{
66 return reinterpret_cast<void *>(alignUp(PointerInt(p), alignment));
67}
68
69inline const void *alignUp(const void *p, size_t alignment = systemAlignment)
70{
71 return reinterpret_cast<const void *>(alignUp(PointerInt(p), alignment));
72}
73
74template <class T>
75inline const T *increment(const void *p, ptrdiff_t offset)
76{ return reinterpret_cast<const T *>(PointerInt(p) + offset); }
77
78template <class T>
79inline T *increment(void *p, ptrdiff_t offset)
80{ return reinterpret_cast<T *>(PointerInt(p) + offset); }
81
82inline const void *increment(const void *p, ptrdiff_t offset)
83{ return increment<const void>(p, offset); }
84
85inline void *increment(void *p, ptrdiff_t offset)
86{ return increment<void>(p, offset); }
87
88template <class T>
89inline const T *increment(const void *p, ptrdiff_t offset, size_t alignment)
90{ return increment<const T>(alignUp(p, alignment), offset); }
91
92template <class T>
93inline T *increment(void *p, ptrdiff_t offset, size_t alignment)
94{ return increment<T>(alignUp(p, alignment), offset); }
95
96inline const void *increment(const void *p, ptrdiff_t offset, size_t alignment)
97{ return increment<const void>(p, offset, alignment); }
98
99inline void *increment(void *p, ptrdiff_t offset, size_t alignment)
100{ return increment<void>(p, offset, alignment); }
101
102inline ptrdiff_t difference(const void *p1, const void *p2)
103{ return PointerInt(p1) - PointerInt(p2); }
104
105
bac41a7b
A
106} // end namespace LowLevelMemoryUtilities
107
108} // end namespace Security
109
bac41a7b 110#endif //_H_MEMUTILS