1 // SPDX-License-Identifier: LGPL-2.1-or-later
3 * This file is part of the PCEPlib, a PCEP protocol library.
5 * Copyright (C) 2020 Volta Networks https://voltanet.io/
7 * Author : Brady Johnson <brady@voltanet.io>
18 #include "pcep_utils_logging.h"
19 #include "pcep_utils_memory.h"
21 /* Set default values for memory function pointers */
22 static pceplib_malloc_func mfunc
= NULL
;
23 static pceplib_calloc_func cfunc
= NULL
;
24 static pceplib_realloc_func rfunc
= NULL
;
25 static pceplib_strdup_func sfunc
= NULL
;
26 static pceplib_free_func ffunc
= NULL
;
28 /* Internal memory types */
29 struct pceplib_memory_type pceplib_infra_mt
= {
30 .memory_type_name
= "PCEPlib Infrastructure memory",
31 .total_bytes_allocated
= 0,
33 .total_bytes_freed
= 0,
35 struct pceplib_memory_type pceplib_messages_mt
= {
36 .memory_type_name
= "PCEPlib Messages memory",
37 .total_bytes_allocated
= 0,
39 .total_bytes_freed
= 0,
42 /* The memory type pointers default to the internal memory types */
43 void *PCEPLIB_INFRA
= &pceplib_infra_mt
;
44 void *PCEPLIB_MESSAGES
= &pceplib_messages_mt
;
46 /* Initialize memory function pointers and memory type pointers */
47 bool pceplib_memory_initialize(void *pceplib_infra_mt
,
48 void *pceplib_messages_mt
,
49 pceplib_malloc_func mf
, pceplib_calloc_func cf
,
50 pceplib_realloc_func rf
, pceplib_strdup_func sf
,
53 PCEPLIB_INFRA
= (pceplib_infra_mt
? pceplib_infra_mt
: PCEPLIB_INFRA
);
55 (pceplib_messages_mt
? pceplib_messages_mt
: PCEPLIB_MESSAGES
);
57 mfunc
= (mf
? mf
: mfunc
);
58 cfunc
= (cf
? cf
: cfunc
);
59 rfunc
= (rf
? rf
: rfunc
);
60 sfunc
= (sf
? sf
: sfunc
);
61 ffunc
= (ff
? ff
: ffunc
);
66 void pceplib_memory_reset(void)
68 pceplib_infra_mt
.total_bytes_allocated
= 0;
69 pceplib_infra_mt
.num_allocates
= 0;
70 pceplib_infra_mt
.total_bytes_freed
= 0;
71 pceplib_infra_mt
.num_frees
= 0;
73 pceplib_messages_mt
.total_bytes_allocated
= 0;
74 pceplib_messages_mt
.num_allocates
= 0;
75 pceplib_messages_mt
.total_bytes_freed
= 0;
76 pceplib_messages_mt
.num_frees
= 0;
79 void pceplib_memory_dump(void)
84 "%s: Memory Type [%s] Total [allocs, alloc bytes, frees] [%d, %d, %d]",
86 ((struct pceplib_memory_type
*)PCEPLIB_INFRA
)
88 ((struct pceplib_memory_type
*)PCEPLIB_INFRA
)
90 ((struct pceplib_memory_type
*)PCEPLIB_INFRA
)
91 ->total_bytes_allocated
,
92 ((struct pceplib_memory_type
*)PCEPLIB_INFRA
)
96 if (PCEPLIB_MESSAGES
) {
99 "%s: Memory Type [%s] Total [allocs, alloc bytes, frees] [%d, %d, %d]",
101 ((struct pceplib_memory_type
*)PCEPLIB_MESSAGES
)
103 ((struct pceplib_memory_type
*)PCEPLIB_MESSAGES
)
105 ((struct pceplib_memory_type
*)PCEPLIB_MESSAGES
)
106 ->total_bytes_allocated
,
107 ((struct pceplib_memory_type
*)PCEPLIB_MESSAGES
)
112 /* PCEPlib memory functions:
113 * They either call the supplied function pointers, or use the internal
114 * implementations, which just increment simple counters and call the
115 * C stdlib memory implementations. */
117 void *pceplib_malloc(void *mem_type
, size_t size
)
120 if (mem_type
!= NULL
) {
121 ((struct pceplib_memory_type
*)mem_type
)
122 ->total_bytes_allocated
+= size
;
123 ((struct pceplib_memory_type
*)mem_type
)
129 return mfunc(mem_type
, size
);
133 void *pceplib_calloc(void *mem_type
, size_t size
)
136 if (mem_type
!= NULL
) {
137 ((struct pceplib_memory_type
*)mem_type
)
138 ->total_bytes_allocated
+= size
;
139 ((struct pceplib_memory_type
*)mem_type
)
143 return calloc(1, size
);
145 return cfunc(mem_type
, size
);
149 void *pceplib_realloc(void *mem_type
, void *ptr
, size_t size
)
152 if (mem_type
!= NULL
) {
153 /* TODO should add previous allocated bytes to
154 * total_bytes_freed */
155 ((struct pceplib_memory_type
*)mem_type
)
156 ->total_bytes_allocated
+= size
;
157 ((struct pceplib_memory_type
*)mem_type
)
161 return realloc(ptr
, size
);
163 return rfunc(mem_type
, ptr
, size
);
167 void *pceplib_strdup(void *mem_type
, const char *str
)
170 if (mem_type
!= NULL
) {
171 ((struct pceplib_memory_type
*)mem_type
)
172 ->total_bytes_allocated
+= strlen(str
);
173 ((struct pceplib_memory_type
*)mem_type
)
179 return sfunc(mem_type
, str
);
183 void pceplib_free(void *mem_type
, void *ptr
)
186 if (mem_type
!= NULL
) {
187 /* TODO in order to increment total_bytes_freed, we need
188 * to keep track of the bytes allocated per pointer.
189 * Currently not implemented. */
190 ((struct pceplib_memory_type
*)mem_type
)->num_frees
++;
191 if (((struct pceplib_memory_type
*)mem_type
)
193 < ((struct pceplib_memory_type
*)mem_type
)
197 "%s: pceplib_free MT N_Alloc < N_Free: MemType [%s] NumAllocates [%d] NumFrees [%d]",
199 ((struct pceplib_memory_type
*)mem_type
)
201 ((struct pceplib_memory_type
*)mem_type
)
203 ((struct pceplib_memory_type
*)mem_type
)
210 return ffunc(mem_type
, ptr
);