Logo Search packages:      
Sourcecode: qfits version File versions  Download package

void* qfits_memory_realloc ( void *  ptr,
size_t  size,
const char *  filename,
int  lineno 
)

Re-Allocate memory.

Parameters:
ptr Pointer to free.
size Size (in bytes) to allocate.
filename Name of the file where the alloc took place.
lineno Line number in the file.
Returns:
1 newly allocated pointer.
This function is a replacement call for realloc. It should never be called directly but through a macro instead, as:

  qfits_memory_realloc(nmemb, size, __FILE__, __LINE__)

Definition at line 969 of file qfits_memory.c.

References qfits_memory_free(), and qfits_memory_malloc().

{
    void    *   ptr2 ;
    size_t      small_sz ;
    size_t      ptr_sz ;
    int         pos = -1 ;
    int         i ;
    
    /* If QFITS_MEMORY_MODE is 0 or 1, do not use the qfits_memory model  */
    if (QFITS_MEMORY_MODE == 0) return realloc(ptr, size) ;
    else if (QFITS_MEMORY_MODE == 1) {
        ptr2 = realloc(ptr, size) ;
        if (ptr2 == NULL) exit(1) ;
        else return ptr2 ;
    }

    if (ptr == NULL) return qfits_memory_malloc(size, filename, lineno) ;

    /* Get the pointer size */
    for (i=0 ; i<QFITS_MEMORY_MAXPTRS ; i++) {
        if (qfits_memory_p_val[i] == NULL) continue ;
        if (qfits_memory_p_val[i] == ptr) {
            pos = i ;
            break ;
        }
    }
    if (pos==-1) {
        fprintf(stderr,
            "qfits_mem: %s (%d) realloc requested on unallocated ptr (%p)\n",
            filename, lineno, ptr);
        /* Pointer sent to system's realloc() function, maybe it should not? */
        return realloc(ptr, size) ;
    }
    ptr_sz = qfits_memory_p_size[pos] ;
    
    /* Compute the smaller size */
    small_sz = size < ptr_sz ? size : ptr_sz ;
    
    /* Allocate the new pointer */
    ptr2 = qfits_memory_malloc(size, filename, lineno) ;
    
    /* Copy the common data */
    memcpy(ptr2, ptr, small_sz) ;

    /* Free the passed ptr */
    qfits_memory_free(ptr, filename, lineno) ;
    
    /* Return  */
    return ptr2 ;
}


Generated by  Doxygen 1.6.0   Back to index