├── .gitignore ├── LICENSE ├── Mems.xcodeproj ├── project.pbxproj └── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ └── IDEWorkspaceChecks.plist ├── Mems ├── Mems.swift └── main.swift └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xccheckout 23 | *.xcscmblueprint 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | *.ipa 28 | *.dSYM.zip 29 | *.dSYM 30 | 31 | ## Playgrounds 32 | timeline.xctimeline 33 | playground.xcworkspace 34 | 35 | # Swift Package Manager 36 | # 37 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 38 | # Packages/ 39 | # Package.pins 40 | # Package.resolved 41 | .build/ 42 | 43 | # CocoaPods 44 | # 45 | # We recommend against adding the Pods directory to your .gitignore. However 46 | # you should judge for yourself, the pros and cons are mentioned at: 47 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 48 | # 49 | # Pods/ 50 | 51 | # Carthage 52 | # 53 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 54 | # Carthage/Checkouts 55 | 56 | Carthage/Build 57 | 58 | # fastlane 59 | # 60 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 61 | # screenshots whenever they are needed. 62 | # For more information about the recommended setup visit: 63 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 64 | 65 | fastlane/report.xml 66 | fastlane/Preview.html 67 | fastlane/screenshots/**/*.png 68 | fastlane/test_output 69 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 M了个J 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Mems.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 2DB1C7BE22BDF63600199584 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DB1C7BD22BDF63600199584 /* main.swift */; }; 11 | 2DB1C7C522BDF65300199584 /* Mems.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DB1C7C422BDF65300199584 /* Mems.swift */; }; 12 | /* End PBXBuildFile section */ 13 | 14 | /* Begin PBXCopyFilesBuildPhase section */ 15 | 2DB1C7B822BDF63600199584 /* CopyFiles */ = { 16 | isa = PBXCopyFilesBuildPhase; 17 | buildActionMask = 2147483647; 18 | dstPath = /usr/share/man/man1/; 19 | dstSubfolderSpec = 0; 20 | files = ( 21 | ); 22 | runOnlyForDeploymentPostprocessing = 1; 23 | }; 24 | /* End PBXCopyFilesBuildPhase section */ 25 | 26 | /* Begin PBXFileReference section */ 27 | 2DB1C7BA22BDF63600199584 /* Mems */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Mems; sourceTree = BUILT_PRODUCTS_DIR; }; 28 | 2DB1C7BD22BDF63600199584 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 29 | 2DB1C7C422BDF65300199584 /* Mems.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Mems.swift; sourceTree = ""; }; 30 | /* End PBXFileReference section */ 31 | 32 | /* Begin PBXFrameworksBuildPhase section */ 33 | 2DB1C7B722BDF63600199584 /* Frameworks */ = { 34 | isa = PBXFrameworksBuildPhase; 35 | buildActionMask = 2147483647; 36 | files = ( 37 | ); 38 | runOnlyForDeploymentPostprocessing = 0; 39 | }; 40 | /* End PBXFrameworksBuildPhase section */ 41 | 42 | /* Begin PBXGroup section */ 43 | 2DB1C7B122BDF63600199584 = { 44 | isa = PBXGroup; 45 | children = ( 46 | 2DB1C7BC22BDF63600199584 /* Mems */, 47 | 2DB1C7BB22BDF63600199584 /* Products */, 48 | ); 49 | sourceTree = ""; 50 | }; 51 | 2DB1C7BB22BDF63600199584 /* Products */ = { 52 | isa = PBXGroup; 53 | children = ( 54 | 2DB1C7BA22BDF63600199584 /* Mems */, 55 | ); 56 | name = Products; 57 | sourceTree = ""; 58 | }; 59 | 2DB1C7BC22BDF63600199584 /* Mems */ = { 60 | isa = PBXGroup; 61 | children = ( 62 | 2DB1C7BD22BDF63600199584 /* main.swift */, 63 | 2DB1C7C422BDF65300199584 /* Mems.swift */, 64 | ); 65 | path = Mems; 66 | sourceTree = ""; 67 | }; 68 | /* End PBXGroup section */ 69 | 70 | /* Begin PBXNativeTarget section */ 71 | 2DB1C7B922BDF63600199584 /* Mems */ = { 72 | isa = PBXNativeTarget; 73 | buildConfigurationList = 2DB1C7C122BDF63600199584 /* Build configuration list for PBXNativeTarget "Mems" */; 74 | buildPhases = ( 75 | 2DB1C7B622BDF63600199584 /* Sources */, 76 | 2DB1C7B722BDF63600199584 /* Frameworks */, 77 | 2DB1C7B822BDF63600199584 /* CopyFiles */, 78 | ); 79 | buildRules = ( 80 | ); 81 | dependencies = ( 82 | ); 83 | name = Mems; 84 | productName = Mems; 85 | productReference = 2DB1C7BA22BDF63600199584 /* Mems */; 86 | productType = "com.apple.product-type.tool"; 87 | }; 88 | /* End PBXNativeTarget section */ 89 | 90 | /* Begin PBXProject section */ 91 | 2DB1C7B222BDF63600199584 /* Project object */ = { 92 | isa = PBXProject; 93 | attributes = { 94 | LastSwiftUpdateCheck = 1100; 95 | LastUpgradeCheck = 1100; 96 | ORGANIZATIONNAME = "MJ Lee"; 97 | TargetAttributes = { 98 | 2DB1C7B922BDF63600199584 = { 99 | CreatedOnToolsVersion = 11.0; 100 | }; 101 | }; 102 | }; 103 | buildConfigurationList = 2DB1C7B522BDF63600199584 /* Build configuration list for PBXProject "Mems" */; 104 | compatibilityVersion = "Xcode 9.3"; 105 | developmentRegion = en; 106 | hasScannedForEncodings = 0; 107 | knownRegions = ( 108 | en, 109 | Base, 110 | ); 111 | mainGroup = 2DB1C7B122BDF63600199584; 112 | productRefGroup = 2DB1C7BB22BDF63600199584 /* Products */; 113 | projectDirPath = ""; 114 | projectRoot = ""; 115 | targets = ( 116 | 2DB1C7B922BDF63600199584 /* Mems */, 117 | ); 118 | }; 119 | /* End PBXProject section */ 120 | 121 | /* Begin PBXSourcesBuildPhase section */ 122 | 2DB1C7B622BDF63600199584 /* Sources */ = { 123 | isa = PBXSourcesBuildPhase; 124 | buildActionMask = 2147483647; 125 | files = ( 126 | 2DB1C7C522BDF65300199584 /* Mems.swift in Sources */, 127 | 2DB1C7BE22BDF63600199584 /* main.swift in Sources */, 128 | ); 129 | runOnlyForDeploymentPostprocessing = 0; 130 | }; 131 | /* End PBXSourcesBuildPhase section */ 132 | 133 | /* Begin XCBuildConfiguration section */ 134 | 2DB1C7BF22BDF63600199584 /* Debug */ = { 135 | isa = XCBuildConfiguration; 136 | buildSettings = { 137 | ALWAYS_SEARCH_USER_PATHS = NO; 138 | CLANG_ANALYZER_NONNULL = YES; 139 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 140 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 141 | CLANG_CXX_LIBRARY = "libc++"; 142 | CLANG_ENABLE_MODULES = YES; 143 | CLANG_ENABLE_OBJC_ARC = YES; 144 | CLANG_ENABLE_OBJC_WEAK = YES; 145 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 146 | CLANG_WARN_BOOL_CONVERSION = YES; 147 | CLANG_WARN_COMMA = YES; 148 | CLANG_WARN_CONSTANT_CONVERSION = YES; 149 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 150 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 151 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 152 | CLANG_WARN_EMPTY_BODY = YES; 153 | CLANG_WARN_ENUM_CONVERSION = YES; 154 | CLANG_WARN_INFINITE_RECURSION = YES; 155 | CLANG_WARN_INT_CONVERSION = YES; 156 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 157 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 158 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 159 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 160 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 161 | CLANG_WARN_STRICT_PROTOTYPES = YES; 162 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 163 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 164 | CLANG_WARN_UNREACHABLE_CODE = YES; 165 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 166 | COPY_PHASE_STRIP = NO; 167 | DEBUG_INFORMATION_FORMAT = dwarf; 168 | ENABLE_STRICT_OBJC_MSGSEND = YES; 169 | ENABLE_TESTABILITY = YES; 170 | GCC_C_LANGUAGE_STANDARD = gnu11; 171 | GCC_DYNAMIC_NO_PIC = NO; 172 | GCC_NO_COMMON_BLOCKS = YES; 173 | GCC_OPTIMIZATION_LEVEL = 0; 174 | GCC_PREPROCESSOR_DEFINITIONS = ( 175 | "DEBUG=1", 176 | "$(inherited)", 177 | ); 178 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 179 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 180 | GCC_WARN_UNDECLARED_SELECTOR = YES; 181 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 182 | GCC_WARN_UNUSED_FUNCTION = YES; 183 | GCC_WARN_UNUSED_VARIABLE = YES; 184 | MACOSX_DEPLOYMENT_TARGET = 10.15; 185 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 186 | MTL_FAST_MATH = YES; 187 | ONLY_ACTIVE_ARCH = YES; 188 | SDKROOT = macosx; 189 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 190 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 191 | }; 192 | name = Debug; 193 | }; 194 | 2DB1C7C022BDF63600199584 /* Release */ = { 195 | isa = XCBuildConfiguration; 196 | buildSettings = { 197 | ALWAYS_SEARCH_USER_PATHS = NO; 198 | CLANG_ANALYZER_NONNULL = YES; 199 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 200 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 201 | CLANG_CXX_LIBRARY = "libc++"; 202 | CLANG_ENABLE_MODULES = YES; 203 | CLANG_ENABLE_OBJC_ARC = YES; 204 | CLANG_ENABLE_OBJC_WEAK = YES; 205 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 206 | CLANG_WARN_BOOL_CONVERSION = YES; 207 | CLANG_WARN_COMMA = YES; 208 | CLANG_WARN_CONSTANT_CONVERSION = YES; 209 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 210 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 211 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 212 | CLANG_WARN_EMPTY_BODY = YES; 213 | CLANG_WARN_ENUM_CONVERSION = YES; 214 | CLANG_WARN_INFINITE_RECURSION = YES; 215 | CLANG_WARN_INT_CONVERSION = YES; 216 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 217 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 218 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 219 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 220 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 221 | CLANG_WARN_STRICT_PROTOTYPES = YES; 222 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 223 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 224 | CLANG_WARN_UNREACHABLE_CODE = YES; 225 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 226 | COPY_PHASE_STRIP = NO; 227 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 228 | ENABLE_NS_ASSERTIONS = NO; 229 | ENABLE_STRICT_OBJC_MSGSEND = YES; 230 | GCC_C_LANGUAGE_STANDARD = gnu11; 231 | GCC_NO_COMMON_BLOCKS = YES; 232 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 233 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 234 | GCC_WARN_UNDECLARED_SELECTOR = YES; 235 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 236 | GCC_WARN_UNUSED_FUNCTION = YES; 237 | GCC_WARN_UNUSED_VARIABLE = YES; 238 | MACOSX_DEPLOYMENT_TARGET = 10.15; 239 | MTL_ENABLE_DEBUG_INFO = NO; 240 | MTL_FAST_MATH = YES; 241 | SDKROOT = macosx; 242 | SWIFT_COMPILATION_MODE = wholemodule; 243 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 244 | }; 245 | name = Release; 246 | }; 247 | 2DB1C7C222BDF63600199584 /* Debug */ = { 248 | isa = XCBuildConfiguration; 249 | buildSettings = { 250 | CODE_SIGN_STYLE = Automatic; 251 | PRODUCT_NAME = "$(TARGET_NAME)"; 252 | SWIFT_VERSION = 5.0; 253 | }; 254 | name = Debug; 255 | }; 256 | 2DB1C7C322BDF63600199584 /* Release */ = { 257 | isa = XCBuildConfiguration; 258 | buildSettings = { 259 | CODE_SIGN_STYLE = Automatic; 260 | PRODUCT_NAME = "$(TARGET_NAME)"; 261 | SWIFT_VERSION = 5.0; 262 | }; 263 | name = Release; 264 | }; 265 | /* End XCBuildConfiguration section */ 266 | 267 | /* Begin XCConfigurationList section */ 268 | 2DB1C7B522BDF63600199584 /* Build configuration list for PBXProject "Mems" */ = { 269 | isa = XCConfigurationList; 270 | buildConfigurations = ( 271 | 2DB1C7BF22BDF63600199584 /* Debug */, 272 | 2DB1C7C022BDF63600199584 /* Release */, 273 | ); 274 | defaultConfigurationIsVisible = 0; 275 | defaultConfigurationName = Release; 276 | }; 277 | 2DB1C7C122BDF63600199584 /* Build configuration list for PBXNativeTarget "Mems" */ = { 278 | isa = XCConfigurationList; 279 | buildConfigurations = ( 280 | 2DB1C7C222BDF63600199584 /* Debug */, 281 | 2DB1C7C322BDF63600199584 /* Release */, 282 | ); 283 | defaultConfigurationIsVisible = 0; 284 | defaultConfigurationName = Release; 285 | }; 286 | /* End XCConfigurationList section */ 287 | }; 288 | rootObject = 2DB1C7B222BDF63600199584 /* Project object */; 289 | } 290 | -------------------------------------------------------------------------------- /Mems.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Mems.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Mems/Mems.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Mems.swift 3 | // Mems 4 | // 5 | // Created by MJ Lee on 2019/6/22. 6 | // Copyright © 2019 MJ Lee. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | public enum MemAlign : Int { 12 | case one = 1, two = 2, four = 4, eight = 8 13 | } 14 | 15 | private let _EMPTY_PTR = UnsafeRawPointer(bitPattern: 0x1)! 16 | 17 | /// 辅助查看内存的小工具类 18 | public struct Mems { 19 | private static func _memStr(_ ptr: UnsafeRawPointer, 20 | _ size: Int, 21 | _ aligment: Int) ->String { 22 | if ptr == _EMPTY_PTR { return "" } 23 | 24 | var rawPtr = ptr 25 | var string = "" 26 | let fmt = "0x%0\(aligment << 1)lx" 27 | let count = size / aligment 28 | for i in 0.. 0 { 30 | string.append(" ") 31 | rawPtr += aligment 32 | } 33 | let value: CVarArg 34 | switch aligment { 35 | case MemAlign.eight.rawValue: 36 | value = rawPtr.load(as: UInt64.self) 37 | case MemAlign.four.rawValue: 38 | value = rawPtr.load(as: UInt32.self) 39 | case MemAlign.two.rawValue: 40 | value = rawPtr.load(as: UInt16.self) 41 | default: 42 | value = rawPtr.load(as: UInt8.self) 43 | } 44 | string.append(String(format: fmt, value)) 45 | } 46 | return string 47 | } 48 | 49 | private static func _memBytes(_ ptr: UnsafeRawPointer, 50 | _ size: Int) -> [UInt8] { 51 | var arr: [UInt8] = [] 52 | if ptr == _EMPTY_PTR { return arr } 53 | for i in 0.. [UInt8] { 61 | return _memBytes(ptr(ofVal: &v), MemoryLayout.stride(ofValue: v)) 62 | } 63 | 64 | /// 获得引用所指向的内存数据(字节数组格式) 65 | public static func memBytes(ofRef v: T) -> [UInt8] { 66 | let p = ptr(ofRef: v) 67 | return _memBytes(p, malloc_size(p)) 68 | } 69 | 70 | /// 获得变量的内存数据(字符串格式) 71 | /// 72 | /// - Parameter alignment: 决定了多少个字节为一组 73 | public static func memStr(ofVal v: inout T, alignment: MemAlign? = nil) -> String { 74 | let p = ptr(ofVal: &v) 75 | return _memStr(p, MemoryLayout.stride(ofValue: v), 76 | alignment != nil ? alignment!.rawValue : MemoryLayout.alignment(ofValue: v)) 77 | } 78 | 79 | /// 获得引用所指向的内存数据(字符串格式) 80 | /// 81 | /// - Parameter alignment: 决定了多少个字节为一组 82 | public static func memStr(ofRef v: T, alignment: MemAlign? = nil) -> String { 83 | let p = ptr(ofRef: v) 84 | return _memStr(p, malloc_size(p), 85 | alignment != nil ? alignment!.rawValue : MemoryLayout.alignment(ofValue: v)) 86 | } 87 | 88 | /// 获得变量的内存地址 89 | public static func ptr(ofVal v: inout T) -> UnsafeRawPointer { 90 | return MemoryLayout.size(ofValue: v) == 0 ? _EMPTY_PTR : withUnsafePointer(to: &v) { 91 | UnsafeRawPointer($0) 92 | } 93 | } 94 | 95 | /// 获得引用所指向内存的地址 96 | public static func ptr(ofRef v: T) -> UnsafeRawPointer { 97 | if v is Array 98 | || Swift.type(of: v) is AnyClass 99 | || v is AnyClass { 100 | return UnsafeRawPointer(bitPattern: unsafeBitCast(v, to: UInt.self))! 101 | } else if v is String { 102 | var mstr = v as! String 103 | if mstr.mems.type() != .heap { 104 | return _EMPTY_PTR 105 | } 106 | return UnsafeRawPointer(bitPattern: unsafeBitCast(v, to: (UInt, UInt).self).1)! 107 | } else { 108 | return _EMPTY_PTR 109 | } 110 | } 111 | 112 | /// 获得变量所占用的内存大小 113 | public static func size(ofVal v: inout T) -> Int { 114 | return MemoryLayout.size(ofValue: v) > 0 ? MemoryLayout.stride(ofValue: v) : 0 115 | } 116 | 117 | /// 获得引用所指向内存的大小 118 | public static func size(ofRef v: T) -> Int { 119 | return malloc_size(ptr(ofRef: v)) 120 | } 121 | } 122 | 123 | public enum StringMemType : UInt8 { 124 | /// TEXT段(常量区) 125 | case text = 0xd0 126 | /// taggerPointer 127 | case tagPtr = 0xe0 128 | /// 堆空间 129 | case heap = 0xf0 130 | /// 未知 131 | case unknow = 0xff 132 | } 133 | 134 | public struct MemsWrapper { 135 | public private(set) var base: Base 136 | public init(_ base: Base) { 137 | self.base = base 138 | } 139 | } 140 | 141 | public protocol MemsCompatible {} 142 | public extension MemsCompatible { 143 | static var mems: MemsWrapper.Type { 144 | get { return MemsWrapper.self } 145 | set {} 146 | } 147 | var mems: MemsWrapper { 148 | get { return MemsWrapper(self) } 149 | set {} 150 | } 151 | } 152 | 153 | extension String: MemsCompatible {} 154 | public extension MemsWrapper where Base == String { 155 | mutating func type() -> StringMemType { 156 | let ptr = Mems.ptr(ofVal: &base) 157 | return StringMemType(rawValue: (ptr + 15).load(as: UInt8.self) & 0xf0) 158 | ?? StringMemType(rawValue: (ptr + 7).load(as: UInt8.self) & 0xf0) 159 | ?? .unknow 160 | } 161 | } 162 | -------------------------------------------------------------------------------- /Mems/main.swift: -------------------------------------------------------------------------------- 1 | // 2 | // main.swift 3 | // Mems 4 | // 5 | // Created by MJ Lee on 2019/6/22. 6 | // Copyright © 2019 MJ Lee. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | func show(val: inout T) { 12 | print("-------------- \(type(of: val)) --------------") 13 | print("变量的地址:", Mems.ptr(ofVal: &val)) 14 | print("变量的内存:", Mems.memStr(ofVal: &val)) 15 | print("变量的大小:", Mems.size(ofVal: &val)) 16 | print("") 17 | } 18 | 19 | func show(ref: T) { 20 | print("-------------- \(type(of: ref)) --------------") 21 | print("对象的地址:", Mems.ptr(ofRef: ref)) 22 | print("对象的内存:", Mems.memStr(ofRef: ref)) 23 | print("对象的大小:", Mems.size(ofRef: ref)) 24 | print("") 25 | } 26 | 27 | /// 整型 28 | func showInt() { 29 | var int8: Int8 = 10 30 | show(val: &int8) 31 | 32 | var int16: Int16 = 10 33 | show(val: &int16) 34 | 35 | var int32: Int32 = 10 36 | show(val: &int32) 37 | 38 | var int64: Int64 = 10 39 | show(val: &int64) 40 | } 41 | 42 | /// 枚举 43 | func showEnum() { 44 | enum TestEnum { 45 | case test1(Int, Int, Int) 46 | case test2(Int, Int) 47 | case test3(Int) 48 | case test4(Bool) 49 | case test5 50 | } 51 | var e = TestEnum.test1(1, 2, 3) 52 | show(val: &e) 53 | e = .test2(4, 5) 54 | show(val: &e) 55 | e = .test3(6) 56 | show(val: &e) 57 | e = .test4(true) 58 | show(val: &e) 59 | e = .test5 60 | show(val: &e) 61 | } 62 | 63 | /// 结构体 64 | func showStruct() { 65 | struct Date { 66 | var year = 10 67 | var test = true 68 | var month = 20 69 | var day = 30 70 | } 71 | var s = Date() 72 | show(val: &s) 73 | } 74 | 75 | // 类 76 | func showClass() { 77 | class Point { 78 | var x = 11 79 | var test = true 80 | var y = 22 81 | } 82 | var p = Point() 83 | show(val: &p) 84 | show(ref: p) 85 | } 86 | 87 | /// 数组 88 | func showArray() { 89 | var arr = [1, 2, 3, 4] 90 | show(val: &arr) 91 | show(ref: arr) 92 | } 93 | 94 | /// 字符串 95 | func showString() { 96 | var str1 = "123456789" 97 | // tagPtr(tagger pointer) 98 | print(str1.mems.type()) 99 | show(val: &str1) 100 | 101 | var str2 = "1234567812345678" 102 | // text(TEXT段,常量区) 103 | print(str2.mems.type()) 104 | show(val: &str2) 105 | 106 | var str3 = "1234567812345678" 107 | str3.append("9") 108 | // heap(字符串存储在堆空间) 109 | print(str3.mems.type()) 110 | show(val: &str3) 111 | show(ref: str3) 112 | } 113 | 114 | /// 字节格式 115 | func showByteFormat() { 116 | var int64: Int64 = 10 117 | print("1个字节为1组 :", Mems.memStr(ofVal: &int64, alignment: .one)) 118 | print("2个字节为1组 :", Mems.memStr(ofVal: &int64, alignment: .two)) 119 | print("4个字节为1组 :", Mems.memStr(ofVal: &int64, alignment: .four)) 120 | print("8个字节为1组 :", Mems.memStr(ofVal: &int64, alignment: .eight)) 121 | } 122 | 123 | showInt() 124 | showEnum() 125 | showStruct() 126 | showClass() 127 | showArray() 128 | showString() 129 | showByteFormat() 130 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Mems 2 | > Utils for viewing memory in Swift. 3 | > 4 | > 用来窥探Swift内存的小工具 5 | 6 | 7 | 8 | ## 用法 9 | 10 | ```swift 11 | func show(val: inout T) { 12 | print("-------------- \(type(of: val)) --------------") 13 | print("变量的地址:", Mems.ptr(ofVal: &val)) 14 | print("变量的内存:", Mems.memStr(ofVal: &val)) 15 | print("变量的大小:", Mems.size(ofVal: &val)) 16 | print("") 17 | } 18 | 19 | func show(ref: T) { 20 | print("-------------- \(type(of: ref)) --------------") 21 | print("对象的地址:", Mems.ptr(ofRef: ref)) 22 | print("对象的内存:", Mems.memStr(ofRef: ref)) 23 | print("对象的大小:", Mems.size(ofRef: ref)) 24 | print("") 25 | } 26 | ``` 27 | 28 | ### 整型 29 | 30 | ```swift 31 | var int8: Int8 = 10 32 | show(val: &int8) 33 | // -------------- Int8 -------------- 34 | // 变量的地址: 0x00007ffeefbff598 35 | // 变量的内存: 0x0a 36 | // 变量的大小: 1 37 | 38 | var int16: Int16 = 10 39 | show(val: &int16) 40 | // -------------- Int16 -------------- 41 | // 变量的地址: 0x00007ffeefbff590 42 | // 变量的内存: 0x000a 43 | // 变量的大小: 2 44 | 45 | var int32: Int32 = 10 46 | show(val: &int32) 47 | // -------------- Int32 -------------- 48 | // 变量的地址: 0x00007ffeefbff588 49 | // 变量的内存: 0x0000000a 50 | // 变量的大小: 4 51 | 52 | var int64: Int64 = 10 53 | show(val: &int64) 54 | // -------------- Int64 -------------- 55 | // 变量的地址: 0x00007ffeefbff580 56 | // 变量的内存: 0x000000000000000a 57 | // 变量的大小: 8 58 | ``` 59 | 60 | ### 枚举 61 | 62 | ```swift 63 | enum TestEnum { 64 | case test1(Int, Int, Int) 65 | case test2(Int, Int) 66 | case test3(Int) 67 | case test4(Bool) 68 | case test5 69 | } 70 | 71 | var e = TestEnum.test1(1, 2, 3) 72 | show(val: &e) 73 | // -------------- TestEnum -------------- 74 | // 变量的地址: 0x00007ffeefbff580 75 | // 变量的内存: 0x0000000000000001 0x0000000000000002 0x0000000000000003 0x0000000000000000 76 | // 变量的大小: 32 77 | 78 | e = .test2(4, 5) 79 | show(val: &e) 80 | // -------------- TestEnum -------------- 81 | // 变量的地址: 0x00007ffeefbff580 82 | // 变量的内存: 0x0000000000000004 0x0000000000000005 0x0000000000000000 0x0000000000000001 83 | // 变量的大小: 32 84 | 85 | e = .test3(6) 86 | show(val: &e) 87 | // -------------- TestEnum -------------- 88 | // 变量的地址: 0x00007ffeefbff580 89 | // 变量的内存: 0x0000000000000006 0x0000000000000000 0x0000000000000000 0x0000000000000002 90 | // 变量的大小: 32 91 | 92 | e = .test4(true) 93 | show(val: &e) 94 | // -------------- TestEnum -------------- 95 | // 变量的地址: 0x00007ffeefbff580 96 | // 变量的内存: 0x0000000000000001 0x0000000000000000 0x0000000000000000 0x0000000000000003 97 | // 变量的大小: 32 98 | 99 | e = .test5 100 | show(val: &e) 101 | // -------------- TestEnum -------------- 102 | // 变量的地址: 0x00007ffeefbff580 103 | // 变量的内存: 0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000004 104 | // 变量的大小: 32 105 | ``` 106 | 107 | ### 结构体 108 | 109 | ```swift 110 | struct Date { 111 | var year = 10 112 | var test = true 113 | var month = 20 114 | var day = 30 115 | } 116 | 117 | var s = Date() 118 | show(val: &s) 119 | // -------------- Date -------------- 120 | // 变量的地址: 0x00007ffeefbff580 121 | // 变量的内存: 0x000000000000000a 0x0000000000000001 0x0000000000000014 0x000000000000001e 122 | // 变量的大小: 32 123 | ``` 124 | 125 | ### 类 126 | 127 | ```swift 128 | class Point { 129 | var x = 11 130 | var test = true 131 | var y = 22 132 | } 133 | 134 | var p = Point() 135 | show(val: &p) 136 | // -------------- Point -------------- 137 | // 变量的地址: 0x00007ffeefbff590 138 | // 变量的内存: 0x0000000101023680 139 | // 变量的大小: 8 140 | 141 | show(ref: p) 142 | // -------------- Point -------------- 143 | // 对象的地址: 0x0000000101023680 144 | // 对象的内存: 0x00000001000072d8 0x0000000200000002 0x000000000000000b 0x0000000000000001 0x0000000000000016 0x3030303030303030 145 | // 对象的大小: 48 146 | ``` 147 | 148 | ### 数组 149 | 150 | ```swift 151 | var arr = [1, 2, 3, 4] 152 | show(val: &arr) 153 | // -------------- Array -------------- 154 | // 变量的地址: 0x00007ffeefbff598 155 | // 变量的内存: 0x0000000101023800 156 | // 变量的大小: 8 157 | 158 | show(ref: arr) 159 | // -------------- Array -------------- 160 | // 对象的地址: 0x0000000101023800 161 | // 对象的内存: 0x00007fff9c30f848 0x0000000200000002 0x0000000000000004 0x0000000000000008 0x0000000000000001 0x0000000000000002 0x0000000000000003 0x0000000000000004 162 | // 对象的大小: 64 163 | ``` 164 | 165 | ### 字符串 166 | 167 | ```swift 168 | var str1 = "123456789" 169 | // taggerPtr(tagger pointer) 170 | print(str1.mems.type()) 171 | show(val: &str1) 172 | // -------------- String -------------- 173 | // 变量的地址: 0x00007ffeefbff580 174 | // 变量的内存: 0x3837363534333231 0xe900000000000039 175 | // 变量的大小: 16 176 | 177 | var str2 = "1234567812345678" 178 | // text(字符串存储在TEXT段) 179 | print(str2.mems.type()) 180 | show(val: &str2) 181 | // -------------- String -------------- 182 | // 变量的地址: 0x00007ffeefbff570 183 | // 变量的内存: 0xd000000000000010 0x8000000100007610 184 | // 变量的大小: 16 185 | 186 | var str3 = "1234567812345678" 187 | str3.append("9") 188 | // heap(字符串存储在堆空间) 189 | print(str3.mems.type()) 190 | show(val: &str3) 191 | // -------------- String -------------- 192 | // 变量的地址: 0x00007ffeefbff560 193 | // 变量的内存: 0xf000000000000011 0x00000001007b6ad0 194 | // 变量的大小: 16 195 | 196 | show(ref: str3) 197 | // -------------- String -------------- 198 | // 对象的地址: 0x00000001007b6ad0 199 | // 对象的内存: 0x00007fff963e9660 0x0000000200000002 0x0000000000000018 0xf000000000000011 0x3837363534333231 0x3837363534333231 0x0000000000200039 0x0000000000000000 200 | // 对象的大小: 64 201 | ``` 202 | 203 | ### 设置字节显示格式 204 | 205 | ```swift 206 | var int64: Int64 = 10 207 | 208 | print("1个字节为1组 :", Mems.memStr(ofVal: &int64, alignment: .one)) 209 | // 1个字节为1组 : 0x0a 0x00 0x00 0x00 0x00 0x00 0x00 0x00 210 | 211 | print("2个字节为1组 :", Mems.memStr(ofVal: &int64, alignment: .two)) 212 | // 2个字节为1组 : 0x000a 0x0000 0x0000 0x0000 213 | 214 | print("4个字节为1组 :", Mems.memStr(ofVal: &int64, alignment: .four)) 215 | // 4个字节为1组 : 0x0000000a 0x00000000 216 | 217 | print("8个字节为1组 :", Mems.memStr(ofVal: &int64, alignment: .eight)) 218 | // 8个字节为1组 : 0x000000000000000a 219 | ``` 220 | --------------------------------------------------------------------------------