Source

mpi3-fortran / ompi / contrib / vt / vt / vtlib / vt_mallocwrap.c

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
/**
 * VampirTrace
 * http://www.tu-dresden.de/zih/vampirtrace
 *
 * Copyright (c) 2005-2012, ZIH, TU Dresden, Federal Republic of Germany
 *
 * Copyright (c) 1998-2005, Forschungszentrum Juelich, Juelich Supercomputing
 *                          Centre, Federal Republic of Germany
 *
 * See the file COPYING in the package base directory for details
 **/

#define _BSD_SOURCE /* possibly needed for valloc */

#include "config.h"

#include "vt_defs.h"
#include "vt_env.h"
#include "vt_inttypes.h"
#include "vt_libwrap.h"
#include "vt_mallocwrap.h"
#include "vt_pform.h"
#include "vt_thrd.h"
#include "vt_trc.h"

#include <errno.h>
#include <stdlib.h>
#if (defined(HAVE_MEMALIGN) && HAVE_MEMALIGN) || \
    (defined(HAVE_VALLOC) &&  HAVE_VALLOC)
# include <malloc.h>
#endif /* HAVE_MEMALIGN || HAVE_VALLOC */


/* define the following macro to enable tracing the calloc function which
   needs a quite dirty hack to make it work (see comments in the calloc
   wrapper function below) */
#define MALLOCWRAP_CALLOC


/* special version of VT_LIBWRAP_FUNC_INIT w/o getting the actual function
   pointer (VT_LIBWRAP_FUNC_PTR) and the unique function identifier
   (VT_LIBWRAP_FUNC_ID) */
#define MALLOCWRAP_FUNC_INIT(_func, _rettype, _argtypes)                      \
  _VT_LIBWRAP_FUNC_INIT_DECL_VARS(_func, _rettype, _argtypes);                \
  if( mallocwrap_lw == VT_LIBWRAP_NULL ) {                                    \
    VTLibwrap_create(&mallocwrap_lw, &mallocwrap_lw_attr);                    \
  }

/* get pointer to actual library function
   (sets VT_LIBWRAP_FUNC_PTR, must be called after MALLOCWRAP_FUNC_INIT!) */
#define MALLOCWRAP_GET_FUNC_PTR()                                             \
  if( VT_LIBWRAP_FUNC_PTR == VT_LIBWRAP_NULL ) {                              \
    VTLibwrap_func_init(mallocwrap_lw, VT_LIBWRAP_FUNC_NAME, NULL, 0,         \
      (void**)(&VT_LIBWRAP_FUNC_PTR), NULL);                                  \
  }

/* get unique function identifier
   (sets VT_LIBWRAP_FUNC_ID, must be called after MALLOCWRAP_FUNC_INIT!) */
#define MALLOCWRAP_GET_FUNC_ID()                                              \
  if( VT_LIBWRAP_FUNC_ID == VT_LIBWRAP_NOID ) {                               \
    VTLibwrap_func_init(mallocwrap_lw, VT_LIBWRAP_FUNC_NAME, NULL, 0,         \
      NULL, &VT_LIBWRAP_FUNC_ID);                                             \
  }

/* simplified version of VT_LIBWRAP_FUNC_CALL w/o argument for the library
   wrapper object */
#define MALLOCWRAP_FUNC_CALL(_args) VT_LIBWRAP_FUNC_CALL(mallocwrap_lw, _args)

/* check whether tracing of LIBC memory (de)allocation functions is
   currently enabled */
#define MALLOCWRAP_DO_TRACE()                                                 \
  ( vt_is_alive && VT_MY_THREAD_IS_ALIVE &&                                   \
    VTTHRD_MALLOC_TRACING_ENABLED(VTTHRD_MY_VTTHRD) )

/* library wrapper object */
static VTLibwrap* mallocwrap_lw = VT_LIBWRAP_NULL;

