root/ompi/mca/io/romio321/romio/adio/common/malloc.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ADIOI_Malloc_fn
  2. ADIOI_Calloc_fn
  3. ADIOI_Realloc_fn
  4. ADIOI_Free_fn

   1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
   2 /* 
   3  *
   4  *   Copyright (C) 1997 University of Chicago. 
   5  *   See COPYRIGHT notice in top-level directory.
   6  */
   7 
   8 /* These are routines for allocating and deallocating memory.
   9    They should be called as ADIOI_Malloc(size) and
  10    ADIOI_Free(ptr). In adio.h, they are macro-replaced to 
  11    ADIOI_Malloc(size,__LINE__,__FILE__) and 
  12    ADIOI_Free(ptr,__LINE__,__FILE__).
  13 
  14    Later on, add some tracing and error checking, similar to 
  15    MPID_trmalloc. */
  16 
  17 #include "adio.h"
  18 #include "mpi.h"
  19 #include <stdlib.h>
  20 #include <stdio.h>
  21 #include "mpipr.h"
  22 
  23 #ifdef HAVE_MALLOC_H
  24 #include <malloc.h>
  25 #endif
  26 
  27 /* for the style checker */
  28 /* style: allow:malloc:1 sig:0 */
  29 /* style: allow:free:1 sig:0 */
  30 /* style: allow:calloc:1 sig:0 */
  31 /* style: allow:realloc:1 sig:0 */
  32 
  33 #define FPRINTF fprintf
  34 
  35 void *ADIOI_Malloc_fn(size_t size, int lineno, const char *fname);
  36 void *ADIOI_Calloc_fn(size_t nelem, size_t elsize, int lineno, const char *fname);
  37 void *ADIOI_Realloc_fn(void *ptr, size_t size, int lineno, const char *fname);
  38 void ADIOI_Free_fn(void *ptr, int lineno, const char *fname);
  39 
  40 void *ADIOI_Malloc_fn(size_t size, int lineno, const char *fname)
  41 {
  42     void *new;
  43 
  44 #ifdef ROMIO_XFS
  45     new = (void *) memalign(XFS_MEMALIGN, size);
  46 #else
  47 #ifdef HAVE_MPIU_FUNCS
  48     new = (void *) MPIU_trmalloc(size, lineno, fname);
  49 #else
  50     new = (void *) malloc(size);
  51 #endif
  52 #endif
  53     if (!new && size) {
  54         FPRINTF(stderr, "Out of memory in file %s, line %d\n", fname, lineno);
  55         MPI_Abort(MPI_COMM_WORLD, 1);
  56     }
  57     DBG_FPRINTF(stderr, "ADIOI_Malloc %s:<%d> %p (%#zX)\n", fname, lineno, new, size);
  58     return new;
  59 }
  60 
  61 
  62 void *ADIOI_Calloc_fn(size_t nelem, size_t elsize, int lineno, const char *fname)
  63 {
  64     void *new;
  65 
  66 #ifdef HAVE_MPIU_FUNCS
  67     new = (void *) MPIU_trcalloc(nelem, elsize, lineno, fname);
  68 #else
  69     new = (void *) calloc(nelem, elsize);
  70 #endif
  71     if (!new && nelem) {
  72         FPRINTF(stderr, "Out of memory in file %s, line %d\n", fname, lineno);
  73         MPI_Abort(MPI_COMM_WORLD, 1);
  74     }
  75     DBG_FPRINTF(stderr, "ADIOI_Calloc %s:<%d> %p\n", fname, lineno, new);
  76     return new;
  77 }
  78 
  79 
  80 void *ADIOI_Realloc_fn(void *ptr, size_t size, int lineno, const char *fname)
  81 {
  82     void *new;
  83 
  84 #ifdef HAVE_MPIU_FUNCS
  85     new = (void *) MPIU_trrealloc(ptr, size, lineno, fname);
  86 #else
  87     new = (void *) realloc(ptr, size);
  88 #endif
  89     if (!new && size) {
  90         FPRINTF(stderr, "realloc failed in file %s, line %d\n", fname, lineno);
  91         MPI_Abort(MPI_COMM_WORLD, 1);
  92     }
  93     DBG_FPRINTF(stderr, "ADIOI_Realloc %s:<%d> %p\n", fname, lineno, new);
  94     return new;
  95 }
  96 
  97 
  98 void ADIOI_Free_fn(void *ptr, int lineno, const char *fname)
  99 {
 100     DBG_FPRINTF(stderr, "ADIOI_Free %s:<%d> %p\n", fname, lineno, ptr);
 101     if (!ptr) {
 102         FPRINTF(stderr, "Attempt to free null pointer in file %s, line %d\n", fname, lineno);
 103         MPI_Abort(MPI_COMM_WORLD, 1);
 104     }
 105 
 106 #ifdef HAVE_MPIU_FUNCS
 107     MPIU_trfree(ptr, lineno, fname);
 108 #else
 109     free(ptr);
 110 #endif
 111 }
 112 
 113 

/* [<][>][^][v][top][bottom][index][help] */