-
Notifications
You must be signed in to change notification settings - Fork 81
/
counters.c
140 lines (122 loc) · 4 KB
/
counters.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (c) 2013-2014 Cloudozer LLP. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Redistributions in any form must be accompanied by information on how to
// obtain complete source code for the LING software and any accompanying
// software that uses the LING software. The source code must either be included
// in the distribution or be available for no more than the cost of distribution
// plus a nominal fee, and must be freely redistributable under reasonable
// conditions. For an executable file, complete source code means the source
// code for all modules it contains. It does not include source code for modules
// or files that typically accompany the major components of the operating
// system on which the executable file runs.
//
// THIS SOFTWARE IS PROVIDED BY CLOUDOZER LLP ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE
// DISCLAIMED. IN NO EVENT SHALL CLOUDOZER LLP BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//
//
#include <stdint.h>
#include <string.h>
#include "ling_common.h"
#include "nalloc.h"
#include "limits.h"
typedef struct counter_t counter_t;
struct counter_t {
uint64_t id;
uint64_t value;
uint64_t mask;
};
// All counters are in a single array
static counter_t *all_counters = 0;
static int nr_counters = 0;
static memnode_t *counters_node = 0;
static counter_t *counter_lookup(uint64_t id);
void counters_init()
{
counters_node = nalloc(QUICK_SIZE -sizeof(memnode_t));
all_counters = (counter_t *)counters_node->starts;
}
uint64_t counter_add(uint64_t mask)
{
static uint64_t next_counter_id = 0;
uint64_t id = next_counter_id++;
if (all_counters +nr_counters +1 > (counter_t *)counters_node->ends)
{
// overflow, need a bigger node
int cur_size = counters_node->index *PAGE_SIZE;
memnode_t *new_node = nalloc(cur_size *2 -sizeof(memnode_t));
memcpy((void *)new_node->starts,
all_counters, nr_counters *sizeof(counter_t));
nfree(counters_node);
counters_node = new_node;
all_counters = (counter_t *)new_node->starts;
}
counter_t *cntr = all_counters +nr_counters;
cntr->id = id;
cntr->value = 0;
cntr->mask = mask;
nr_counters++;
return id;
}
int counter_read(uint64_t id, uint64_t *pval)
{
counter_t *cntr = counter_lookup(id);
if (cntr == 0)
return -NOT_FOUND;
*pval = cntr->value;
return 0;
}
int counter_increment(uint64_t id, uint64_t incr)
{
counter_t *cntr = counter_lookup(id);
if (cntr == 0)
return -NOT_FOUND;
cntr->value += incr;
cntr->value &= cntr->mask;
return 0;
}
int counter_remove(uint64_t id)
{
counter_t *cntr = counter_lookup(id);
if (cntr == 0)
return -NOT_FOUND;
memmove(cntr, cntr +1,
(void *)(all_counters +nr_counters -1) -(void *)cntr);
nr_counters--;
return 0;
}
static counter_t *counter_lookup(uint64_t id)
{
counter_t *alpha = all_counters;
counter_t *beta = all_counters +nr_counters;
while (beta > alpha)
{
counter_t *mid = alpha + (beta-alpha)/2;
if (mid->id == id)
return mid;
if (mid->id > id)
beta = mid;
else
alpha = mid +1;
}
return 0;
}
//EOF