/* library wrapper attributes */
static VTLibwrapAttr mallocwrap_lw_attr = {

  /* The functions to be wrapped are defined in the LIBC which is linked to
     the application. So there is no need to search the actual function
     pointers in an additional library. */
  0,        /* shlibs_num */
  { NULL }, /* shlibs */

  /* Function group to define, finally including the recorded LIBC memory
     (de)allocation functions */
  "LIBC-MALLOC", /* func_group */

  /* Do not search the actual function pointers in an external LIBC, because
     dlopen calls malloc which would result in an infinite recursion when
     determining the actual function pointer of malloc. Using RTLD_NEXT
     instead. */
  0, /* libc */

  /* Do not initialize VampirTrace when creating the library wrapper object,
     resp. when a wrapper function is entered */
  1 /* wait_for_init */
};

/* id of memory related counter group */
static uint32_t mallocwrap_counter_group_id = 0;

/* id of memory allocation counter */
static uint32_t mallocwrap_counter_id = 0;

/* ids of memory (de)allocation markers */
static uint32_t mallocwrap_marker_alloc_id = 0;
static uint32_t mallocwrap_marker_free_id = 0;

/* flag: write memory (de)allocation markers? (env. VT_MEMTRACE_MARKER) */
static uint32_t mallocwrap_write_markers = 0;


/* memory allocation wrapper initialization/finalization functions called
   by vt_open/vt_close */

void vt_mallocwrap_init()
{
  /* define memory related counter group */
  mallocwrap_counter_group_id =
    vt_def_counter_group(VT_CURRENT_THREAD, "Memory");

  /* define memory allocation counter */
  mallocwrap_counter_id =
    vt_def_counter(VT_CURRENT_THREAD, "Memory Allocation", "Bytes",
      VT_CNTR_ABS | VT_CNTR_NEXT, mallocwrap_counter_group_id, 0);

  /* define memory (de)allocation markers, if desired
     (env. VT_MEMTRACE_MARKER) */
  if( (mallocwrap_write_markers = vt_env_memtrace_marker()) )
  {
    mallocwrap_marker_alloc_id =
      vt_def_marker(VT_CURRENT_THREAD, "Memory Allocation", VT_MARKER_HINT);
    mallocwrap_marker_free_id =
      vt_def_marker(VT_CURRENT_THREAD, "Memory Deallocation", VT_MARKER_HINT);
  }
}

void vt_mallocwrap_finalize()
{
  /* delete library wrapper object, if necessary */
  if( mallocwrap_lw != VT_LIBWRAP_NULL )
    VTLibwrap_delete(mallocwrap_lw);
}


/* wrapper functions */

/* -- stdlib.h:malloc -- */
void* malloc(size_t size)
{
  void* ret;

  /* initialize this wrapper function */
  MALLOCWRAP_FUNC_INIT("malloc", void*, (size_t));

  /* once, get the actual function pointer */
  MALLOCWRAP_GET_FUNC_PTR();

  if( MALLOCWRAP_DO_TRACE() )
  {
    uint32_t tid;
    uint64_t time;
    uint64_t bytes;
    uint64_t* counter_val;
    uint8_t was_recorded;

    /* get calling thread id */
    tid = VT_MY_THREAD;

    /* suspend LIBC memory (de)allocation tracing */
    VT_SUSPEND_MALLOC_TRACING(tid);

    /* get timestamp for the following function enter event */
    time = vt_pform_wtime();

    /* once, get unique function identifier */
    MALLOCWRAP_GET_FUNC_ID();

    /* record function enter event */
    was_recorded = vt_enter(tid, &time, VT_LIBWRAP_FUNC_ID);

    /* call the actual library function */
    ret = MALLOCWRAP_FUNC_CALL((size));

    /* get total allocated memory */
    if( ret != NULL )
    {
/*      bytes = ( ~ (uint64_t) 3 ) & (uint64_t) *( (size_t*) ( (char*)ret - SIZEOF_VOIDP ) );*/
      bytes = (uint64_t)malloc_usable_size(ret);
    }
    else
    {
      bytes = 0;
    }

    /* get pointer to thread's memory allocation counter value and update */
    counter_val = &(VTTHRD_MALLOC_TRACING_COUNTER_VAL(VTThrdv[tid]));
    *counter_val += bytes;

    /* get timestamp for the following function exit event [+ marker] */
    time = vt_pform_wtime();

    if( was_recorded && bytes > 0 )
    {
      /* write marker, if desired */
      if( mallocwrap_write_markers )
      {
        vt_marker(tid, &time, mallocwrap_marker_alloc_id,
          "Allocated %llu Bytes", (unsigned long long)bytes);
      }

      /* write counter value */
      vt_count(tid, &time, mallocwrap_counter_id, *counter_val);
    }

    /* record function exit event */
    vt_exit(tid, &time);

    /* resume LIBC memory (de)allocation tracing */
    VT_RESUME_MALLOC_TRACING(tid);
  }
  else
  {
    /* call the actual library function */
    ret = MALLOCWRAP_FUNC_CALL((size));
  }

  /* get errno from external LIBC (not necessary if using RTLD_NEXT) */
  /*errno = vt_libwrap_get_libc_errno();*/

  return ret;
}

