Skip to content

Instantly share code, notes, and snippets.

@bdash
bdash / direntrycount.m
Created November 26, 2021 04:25
getattrlist / ATTR_DIR_ENTRYCOUNT
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <sys/attr.h>
#include <sys/errno.h>
#include <unistd.h>
#include <sys/vnode.h>
struct DirEntryCount {
u_int32_t length;
@bdash
bdash / IntentionalRetainCycle.m
Created January 5, 2020 17:44
Demonstrate a case of a spurious warning about an instance being immediately deallocated
#import <Foundation/Foundation.h>
@interface RetainCycle : NSObject
@end
@implementation RetainCycle {
id _object;
}
- (instancetype)init {
@bdash
bdash / dynamic_cast.mm
Last active July 29, 2020 03:09
Excerpt from Hopper pseudocode from __dynamic_cast in libc++abi.dylib
if ((*(int8_t *)guard variable for __dynamic_cast::use_strcmp == 0x0) && (___cxa_guard_acquire(guard variable for __dynamic_cast::use_strcmp, rsi, rdx, rcx, r8, r9) != 0x0)) {
rsi = *_NSGetProgname();
rcx = 0x1;
if (strcmp("Adobe Illustrator", rsi) != 0x0) {
rsi = *_NSGetProgname();
rdx = 0x13;
rcx = strncmp("Adobe Photoshop CS5", rsi, rdx) == 0x0 ? 0x1 : 0x0;
}
*(int8_t *)__dynamic_cast::use_strcmp = rcx;
___cxa_guard_release(guard variable for __dynamic_cast::use_strcmp, rsi, rdx, rcx, r8, r9);
@bdash
bdash / MY_ERROR_ENUM.m
Last active August 1, 2016 22:51
Implementation of MY_ERROR_ENUM
// SE-0112 mentions an NS_ERROR_ENUM macro to convey the association between an error code enumeration and
// its associated error domain. Xcode 8 beta 4 includes the Swift changes from SE-0112, but does not appear
// to provide NS_ERROR_ENUM.
//
// NS_ERROR_ENUM is intended to expand to an enum with the ns_error_domain(…) attribute.
// Writing our own macro is complicated by the fact that the Swift Objective-C importer
// for Swift 2.3 gives special importing treatment to enums declared using NS_ENUM / CF_ENUM.
// This requires some hoop-jumping to ensure that the enum declared by our macro is handled
// nicely by both Swift 2.3 and Swift 3.0.
@bdash
bdash / generics.m
Last active March 13, 2018 22:06
Using Objective-C generics with type constraints on forward-declared classes?
#import <Foundation/Foundation.h>
@interface Base : NSObject
@end
@interface GenericCollection<T: Base *> : NSObject
@end
@class Derived2;
@bdash
bdash / dladdr-lambda-test.mm
Created October 20, 2015 19:30
Test of backtrace and dladdr with C++ lambdas
// c++ -framework Foundation -std=c++14 -o dladdr-lambda-test dladdr-lambda-test.mm
#import <Foundation/Foundation.h>
#include <cxxabi.h>
#include <dlfcn.h>
#include <execinfo.h>
@interface MyObject : NSObject
@end
@bdash
bdash / javascriptcore-constructor-test.m
Last active July 31, 2018 05:53
Expose an Objective-C class to JavaScriptCore, with automatic JavaScript constructor generation.
// cc -framework JavaScriptCore -fobjc-arc -o javascriptcore-constructor-test javascriptcore-constructor-test.m
#include <JavaScriptCore/JavaScriptCore.h>
@protocol TestInterface <JSExport>
- (instancetype)initWithString:(NSString *)string;
@property (readonly) NSString *string;