double *Zoltan_Realloc(void *ptr, int n, char *filename, int lineno) { char *yo = "Zoltan_Realloc"; struct malloc_debug_data *dbptr; /* loops through debug list */ int proc; /* processor ID */ double *p; /* returned pointer */ if (ptr == NULL) { /* Previous allocation not important */ if (n == 0) { p = NULL; } else { p = Zoltan_Malloc(n, filename, lineno); } } else { if (n == 0) { Zoltan_Free((void **) &ptr, filename, lineno); p = NULL; } else { p = (double *) realloc((char *) ptr, n); if (DEBUG_MEMORY > 1) { /* Need to replace item in allocation list */ for (dbptr = top; dbptr != NULL && (void *) (dbptr->ptr) != ptr; dbptr = dbptr->next); if (dbptr == NULL) { /* previous allocation not found in list. */ GET_RANK(&proc); fprintf(stderr, "Proc %d: Memory error: " "In realloc, address not found in debug list (%p)\n", proc, ptr); } else { /* Update entry in allocation list */ bytes_used += (n - dbptr->size); dbptr->size = n; dbptr->ptr = p; if (bytes_used > bytes_max) { bytes_max = bytes_used; } } } if (p == NULL) { GET_RANK(&proc); fprintf(stderr, "%s (from %s,%d) No space on proc %d - " "number of bytes requested = %d\n", yo, filename, lineno, proc, n); } } } return (p); } /* Zoltan_Realloc */
double *Zoltan_Realloc(void *ptr, size_t n, char *filename, int lineno) { char *yo = "Zoltan_Realloc"; struct malloc_debug_data *dbptr; /* loops through debug list */ int proc; /* processor ID */ double *p; /* returned pointer */ #ifdef REALLOC_BUG int n_old; #endif if (ptr == NULL) { /* Previous allocation not important */ if (n == 0) { p = NULL; } else { p = Zoltan_Malloc(n, filename, lineno); } } else { if (n == 0) { Zoltan_Free((void **) &ptr, filename, lineno); p = NULL; } else { #ifdef REALLOC_BUG /* Feb 10, 2010: Several platforms show a realloc bug where realloc * either fails to allocate memory when there is sufficient memory * or it crashes. If realloc shows this failure, then build Zoltan * with REALLOC_BUG, and we will call malloc/memcpy/free instead. */ p = (double *)ptr; p--; n_old = p[0]; if ((n_old < 1) || (n_old > max_alloc)){ /* sanity check */ GET_RANK(&proc); fprintf(stderr, "%s (from %s,%d) Zoltan_Realloc called on a pointer " "that was not returned by Zoltan_Malloc (proc %d)\n", yo, filename, lineno, proc); return NULL; } p = (double *) Zoltan_Malloc(n, filename, lineno); if (p){ if (n > n_old){ memcpy(p, ptr, n_old); } else if (n <= n_old){ memcpy(p, ptr, n); } Zoltan_Free((void **) &ptr, filename, lineno); } #else p = (double *) realloc((char *) ptr, n); if (DEBUG_MEMORY > 1) { /* Need to replace item in allocation list */ for (dbptr = top; dbptr != NULL && (void *) (dbptr->ptr) != ptr; dbptr = dbptr->next); if (dbptr == NULL) { /* previous allocation not found in list. */ GET_RANK(&proc); fprintf(stderr, "Proc %d: Memory error: " "In realloc, address not found in debug list (0x%lx)\n", proc, (long) ptr); } else { /* Update entry in allocation list */ bytes_used += (n - dbptr->size); dbptr->size = n; dbptr->ptr = p; if (bytes_used > bytes_max) { bytes_max = bytes_used; } } } if (p == NULL) { GET_RANK(&proc); fprintf(stderr, "%s (from %s,%d) No space on proc %d - " "number of bytes requested = %lu\n", yo, filename, lineno, proc, (unsigned long) n); } #endif } } return (p); } /* Zoltan_Realloc */