#ifdef MALLOCWRAP_CALLOC

/* -- stdlib.h:calloc -- */
void* calloc(size_t nmemb, size_t size)
{
  void* ret;

  /* initialize this wrapper function */
  MALLOCWRAP_FUNC_INIT("calloc", void*, (size_t, size_t));

  /* once, get the actual function pointer

     NOTE: The dlsym function which is used to determine the actual function
     pointer of calloc uses itself this function, which would ends up in an
     infinite recursion.
     In order to make it work we have to perform a quite dirty hack found on
     http://blog.bigpixel.ro/2010/09/interposing-calloc-on-linux:
     While we are trying to get the actual function pointer, we're returning
     NULL for the memory which needs to be allocated by dlsym, in hope that
     dlsym can handle this situation.
     If this workaround causes any problems, just undefine the MALLOCWRAP_CALLOC
     macro above to disable the calloc wrapper function completely. */
  if( VT_LIBWRAP_FUNC_PTR == VT_LIBWRAP_NULL )
  {
    /* flag for indicating that we are trying to get the actual function
       pointer of calloc */
    static uint8_t getting_func_ptr = 0;
    if( !getting_func_ptr )
    {
      /* before trying to get the actual function pointer of calloc, set
         an indicator in order to return NULL from the next calloc called from
         dlsym */
      getting_func_ptr = 1;
      VTLibwrap_func_init(mallocwrap_lw, VT_LIBWRAP_FUNC_NAME, NULL, 0,
        (void**)(&VT_LIBWRAP_FUNC_PTR), NULL);
      getting_func_ptr = 0;
    }
    else
    {
      /* assumed that this calloc is called from dlsym, return NULL */
      return NULL;
    }
  }

  if( MALLOCWRAP_DO_TRACE() )
  {
    uint32_t tid;
    uint64_t time;
    uint64_t bytes;
    uint64_t* counter_val;
    uint8_t was_recorded;

    /* get calling thread id */
    tid = VT_MY_THREAD;

    /* suspend LIBC memory (de)allocation tracing */
    VT_SUSPEND_MALLOC_TRACING(tid);

    /* get current timestamp for the following function enter event */
    time = vt_pform_wtime();

    /* once, get unique function identifier */
    MALLOCWRAP_GET_FUNC_ID();

    /* record function enter event */
    was_recorded = vt_enter(tid, &time, VT_LIBWRAP_FUNC_ID);

    /* call the actual library function */
    ret = MALLOCWRAP_FUNC_CALL((nmemb, size));

    /* get total allocated memory */
    if( ret != NULL )
    {
/*      bytes = ( ~ (uint64_t) 3 ) & (uint64_t) *( (size_t*) ( (char*)ret - SIZEOF_VOIDP ) );*/
      bytes = (uint64_t)malloc_usable_size(ret);
    }
    else
    {
      bytes = 0;
    }

    /* get pointer to thread's memory allocation counter value and update */
    counter_val = &(VTTHRD_MALLOC_TRACING_COUNTER_VAL(VTThrdv[tid]));
    *counter_val += bytes;

    /* get timestamp for the following function exit event [+ marker] */
    time = vt_pform_wtime();

    if( was_recorded && bytes > 0 )
    {
      /* write marker, if desired */
      if( mallocwrap_write_markers )
      {
        vt_marker(tid, &time, mallocwrap_marker_alloc_id,
          "Allocated %llu Bytes", (unsigned long long)bytes);
      }

      /* write counter value */
      vt_count(tid, &time, mallocwrap_counter_id, *counter_val);
    }

    /* record function exit event */
    vt_exit(tid, &time);

    /* resume LIBC memory (de)allocation tracing */
    VT_RESUME_MALLOC_TRACING(tid);
  }
  else
  {
    /* call the actual library function */
    ret = MALLOCWRAP_FUNC_CALL((nmemb, size));
  }

  /* get errno from external LIBC (not necessary if using RTLD_NEXT) */
  /*errno = vt_libwrap_get_libc_errno();*/

  return ret;
}

