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