]> git.saurik.com Git - apple/xnu.git/blame - libkern/c++/OSNumber.cpp
xnu-4903.270.47.tar.gz
[apple/xnu.git] / libkern / c++ / OSNumber.cpp
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
0a7de745 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/* IOOffset.m created by rsulack on Wed 17-Sep-1997 */
29
9bccf70c
A
30#include <sys/cdefs.h>
31
1c79356b
A
32#include <libkern/c++/OSNumber.h>
33#include <libkern/c++/OSString.h>
34#include <libkern/c++/OSSerialize.h>
35#include <libkern/c++/OSLib.h>
36
0b4e3aa0 37#define sizeMask (~0ULL >> (64 - size))
1c79356b
A
38
39#define super OSObject
40
41OSDefineMetaClassAndStructors(OSNumber, OSObject)
9bccf70c 42
1c79356b
A
43OSMetaClassDefineReservedUnused(OSNumber, 0);
44OSMetaClassDefineReservedUnused(OSNumber, 1);
45OSMetaClassDefineReservedUnused(OSNumber, 2);
46OSMetaClassDefineReservedUnused(OSNumber, 3);
47OSMetaClassDefineReservedUnused(OSNumber, 4);
48OSMetaClassDefineReservedUnused(OSNumber, 5);
49OSMetaClassDefineReservedUnused(OSNumber, 6);
50OSMetaClassDefineReservedUnused(OSNumber, 7);
51
0a7de745
A
52bool
53OSNumber::init(unsigned long long inValue, unsigned int newNumberOfBits)
1c79356b 54{
0a7de745
A
55 if (!super::init()) {
56 return false;
57 }
58 if (newNumberOfBits > 64) {
59 return false;
60 }
1c79356b 61
0a7de745
A
62 size = newNumberOfBits;
63 value = (inValue & sizeMask);
1c79356b 64
0a7de745 65 return true;
1c79356b
A
66}
67
0a7de745
A
68bool
69OSNumber::init(const char *newValue, unsigned int newNumberOfBits)
1c79356b 70{
0a7de745 71 return init((unsigned long long)strtoul(newValue, NULL, 0), newNumberOfBits);
1c79356b
A
72}
73
0a7de745
A
74void
75OSNumber::free()
76{
77 super::free();
78}
1c79356b 79
0a7de745
A
80OSNumber *
81OSNumber::withNumber(unsigned long long value,
82 unsigned int newNumberOfBits)
1c79356b 83{
0a7de745 84 OSNumber *me = new OSNumber;
1c79356b 85
0a7de745
A
86 if (me && !me->init(value, newNumberOfBits)) {
87 me->release();
88 return 0;
89 }
1c79356b 90
0a7de745 91 return me;
1c79356b
A
92}
93
0a7de745
A
94OSNumber *
95OSNumber::withNumber(const char *value, unsigned int newNumberOfBits)
1c79356b 96{
0a7de745 97 OSNumber *me = new OSNumber;
1c79356b 98
0a7de745
A
99 if (me && !me->init(value, newNumberOfBits)) {
100 me->release();
101 return 0;
102 }
1c79356b 103
0a7de745 104 return me;
1c79356b
A
105}
106
0a7de745
A
107unsigned int
108OSNumber::numberOfBits() const
109{
110 return size;
111}
1c79356b 112
0a7de745
A
113unsigned int
114OSNumber::numberOfBytes() const
115{
116 return (size + 7) / 8;
117}
1c79356b
A
118
119
0a7de745
A
120unsigned char
121OSNumber::unsigned8BitValue() const
1c79356b 122{
0a7de745 123 return (unsigned char) value;
1c79356b
A
124}
125
0a7de745
A
126unsigned short
127OSNumber::unsigned16BitValue() const
1c79356b 128{
0a7de745 129 return (unsigned short) value;
1c79356b
A
130}
131
0a7de745
A
132unsigned int
133OSNumber::unsigned32BitValue() const
1c79356b 134{
0a7de745 135 return (unsigned int) value;
1c79356b
A
136}
137
0a7de745
A
138unsigned long long
139OSNumber::unsigned64BitValue() const
1c79356b 140{
0a7de745 141 return value;
1c79356b
A
142}
143
0a7de745
A
144void
145OSNumber::addValue(signed long long inValue)
1c79356b 146{
0a7de745 147 value = ((value + inValue) & sizeMask);
1c79356b
A
148}
149
0a7de745
A
150void
151OSNumber::setValue(unsigned long long inValue)
1c79356b 152{
0a7de745 153 value = (inValue & sizeMask);
1c79356b
A
154}
155
0a7de745
A
156bool
157OSNumber::isEqualTo(const OSNumber *integer) const
1c79356b 158{
0a7de745 159 return value == integer->value;
1c79356b
A
160}
161
0a7de745
A
162bool
163OSNumber::isEqualTo(const OSMetaClassBase *obj) const
1c79356b 164{
0a7de745
A
165 OSNumber * offset;
166 if ((offset = OSDynamicCast(OSNumber, obj))) {
167 return isEqualTo(offset);
168 } else {
169 return false;
170 }
1c79356b
A
171}
172
0a7de745
A
173bool
174OSNumber::serialize(OSSerialize *s) const
1c79356b 175{
0a7de745
A
176 char temp[32];
177
178 if (s->previouslySerialized(this)) {
179 return true;
180 }
181
182 snprintf(temp, sizeof(temp), "integer size=\"%d\"", size);
183 if (!s->addXMLStartTag(this, temp)) {
184 return false;
185 }
186
187 //XXX sprintf(temp, "0x%qx", value);
188 if ((value >> 32)) {
189 snprintf(temp, sizeof(temp), "0x%lx%08lx", (unsigned long)(value >> 32),
190 (unsigned long)(value & 0xFFFFFFFF));
191 } else {
192 snprintf(temp, sizeof(temp), "0x%lx", (unsigned long)value);
193 }
194 if (!s->addString(temp)) {
195 return false;
196 }
197
198 return s->addXMLEndTag("integer");
1c79356b 199}