#endif /* MALLOCWRAP_CALLOC */

/* -- stdlib.h:realloc -- */
void* realloc(void* ptr, size_t size)
{
  void* ret;

  /* initialize this wrapper function */
  MALLOCWRAP_FUNC_INIT("realloc", void*, (void*, size_t));

  /* once, get the actual function pointer */
  MALLOCWRAP_GET_FUNC_PTR();

  if( MALLOCWRAP_DO_TRACE() )
  {
    uint32_t tid;
    uint64_t time;
    uint64_t bytes;
    uint64_t bytes1;
    uint64_t bytes2;
    uint64_t* counter_val;
    uint8_t was_recorded;

    /* get calling thread id */
    tid = VT_MY_THREAD;

    /* suspend LIBC memory (de)allocation tracing */
    VT_SUSPEND_MALLOC_TRACING(tid);

    /* get current timestamp for the following function enter event */
    time = vt_pform_wtime();

    /* once, get unique function identifier */
    MALLOCWRAP_GET_FUNC_ID();

    /* record function enter event */
    was_recorded = vt_enter(tid, &time, VT_LIBWRAP_FUNC_ID);

    /* get total allocated memory before realloc */
    if( ptr != NULL )
    {
/*      bytes1 = ( ~ (uint64_t) 3 ) & (uint64_t) *( (size_t*) ( (char*)ptr - SIZEOF_VOIDP ) );*/
      bytes1 = (uint64_t)malloc_usable_size(ptr);
    }
    else
    {
      bytes1 = bytes = 0;
    }

    /* call the actual library function */
    ret = MALLOCWRAP_FUNC_CALL((ptr, size));

    /* get total allocated memory after realloc */
    if( ret != NULL )
    {
/*      bytes2 = ( ~ (uint64_t) 3 ) & (uint64_t) *( (size_t*) ( (char*)ret - SIZEOF_VOIDP ) );*/
      bytes2 = (uint64_t)malloc_usable_size(ret);
      bytes = bytes2 < bytes1 ? bytes1 - bytes2 : bytes2 - bytes1;
    }
    else
    {
      bytes2 = bytes = 0;
    }

    /* get pointer to thread's memory allocation counter value and update */
    counter_val = &(VTTHRD_MALLOC_TRACING_COUNTER_VAL(VTThrdv[tid]));
    if( bytes2 < bytes1 )
    {
      if( bytes <= *counter_val )
        *counter_val -= bytes;
      else
        *counter_val = 0;
    }
    else
    {
      *counter_val += bytes;
    }

    /* get timestamp for the following function exit event [+ marker] */
    time = vt_pform_wtime();

    if( was_recorded && bytes > 0 )
    {
      /* write marker, if desired */
      if( mallocwrap_write_markers )
      {
        static const char* marker_prefix_alloced = "Allocated";
        static const char* marker_prefix_freed   = "Freed";

        uint32_t marker_id;
        const char* marker_prefix;

        if ( bytes2 < bytes1 )
        {
          marker_id = mallocwrap_marker_free_id;
          marker_prefix = marker_prefix_freed;
        }
        else
        {
          marker_id = mallocwrap_marker_alloc_id;
          marker_prefix = marker_prefix_alloced;
        }

        vt_marker(tid, &time, marker_id,
          "%s %llu Bytes", marker_prefix, (unsigned long long)bytes);
      }

      /* write counter value */
      vt_count(tid, &time, mallocwrap_counter_id, *counter_val);
    }

    /* record function exit event */
    vt_exit(tid, &time);

    /* resume LIBC memory (de)allocation tracing */
    VT_RESUME_MALLOC_TRACING(tid);
  }
  else
  {
    /* call the actual library function */
    ret = MALLOCWRAP_FUNC_CALL((ptr, size));
  }

  /* get errno from external LIBC (not necessary if using RTLD_NEXT) */
  /*errno = vt_libwrap_get_libc_errno();*/

  return ret;
}

