-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhistory.cpp
69 lines (61 loc) · 1.43 KB
/
history.cpp
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
/* Copyright (c) 2023, Julia Desmazes. All rights reserved.
*
* This work is licensed under the Creative Commons Attribution-NonCommercial
* 4.0 International License.
*
* This code is provided "as is" without any express or implied warranties. */
#include "history.hpp"
#include <cassert>
#include <cstdlib>
#include <cstring>
/* default constructor */
hst::hst(){
/* init history ... nothing to do */
}
/* default destructor */
hst::~hst(){
/* free memory allocated by maps
* iterate over the map and free memory
* used for itch messages */
for(auto i = hmap.begin(); i != hmap.end(); i++){
mmap_t inner = i->second;
for(auto j = inner.begin(); j!= inner.end(); j++){
assert(j->second);
free(j->second);
}
}
}
void hst::add_itch_msg(
const sid_t sid,
const uint64_t seq,
const moldudp64_msg_s* msg
){
assert(msg);
moldudp64_msg_s *cpy;
cpy = (moldudp64_msg_s*)malloc(sizeof(moldudp64_msg_s));
cpy->data = (uint8_t*) malloc(msg->len);
cpy->len = msg->len;
memcpy(cpy->data, msg->data, msg->len);
this->hmap[sid][seq] = cpy;
}
moldudp64_msg_s* hst::get_itch_msg(
const sid_t sid,
const uint64_t seq
){
moldudp64_msg_s* ret = NULL;
hmap_t::iterator i;
i = this->hmap.find(sid);
if(i!=this->hmap.find(sid)){
mmap_t::iterator j;
j = i->second.find(seq);
if(j!= i->second.end()){
ret = j->second;
}
}
return ret;
}
void hst::forget_session(
const sid_t sid
){
this->hmap[sid].clear();
}