]>
Commit | Line | Data |
---|---|---|
74971473 JG |
1 | /* |
2 | * This file is part of the PCEPlib, a PCEP protocol library. | |
3 | * | |
4 | * Copyright (C) 2020 Volta Networks https://voltanet.io/ | |
5 | * | |
6 | * This library is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU Lesser General Public | |
8 | * License as published by the Free Software Foundation; either | |
9 | * version 2 of the License, or (at your option) any later version. | |
10 | * | |
11 | * This library is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * Lesser General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | |
18 | * | |
19 | * Author : Brady Johnson <brady@voltanet.io> | |
20 | * | |
21 | */ | |
22 | ||
23 | #ifndef PCEP_UTILS_INCLUDE_PCEP_UTILS_MEMORY_H_ | |
24 | #define PCEP_UTILS_INCLUDE_PCEP_UTILS_MEMORY_H_ | |
25 | ||
26 | #include <stdbool.h> | |
27 | #include <stddef.h> | |
28 | #include <stdint.h> | |
29 | ||
30 | /* This module is intended to be used primarily with FRR's memory module, | |
31 | * which has memory groups and memory types, although any memory infrastructure | |
32 | * can be used that has memory types or the memory types in this module can be | |
33 | * set to NULL. The PCEPlib can be used stand-alone, in which case the simple | |
34 | * internal memory type system will be used. | |
35 | */ | |
36 | ||
37 | /* These memory function pointers are modeled after the memory functions | |
38 | * in frr/lib/memory.h */ | |
39 | typedef void *(*pceplib_malloc_func)(void *mem_type, size_t size); | |
40 | typedef void *(*pceplib_calloc_func)(void *mem_type, size_t size); | |
41 | typedef void *(*pceplib_realloc_func)(void *mem_type, void *ptr, size_t size); | |
42 | typedef void *(*pceplib_strdup_func)(void *mem_type, const char *str); | |
43 | typedef void (*pceplib_free_func)(void *mem_type, void *ptr); | |
44 | ||
45 | /* Either an internal pceplib_memory_type pointer | |
46 | * or could be an FRR memory type pointer */ | |
47 | extern void *PCEPLIB_INFRA; | |
48 | extern void *PCEPLIB_MESSAGES; | |
49 | ||
50 | /* Internal PCEPlib memory type */ | |
51 | struct pceplib_memory_type { | |
52 | char memory_type_name[64]; | |
53 | uint32_t total_bytes_allocated; | |
54 | uint32_t num_allocates; | |
55 | uint32_t total_bytes_freed; /* currently not used */ | |
56 | uint32_t num_frees; | |
57 | }; | |
58 | ||
59 | /* Initialize this module by passing in the 2 memory types used in the PCEPlib | |
60 | * and by passing in the different memory allocation/free function pointers. | |
61 | * Any of these parameters can be NULL, in which case an internal implementation | |
62 | * will be used. | |
63 | */ | |
64 | bool pceplib_memory_initialize(void *pceplib_infra_mt, | |
65 | void *pceplib_messages_mt, | |
66 | pceplib_malloc_func mfunc, | |
67 | pceplib_calloc_func cfunc, | |
68 | pceplib_realloc_func rfunc, | |
69 | pceplib_strdup_func sfunc, | |
70 | pceplib_free_func ffunc); | |
71 | ||
72 | /* Reset the internal allocation/free counters. Used mainly for internal | |
73 | * testing. */ | |
74 | void pceplib_memory_reset(void); | |
75 | void pceplib_memory_dump(void); | |
76 | ||
77 | /* Memory functions to be used throughout the PCEPlib. Internally, these | |
78 | * functions will either used the function pointers passed in via | |
79 | * pceplib_memory_initialize() or a simple internal implementation. The | |
80 | * internal implementations just increment the internal memory type | |
81 | * counters and call the C stdlib memory functions. | |
82 | */ | |
83 | void *pceplib_malloc(void *mem_type, size_t size); | |
84 | void *pceplib_calloc(void *mem_type, size_t size); | |
85 | void *pceplib_realloc(void *mem_type, void *ptr, size_t size); | |
86 | void *pceplib_strdup(void *mem_type, const char *str); | |
87 | void pceplib_free(void *mem_type, void *ptr); | |
88 | ||
89 | #endif /* PCEP_UTILS_INCLUDE_PCEP_UTILS_MEMORY_H_ */ |