/* -- stdlib.h:free -- */
void free(void* ptr)
{
  /* initialize this wrapper function */
  MALLOCWRAP_FUNC_INIT("free", void, (void*));

  /* once, get the actual function pointer */
  MALLOCWRAP_GET_FUNC_PTR();

  if( MALLOCWRAP_DO_TRACE() )
  {
    uint32_t tid;
    uint64_t time;
    uint64_t bytes;
    uint64_t* counter_val;
    uint8_t was_recorded;

    /* get calling thread id */
    tid = VT_MY_THREAD;

    /* suspend LIBC memory (de)allocation tracing */
    VT_SUSPEND_MALLOC_TRACING(tid);

    /* get current timestamp for the following function enter event */
    time = vt_pform_wtime();

    /* once, get unique function identifier */
    MALLOCWRAP_GET_FUNC_ID();

    /* record function enter event */
    was_recorded = vt_enter(tid, &time, VT_LIBWRAP_FUNC_ID);

    /* get total allocated memory to be freed */
    if( ptr != NULL )
    {
/*      bytes = ( ~ (uint64_t) 3 ) & (uint64_t) *( (size_t*) ( (char*)ptr - SIZEOF_VOIDP ) );*/
      bytes = (uint64_t)malloc_usable_size(ptr);
    }
    else
    {
      bytes = 0;
    }

    /* call the actual library function */
    MALLOCWRAP_FUNC_CALL((ptr));

    /* get pointer to thread's memory allocation counter value and update */
    counter_val = &(VTTHRD_MALLOC_TRACING_COUNTER_VAL(VTThrdv[tid]));
    if( bytes <= *counter_val )
      *counter_val -= bytes;
    else
      *counter_val = 0;

    /* get timestamp for the following function exit event [+ marker] */
    time = vt_pform_wtime();

    if( was_recorded && bytes > 0 )
    {
      /* write marker, if desired */
      if( mallocwrap_write_markers )
      {
        vt_marker(tid, &time, mallocwrap_marker_free_id,
          "Freed %llu Bytes", (unsigned long long)bytes);
      }

      /* write counter value */
      vt_count(tid, &time, mallocwrap_counter_id, *counter_val);
    }

    /* record function exit event */
    vt_exit(tid, &time);

    /* resume LIBC memory (de)allocation tracing */
    VT_RESUME_MALLOC_TRACING(tid);
  }
  else
  {
    /* call the actual library function */
    MALLOCWRAP_FUNC_CALL((ptr));
  }

  /* get errno from external LIBC (not necessary if using RTLD_NEXT) */
  /*errno = vt_libwrap_get_libc_errno();*/
}


#if defined(HAVE_POSIX_MEMALIGN) && HAVE_POSIX_MEMALIGN

