【问题标题】:Cocoa equivalent of the Carbon method getPtrSize可可等效于 Carbon 方法 getPtrSize
【发布时间】:2010-03-09 02:24:36
【问题描述】:

我需要将 a carbon 方法翻译成 cocoa,但我无法找到任何有关 carbon 方法 getPtrSize 真正作用的文档。从我正在翻译的代码来看,它似乎返回了图像的字节表示,但这与名称并不匹配。有人可以给我这个方法的一个很好的解释,或者将我链接到一些描述它的文档。我正在翻译的代码在一个名为 MCL 的通用 lisp 实现中,它有一个到 carbon 的桥(我正在翻译成 CCL,它是一个带有 Cocoa 桥的通用 lisp 实现)。这是MCL代码(#_before方法调用表示它是碳方法):

(defmethod COPY-CONTENT-INTO ((Source inflatable-icon)
                              (Destination inflatable-icon))
  ;; check for size compatibility to avoid disaster
  (unless (and (= (rows Source) (rows Destination)) 
               (= (columns Source) (columns Destination))
               (= (#_getPtrSize (image Source))
                  (#_getPtrSize (image Destination))))
    (error "cannot copy content of source into destination
inflatable icon: incompatible sizes"))
  ;; given that they are the same size only copy content
  (setf (is-upright Destination) (is-upright Source))
  (setf (height Destination) (height Source))
  (setf (dz Destination) (dz Source))
  (setf (surfaces Destination) (surfaces Source))
  (setf (distance Destination) (distance Source))
  ;; arrays
  (noise-map Source)  ;; accessor makes array if needed
  (noise-map Destination)  ;; ;; accessor makes array if needed
  (dotimes (Row (rows Source))
    (dotimes (Column (columns Source))
      (setf (aref (noise-map Destination) Row Column)
            (aref (noise-map Source) Row Column))
      (setf (aref (altitudes Destination) Row Column)
            (aref (altitudes Source) Row Column))))
  (setf (connectors Destination)
        (mapcar #'copy-instance (connectors Source)))
  (setf (visible-alpha-threshold Destination)
        (visible-alpha-threshold Source))
  ;; copy Image: slow byte copy
  (dotimes (I (#_getPtrSize (image Source)))
    (%put-byte (image Destination) (%get-byte (image Source) i) i))
  ;; flat texture optimization:
  ;; do not copy texture-id
  ;;   -> destination should get its own texture id from OpenGL
  (setf (is-flat Destination) (is-flat Source))
  ;; do not compile flat textures: the display list overhead
  ;; slows things down by about 2x
  (setf (auto-compile Destination) (not (is-flat Source)))
  ;; to make change visible we have to reset the compiled flag
  (setf (is-compiled Destination) nil))

【问题讨论】:

  • 这是真的吗? +1 让我大吃一惊。

标签: cocoa lisp common-lisp macos-carbon


【解决方案1】:

GetPtrSize 是来自Memory Manager 的函数。当您使用NewPtr(另一个内存管理器功能)分配内存时,内存管理器会跟踪您请求的内存量,以便您可以使用GetPtrSize 检索该数字。

NewPtr 的现代替代品是 malloc,它不提供此类功能。有一个malloc_size 函数,但它返回的数字可能会四舍五入到某个增量,因此它可能大于您最初要求的数字。您可以看到(至少在概念上)这将是多么糟糕。

GetPtrSize 的唯一准确替换是自己跟踪缓冲区的大小。

或者,您可以将这些缓冲区替换为 NSMutableData 对象。 NSMutableData 封装了一个缓冲区及其大小,可以很容易地将它们放在一起。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-20
    • 1970-01-01
    • 1970-01-01
    • 2016-01-29
    • 2010-11-11
    • 2011-10-04
    • 2012-01-14
    • 2019-07-16
    相关资源
    最近更新 更多