Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Pointer と向き合う / Facing the Pointer

Pointer と向き合う / Facing the Pointer

登壇者名:Masatoshi Tsushima
登壇したイベント:Swift Expands! #2 ~組み込みシステムに挑戦してみよう~
         (https://bitkey.connpass.com/event/317143/
登壇したイベントの登壇者URL:https://www.youtube.com/live/krNTPlRP8kc?feature=shared

More Decks by 株式会社ビットキー / Bitkey Inc.

Other Decks in Technology

Transcript

  1. Copyright © Bitkey Inc. All rights reserved. ௡ౡ խढ़ Masatoshi

    Tsushima 2004 ιϑτ΢ΣΞ։ൃͱͷग़ձ͍ WebαΠτ΍ܞଳΞϓϦΛ࡞ͬͯ༡ΜͰ͍·ͨ͠ 2018ळ Bitkeyʹࢀը ౰ॳ͸bitkey platformͷ։ൃΛ୲౰ ओʹGoͰαʔόαΠυΛ։ൃ 2019ळ ϑΝʔϜ΢ΣΞνʔϜΛ্ཱͪ͛ bitlock LITEͷϑΝʔϜ΢ΣΞΛ಺੡Խ ݱࡏʹଓ͘։ൃελΠϧΛߏங 2020Ն ୈೋੈ୅ϩοΫσόΠεͷ։ൃ ಺੡Λલఏͱͨ͠ϑΝʔϜ΢ΣΞ։ൃ 2021य़ϦϦʔε 2022Ն Individual contributor (?) Android/iOS/FlutterɺσόΠεQA༻ΞϓϦɺࣾ಺ ࢓༷ͷඪ४ԽɺMakefile৬ਓɺCI͓͡͞Μ
  2. Copyright © Bitkey Inc. All rights reserved. C Ҏ֎Ͱݟͳ͍ͩΖͱࢥͬͯ·ͨ͠ ૊ࠐΈ͔Β

    iOS ։ൃʹҟಈ͢Δ·Ͱ͸ var x = 1 let p = UnsafePointer(&x) print("\(p.pointee), \(p)")
  3. Copyright © Bitkey Inc. All rights reserved. Q: ࣮ࡍͷॴϙΠϯλͬͯԿʹ࢖͏ͷʁ A:

    ओʹόΠτྻͷૢ࡞ • C ݴޠͷ഑ྻ • Swift ͷ Array/Data ಡΈସ͑ɾॻ͖׵͑ɾసૹɾಠࣗͷ؅ཧ…
  4. Copyright © Bitkey Inc. All rights reserved. Data ͔Β Int

    ʹ͍ͨ͠ ݕࡧ͢ΔͱΑ͘ग़ͯ͘Δ΍ͭ import Foundation let data = Data([1, 2, 3, 4]) let x = data.withUnsafeBytes { $0.load(as: UInt16.self) } print(x) // Output: 513
  5. Copyright © Bitkey Inc. All rights reserved. Data ͔Β Int

    ʹ͍ͨ͠ load ͷ୅ΘΓʹ OpaquePointer Λܦ༝ͤͯ͞Έͨ import Foundation let data = Data([1, 2, 3, 4]) let x = data.withUnsafeBytes { UnsafePointer<UInt16>(OpaquePointer($0.baseAddress!)).pointee } print(x) // Output: 513 खݩͰ͸ -O -emit-assembly Ͱಉ݁͡Ռ
  6. Copyright © Bitkey Inc. All rights reserved. ͪͳΈʹ: Swift ͷΤϯσΟΞϯͷૢ࡞

    λʔήοτͱҟͳΔͳΒٯॱʹ͢Δ print(x) // Apple Silicon ͸ 513 print(x.bigEndian) // 258 print(x.littleEndian) // 513 print(x.bigEndian.bigEndian) // 513 print(x.littleEndian.littleEndian) // 513
  7. Copyright © Bitkey Inc. All rights reserved. ༷ʑͳܕ • UnsafePointer<T>

    • UnsafeMutablePointer<T> • UnsafeRawPointer • UnsafeMutableRawPointer • UnsafeBufferPointer<T> • UnsafeMutableBufferPointer<T> • UnsafeRawBufferPointer • UnsafeMutableRawBufferPointer Mutable ॻ͖׵͑Մೳ Buffer ௕͞ (count) ෇͖ Raw ૯শܕ (<T>) ແ͠
  8. Copyright © Bitkey Inc. All rights reserved. Unsafe ͱ͸ʁ •

    UnsafePointer<T> • UnsafeMutablePointer<T> • UnsafeRawPointer • UnsafeMutableRawPointer • UnsafeBufferPointer<T> • UnsafeMutableBufferPointer<T> • UnsafeRawBufferPointer • UnsafeMutableRawBufferPointer
  9. Copyright © Bitkey Inc. All rights reserved. C ͷͱ͖Λࢥ͍ग़ͯ͠ΈΔ ࡾେҙຯෆ໌ͳڍಈ͢ΔόάͷݪҼ

    • ະ֬อ ◦ ࢦࣔ͢͠ઌ͕֬อ͞Ε͍ͯͳ͍͔΋ • ະॳظԽ ◦ ॳظԽ͞Ε͍ͯͳ͍͔΋ • ະ։์ ◦ ։์͞Εͳ͍͔΋
  10. Copyright © Bitkey Inc. All rights reserved. ະ֬อ ֬อͤͣʹ࢖͏ var

    x: Int = 10 let p = UnsafePointer<Int>(&x) print(p.advanced(by: 10).pointee)
  11. Copyright © Bitkey Inc. All rights reserved. ະ֬อ ֬อͤͣʹ࢖͏ var

    x: Int = 10 let p = UnsafePointer<Int>(&x) print(p.advanced(by: 10).pointee) ਖ਼ৗऴྃͪ͠Ό͏Μͩͳ͜Ε͕…
  12. Copyright © Bitkey Inc. All rights reserved. ະ֬อ ൣғ֎ʹΞΫηε͢Δ import

    Foundation let data = Data([1, 2, 3, 4]) let x = data.withUnsafeBytes { let op = OpaquePointer($0.baseAddress!.advanced(by: 10)) return UnsafePointer<UInt16>(op).pointee } print(x) load ͷ count νΣοΫΛආ͚Δͱਖ਼ৗऴྃ͢Δ
  13. Copyright © Bitkey Inc. All rights reserved. ະ։์ deallocate ͠ͳ͍

    for _ in 1...1_000_000 { let _ = UnsafeMutablePointer<Int> .allocate(capacity: 1_000) }
  14. Copyright © Bitkey Inc. All rights reserved. ؀ڥม਺ MallocScribble=1 ֬อͨ͠ϝϞϦʹॻ͖ࠐΉ

    ։์͞Εͨ஋ʹґଘ͠ͳ͍Α͏ʹ ద౰ͳ஋ (0x55) Λॻ͖ࠐΉ
  15. Copyright © Bitkey Inc. All rights reserved. ·ͱΊ: Swift ͷ

    Pointer • Data ͱ Int ͷ૬ޓม׵͕୅දྫ • Ͳͷ͋ͨΓ͕ Unsafe ʁ ◦ ະ֬อ: ࢦࣔ͢͠ઌ͕֬อ͞Ε͍ͯͳ͍͔΋ ◦ ະॳظԽ: ॳظԽ͞Ε͍ͯͳ͍͔΋ ◦ ະ։์: ։์͞Εͳ͍͔΋ • ਖ਼͘͠࢖Θͳ͍ͱͲ͏ͳΔʁ ◦ ίϯύΠϧ͸௨Δ ◦ Ϋϥογϡͨ͠Γ͠ͳ͔ͬͨΓϝϞϦΛ৯͍ͭͿͨ͠Γ͢Δ
  16. Copyright © Bitkey Inc. All rights reserved. ࢀߟจݙ • Safely

    manage pointers in Swift - WWWDC20 • [Swift] IntͱDataͷ૬ޓม׵ #Swift - Qiita • ʲSwift5ʳDataΛInt΍DoubleͳͲͱ૬ޓม׵͢Δํ๏ – Ώ͖ͬͿΖ͙ • ios - how to convert Data to Int in swift - Stack Overflow • ૉ੖Β͍͠SwiftͷϙΠϯλܕͷղઆ #Swift - Qiita