Skip to content

Instantly share code, notes, and snippets.

View dmaclach's full-sized avatar

dmaclach dmaclach

View GitHub Profile
@dmaclach
dmaclach / main.m
Last active December 3, 2022 22:49
Apple Archive Example In Objective-C
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *dst =
[NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/file.aar"];
AAByteStream fileStream = AAFileStreamOpenWithPath(dst.UTF8String,
O_CREAT | O_WRONLY, 0644);
AAByteStream compressionStream = AACompressionOutputStreamOpen(fileStream,
AA_COMPRESSION_ALGORITHM_LZFSE, 1<<20, AA_FLAG_VERBOSITY_3, 0);
AAArchiveStream encodeStream = AAEncodeArchiveOutputStreamOpen(compressionStream,
@dmaclach
dmaclach / MisalignedRead.m
Last active August 9, 2020 16:44
Compile this with Xcode 11 "release" and it will crash on ARMv7 due to misaligned read. Use a "single view" iOS App as your template, and replace AppDelegate.m with this code.
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@end
__attribute__((noinline)) double GPBCodedInputStreamReadDouble(uint8_t *buf) {
int64_t value = OSReadLittleInt64(buf, 0);
double result;
memcpy(&result, &value, sizeof(result));
return result;
<?xml version="1.0" encoding="UTF-8" ?>
<package>
<id>com.google.ImageMapper</id>
<title>ImageMapper</title>
<owner>
<name>Dave MacLachlan</name>
</owner>
<import-schema>kdebug-strings</import-schema>
<ktrace-interval-schema>
<id>com-google-dyld-map-image-schema</id>
total time: 635.49 milliseconds (100.0%)
total images loaded: 457 (451 from dyld shared cache)
total segments mapped: 15, into 3756 pages with 408 pages pre-fetched
total images loading time: 376.31 milliseconds (59.2%)
total load time in ObjC: 64.57 milliseconds (10.1%)
total debugger pause time: 0.00 milliseconds (0.0%)
total dtrace DOF registration time: 0.11 milliseconds (0.0%)
total rebase fixups: 1,164,368
total rebase fixups time: 76.51 milliseconds (12.0%)
total binding fixups: 83,028
#include <sys/sysctl.h>
struct timeval AppLaunchTimeRelativeTo1970(void) {
id_t pid = getpid();
int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, (int)pid };
const size_t mibSize = sizeof(mib) / sizeof(mib[0]);
size_t infoSize = 0;
// Get initial size of KERN_PROC data structure.
if (sysctl(mib, mibSize, NULL, &infoSize, NULL, 0) != 0) {
@dmaclach
dmaclach / property_encoding.csv
Last active December 10, 2018 17:39
Objective C property encodings
code meaning
R read-only
C by-copy
& by-reference
D dynamic
G getter (followed by selector name)
S setter (followed by selector name)
V instance variable (followed by variable name)
T type encoding (followed by old-style type encoding)
W weak
/**
* Wraps C++ classes (ptrs and refs) so that they can be passed around in
* Objective-C++ programs without generating a lot of useless Objective-C
* runtime metadata.
* Use objc_metadata_hider_ptr for ptrs and objc_metadata_hider_ref for refs.
* Example use:
* ```
* class Foo {
* public:
* ....
@dmaclach
dmaclach / main.mm
Last active November 29, 2018 21:59
ObjectiveC wrapping C++
#import <Foundation/Foundation.h>
#include <map>
#include <string>
struct CppType {
std::map<std::string, std::string> myMap;
};
@interface ObjCType : NSObject {
CppType map;
@dmaclach
dmaclach / main.m
Last active November 29, 2018 05:55
Create an Objective C Class at runtime
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
Class myClass = objc_allocateClassPair([NSObject class], "MyClass", 0);
IMP methodIMP = imp_implementationWithBlock(^(id self) {
return @"Hello";
});
if (!class_addMethod(myClass, @selector(description), methodIMP, "@@:")) {
//
// main.m
// signaltest
//
// Created by Dave MacLachlan on 11/12/18.
// Copyright © 2018 Dave MacLachlan. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <pthread.h>