Skip to content

Commit

Permalink
Support swift 4.1.
Browse files Browse the repository at this point in the history
  • Loading branch information
kawoou committed Apr 10, 2018
1 parent fb6a4a7 commit add9345
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,11 @@ class ViewController: NSViewController, NSComboBoxDataSource {
self.sizeCG.stringValue = "\(cgSize.width)x\(cgSize.height)"
}
@IBAction func resetClicked(_ sender: NSButton) {
#if swift(>=4.0)
let image = NSImage(named: NSImage.Name("macaron.jpg"))
#else
let image = NSImage(named: "macaron.jpg")
#endif

self.imageMetal.image = image
self.imageCG.image = image
Expand Down
16 changes: 14 additions & 2 deletions Sources/Device/ImageMetalDevice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@
/// Alloc Memory
let memorySize = width * height * 4
let memoryPool = UnsafeMutablePointer<UInt8>.allocate(capacity: memorySize)
defer { memoryPool.deallocate(capacity: memorySize) }
defer {
#if swift(>=4.1)
memoryPool.deallocate()
#else
memoryPool.deallocate(capacity: memorySize)
#endif
}
memset(memoryPool, 0, memorySize)

/// Create Context
Expand Down Expand Up @@ -176,7 +182,13 @@

let memorySize = width * height * 4
let memoryPool = UnsafeMutablePointer<UInt8>.allocate(capacity: memorySize)
defer { memoryPool.deallocate(capacity: memorySize) }
defer {
#if swift(>=4.1)
memoryPool.deallocate()
#else
memoryPool.deallocate(capacity: memorySize)
#endif
}

texture.getBytes(
memoryPool,
Expand Down
8 changes: 8 additions & 0 deletions Sources/Device/ImageNoneDevice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ internal class ImageNoneDevice: ImageDevice {
/// Push Context
guard let context = self.context else {
self.drawRect = nil
#if swift(>=4.1)
self.memoryPool!.deallocate()
#else
self.memoryPool!.deallocate(capacity: self.memorySize!)
#endif

self.memorySize = nil
self.memoryPool = nil
Expand Down Expand Up @@ -138,7 +142,11 @@ internal class ImageNoneDevice: ImageDevice {
guard let context = self.context else { return nil }
defer {
self.drawRect = nil
#if swift(>=4.1)
self.memoryPool!.deallocate()
#else
self.memoryPool!.deallocate(capacity: self.memorySize!)
#endif

self.memorySize = nil
self.memoryPool = nil
Expand Down
16 changes: 14 additions & 2 deletions Sources/Filter/BlurFilter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@
}

let rawData = UnsafeMutablePointer<Float>.allocate(capacity: size * size)
defer { rawData.deallocate(capacity: size * size) }
defer {
#if swift(>=4.1)
rawData.deallocate()
#else
rawData.deallocate(capacity: size * size)
#endif
}

var weightSum = Float(0)
var y = -self.radius
Expand Down Expand Up @@ -183,7 +189,13 @@
/// Alloc Memory
let destMemorySize = width * height * 4
let destMemoryPool = UnsafeMutablePointer<UInt8>.allocate(capacity: destMemorySize)
defer { destMemoryPool.deallocate(capacity: destMemorySize) }
defer {
#if swift(>=4.1)
destMemoryPool.deallocate()
#else
destMemoryPool.deallocate(capacity: destMemorySize)
#endif
}
memcpy(destMemoryPool, memoryPool, destMemorySize)

/// Destination Buffer
Expand Down
8 changes: 7 additions & 1 deletion Sources/Filter/TextureAppendFilter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ internal class TextureAppendFilter: ImageFilter {
let memorySize = bytesPerRow * height

let memoryPool = UnsafeMutablePointer<UInt8>.allocate(capacity: memorySize)
defer { memoryPool.deallocate(capacity: memorySize) }
defer {
#if swift(>=4.1)
memoryPool.deallocate()
#else
memoryPool.deallocate(capacity: memorySize)
#endif
}
memset(memoryPool, 0, memorySize)

/// Create Context
Expand Down

0 comments on commit add9345

Please sign in to comment.