重定位分配器发生了什么?

What happened with the relocating allocator?

我在阅读旧的 glibc 文档 here 时看到三个我以前从未见过的奇怪函数(r_alloc、r_alloc_free 和 r_re_alloc)。我认为,他们实施了一个重新分配内存以进行碎片整理的分配器,但我无法在其他任何地方找到更多信息。

你能告诉我更多关于那个功能的信息吗?他们还在 Glibc 中吗?如果不是,为什么他们被删除了?

Can you tell me more about that functions?

你想了解他们什么?在您找到它们的手册中对它们进行了非常清楚的描述。

它们有点类似于 Win32 LocalAllocLocalLock -- 您获得内存对象的句柄,但要获得该对象的可用地址需要额外的步骤。这些通常不是一个好主意,除非是在内存极其受限的系统上。

Are they still in Glibc?

没有

If not, why they were removed?

因为它们通常不是一个好主意,并且滋生出难以发现的错误。

更新:

What kind of bugs are possible by using something like that?

这是一个例子:

const char *my_strcat(const char *a, const char *b)
{
  const size_t len_a = strlen(a);
  const size_t len_b = strlen(b);
  char *handle;

  if (r_alloc((void**)&handle, len_a + len_b + 1) == NULL) return NULL;
  memcpy(handle, a, len_a);
  memcpy(handle + len_a, b, len_b + 1);

  return handle;
}

// There are memory leaks here. Ignore them for now.
int main()
{
  const char *result = my_strcat("abc", my_strcat("def", "ghi"));

  return strcmp(result, "abcdefghi");
}

你能找出错误吗?

程序有时会成功,有时会失败并返回非零退出代码,有时会崩溃并返回 SIGSEGV