Debian Codegraph @

agent/mibgroup/hardware/memory/hw_mem.c

raw
1 #include <net-snmp/net-snmp-config.h>
2 #include <net-snmp/net-snmp-includes.h>
3 #include <net-snmp/agent/net-snmp-agent-includes.h>
4 #include <net-snmp/agent/hardware/memory.h>
5 #include <net-snmp/net-snmp-features.h>
6 #include "hw_mem.h"
7
8 netsnmp_feature_child_of(hardware_memory, netsnmp_unused);
9
10 netsnmp_feature_child_of(memory_get_cache, hardware_memory);
11
12 netsnmp_memory_info *_mem_head = NULL;
13 netsnmp_cache *_mem_cache = NULL;
14
15 #ifdef darwin
16 /*
17 * the code in memory_darwin.c is *very* expensive
18 */
19 #define MEMORY_CACHE_SECONDS 300
20 #else
21 #define MEMORY_CACHE_SECONDS 5
22 #endif
23
24 void
25 init_hw_mem(void)
26 {
27 oid nsMemory[] = { 1, 3, 6, 1, 4, 1, 8072, 1, 31 };
28 _mem_cache =
29 netsnmp_cache_create(MEMORY_CACHE_SECONDS, netsnmp_mem_arch_load,
30 NULL, nsMemory, OID_LENGTH(nsMemory));
31 }
32
33
34
35 netsnmp_memory_info *
36 netsnmp_memory_get_first(int type)
37 {
38 netsnmp_memory_info *mem;
39
40 for (mem = _mem_head; mem; mem = mem->next)
41 if (mem->type == type) /* Or treat as bits? */
42 return mem;
43 return NULL;
44 }
45
46 netsnmp_memory_info *
47 netsnmp_memory_get_next(netsnmp_memory_info * this_ptr, int type)
48 {
49 netsnmp_memory_info *mem;
50
51 if (this_ptr)
52 for (mem = this_ptr->next; mem; mem = mem->next)
53 if (mem->type == type) /* Or treat as bits? */
54 return mem;
55 return NULL;
56 }
57
58 /*
59 * Work with a list of Memory entries, indexed numerically
60 */
61 netsnmp_memory_info *
62 netsnmp_memory_get_byIdx(int idx, int create)
63 {
64 netsnmp_memory_info *mem, *mem2;
65
66 /*
67 * Find the specified Memory entry
68 */
69 for (mem = _mem_head; mem; mem = mem->next) {
70 if (mem->idx == idx)
71 return mem;
72 }
73 if (!create)
74 return NULL;
75
76 /*
77 * Create a new memory entry, and insert it into the list....
78 */
79 mem = SNMP_MALLOC_TYPEDEF(netsnmp_memory_info);
80 if (!mem)
81 return NULL;
82 mem->idx = idx;
83 /*
84 * ... either as the first (or only) entry....
85 */
86 if (!_mem_head || _mem_head->idx > idx) {
87 mem->next = _mem_head;
88 _mem_head = mem;
89 return mem;
90 }
91 /*
92 * ... or in the appropriate position
93 */
94 for (mem2 = _mem_head; mem2; mem2 = mem2->next) {
95 if (!mem2->next || mem2->next->idx > idx) {
96 mem->next = mem2->next;
97 mem2->next = mem;
98 return mem;
99 }
100 }
101 SNMP_FREE(mem);
102 return NULL; /* Shouldn't happen! */
103 }
104
105 netsnmp_memory_info *
106 netsnmp_memory_get_next_byIdx(int idx, int type)
107 {
108 netsnmp_memory_info *mem;
109
110 for (mem = _mem_head; mem; mem = mem->next)
111 if (mem->type == type && mem->idx > idx) /* Or treat as bits? */
112 return mem;
113 return NULL;
114 }
115
116
117
118 #ifndef NETSNMP_FEATURE_REMOVE_MEMORY_GET_CACHE
119 netsnmp_cache *
120 netsnmp_memory_get_cache(void)
121 {
122 return _mem_cache;
123 }
124 #endif /* NETSNMP_FEATURE_REMOVE_MEMORY_GET_CACHE */
125
126 int
127 netsnmp_memory_load(void)
128 {
129 return netsnmp_cache_check_and_reload(_mem_cache);
130 }