This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

malloc returning high memory addresses with pthread_create


Hi,

I observed a behavior with malloc returning high memory addresses and
I'm wondering if someone could give me some insight into why this
happens.

The attached program sets the M_MMAP_MAX threshold to 0 (ie disabling
the use of mmap by malloc), spawns a thread which simply allocates and
frees 4KB chunk of memory. Now, according to whether both threads
(main thread and the spawned thread) execute concurrently or not, I
see a difference in the values of the pointers returned.

The program when compiled with:

$ gcc -DMALLOC_SERIALIZED mallopt_test.c -lpthread

does not return any high memory addresses. Whereas, when compiled with:

$ gcc -DMALLOC_CONCURRENT mallopt_test.c -lpthread

returns high memory addresses, which are printed out like:

...
Got ptr 0x2aaaaab008d0 in Main thread
Got ptr 0x2aaaaab008d0 in Main thread
Got ptr 0x2aaaaab008d0 in Main thread
...

Why is that when both threads execute concurrently, memory is
allocated at such high values? Is it a known issue? or is it an issue
at all?

My machine is an EM64T Xeon 3.4 GHz with 2GB memory. I'm using RedHat
AS 4 (Red Hat Enterprise Linux AS release 4 (Nahant Update 2)).

TIA,
Sayantan.

--
http://www.cse.ohio-state.edu/~surs
#include <stdio.h>
#include <malloc.h>
#include <stdint.h>
#include <pthread.h>

#define ONE_G (1024*1024*1024)

volatile int barr_flag = 1;

void my_thread_func()
{
    int j;
    void *thread_ptr;

#ifdef MALLOC_CONCURRENT
    barr_flag = 0;
#endif

    for(j = 0; j < 10000; j++) {

        thread_ptr = malloc(4096);

        if((uintptr_t) thread_ptr > ONE_G) {
            fprintf(stdout,"Got ptr %p in Child thread\n",
                    thread_ptr);
        }

        free(thread_ptr);
    }

#ifdef MALLOC_SERIALIZED
    barr_flag = 0;
#endif

}

int main(int argc, char* argv[])
{
    pthread_t my_thread;
    int i;
    void * main_ptr;

    mallopt(M_MMAP_MAX, 0);

    pthread_create(&my_thread, NULL, (void *) my_thread_func, NULL);

    /* Child thread SYNC */
    while(barr_flag == 1);

    for(i = 0; i < 10000; i++) {
        main_ptr = malloc(4096);

        if((uintptr_t) main_ptr > ONE_G) {
            fprintf(stdout,"Got ptr %p in Main thread\n", main_ptr);
        }

        free(main_ptr);
    }


    pthread_cancel(my_thread);

    return 0;
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]