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>
11 #ifndef PCEP_UTILS_INCLUDE_PCEP_UTILS_MEMORY_H_
12 #define PCEP_UTILS_INCLUDE_PCEP_UTILS_MEMORY_H_
18 /* This module is intended to be used primarily with FRR's memory module,
19 * which has memory groups and memory types, although any memory infrastructure
20 * can be used that has memory types or the memory types in this module can be
21 * set to NULL. The PCEPlib can be used stand-alone, in which case the simple
22 * internal memory type system will be used.
25 /* These memory function pointers are modeled after the memory functions
26 * in frr/lib/memory.h */
27 typedef void *(*pceplib_malloc_func
)(void *mem_type
, size_t size
);
28 typedef void *(*pceplib_calloc_func
)(void *mem_type
, size_t size
);
29 typedef void *(*pceplib_realloc_func
)(void *mem_type
, void *ptr
, size_t size
);
30 typedef void *(*pceplib_strdup_func
)(void *mem_type
, const char *str
);
31 typedef void (*pceplib_free_func
)(void *mem_type
, void *ptr
);
33 /* Either an internal pceplib_memory_type pointer
34 * or could be an FRR memory type pointer */
35 extern void *PCEPLIB_INFRA
;
36 extern void *PCEPLIB_MESSAGES
;
38 /* Internal PCEPlib memory type */
39 struct pceplib_memory_type
{
40 char memory_type_name
[64];
41 uint32_t total_bytes_allocated
;
42 uint32_t num_allocates
;
43 uint32_t total_bytes_freed
; /* currently not used */
47 /* Initialize this module by passing in the 2 memory types used in the PCEPlib
48 * and by passing in the different memory allocation/free function pointers.
49 * Any of these parameters can be NULL, in which case an internal implementation
52 bool pceplib_memory_initialize(void *pceplib_infra_mt
,
53 void *pceplib_messages_mt
,
54 pceplib_malloc_func mfunc
,
55 pceplib_calloc_func cfunc
,
56 pceplib_realloc_func rfunc
,
57 pceplib_strdup_func sfunc
,
58 pceplib_free_func ffunc
);
60 /* Reset the internal allocation/free counters. Used mainly for internal
62 void pceplib_memory_reset(void);
63 void pceplib_memory_dump(void);
65 /* Memory functions to be used throughout the PCEPlib. Internally, these
66 * functions will either used the function pointers passed in via
67 * pceplib_memory_initialize() or a simple internal implementation. The
68 * internal implementations just increment the internal memory type
69 * counters and call the C stdlib memory functions.
71 void *pceplib_malloc(void *mem_type
, size_t size
);
72 void *pceplib_calloc(void *mem_type
, size_t size
);
73 void *pceplib_realloc(void *mem_type
, void *ptr
, size_t size
);
74 void *pceplib_strdup(void *mem_type
, const char *str
);
75 void pceplib_free(void *mem_type
, void *ptr
);
77 #endif /* PCEP_UTILS_INCLUDE_PCEP_UTILS_MEMORY_H_ */