]>
git.saurik.com Git - apple/xnu.git/blob - libkern/c++/OSNumber.cpp
2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* IOOffset.m created by rsulack on Wed 17-Sep-1997 */
30 #define IOKIT_ENABLE_SHARED_PTR
32 #include <sys/cdefs.h>
34 #include <libkern/c++/OSNumber.h>
35 #include <libkern/c++/OSString.h>
36 #include <libkern/c++/OSSerialize.h>
37 #include <libkern/c++/OSSharedPtr.h>
38 #include <libkern/c++/OSLib.h>
40 #define sizeMask (~0ULL >> (64 - size))
42 #define super OSObject
44 OSDefineMetaClassAndStructorsWithZone(OSNumber
, OSObject
,
45 (zone_create_flags_t
) (ZC_CACHING
| ZC_ZFREE_CLEARMEM
))
47 OSMetaClassDefineReservedUnused(OSNumber
, 0);
48 OSMetaClassDefineReservedUnused(OSNumber
, 1);
49 OSMetaClassDefineReservedUnused(OSNumber
, 2);
50 OSMetaClassDefineReservedUnused(OSNumber
, 3);
51 OSMetaClassDefineReservedUnused(OSNumber
, 4);
52 OSMetaClassDefineReservedUnused(OSNumber
, 5);
53 OSMetaClassDefineReservedUnused(OSNumber
, 6);
54 OSMetaClassDefineReservedUnused(OSNumber
, 7);
57 OSNumber::init(unsigned long long inValue
, unsigned int newNumberOfBits
)
62 if (newNumberOfBits
> 64) {
66 size
= newNumberOfBits
;
67 value
= (inValue
& sizeMask
);
73 OSNumber::init(const char *newValue
, unsigned int newNumberOfBits
)
75 return init((unsigned long long)strtoul(newValue
, NULL
, 0), newNumberOfBits
);
85 OSNumber::withNumber(unsigned long long value
,
86 unsigned int newNumberOfBits
)
88 OSSharedPtr
<OSNumber
> me
= OSMakeShared
<OSNumber
>();
90 if (me
&& !me
->init(value
, newNumberOfBits
)) {
98 OSNumber::withNumber(const char *value
, unsigned int newNumberOfBits
)
100 OSSharedPtr
<OSNumber
> me
= OSMakeShared
<OSNumber
>();
102 if (me
&& !me
->init(value
, newNumberOfBits
)) {
110 OSNumber::numberOfBits() const
116 OSNumber::numberOfBytes() const
118 return (size
+ 7) / 8;
123 OSNumber::unsigned8BitValue() const
125 return (unsigned char) value
;
129 OSNumber::unsigned16BitValue() const
131 return (unsigned short) value
;
135 OSNumber::unsigned32BitValue() const
137 return (unsigned int) value
;
141 OSNumber::unsigned64BitValue() const
147 OSNumber::addValue(signed long long inValue
)
149 value
= ((value
+ inValue
) & sizeMask
);
153 OSNumber::setValue(unsigned long long inValue
)
155 value
= (inValue
& sizeMask
);
159 OSNumber::isEqualTo(const OSNumber
*integer
) const
161 return value
== integer
->value
;
165 OSNumber::isEqualTo(const OSMetaClassBase
*obj
) const
168 if ((offset
= OSDynamicCast(OSNumber
, obj
))) {
169 return isEqualTo(offset
);
176 OSNumber::serialize(OSSerialize
*s
) const
180 if (s
->previouslySerialized(this)) {
184 snprintf(temp
, sizeof(temp
), "integer size=\"%d\"", size
);
185 if (!s
->addXMLStartTag(this, temp
)) {
189 //XXX sprintf(temp, "0x%qx", value);
191 snprintf(temp
, sizeof(temp
), "0x%lx%08lx", (unsigned long)(value
>> 32),
192 (unsigned long)(value
& 0xFFFFFFFF));
194 snprintf(temp
, sizeof(temp
), "0x%lx", (unsigned long)value
);
196 if (!s
->addString(temp
)) {
200 return s
->addXMLEndTag("integer");