1 /* ========================================================================== */
2 /* === UMF_malloc =========================================================== */
3 /* ========================================================================== */
4 
5 /* -------------------------------------------------------------------------- */
6 /* Copyright (c) 2005-2012 by Timothy A. Davis, http://www.suitesparse.com.   */
7 /* All Rights Reserved.  See ../Doc/License.txt for License.                  */
8 /* -------------------------------------------------------------------------- */
9 
10 /*
11     Allocate a block of n objects, each of a given size.  This routine does not
12     handle the case when the size is 1 (allocating char's) because of potential
13     integer overflow.  UMFPACK never does that.
14     Also maintains the UMFPACK malloc count.
15 */
16 
17 #include "umf_internal.h"
18 #include "umf_malloc.h"
19 
20 #if defined (UMF_MALLOC_COUNT) || !defined (NDEBUG)
21 
22 /*
23     UMF_malloc_count is a count of the objects malloc'd by UMFPACK.  If you
24     suspect a memory leak in your program (caused by not properly destroying
25     the Symbolic and Numeric objects) then compile with -DUMF_MALLOC_COUNT and
26     check value of UMF_malloc_count.  By default, UMF_MALLOC_COUNT is not
27     defined, and thus UMFPACK has no global variables.
28 */
29 
30 GLOBAL Int UMF_malloc_count = 0 ;
31 
32 #endif
33 
34 #ifdef UMF_TCOV_TEST
35 /* For exhaustive statement coverage testing only! */
36 GLOBAL int umf_fail, umf_fail_lo, umf_fail_hi ;
37 GLOBAL int umf_realloc_fail, umf_realloc_lo, umf_realloc_hi ;
38 #endif
39 
UMF_malloc(Int n_objects,size_t size_of_object)40 GLOBAL void *UMF_malloc
41 (
42     Int n_objects,
43     size_t size_of_object
44 )
45 {
46     size_t size ;
47     void *p ;
48 
49 #ifdef UMF_TCOV_TEST
50     /* For exhaustive statement coverage testing only! */
51     /* Pretend to fail, to test out-of-memory conditions. */
52     umf_fail-- ;
53     if (umf_fail <= umf_fail_hi && umf_fail >= umf_fail_lo)
54     {
55 	DEBUG0 (("umf_malloc: Pretend to fail %d %d %d\n",
56 	    umf_fail, umf_fail_hi, umf_fail_lo)) ;
57 	return ((void *) NULL) ;
58     }
59 #endif
60 
61     DEBUG0 (("UMF_malloc: ")) ;
62 
63     p = SuiteSparse_malloc (n_objects, size_of_object) ;
64 
65     DEBUG0 ((ID"\n", (Int) p)) ;
66 
67 #if defined (UMF_MALLOC_COUNT) || !defined (NDEBUG)
68     if (p)
69     {
70 	/* One more object has been malloc'ed.  Keep track of the count. */
71 	/* (purely for sanity checks). */
72 	UMF_malloc_count++ ;
73 	DEBUG0 (("  successful, new malloc count: "ID"\n", UMF_malloc_count)) ;
74     }
75 #endif
76 
77     return (p) ;
78 }
79