2 * Copyright (c) 2018 Apple Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 * Non-standard, Darwin-specific additions to the stdlib(3) family of APIs.
28 * The os_malloc() and os_strdup() routines are wrappers to be used for small,
29 * fixed-size allocations, the assumption being that such allocation should
30 * always succeed absent other critical problems. Thus, if the requested size is
31 * is a compile-time constant, the return value is asserted to be non-NULL.
32 * Otherwise, for sizes that are not known at compile-time, the implementations
33 * loop until the allocation succeeds, assuming the failure to be due to
34 * transient resource shortages. The implementation will not loop if the program
35 * has not become multi-threaded, under the assertion that there would be no
36 * point since no other thread could possibly free up resources for the calling
37 * thread to use. Thus, in a single-threaded program, all allocations will
38 * be treated like small, fixed-size allocations and be expected to succeed.
40 * These wrappers should not be used for large allocations whose bounds cannot
41 * be determined at compile-time. For such allocations, malloc(3), calloc(3), et
42 * al. (with appropriate error handling) are the appropriate interfaces.
44 #ifndef __DARWIN_STDLIB_H
45 #define __DARWIN_STDLIB_H
49 #include <os/assumes.h>
51 #include <dispatch/private.h>
54 #include <sys/types.h>
55 #include <sys/cdefs.h>
56 #include <sys/syslimits.h>
61 * @function __os_temporary_resource_shortage
62 * A function whose purpose is to appear in stack traces to indicate transient
63 * resource shortage conditions. Do not call.
65 DARWIN_API_AVAILABLE_20170407
66 OS_EXPORT OS_NOT_TAIL_CALLED
68 __os_temporary_resource_shortage(void);
72 * Internal inline definitions.
74 DARWIN_API_AVAILABLE_20170407
75 OS_ALWAYS_INLINE OS_WARN_RESULT OS_MALLOC
__alloc_size(1)
77 _os_malloc_loop(size_t size
)
80 while (!(ptr
= malloc(size
))) {
81 __os_temporary_resource_shortage();
86 DARWIN_API_AVAILABLE_20170407
87 OS_ALWAYS_INLINE OS_WARN_RESULT OS_MALLOC
__alloc_size(1)
89 _os_malloc_known(size_t size
)
94 DARWIN_API_AVAILABLE_20170407
95 OS_ALWAYS_INLINE OS_WARN_RESULT OS_MALLOC
__alloc_size(1, 2)
97 _os_calloc_loop(size_t cnt
, size_t size
)
100 while (!(ptr
= calloc(cnt
, size
))) {
101 __os_temporary_resource_shortage();
106 DARWIN_API_AVAILABLE_20170407
107 OS_ALWAYS_INLINE OS_WARN_RESULT OS_MALLOC
__alloc_size(1, 2)
109 _os_calloc_known(size_t cnt
, size_t size
)
111 return calloc(cnt
, size
);
114 DARWIN_API_AVAILABLE_20170407
115 OS_ALWAYS_INLINE OS_WARN_RESULT OS_MALLOC
117 _os_strdup_loop(const char *str
)
120 while (!(ptr
= strdup(str
))) {
121 __os_temporary_resource_shortage();
126 DARWIN_API_AVAILABLE_20170407
127 OS_ALWAYS_INLINE OS_WARN_RESULT OS_MALLOC
129 _os_strdup_known(const char *str
)
134 #define __os_requires_experimental_libtrace \
135 _Pragma("GCC error \"requires OS_CRASH_ENABLE_EXPERIMENTAL_LIBTRACE\"")
138 * @function os_malloc
139 * Wrapper around malloc(3) which guarantees that the allocation succeeds.
142 * The size of the allocation.
145 * A new object that the caller is responsible for free(3)ing.
147 * This routine will never return NULL. If the size of the allocation is known
148 * at compile-time, a failure to allocate the object will abort the caller. If
149 * the size is not known at compile-time, the routine will retry until it is
152 #if defined(OS_CRASH_ENABLE_EXPERIMENTAL_LIBTRACE)
153 #define os_malloc(__size) ({ \
155 size_t _size = (__size); \
156 if (__builtin_constant_p(__size) || !_dispatch_is_multithreaded()) { \
157 ptr = _os_malloc_known(_size); \
158 os_assert_malloc("known-constant allocation", ptr, _size); \
160 ptr = _os_malloc_loop(_size); \
165 #define os_malloc(__size) __os_requires_experimental_libtrace
169 * @function os_calloc
170 * Wrapper around calloc(3) which guarantees that the allocation succeeds.
173 * The number of elements to allocate.
176 * The size of each element to allocate.
179 * A new object that the caller is responsible for free(3)ing.
181 * This routine will never return NULL. If the size of the allocation is known
182 * at compile-time, a failure to allocate the object will abort the caller. If
183 * the size is not known at compile-time, the routine will retry until it is
186 #if defined(OS_CRASH_ENABLE_EXPERIMENTAL_LIBTRACE)
187 #define os_calloc(__cnt, __size) ({ \
189 size_t _size = (__size); \
190 size_t _cnt = (__size); \
191 if ((__builtin_constant_p(__cnt) && __builtin_constant_p(__size)) || \
192 !_dispatch_is_multithreaded()) { \
193 ptr = _os_calloc_known(_cnt, _size); \
194 os_assert_malloc("known-constant allocation", ptr, _size); \
196 ptr = _os_calloc_loop(_cnt, _size); \
201 #define os_calloc(__size) __os_requires_experimental_libtrace
205 * @function os_strdup
206 * A wrapper around strdup(3) which guarantees that the string duplication
210 * The string to duplicate.
213 * A new string that the caller is responsible for free(3)ing.
215 * This routine will never return NULL. If the given string is a compile-time
216 * constant, a failure to duplicate it will abort the caller. If the string is
217 * not a compile-time constant, the routine will retry until it is successful.
220 * strdup(3) is found in the string(3) API family, but this interface is in the
221 * stdlib.h header because its semantic changes are solely related to the manner
222 * in which memory is allocated.
224 #if defined(OS_CRASH_ENABLE_EXPERIMENTAL_LIBTRACE)
225 #define os_strdup(__str) ({ \
227 const char *_str = (__str); \
228 if (__builtin_constant_p(__str) || !_dispatch_is_multithreaded()) { \
229 ptr = _os_strdup_known(_str); \
230 os_assert_malloc("known-constant allocation", ptr, strlen(_str)); \
232 ptr = _os_strdup_loop(_str); \
237 #define os_strdup(__size) __os_requires_experimental_libtrace
241 * @function os_localtime_file
242 * A routine to generate a time stamp that is suitable for embedding in a file
246 * A pointer to a buffer where the resulting time stamp will be stored.
249 * The resulting time stamp will not include characters which require escaping
250 * in shells, such as spaces. The current implementation format is
252 * YYYY-MM-DD_HH.MM.SS.us
256 * 2017-04-24_12.45.15.045609
258 DARWIN_API_AVAILABLE_20170407
261 os_localtime_file(char buff
[static 32]);
264 * @function os_simple_hash
265 * An implementation of a simple non-cryptographic hashing algorithm.
268 * A pointer to the buffer to hash.
271 * The length of the buffer.
274 * The hashed value of the input.
277 * This routine is meant to be used as a simple way to obtain a value that can
278 * be used to choose a bucket in a simple hash table. Do not attach security
279 * assumptions to the output of this routine. Do not assume that the computed
280 * hash is stable between hosts, OS versions, or boot sessions.
282 DARWIN_API_AVAILABLE_20170407
283 OS_EXPORT OS_NONNULL1
285 os_simple_hash(const void *buff
, size_t len
);
288 * @function os_simple_hash_with_seed
289 * A seeded variant of os_simple_hash.
292 * A pointer to the buffer to hash.
295 * The length of the buffer.
298 * The seed value for the hash.
301 * The hashed value of the input.
304 * Usually, hashing the same buffer with different seeds will produce
305 * different hash values.
306 * All the same considerations of {@link os_simple_hash} apply.
308 DARWIN_API_AVAILABLE_20181020
309 OS_EXPORT OS_NONNULL1
311 os_simple_hash_with_seed(const void *buff
, size_t len
, uint64_t seed
);
314 * @function os_simple_hash_string
315 * An implementation of a simple non-cryptographic hashing algorithm.
318 * A pointer to the null-terminated string to hash.
321 * The hashed value of the input.
324 * This routine is the moral equivalent of a call to
326 * os_simple_hash(buff, strlen(buff));
328 * All the same considerations of {@link os_simple_hash} apply.
330 DARWIN_API_AVAILABLE_20170407
331 OS_EXPORT OS_NONNULL1
333 os_simple_hash_string(const char *string
);
336 * @function os_simple_hash_string_with_seed
337 * A seeded variant of os_simple_hash_string.
340 * A pointer to the null-terminated string to hash.
343 * The hashed value of the input.
346 * Usually, hashing the same string with different seeds will produce
347 * different hash values.
348 * All the same considerations of {@link os_simple_hash_string} apply.
350 DARWIN_API_AVAILABLE_20181020
351 OS_EXPORT OS_NONNULL1
353 os_simple_hash_string_with_seed(const char *string
, uint64_t seed
);
356 * @function realpath_np
357 * Obtains a fully-resolved representation of the path to the file represented
358 * by the given descriptor.
361 * The file descriptor whose path is to be obtained.
364 * The buffer in which to write the path.
367 * On success, zero is returned. Otherwise, the implementation may return any
368 * error that can be returned by fcntl(2).
370 DARWIN_API_AVAILABLE_20180727
371 OS_EXPORT OS_WARN_RESULT
373 realpath_np(os_fd_t fd
, char buff
[static PATH_MAX
]);
377 #endif // __DARWIN_STDLIB_H