ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
allocate.c
Go to the documentation of this file.
1#include "allocate.h"
2#include "error.h"
3#include "internal.h"
4#include "logging.h"
5
6#undef LOGPREFIX
7#define LOGPREFIX "ALLOCATE"
8
9#include <string.h>
10
11#ifdef LOGGING
12#include <inttypes.h>
13#endif
14
16
17static void inc_bytes (kissat *solver, size_t bytes) {
18#ifdef METRICS
19 if (!solver)
20 return;
21 ADD (allocated_current, bytes);
22 LOG5 ("allocated_current = %s",
23 FORMAT_BYTES (solver->statistics.allocated_current));
24 if (solver->statistics.allocated_current >=
25 solver->statistics.allocated_max) {
26 solver->statistics.allocated_max = solver->statistics.allocated_current;
27 LOG5 ("allocated_max = %s",
28 FORMAT_BYTES (solver->statistics.allocated_max));
29 }
30#else
31 (void) solver;
32 (void) bytes;
33#endif
34}
35
36static void dec_bytes (kissat *solver, size_t bytes) {
37#ifdef METRICS
38 if (!solver)
39 return;
40 SUB (allocated_current, bytes);
41 LOG5 ("allocated_current = %s",
42 FORMAT_BYTES (solver->statistics.allocated_current));
43#else
44 (void) solver;
45 (void) bytes;
46#endif
47}
48
49void *kissat_malloc (kissat *solver, size_t bytes) {
50 void *res;
51 if (!bytes)
52 return 0;
53 res = malloc (bytes);
54 LOG4 ("malloc (%zu) = %p", bytes, res);
55 if (!res)
56 kissat_fatal ("out-of-memory allocating %zu bytes", bytes);
57 inc_bytes (solver, bytes);
58 return res;
59}
60
61void kissat_free (kissat *solver, void *ptr, size_t bytes) {
62 if (ptr) {
63 LOG4 ("free (%p[%zu])", ptr, bytes);
64 dec_bytes (solver, bytes);
65 free (ptr);
66 } else
67 KISSAT_assert (!bytes);
68}
69
70char *kissat_strdup (kissat *solver, const char *str) {
71 char *res = (char*)kissat_malloc (solver, strlen (str) + 1);
72 return strcpy (res, str);
73}
74
75void kissat_freestr (struct kissat *solver, char *str) {
76 KISSAT_assert (str);
77 kissat_free (solver, str, strlen (str) + 1);
78}
79
80void *kissat_nalloc (kissat *solver, size_t n, size_t size) {
81 void *res;
82 if (!n || !size)
83 return 0;
84 if (MAX_SIZE_T / size < n)
85 kissat_fatal ("invalid 'kissat_nalloc (..., %zu, %zu)' call", n, size);
86 const size_t bytes = n * size;
87 res = malloc (bytes);
88 LOG4 ("nalloc (%zu, %zu) = %p", n, size, res);
89 if (!res)
90 kissat_fatal ("out-of-memory allocating "
91 "%zu = %zu x %zu bytes",
92 bytes, n, size);
93 inc_bytes (solver, bytes);
94 return res;
95}
96
97void *kissat_calloc (kissat *solver, size_t n, size_t size) {
98 void *res;
99 if (!n || !size)
100 return 0;
101 if (MAX_SIZE_T / size < n)
102 kissat_fatal ("invalid 'kissat_calloc (..., %zu, %zu)' call", n, size);
103 res = calloc (n, size);
104 LOG4 ("calloc (%zu, %zu) = %p", n, size, res);
105 const size_t bytes = n * size;
106 if (!res)
107 kissat_fatal ("out-of-memory allocating "
108 "%zu = %zu x %zu bytes",
109 bytes, n, size);
110 inc_bytes (solver, bytes);
111 return res;
112}
113
114void kissat_dealloc (kissat *solver, void *ptr, size_t n, size_t size) {
115 if (!n || !size)
116 return;
117 if (MAX_SIZE_T / size < n)
118 kissat_fatal ("invalid 'kissat_dealloc (..., %zu, %zu)' call", n, size);
119 const size_t bytes = n * size;
120 kissat_free (solver, ptr, bytes);
121}
122
123void *kissat_realloc (kissat *solver, void *p, size_t old_bytes,
124 size_t new_bytes) {
125 if (old_bytes == new_bytes)
126 return p;
127 if (!new_bytes) {
128 kissat_free (solver, p, old_bytes);
129 return 0;
130 }
131 dec_bytes (solver, old_bytes);
132#ifdef LOGGING
133 if (GET_OPTION (log) > 3)
134 kissat_begin_logging (solver, LOGPREFIX, "realloc (%p[%zu, %zu) = ", p,
135 old_bytes, new_bytes);
136#endif
137 void *res = realloc (p, new_bytes);
138#ifdef LOGGING
139 if (GET_OPTION (log) > 3) {
140 printf ("%p", res);
141 kissat_end_logging ();
142 }
143#endif
144 if (new_bytes && !res)
145 kissat_fatal ("out-of-memory reallocating from %zu to %zu bytes",
146 old_bytes, new_bytes);
147 inc_bytes (solver, new_bytes);
148 return res;
149}
150
151void *kissat_nrealloc (kissat *solver, void *p, size_t o, size_t n,
152 size_t size) {
153 if (!size) {
154 KISSAT_assert (!p);
155 KISSAT_assert (!o);
156 return 0;
157 }
158 const size_t max = MAX_SIZE_T / size;
159 if (max < o || max < n)
160 kissat_fatal ("invalid 'kissat_nrealloc (..., %zu, %zu, %zu)' call", o,
161 n, size);
162 return kissat_realloc (solver, p, o * size, n * size);
163}
164
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
void * kissat_malloc(kissat *solver, size_t bytes)
Definition allocate.c:49
void * kissat_nalloc(kissat *solver, size_t n, size_t size)
Definition allocate.c:80
#define LOGPREFIX
Definition allocate.c:7
void * kissat_calloc(kissat *solver, size_t n, size_t size)
Definition allocate.c:97
void kissat_freestr(struct kissat *solver, char *str)
Definition allocate.c:75
void kissat_free(kissat *solver, void *ptr, size_t bytes)
Definition allocate.c:61
void * kissat_realloc(kissat *solver, void *p, size_t old_bytes, size_t new_bytes)
Definition allocate.c:123
void * kissat_nrealloc(kissat *solver, void *p, size_t o, size_t n, size_t size)
Definition allocate.c:151
void kissat_dealloc(kissat *solver, void *ptr, size_t n, size_t size)
Definition allocate.c:114
char * kissat_strdup(kissat *solver, const char *str)
Definition allocate.c:70
#define ADD(NAME, DELTA)
void kissat_fatal(const char *fmt,...)
Definition error.c:58
Cube * p
Definition exorList.c:222
#define FORMAT_BYTES(BYTES)
Definition format.h:31
#define KISSAT_assert(ignore)
Definition global.h:13
#define solver
Definition kitten.c:211
#define LOG4(...)
Definition logging.h:358
#define LOG5(...)
Definition logging.h:361
#define GET_OPTION(N)
Definition options.h:295
#define SUB(NAME, N)
Definition statistics.h:415
char * calloc()
int strlen()
char * strcpy()
VOID_HACK free()
char * malloc()
char * realloc()
#define MAX_SIZE_T
Definition utilities.h:45