/* -- stdlib.h:posix_memalign -- */
int posix_memalign(void** memptr, size_t alignment, size_t size)
{
  int ret;

  /* initialize this wrapper function */
  MALLOCWRAP_FUNC_INIT("posix_memalign", int, (void**, size_t, size_t));

  /* once, get the actual function pointer */
  MALLOCWRAP_GET_FUNC_PTR();

  if( MALLOCWRAP_DO_TRACE() )
  {
    uint32_t tid;
    uint64_t time;
    uint64_t bytes;
    uint64_t* counter_val;
    uint8_t was_recorded;

    /* get calling thread id */
    tid = VT_MY_THREAD;

    /* suspend LIBC memory (de)allocation tracing */
    VT_SUSPEND_MALLOC_TRACING(tid);

    /* get current timestamp for the following function enter event */
    time = vt_pform_wtime();

    /* once, get unique function identifier */
    MALLOCWRAP_GET_FUNC_ID();

    /* record function enter event */
    was_recorded = vt_enter(tid, &time, VT_LIBWRAP_FUNC_ID);

    /* call the actual library function */
    ret = MALLOCWRAP_FUNC_CALL((memptr, alignment, size));

    /* get total allocated memory */
    if( ret == 0 && memptr != NULL )
    {
/*      bytes = ( ~ (uint64_t) 3 ) & (uint64_t) *( (size_t*) ( (char*)*memptr - SIZEOF_VOIDP ) );*/
      bytes = (uint64_t)malloc_usable_size(*memptr);
    }
    else
    {
      bytes = 0;
    }

    /* get pointer to thread's memory allocation counter value and update */
    counter_val = &(VTTHRD_MALLOC_TRACING_COUNTER_VAL(VTThrdv[tid]));
    *counter_val += bytes;

    /* get timestamp for the following function exit event [+ marker] */
    time = vt_pform_wtime();

    if( was_recorded && bytes > 0 )
    {
      /* write marker, if desired */
      if( mallocwrap_write_markers )
      {
        vt_marker(tid, &time, mallocwrap_marker_alloc_id,
          "Allocated %llu Bytes", (unsigned long long)bytes);
      }

      /* write counter value */
      vt_count(tid, &time, mallocwrap_counter_id, *counter_val);
    }

    /* record function exit event */
    vt_exit(tid, &time);

    /* resume LIBC memory (de)allocation tracing */
    VT_RESUME_MALLOC_TRACING(tid);
  }
  else
  {
    /* call the actual library function */
    ret = MALLOCWRAP_FUNC_CALL((memptr, alignment, size));
  }

  return ret;
}

#endif /* HAVE_POSIX_MEMALIGN */

#if defined(HAVE_MEMALIGN) && HAVE_MEMALIGN

/* -- malloc.h:memalign -- */
void* memalign(size_t boundary, size_t size)
{
  void* ret;

  /* initialize this wrapper function */
  MALLOCWRAP_FUNC_INIT("memalign", void*, (size_t, size_t));

  /* once, get the actual function pointer */
  MALLOCWRAP_GET_FUNC_PTR();

  if( MALLOCWRAP_DO_TRACE() )
  {
    uint32_t tid;
    uint64_t time;
    uint64_t bytes;
    uint64_t* counter_val;
    uint8_t was_recorded;

    /* get calling thread id */
    tid = VT_MY_THREAD;

    /* suspend LIBC memory (de)allocation tracing */
    VT_SUSPEND_MALLOC_TRACING(tid);

    /* get current timestamp for the following function enter event */
    time = vt_pform_wtime();

    /* once, get unique function identifier */
    MALLOCWRAP_GET_FUNC_ID();

    /* record function enter event */
    was_recorded = vt_enter(tid, &time, VT_LIBWRAP_FUNC_ID);

    /* call the actual library function */
    ret = MALLOCWRAP_FUNC_CALL((boundary, size));

    /* get total allocated memory */
    if( ret != NULL )
    {
/*      bytes = ( ~ (uint64_t) 3 ) & (uint64_t) *( (size_t*) ( (char*)ret - SIZEOF_VOIDP ) );*/
      bytes = (uint64_t)malloc_usable_size(ret);
    }
    else
    {
      bytes = 0;
    }

    /* get pointer to thread's memory allocation counter value and update */
    counter_val = &(VTTHRD_MALLOC_TRACING_COUNTER_VAL(VTThrdv[tid]));
    *counter_val += bytes;

    /* get timestamp for the following function exit event [+ marker] */
    time = vt_pform_wtime();

    if( was_recorded && bytes > 0 )
    {
      /* write marker, if desired */
      if( mallocwrap_write_markers )
      {
        vt_marker(tid, &time, mallocwrap_marker_alloc_id,
          "Allocated %llu Bytes", (unsigned long long)bytes);
      }

      /* write counter value */
      vt_count(tid, &time, mallocwrap_counter_id, *counter_val);
    }

    /* record function exit event */
    vt_exit(tid, &time);

    /* resume LIBC memory (de)allocation tracing */
    VT_RESUME_MALLOC_TRACING(tid);
  }
  else
  {
    /* call the actual library function */
    ret = MALLOCWRAP_FUNC_CALL((boundary, size));
  }

  /* get errno from external LIBC (not necessary if using RTLD_NEXT) */
  /*errno = vt_libwrap_get_libc_errno();*/

  return ret;
}

