Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | /* * Copyright (c) 2020 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ #include <sys/types.h> #include "collections_utilities.h" #define opaque_os_set_t IN_SET(,_t) #define _os_set_data_segregated IN_SET(_,_data_segregated) struct _os_set_data_segregated { os_set_insert_val_t *vals; }; #define _os_com_data_ref_t IN_SET(_,_data_ref_t) typedef struct _os_set_data_segregated _os_com_data_ref_t; #define _alloc_data IN_SET(_,_alloc_data) static inline void * _alloc_data(uint32_t size) { assert(size < UINT32_MAX); void *result = calloc(size, sizeof(os_set_insert_val_t)); assert(result != NULL); return result; } #define _get_data_ref IN_SET(_,_get_data_ref) static inline void _get_data_ref(void *data_pointer, uint32_t size, _os_com_data_ref_t *data) { data->vals = (os_set_insert_val_t *)data_pointer; } #define _free_data IN_SET(_,_free_data) static inline void _free_data(_os_com_data_ref_t *data) { free((void *)data->vals); } // For sets - the 'key' and 'val' used by the common implementation are the // same value. #define _get_key(data, i) data.vals[i] #define _get_val(data, i) data.vals[i] #define _set_key(data, i, key) (void)data #define _set_val(data, i, val) data.vals[i] = val #define IN_COMMON(X,Y) IN_SET(X,Y) #define os_com_key_t os_set_insert_val_t #define os_com_config_t os_set_config_t #define opaque_os_com_t opaque_os_set_t #include "collections_common.in.c" #define os_set_init IN_SET(,_init) void os_set_init(opaque_os_set_t *s_raw, os_set_config_t *config, int struct_version) { os_common_init(s_raw, config, struct_version); } #define os_set_destroy IN_SET(,_destroy) void os_set_destroy(opaque_os_set_t *s_raw) { os_common_destroy(s_raw); } #define os_set_insert IN_SET(,_insert) void os_set_insert(opaque_os_set_t *s, os_set_insert_val_t val) { os_common_insert(s, val, val); } #define os_set_find IN_SET(,_find) void * _Nullable os_set_find(opaque_os_set_t *s, os_set_find_val_t val) { return os_common_find(s, &val); } #define os_set_delete IN_SET(,_delete) void * _Nullable os_set_delete(opaque_os_set_t *s, os_set_find_val_t val) { return os_common_delete(s, &val); } #define os_set_payload_handler_t IN_SET(,_payload_handler_t) typedef bool (^os_set_payload_handler_t) (os_set_insert_val_t); #define os_set_clear IN_SET(,_clear) void os_set_clear(opaque_os_set_t *s, OS_NOESCAPE os_set_payload_handler_t handler) { if (handler == NULL) { os_common_clear(s, NULL); } else { os_common_clear(s, ^bool (os_set_insert_val_t val, __unused void *p){ return handler(val); }); } } #define os_set_count IN_SET(,_count) size_t os_set_count(opaque_os_set_t *s) { return os_common_count(s); } #define os_set_foreach IN_SET(,_foreach) void os_set_foreach(opaque_os_set_t *s, OS_NOESCAPE os_set_payload_handler_t handler) { os_common_foreach(s, ^bool (os_set_insert_val_t val, __unused void *p){ return handler(val); }); } #undef opaque_os_com_t #undef os_com_config_t #undef os_com_key_t #undef IN_COMMON |