如何在Boehm GC中使用asprintf?

时间:2010-09-07 00:27:24

标签: c malloc printf boehm-gc

据我所知,asprintf调用了malloc。如果我用Boehm GC替换malloc,对asprintf的调用仍会调用传统的malloc - 至少这是valgrind告诉我的:

这是malloc宏:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

#include <gc.h>
#define malloc(n)    GC_MALLOC(n)
#define calloc(m,n)  GC_MALLOC((m)*(n))
#define realloc(p,n) GC_REALLOC((p),(n))

typedef char * string;

以下是valgrind报告:

hopcroft:didactic_scheme(flexible_strings) scotttaylor$ valgrind --suppressions=./boehm-gc.suppressions --leak-check=full bin/escheme -e 1
==16130== Memcheck, a memory error detector
==16130== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==16130== Using Valgrind-3.6.0.SVN and LibVEX; rerun with -h for copyright info
==16130== Command: bin/escheme -e 1
==16130== 
--16130-- bin/escheme:
--16130-- dSYM directory is missing; consider using --dsymutil=yes
1==16130== 
==16130== HEAP SUMMARY:
==16130==     in use at exit: 4,312 bytes in 3 blocks
==16130==   total heap usage: 3 allocs, 0 frees, 4,312 bytes allocated
==16130== 
==16130== 128 bytes in 1 blocks are definitely lost in loss record 2 of 3
==16130==    at 0x100012D75: malloc (vg_replace_malloc.c:236)
==16130==    by 0x1000918EC: asprintf (in /usr/lib/libSystem.B.dylib)
==16130==    by 0x1000013FA: printInt (in bin/escheme)
==16130==    by 0x100001D38: print (in bin/escheme)
==16130==    by 0x100001DC5: main (in bin/escheme)
==16130== 
==16130== LEAK SUMMARY:
==16130==    definitely lost: 128 bytes in 1 blocks
==16130==    indirectly lost: 0 bytes in 0 blocks
==16130==      possibly lost: 0 bytes in 0 blocks
==16130==    still reachable: 4,184 bytes in 2 blocks
==16130==         suppressed: 0 bytes in 0 blocks
==16130== Reachable blocks (those to which a pointer was found) are not shown.
==16130== To see them, rerun with: --leak-check=full --show-reachable=yes
==16130== 
==16130== For counts of detected and suppressed errors, rerun with: -v
==16130== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 66 from 13)

以下是malloc调用来自的代码:

static string printInt(Object self) {
  string str;
  asprintf(&str, "%lu", getValueInt(self));
  return str;
}

解决方法可能是使用asprintf,然后使用malloc来复制它 这样就可以使用malloc宏代替原始函数:

static string printInt(Object self) {
  string tmp;
  string str;

  asprintf(&tmp, "%lu", getValueInt(self));
  str = calloc(sizeof(string), strlen(tmp) + 1);

  strcpy(str, tmp);
  free(tmp);

  return str;
}

这看起来很愚蠢 - 它涉及一堆不必要的复制,也恰好是IHMO的代码眼睛疼痛。那么是否有一种安全的方法可以使用asprintf和其他系统库,它们可以在使用Boehm GC的同时调用本机malloc?我应该使用asprintf替代吗?

2 个答案:

答案 0 :(得分:2)

如果提供的缓冲区足够大,

snprintf将返回已写入的字符数。您可以调用此方法两次(一次获得正确的缓冲区大小,然后再使用足够大的缓冲区来获取输出),但这可能不如仅仅将asprintf的输出复制到一个可收集的缓冲区。这是一个包含代码的示例,该代码分配足够大的缓冲区以包含32位系统的无符号long的最大值。在没有足够空间的系统上,缓冲区将重新分配并重新格式化。

#include <limits.h>

...

unsigned long intValue = getValueInt(self);
size_t maxLength = 11; // heuristic
char *buf = malloc(maxLength);

int result = snprintf(buf, maxLength, "%lu", intValue);
if (result > maxLength)
{
    // shouldn't ever get here, but just in case the buffer is too small
    // we reallocate it to the correct size and try again.
    buf = malloc(result);
    snprintf(buf, result, "%lu", intValue);
}

return buf;

答案 1 :(得分:-1)

根据this page,您可以使用

编译libgc(boehm-gc库)
-DREDIRECT_MALLOC=GC_malloc -DIGNORE_FREE

在asprintf中应该intercept the call到malloc。注意:没试过这个。