master
1/*
2 * Copyright (c) 2018-2023 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24#ifndef _MALLOC_UNDERSCORE_MALLOC_H_
25#define _MALLOC_UNDERSCORE_MALLOC_H_
26
27/*
28 * This header is included from <stdlib.h>, so the contents of this file have
29 * broad source compatibility and POSIX conformance implications.
30 * Be cautious about what is included and declared here.
31 */
32
33#include <Availability.h>
34#include <sys/cdefs.h>
35#if __has_include(<sys/_types/_size_t.h>)
36#include <sys/_types/_size_t.h>
37#else
38#define __need_size_t
39#include <stddef.h>
40#undef __need_size_t
41#endif
42
43#if !defined(_ANSI_SOURCE) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))
44#include <malloc/_malloc_type.h>
45#else
46#define _MALLOC_TYPED(override, type_param_pos)
47#endif
48
49#include <malloc/_ptrcheck.h>
50__ptrcheck_abi_assume_single()
51
52__BEGIN_DECLS
53
54void * __sized_by_or_null(__size) malloc(size_t __size) __result_use_check __alloc_size(1) _MALLOC_TYPED(malloc_type_malloc, 1);
55void * __sized_by_or_null(__count * __size) calloc(size_t __count, size_t __size) __result_use_check __alloc_size(1,2) _MALLOC_TYPED(malloc_type_calloc, 2);
56void free(void * __unsafe_indexable);
57void * __sized_by_or_null(__size) realloc(void * __unsafe_indexable __ptr, size_t __size) __result_use_check __alloc_size(2) _MALLOC_TYPED(malloc_type_realloc, 2);
58#if !defined(_ANSI_SOURCE) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))
59void * __sized_by_or_null(__size) reallocf(void * __unsafe_indexable __ptr, size_t __size) __result_use_check __alloc_size(2);
60void * __sized_by_or_null(__size) valloc(size_t __size) __result_use_check __alloc_size(1) _MALLOC_TYPED(malloc_type_valloc, 1);
61#endif /* !defined(_ANSI_SOURCE) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)) */
62#if (defined(__DARWIN_C_LEVEL) && defined(__DARWIN_C_FULL) && __DARWIN_C_LEVEL >= __DARWIN_C_FULL) || \
63 (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || \
64 (defined(__cplusplus) && __cplusplus >= 201703L)
65void * __sized_by_or_null(__size) aligned_alloc(size_t __alignment, size_t __size) __result_use_check __alloc_align(1) __alloc_size(2) _MALLOC_TYPED(malloc_type_aligned_alloc, 2) __OSX_AVAILABLE(10.15) __IOS_AVAILABLE(13.0) __TVOS_AVAILABLE(13.0) __WATCHOS_AVAILABLE(6.0);
66#endif
67/* rdar://120689514 */
68int posix_memalign(void * __unsafe_indexable *__memptr, size_t __alignment, size_t __size) _MALLOC_TYPED(malloc_type_posix_memalign, 3) __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_0);
69
70#if defined(_MALLOC_TYPE_MALLOC_IS_BACKDEPLOYING) && _MALLOC_TYPE_MALLOC_IS_BACKDEPLOYING
71static void * __sized_by_or_null(size) __attribute__((always_inline)) malloc_type_malloc_backdeploy(size_t size, malloc_type_id_t type_id) __result_use_check __alloc_size(1) {
72 __attribute__((weak_import)) void * __sized_by_or_null(size) malloc_type_malloc(size_t size, malloc_type_id_t type_id) __result_use_check __alloc_size(1);
73 __auto_type func = malloc;
74 if (malloc_type_malloc) {
75 return malloc_type_malloc(size, type_id);
76 }
77 return func(size);
78}
79
80static void * __sized_by_or_null(count * size) __attribute__((always_inline)) malloc_type_calloc_backdeploy(size_t count, size_t size, malloc_type_id_t type_id) __result_use_check __alloc_size(1,2) {
81 __attribute__((weak_import)) void * __sized_by_or_null(count * size) malloc_type_calloc(size_t count, size_t size, malloc_type_id_t type_id) __result_use_check __alloc_size(1,2);
82 __auto_type func = calloc;
83 if (malloc_type_calloc) {
84 return malloc_type_calloc(count, size, type_id);
85 }
86 return func(count, size);
87}
88
89static void __attribute__((always_inline)) malloc_type_free_backdeploy(void * __unsafe_indexable ptr, malloc_type_id_t type_id) {
90 __attribute__((weak_import)) void malloc_type_free(void * __unsafe_indexable ptr, malloc_type_id_t type_id);
91 __auto_type func = free;
92 if (malloc_type_free) {
93 malloc_type_free(ptr, type_id);
94 } else {
95 func(ptr);
96 }
97}
98
99static void * __sized_by_or_null(size) __attribute__((always_inline)) malloc_type_realloc_backdeploy(void * __unsafe_indexable ptr, size_t size, malloc_type_id_t type_id) __result_use_check __alloc_size(2) {
100 __attribute__((weak_import)) void * __sized_by_or_null(size) malloc_type_realloc(void * __unsafe_indexable ptr, size_t size, malloc_type_id_t type_id) __result_use_check __alloc_size(2);
101 __auto_type func = realloc;
102 if (malloc_type_realloc) {
103 return malloc_type_realloc(ptr, size, type_id);
104 }
105 return func(ptr, size);
106}
107
108static void * __sized_by_or_null(size) __attribute__((always_inline)) malloc_type_valloc_backdeploy(size_t size, malloc_type_id_t type_id) __result_use_check __alloc_size(1) {
109 __attribute__((weak_import)) void * __sized_by_or_null(size) malloc_type_valloc(size_t size, malloc_type_id_t type_id) __result_use_check __alloc_size(1);
110 __auto_type func = valloc;
111 if (malloc_type_valloc) {
112 return malloc_type_valloc(size, type_id);
113 }
114 return func(size);
115}
116
117#if (defined(__DARWIN_C_LEVEL) && defined(__DARWIN_C_FULL) && __DARWIN_C_LEVEL >= __DARWIN_C_FULL) || \
118 (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || \
119 (defined(__cplusplus) && __cplusplus >= 201703L)
120static void * __sized_by_or_null(size) __attribute__((always_inline)) malloc_type_aligned_alloc_backdeploy(size_t alignment, size_t size, malloc_type_id_t type_id) __result_use_check __alloc_align(1) __alloc_size(2) {
121 __attribute__((weak_import)) void * __sized_by_or_null(size) malloc_type_aligned_alloc(size_t alignment, size_t size, malloc_type_id_t type_id) __result_use_check __alloc_align(1) __alloc_size(2);
122 __auto_type func = aligned_alloc;
123 if (malloc_type_aligned_alloc) {
124 return malloc_type_aligned_alloc(alignment, size, type_id);
125 }
126 return func(alignment, size);
127}
128#endif
129
130static int __attribute__((always_inline)) malloc_type_posix_memalign_backdeploy(void * __unsafe_indexable *memptr, size_t alignment, size_t size, malloc_type_id_t type_id) {
131 __attribute__((weak_import)) int malloc_type_posix_memalign(void * __unsafe_indexable *memptr, size_t alignment, size_t size, malloc_type_id_t type_id);
132 __auto_type func = posix_memalign;
133 if (malloc_type_posix_memalign) {
134 return malloc_type_posix_memalign(memptr, alignment, size, type_id);
135 }
136 return func(memptr, alignment, size);
137}
138#endif
139__END_DECLS
140
141#endif /* _MALLOC_UNDERSCORE_MALLOC_H_ */