]>
git.saurik.com Git - apple/xnu.git/blob - libkern/os/hash.h
2 * Copyright (c) 2018 Apple 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@
38 * @function os_hash_jenkins
41 * The original Jenkins "one at a time" hash.
44 * TBD: There may be some value to unrolling here,
45 * depending on the architecture.
48 * The address of the data to hash.
51 * The length of the data to hash
54 * The jenkins hash for this data.
56 static inline uint32_t
57 os_hash_jenkins(const void *data
, size_t length
)
59 const uint8_t *key
= (const uint8_t *)data
;
62 for (size_t i
= 0; i
< length
; i
++) {
76 * @function os_hash_kernel_pointer
79 * Hashes a pointer from a zone.
82 * This is a really cheap and fast hash that will behave well for pointers
83 * allocated by the kernel.
85 * This should be not used for untrusted pointer values from userspace,
86 * or cases when the pointer is somehow under the control of userspace.
88 * This hash function utilizes knowledge about the span of the kernel
89 * address space and inherent alignment of zalloc/kalloc.
92 * The pointer to hash.
95 * The hash for this pointer.
97 static inline uint32_t
98 os_hash_kernel_pointer(const void *pointer
)
100 uintptr_t key
= (uintptr_t)pointer
>> 4;
102 return (uint32_t)key
^ __builtin_bswap32((uint32_t)key
);
108 #endif // _OS_HASH_H_