rsstats 0.0.1
Redis Enterprise Statistic collector
sclist.c
Go to the documentation of this file.
1
19#ifdef HAVE_CONFIG_H
20#include "config.h"
21#endif
22
23#include "sclist.h"
24
25#include <stdlib.h>
26#include <stdio.h>
27#include <assert.h>
28
31typedef struct sclistrecord_s {
32 void* value;
35
38typedef struct sclist_s {
42
44 sclist_t* sclist;
45 if (NULL==(sclist=malloc(sizeof(struct sclist_s)))) {
46 perror("sclist_new OOM");
47 abort();
48 }
49 sclist->first=NULL;
50 sclist->last=NULL;
51
52 return sclist;
53}
54
55void sclist_del(sclist_t* sclist) {
56 assert(sclist);
57
58 while (sclist->first!=NULL) {
59 sclistrecord_t* first = sclist->first;
60 sclist->first = sclist->first->next;
61 free(first);
62 }
63 free(sclist);
64}
65
67 sclistrecord_t* record;
68
69 assert(sclist);
70
71 /* Create the record record */
72 if (NULL==(record=malloc(sizeof(struct sclistrecord_s)))) {
73 perror("sclist_addrecord OOM");
74 abort();
75 }
76 record->value = value;
77 record->next = NULL;
78
79 /* Add to the list */
80 if (NULL==sclist->first) {
81 /* First record to be added in an empty list */
82 /* A sentinel record would avoid this test and optimize performances
83 * at the cost of sentinel size RAM consumption */
84 sclist->first = record;
85 sclist->last = record;
86 } else {
87 /* Add to the end of a non-empty list */
88 sclist->last->next = record;
89 sclist->last = record;
90 }
91 return record;
92}
93
95 sclistrecord_t* cur;
96 sclistrecord_t* prev;
97
98 assert(sclist);
99 assert(record);
100
101 for (cur=sclist->first, prev=NULL; (cur)&&(cur!=record); prev=cur,cur=cur->next);
102
103 /* If found */
104 if (NULL!=cur) {
105 /* Remove from the chain */
106 if (sclist->first==cur)
107 sclist->first = cur->next;
108 else
109 prev->next = cur->next;
110 /* Update the last pointer if needed */
111 if (sclist->last==cur)
112 sclist->last = prev;
113 } else {
114 fprintf(stderr,"sclist_remrecord record not found.\n");
115 abort();
116 }
117}
118
120 assert(sclist);
121
122 return sclist->first;
123}
124
126 assert(record);
127
128 return record->next;
129}
130
132 assert(record);
133
134 return record->value;
135}
136/* vim: set tw=80: */
#define NULL
Definition: rsstats-opts.c:64
void * sclist_getvalue(sclistrecord_t *record)
Returns the value pointer stored in the record.
Definition: sclist.c:131
sclist_t * sclist_new()
Allocate and initialize the internal list structure.
Definition: sclist.c:43
sclistrecord_t * sclist_nextrecord(const sclistrecord_t *record)
Returns the pointer on the record following the specified one.
Definition: sclist.c:125
void sclist_del(sclist_t *sclist)
Free all the list structure but not the values.
Definition: sclist.c:55
struct sclistrecord_s sclistrecord_t
Private list record structure.
sclistrecord_t * sclist_addrecord(sclist_t *sclist, void *value)
Add a value at the end of the list.
Definition: sclist.c:66
struct sclist_s sclist_t
Opaque sclist structure.
sclistrecord_t * sclist_firstrecord(const sclist_t *sclist)
Returns the pointer on the first list record.
Definition: sclist.c:119
void sclist_remrecord(sclist_t *sclist, sclistrecord_t *record)
Remove a record in a list.
Definition: sclist.c:94
Basic single chained generic list.
Opaque sclist structure.
Definition: sclist.c:38
sclistrecord_t * last
Pointer to the last record.
Definition: sclist.c:40
sclistrecord_t * first
Pointer to the first record.
Definition: sclist.c:39
Private list record structure.
Definition: sclist.c:31
void * value
Pointer to value.
Definition: sclist.c:32
struct sclistrecord_s * next
Next record in the list.
Definition: sclist.c:33