]>
git.saurik.com Git - apple/objc4.git/blob - runtime/objc-zalloc.h
2 * Copyright (c) 2019 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@
27 * "zone allocator" for objc.
29 * Provides packed allocation for data structures the runtime
33 #ifndef _OBJC_ZALLOC_H
34 #define _OBJC_ZALLOC_H
42 // Darwin malloc always aligns to 16 bytes
43 #define MALLOC_ALIGNMENT 16
47 using pair_t
= __int128_t
;
49 using pair_t
= uint64_t;
51 static constexpr auto relaxed
= std::memory_order_relaxed
;
52 static constexpr auto release
= std::memory_order_release
;
63 std::atomic
<pair_t
> atomic_pair
;
69 void push_list(void *_head
, void *_tail
);
70 inline void push(void *head
)
72 push_list(head
, head
);
76 template<class T
, bool useMalloc
>
81 class Zone
<T
, false> {
84 char buf
[sizeof(T
) - sizeof(void *)];
85 } __attribute__((packed
));
87 static AtomicQueue _freelist
;
88 static T
*alloc_slow();
92 static void free(T
*);
98 static inline T
*alloc() {
99 return reinterpret_cast<T
*>(::calloc(sizeof(T
), 1));
101 static inline void free(T
*ptr
) {
107 * This allocator returns always zeroed memory,
108 * and the template needs to be instantiated in objc-zalloc.mm
114 return Zone
<T
, sizeof(T
) % MALLOC_ALIGNMENT
== 0>::alloc();
120 Zone
<T
, sizeof(T
) % MALLOC_ALIGNMENT
== 0>::free(e
);