LD_PRELOADing Malloc und frei

Ich habe mein eigenes geschriebenMalloc undkostenlos und kompilierte sie in einer gemeinsam genutzten Bibliothek. Ich LD_PRELOAD diese Bibliothek mit meinem Programm. Auf diese Weise würde mein Programm immer meine Gewohnheit verwendenMalloc undkostenlos oder gibt es Fälle, in denen es nicht so ist. Ich habe gehört, dass gcc malloc eingebaut hat undkostenlos auch. Ist es möglich, dass der mit meinem gcc gelieferte glibc den eingebauten verwendet?Malloc undkostenlos.

Zweitens stelle ich fest, dass ich beim Ausführen meines Programms diekostenlos Funktionsaufruf öfter als derMalloc / Calloc Anrufe (98 bis 16). Ich mache keine Speicherzuweisung selbst (außer an einer Stelle), so dass die gesamte Zuweisung über die von mir verwendeten Standardbibliotheksfunktionen erfolgt. Beachten Sie auch, dass ich in meinem Programm pthread verwende. Wenn Sie wissen wollen, sieht mein Programm so aus.

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>

#define NUM_THREADS     8

pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;

int sum;
float total = 1;
extern int __did_libc_start_main;

void *PrintHello(void *threadid)
{
   long tid;
   tid = (long)threadid;
   pthread_mutex_lock( &m );
   sum++;
   total *= total + tid * 0.097891313423578;
   printf( "p%d, tid%d, total = %g, start = %d!\n", getpid(), tid, total, 0 );
   pthread_mutex_unlock( &m );
   printf("Hello World! It's me, thread #%ld!\n", tid);
   pthread_exit(NULL);
}

int main (int argc, char *argv[])
{
   pthread_t threads[NUM_THREADS];
   int rc;
   long t;
   char * p;
   char * m;

   fork();

   p = (char*)mmap(0, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
   p[0] = 78;
   printf( "p = %p, p[0] = %d, pid = %d!\n", p, p[0], getpid() );
   m = (char*)malloc( 80 );
   printf( "m = %p!\n", m );
#if 1  
   for(t=0; t<NUM_THREADS; t++)
   {
      printf("In main: creating thread %ld\n", t);
      rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
      if (rc){
         printf("ERROR; return code from pthread_create() is %d\n", rc);
         exit(-1);
      }
   }
   for(t=0; t<NUM_THREADS; t++)
    pthread_join(threads[t], NULL);

   printf( "\n\nTotal = %g\n\n", total );

   /* Last thing that main() should do */
   pthread_exit(NULL);
#endif
   printf( "\n\n%d: Done without major problems\n\n", getpid() );
   return 0;
}

Antworten auf die Frage(1)

Ihre Antwort auf die Frage