File: ets_cache.c

package info (click to toggle)
erlang-p1-cache-tab 1.0.31-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 376 kB
  • sloc: erlang: 1,365; ansic: 123; makefile: 32
file content (171 lines) | stat: -rw-r--r-- 4,729 bytes parent folder | download | duplicates (3)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * Author: Evgeny Khramtsov <ekhramtsov@process-one.net>
 * Created : 26 Apr 2017 by Evgeny Khramtsov <ekhramtsov@process-one.net>
 *
 *
 * Copyright (C) 2002-2021 ProcessOne, SARL. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <erl_nif.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#define MAX_UINT64 ((ErlNifUInt64) -1)

typedef struct State {
  unsigned int max_counters;
  unsigned int counters_num;
  ErlNifUInt64 *counters;
  ErlNifMutex *counters_lock;
} State;

static int load(ErlNifEnv* env, void** priv, ERL_NIF_TERM max)
{
  size_t size;
  unsigned int max_counters;

  State *state;

  if (!(state = enif_alloc(sizeof(*state)))) {
      return ENOMEM;
  }

  if (enif_get_uint(env, max, &max_counters)) {
    size = sizeof(ErlNifUInt64) * max_counters;
    if ((state->counters = enif_alloc(size))) {
      memset(state->counters, -1, size);
      state->counters_num = 0;
      state->counters_lock = enif_mutex_create("counters_lock");
      *priv = state;
      return 0;
    } else {
      enif_free(state);
    }
  }

  return ENOMEM;
}

static void unload(ErlNifEnv* env, void* priv)
{
  if (!priv)
    return;

  State *state = (State*)priv;

  if (state->counters_lock) enif_mutex_destroy(state->counters_lock);
  if (state->counters) enif_free(state->counters);

  enif_free(state);
}

static int upgrade(ErlNifEnv* caller_env, void** priv_data, void** old_priv_data, ERL_NIF_TERM load_info) {
  if (!*old_priv_data) {
    return 1;
  }

  *priv_data = *old_priv_data;
  *old_priv_data = NULL;

  return 0;
}

static ERL_NIF_TERM new_counter(ErlNifEnv* env, int argc,
				const ERL_NIF_TERM argv[])
{
  unsigned int counter;
  State *state = (State*)enif_priv_data(env);
  enif_mutex_lock(state->counters_lock);

  if (state->counters_num < state->max_counters) {
    counter = state->counters_num++;
    state->counters[counter] = 0;
    enif_mutex_unlock(state->counters_lock);
    return enif_make_tuple2(env, enif_make_atom(env, "ok"),
			    enif_make_ulong(env, counter));
  } else {
    for (counter = 0; counter < state->max_counters; counter++) {
      if (state->counters[counter] == MAX_UINT64) {
        state->counters[counter] = 0;
        enif_mutex_unlock(state->counters_lock);
        return enif_make_tuple2(env, enif_make_atom(env, "ok"),
				enif_make_ulong(env, counter));
      }
    }
    enif_mutex_unlock(state->counters_lock);
    return enif_make_tuple2(env, enif_make_atom(env, "error"),
                            enif_make_tuple2(env, enif_make_atom(env, "system_limit"),
                                             enif_make_uint(env, state->max_counters)));
  }
}

static ERL_NIF_TERM delete_counter(ErlNifEnv* env, int argc,
				   const ERL_NIF_TERM argv[])
{
  unsigned int counter;
  State *state = (State*)enif_priv_data(env);

  if (enif_get_uint(env, argv[0], &counter))
    if (counter < state->max_counters) {
      state->counters[counter] = MAX_UINT64;
      return enif_make_atom(env, "ok");
    }

  return enif_make_badarg(env);
}

static ERL_NIF_TERM incr_counter(ErlNifEnv* env, int argc,
				 const ERL_NIF_TERM argv[])
{
  unsigned int counter;
  State *state = (State*)enif_priv_data(env);

  if (enif_get_uint(env, argv[0], &counter))
    if (counter < state->max_counters) {
      if (state->counters[counter] != MAX_UINT64)
        return enif_make_uint64(env, __atomic_add_fetch(state->counters + counter, 1, __ATOMIC_SEQ_CST));
    }

  return enif_make_badarg(env);
}

static ERL_NIF_TERM get_counter(ErlNifEnv* env, int argc,
				const ERL_NIF_TERM argv[])
{
  unsigned int counter;
  ErlNifUInt64 val;
  State *state = (State*)enif_priv_data(env);

  if (enif_get_uint(env, argv[0], &counter))
    if (counter < state->max_counters) {
      val = state->counters[counter];
      if (val != MAX_UINT64)
        return enif_make_uint64(env, val);
    }

  return enif_make_badarg(env);
}

static ErlNifFunc nif_funcs[] =
    {
      {"new_counter_nif", 0, new_counter},
      {"delete_counter_nif", 1, delete_counter},
      {"incr_counter_nif", 1, incr_counter},
      {"get_counter_nif", 1, get_counter}
    };

ERL_NIF_INIT(ets_cache, nif_funcs, load, NULL, upgrade, unload)