-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathepqueue_item.cc
57 lines (43 loc) · 1.17 KB
/
epqueue_item.cc
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
#include "epqueue_item.h"
#include "epqueue_nif.h"
#include "macros.h"
#include <cstdint>
namespace {
uint64_t next_id()
{
static uint64_t next_id = 1;
if(next_id == UINT64_MAX)
next_id = 1;
return next_id++;
}
}
bool epqueue_item_less(void* ax, void* bx)
{
queue_item* a = static_cast<queue_item*>(ax);
queue_item* b = static_cast<queue_item*>(bx);
if(a->priority == b->priority)
return a->internal_id < b->internal_id;
return a->priority < b->priority;
}
void epqueue_item_update_pos(void* ax, int32_t pos)
{
queue_item* a = static_cast<queue_item*>(ax);
a->heap_index = pos;
}
void epqueue_item_free(ErlNifEnv* env, void* obj)
{
UNUSED(env);
queue_item* item = static_cast<queue_item*>(obj);
enif_release_binary(&item->data);
}
queue_item* epqueue_item_new(const epqueue_data* data, const ErlNifBinary& bin, uint64_t priority)
{
queue_item* item = static_cast<queue_item*>(enif_alloc_resource(data->resPQueueItem, sizeof(queue_item)));
if(item == NULL)
return NULL;
item->internal_id = next_id();
item->heap_index = -1;
item->priority = priority;
item->data = bin;
return item;
}