gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:39:25 25 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,并且在Delphi中有一个 Macapi.Mach.pas单元,但是它没有实现此处使用的任何定义。

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

此代码应如何转换为Delphi XE8?
(我可以自行转换定义,但是在哪里可以找到标头?)

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

更新资料

这是我对该功能的翻译和实现。当与32位iOS设备配合使用时,它可与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的子文件夹中找到标题。

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

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

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