2010-03-08 60 views
5

我需要將碳方法轉化爲可可,並且我無法找到有關碳方法getPtrSize真正做什麼的任何文檔。從我翻譯的代碼看來,它返回的是一個圖像的字節表示,但並不真正與該名稱匹配。有人能給我一個關於這種方法的很好的解釋,或者將我鏈接到描述它的一些文檔。我正在翻譯的代碼是在一個叫做MCL的通用lisp實現中,它有一個通向碳的橋樑(我正在翻譯成CCL,這是一個Cocoa橋的常見lisp實現)。下面是MCL代碼(#_before方法調用意味着它是一個碳法):Carbon方法的可可等價物getPtrSize

(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

這是真的嗎? +1因爲吹我的頭腦。 – 2010-03-08 17:53:53

回答

4

GetPtrSize距離Memory Manager的功能。當您使用NewPtr(另一個內存管理器功能)分配內存時,內存管理器將跟蹤您請求的內存量,以便您可以使用GetPtrSize檢索該數字。

現代替代NewPtrmalloc,它沒有提供這樣的功能。有一個malloc_size函數,但它返回的數字可能會四捨五入到某個增量,因此它可能會大於最初請求的數字。你可以看到這將如何(至少在概念上)不好。

GetPtrSize唯一正確的替代方法是簡單地跟蹤緩衝區的大小。

或者,您可以用NSMutableData對象替換這些緩衝區。一個NSMutableData封裝了一個緩衝區和它的大小,使它們很容易保持在一起。