2

私の知る限り、asprintfはmallocを呼び出します。mallocをBoehmGCに置き換えた場合でも、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でもあります。では、Boehm GCを使用しながら、ネイティブのmallocを呼び出す可能性のあるasprintfやその他のシステムライブラリを安全に使用する方法はありますか?代わりに使用する必要があるasprintfの代替手段はありますか?

4

2 に答える 2

2

snprintf提供されたバッファーが十分に大きい場合に書き込まれたであろう文字数を返します。asprintfこのメソッドを2回呼び出すことができます(1回は適切なバッファーサイズを取得し、次に出力を取得するのに十分な大きさのバッファーを使用して)。ただし、これは、の出力を収集可能なバッファーにコピーするよりも効率が悪い可能性があります。これは、32ビットシステムのunsignedlongの最大値を含むのに十分な大きさのバッファーを割り当てるコードを含む例です。十分なスペースがないシステムでは、バッファーが再割り当てされ、再フォーマットされます。

#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;
于 2010-09-07T00:47:33.770 に答える
-1

このページによると、libgc(boehm-gcライブラリ)を次のようにコンパイルできます。

-DREDIRECT_MALLOC=GC_malloc -DIGNORE_FREE

asprintfでmallocへの呼び出しをインターセプトする必要があります。注意:これは試していません。

于 2011-05-03T13:42:15.160 に答える