c - Postgres 用户定义类型和正确分配内存
问题描述
我正在为 postgres 编写一个扩展,其中包括创建一个新的可变长度基类型,但是我在理解 SET_VARSIZE 的语义时遇到了一些困难。
以下面的示例代码为例,这并不能准确地反映我的用例,但它说明了这一点。
typedef struct another_struct
{
char *a;
char *b;
} another_struct;
typedef struct test_struct
{
char vl_len_[4];
another_struct *data;
} test_struct;
1)在为test_struct的新实例分配内存时,大概我可以简单地做以下事情,它会考虑到变长成员vl_len_的大小?
test_struct *t = palloc0(sizeof(struct test_struct));
2) 因为 another_struct 的两个成员都有可变长度,我假设我还需要跟踪为这两个字段分配了多少内存,以便将正确的长度传递给 SET_VARSIZE?
3) 调用 SET_VARSIZE 时是否还需要考虑 another_struct 指针的大小?
我认为对 SET_VARSIZE 的最终调用看起来像这样
SET_VARSIZE(t, sizeof(struct test_struct) + sizeof(struct another_struct) + a_and_b_length);
这接近正确吗?对任何错误表示歉意,我对 C 编程相当陌生。
谢谢
解决方案
仅使用常规指针是无法做到的。所有内存都必须在 size 成员之后。
typedef struct another_struct
{
char a[FLEXIBLE_ARRAY_MEMBER];
char b[FLEXIBLE_ARRAY_MEMBER];
} another_struct;
typedef struct test_struct
{
char vl_len_[4];
another_struct data;
} test_struct;
你现在必须知道数据的实际大小。
test_struct *t = palloc0(VARHDRSZ + size_of_a + size_of_b);
SET_VARSIZE(t, VARHDRSZ + size_of_a + size_of_b );
memcpy(t->data.a, src_a, size_of_a);
memcpy(t->data.b, src_b, size_of_b);
文档中的示例只有一个字段,所以我只是假设这种方式有效。间接可能还有其他问题。来源:https ://www.postgresql.org/docs/current/xfunc-c.html
推荐阅读
- c# - 如何在单个视图中使用多个布局
- c# - 使用时如何确定是否需要在 MySqlConnection 中调用 Close()?
- c - 找到最大的价值
- reactjs - React 站点警告:href 属性需要一个有效的地址。提供一个有效的、可导航的地址作为 href 值 jsx-a11y/anchor-is-valid
- java - java.lang.NoClassDefFoundError: org/springframework/core/KotlinDetector 同时从 spring-core-3.2.0 迁移到 spring 4.x
- android - 删除数据库中的命令
- android - 无法在 mqtt 代理上发布消息
- listview - 添加列表
动态到 ListView.children - c# - 存储库接口命名约定
- java - Javafx-图像到PDF转换使用PDFBox显示一半图像