#endif /* HAVE_MEMALIGN */

#if defined(HAVE_VALLOC) && HAVE_VALLOC

/* -- malloc.h:valloc -- */
void* valloc(size_t size)
{
  void* ret;

  /* initialize this wrapper function */
  MALLOCWRAP_FUNC_INIT("valloc", void*, (size_t));

  /* once, get the actual function pointer */
  MALLOCWRAP_GET_FUNC_PTR();

  if( MALLOCWRAP_DO_TRACE() )
  {
    uint32_t tid;
    uint64_t time;
    uint64_t bytes;
    uint64_t* counter_val;
    uint8_t was_recorded;

    /* get calling thread id */
    tid = VT_MY_THREAD;

    /* suspend LIBC memory (de)allocation tracing */
    VT_SUSPEND_MALLOC_TRACING(tid);

    /* get current timestamp for the following function enter event */
    time = vt_pform_wtime();

    /* once, get unique function identifier */
    MALLOCWRAP_GET_FUNC_ID();

    /* record function enter event */
    was_recorded = vt_enter(tid, &time, VT_LIBWRAP_FUNC_ID);

    /* call the actual library function */
    ret = MALLOCWRAP_FUNC_CALL((size));

    /* get total allocated memory */
    if( ret != NULL )
    {
/*      bytes = ( ~ (uint64_t) 3 ) & (uint64_t) *( (size_t*) ( (char*)ret - SIZEOF_VOIDP ) );*/
      bytes = (uint64_t)malloc_usable_size(ret);
    }
    else
    {
      bytes = 0;
    }

    /* get pointer to thread's memory allocation counter value and update */
    counter_val = &(VTTHRD_MALLOC_TRACING_COUNTER_VAL(VTThrdv[tid]));
    *counter_val += bytes;

    /* get timestamp for the following function exit event [+ marker] */
    time = vt_pform_wtime();

    if( was_recorded && bytes > 0 )
    {
      /* write marker, if desired */
      if( mallocwrap_write_markers )
      {
        vt_marker(tid, &time, mallocwrap_marker_alloc_id,
          "Allocated %llu Bytes", (unsigned long long)bytes);
      }

      /* write counter value */
      vt_count(tid, &time, mallocwrap_counter_id, *counter_val);
    }

    /* record function exit event */
    vt_exit(tid, &time);

    /* resume LIBC memory (de)allocation tracing */
    VT_RESUME_MALLOC_TRACING(tid);
  }
  else
  {
    /* call the actual library function */
    ret = MALLOCWRAP_FUNC_CALL((size));
  }

  /* get errno from external LIBC (not necessary if using RTLD_NEXT) */
  /*errno = vt_libwrap_get_libc_errno();*/

  return ret;
}

#endif /* HAVE_VALLOC */