1 //------------------------------------------------------------------------------
2 // SLIP_LU/Tcov/tcov_malloc_test.c
3 //------------------------------------------------------------------------------
4
5 // SLIP_LU: (c) 2019-2020, Chris Lourenco, Jinhao Chen, Erick Moreno-Centeno,
6 // Timothy A. Davis, Texas A&M University. All Rights Reserved. See
7 // SLIP_LU/License for the license.
8
9 //------------------------------------------------------------------------------
10
11 #include "tcov_malloc_test.h"
12
13 int64_t malloc_count = INT64_MAX ;
14
15 // Note that only the ANSI C memory manager is used here
16 // (malloc, calloc, realloc, free)
17
18 // wrapper for malloc
tcov_malloc(size_t size)19 void *tcov_malloc
20 (
21 size_t size // Size to alloc
22 )
23 {
24 if (--malloc_count < 0)
25 {
26 /* pretend to fail */
27 printf("malloc pretend to fail\n");
28 return (NULL) ;
29 }
30 return (malloc (size)) ;
31 }
32
33 // wrapper for calloc
tcov_calloc(size_t n,size_t size)34 void *tcov_calloc
35 (
36 size_t n, // Size of array
37 size_t size // Size to alloc
38 )
39 {
40 if (--malloc_count < 0)
41 {
42 /* pretend to fail */
43 printf ("calloc pretend to fail\n");
44 return (NULL) ;
45 }
46 // ensure at least one byte is calloc'd
47 return (calloc (n, size)) ;
48 }
49
50 // wrapper for realloc
tcov_realloc(void * p,size_t new_size)51 void *tcov_realloc
52 (
53 void *p, // Pointer to be realloced
54 size_t new_size // Size to alloc
55 )
56 {
57 if (--malloc_count < 0)
58 {
59 /* pretend to fail */
60 printf("realloc pretend to fail\n");
61 return (NULL);
62 }
63 return (realloc (p, new_size)) ;
64 }
65
66 // wrapper for free
tcov_free(void * p)67 void tcov_free
68 (
69 void *p // Pointer to be free
70 )
71 {
72 // This not really needed, but placed here anyway in case the Tcov tests
73 // want to do something different that free(p) in the future.
74 free (p) ;
75 }
76
77 jmp_buf slip_gmp_environment ; // for setjmp and longjmp
78
slip_gmp_realloc_test(void ** p_new,void * p_old,size_t old_size,size_t new_size)79 int slip_gmp_realloc_test
80 (
81 void **p_new,
82 void * p_old,
83 size_t old_size,
84 size_t new_size
85 )
86 {
87 int slip_gmp_status = setjmp (slip_gmp_environment);
88 if (slip_gmp_status != 0)
89 {
90 return SLIP_OUT_OF_MEMORY;
91 }
92 *p_new = slip_gmp_reallocate(p_old, old_size, new_size);
93 return SLIP_OK;
94 }
95