gpt4 book ai didi

delphi - 将 Objective-C 代码转换为 Delphi (XE8)

转载 作者:行者123 更新时间:2023-12-03 17:39:25 26 4
gpt4 key购买 nike

为了跟踪我的代码的各个部分分配了多少内存,我找到了我想在 Delphi XE8 中运行的这个 Objective-C 代码 (here):

void report_memory(void)
{
struct mach_task_basic_info info;
mach_msg_type_number_t size = MACH_TASK_BASIC_INFO_COUNT;
kern_return_t kerr = task_info(mach_task_self(),
MACH_TASK_BASIC_INFO,
(task_info_t)&info,
&size);
if( kerr == KERN_SUCCESS ) {
NSLog(@"Memory in use (in bytes): %u", info.resident_size);
}
}

此代码只是读取程序使用的当前内存量并记录它。我将使用它来检测调用代码的不同部分时内存使用量的增长情况。

它包括 Mach.h , 并且有一个 Macapi.Mach.pas Delphi 中的单元,但它没有实现这里使用的任何定义。

(NSLog功能日志已经实现: iOSApi.Foundation.NSLog((StrToNSStr(aMessage) as ILocalObject).GetObjectID))

这段代码应该如何转换为Delphi XE8?
(我也许可以自己转换定义,但是在哪里可以找到标题?)

PS。我知道这是一种追踪内存的原始方式,但我还没有找到更好的解决方案。 Xcode Instruments ,告诉我我的应用程序调用了 malloc 180.000 次,但它没有指出是什么代码或对象启动了它。由于我使用的是 Delphi,这些信息可能会丢失。

更新

下面是我对函数的翻译和实现。它适用于 iOS 模拟器和针对 32 位 iOS 设备时,但它不适用于 64 位 iOS 设备(返回值为 4)。

unit uMachExt;

interface

uses Macapi.Mach, Posix.Base;

type
integer_t = Integer;
natural_t = NativeInt;
mach_vm_size_t = UInt64;
//typedef int policy_t
policy_t = Integer;

//type time_value_t = struct[2] of integer_t;
time_value_t = array[0..1] of Integer; //0:seconds, 1:microseconds

//typedef natural_t mach_msg_type_number_t
mach_msg_type_number_t = natural_t;

//type kern_return_t = int;
kern_return_t = integer;

//typedef natural_t task_flavor_t;
task_flavor_t = natural_t;

// typedef integer_t *task_info_t; /* varying array of int */
task_info_t = array of integer_t;

{#define MACH_TASK_BASIC_INFO 20 /* always 64-bit basic info */
struct mach_task_basic_info {
mach_vm_size_t virtual_size; /* virtual memory size (bytes) */
mach_vm_size_t resident_size; /* resident memory size (bytes) */
mach_vm_size_t resident_size_max; /* maximum resident memory size (bytes) */
time_value_t user_time; /* total user run time for
terminated threads */
time_value_t system_time; /* total system run time for
terminated threads */
policy_t policy; /* default policy for new threads */
integer_t suspend_count; /* suspend count for task */
}
mach_task_basic_info = Record
virtual_size: mach_vm_size_t ; //* virtual memory size (bytes) */
resident_size: mach_vm_size_t ; //* resident memory size (bytes) */
resident_size_max: mach_vm_size_t ; //* maximum resident memory size (bytes) */
user_time: time_value_t ; //* total user run time for terminated threads */
system_time: time_value_t ; //* total system run time forterminated threads */
policy: policy_t ; //* default policy for new threads */
suspend_count: integer_t; //* suspend count for task */
end;

const
cMACH_TASK_BASIC_INFO = 20;
{typedef struct mach_task_basic_info mach_task_basic_info_data_t;
#define MACH_TASK_BASIC_INFO_COUNT \
(sizeof(mach_task_basic_info_data_t) / sizeof(natural_t))}
cMACH_TASK_BASIC_INFO_COUNT = SizeOf(mach_task_basic_info) div sizeof(natural_t);

{
#ifdef mig_external
mig_external
#else
extern
#endif /* mig_external */
kern_return_t task_info
(
task_name_t target_task,
task_flavor_t flavor,
task_info_t task_info_out,
mach_msg_type_number_t *task_info_outCnt
);
}

function task_info( target_task: task_name_t;
flavor: task_flavor_t;
var task_info_out: mach_task_basic_info;
var task_info_outCnt: mach_msg_type_number_t) : kern_return_t;
cdecl external libc name _PU + 'task_info';

function GetMemoryUsage: Integer;

implementation

{ struct mach_task_basic_info info;
mach_msg_type_number_t size = MACH_TASK_BASIC_INFO_COUNT;
kern_return_t kerr = task_info(mach_task_self(),
MACH_TASK_BASIC_INFO,
(task_info_t)&info,
&size);
}

function GetMemoryUsage: Integer;
var info: mach_task_basic_info;
size: mach_msg_type_number_t;
kerr: kern_return_t;
begin
Result := 0;
size := cMACH_TASK_BASIC_INFO_COUNT;
kerr := task_info(mach_task_self, cMACH_TASK_BASIC_INFO, info, size);
if kerr=0 then
result := info.resident_size;
end;

end.

对我来说,它适用于 32 位就足够了,但为了完整起见,如果您发现应该更改哪些内容以使其适用于 64 位,请发表评论(如果您在实现中发现其他错误)。

最佳答案

您可以在 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer 的子文件夹中找到标题。 .

我喜欢将这些复制到我硬盘上的另一个文件夹中,以保留未包含在当前版本的 XCode 中的旧版本的 SDK。这样我也可以使用 Spotlight 来快速找到包含特定函数或定义的文件。

关于delphi - 将 Objective-C 代码转换为 Delphi (XE8),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32351687/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com