• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Alan-Jowett / cudd / 19144810377

06 Nov 2025 05:48PM UTC coverage: 32.985% (-0.007%) from 32.992%
19144810377

push

github

Alan Jowett
Merge branch '4.0.0' of https://github.com/Alan-Jowett/cudd into 4.0.0

Signed-off-by: Alan Jowett <alan.jowett@microsoft.com>

9789 of 29677 relevant lines covered (32.99%)

318758.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

40.0
/src/safe_mem.c
1
/**
2
  @file
3

4
  @ingroup util
5

6
  @brief Interface routines to be placed between a program and the
7
  system memory allocator.  
8

9
  The function pointer MMoutOfMemory() contains a vector to handle a
10
  'out-of-memory' error (which, by default, points at a simple wrap-up 
11
  and exit routine).
12

13
  @copyright@parblock
14
  Copyright (c) 1994-1998 The Regents of the Univ. of California.
15
  All rights reserved.
16

17
  Permission is hereby granted, without written agreement and without license
18
  or royalty fees, to use, copy, modify, and distribute this software and its
19
  documentation for any purpose, provided that the above copyright notice and
20
  the following two paragraphs appear in all copies of this software.
21

22
  IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
23
  DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
24
  OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
25
  CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26

27
  THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
28
  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29
  FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN
30
  "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE
31
  MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
32
  @endparblock
33

34
  @copyright@parblock
35
  Copyright (c) 1999-2015, Regents of the University of Colorado
36

37
  All rights reserved.
38

39
  Redistribution and use in source and binary forms, with or without
40
  modification, are permitted provided that the following conditions
41
  are met:
42

43
  Redistributions of source code must retain the above copyright
44
  notice, this list of conditions and the following disclaimer.
45

46
  Redistributions in binary form must reproduce the above copyright
47
  notice, this list of conditions and the following disclaimer in the
48
  documentation and/or other materials provided with the distribution.
49

50
  Neither the name of the University of Colorado nor the names of its
51
  contributors may be used to endorse or promote products derived from
52
  this software without specific prior written permission.
53

54
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
57
  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
58
  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
59
  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
60
  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
61
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
62
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63
  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
64
  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
65
  POSSIBILITY OF SUCH DAMAGE.
66
  @endparblock
67

68
*/
69

70
#include "util.h"
71

72
#ifdef __cplusplus
73
extern "C" {
74
#endif
75

76
/**
77
 * @brief Global out-of-memory handler.
78
 */
79
void (*MMoutOfMemory)(size_t) = MMout_of_memory;
80

81
/**
82
 * @brief Function pointer for out-of-memory injection callback.
83
 * 
84
 * This function pointer can be set by test applications to inject
85
 * out-of-memory failures at runtime. If not NULL, this function is
86
 * called before each memory allocation attempt. If it returns non-zero,
87
 * the allocation will fail immediately without calling malloc/realloc.
88
 * The parameter is the requested allocation size.
89
 */
90
int (*MMoutOfMemoryInjector)(size_t) = NULL;
91

92
#ifdef __cplusplus
93
}
94
#endif
95

96

97
/**
98
 * @brief Out of memory for lazy people: flush and exit.
99
 */
100
void 
101
MMout_of_memory(size_t size)
×
102
{
103
    (void) fflush(stdout);
×
104
    (void) fprintf(stderr,
×
105
                   "\nCUDD: out of memory allocating %" PRIszt " bytes\n",
106
                   (size_t) size);
107
    exit(1);
×
108
}
109

110
/**
111
 * @brief malloc replacement.
112
 */
113
void *
114
MMalloc(size_t size)
18,289,756✔
115
{
116
    void *p;
117

118
    /* Check if out-of-memory injection is enabled and should trigger */
119
    if (MMoutOfMemoryInjector != NULL && (*MMoutOfMemoryInjector)(size)) {
18,289,756✔
120
        /* Simulate allocation failure */
121
        if (MMoutOfMemory != 0) (*MMoutOfMemory)(size);
×
122
        return NIL(void);
×
123
    }
124

125
    if ((p = malloc(size)) == NIL(void)) {
18,289,756✔
126
        if (MMoutOfMemory != 0 ) (*MMoutOfMemory)(size);
×
127
        return NIL(void);
×
128
    }
129
    return p;
18,289,756✔
130
}
131

132

133
/**
134
 * @brief realloc replacement.
135
 */
136
void *
137
MMrealloc(void *obj, size_t size)
73✔
138
{
139
    void *p;
140

141
    /* Check if out-of-memory injection is enabled and should trigger */
142
    if (MMoutOfMemoryInjector != NULL && (*MMoutOfMemoryInjector)(size)) {
73✔
143
        /* Simulate allocation failure */
144
        if (MMoutOfMemory != 0) (*MMoutOfMemory)(size);
×
145
        return NIL(void);
×
146
    }
147

148
    if ((p = realloc(obj, size)) == NIL(void)) {
73✔
149
        if (MMoutOfMemory != 0 ) (*MMoutOfMemory)(size);
×
150
        return NIL(void);
×
151
    }
152
    return p;
73✔
153
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc