benchmarks compiles with clang
[c11concurrency-benchmarks.git] / mabain / src / mb_data.cpp
1 /**
2  * Copyright (C) 2017 Cisco Inc.
3  *
4  * This program is free software: you can redistribute it and/or  modify
5  * it under the terms of the GNU General Public License, version 2,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16
17 // @author Changxue Deng <chadeng@cisco.com>
18
19 #include <string.h>
20
21 #include "mb_data.h"
22 #include "error.h"
23
24 namespace mabain {
25
26 MBData::MBData()
27 {
28     data_len = 0;
29     buff_len = 0;
30     buff = NULL;
31
32     match_len = 0;
33     next = false;
34     options = 0;
35     free_buffer = false;
36 }
37
38 MBData::MBData(int size, int match_options)
39 {
40     buff_len = size;
41     buff = NULL;
42     if(buff_len > 0)
43         buff = static_cast<uint8_t*>(malloc(buff_len + 1));
44
45     if(buff != NULL)
46     {
47         free_buffer = true;
48     }
49     else
50     {
51         buff_len = 0;
52         free_buffer = false;
53     }
54
55     data_len = 0;
56     match_len = 0;
57     next = false;
58     options = match_options;
59 }
60
61 // Caller must free data.
62 int MBData::TransferValueTo(uint8_t* &data, int &dlen)
63 {
64     if(buff == NULL || data_len <= 0)
65     {
66         dlen = 0;
67         data = NULL;
68         return MBError::INVALID_ARG;
69     }
70
71     if(free_buffer)
72     {
73         data = buff;
74         buff = NULL;
75         dlen = data_len;
76         free_buffer = false; 
77         buff_len = 0;
78     }
79     else
80     {
81         data = (uint8_t *) malloc(data_len + 1);
82         if(data == NULL)
83             return MBError::NO_MEMORY;
84         memcpy(data, buff, data_len);
85         dlen = data_len;
86     }
87
88     return MBError::SUCCESS;
89 }
90
91 // Data must be allocated using malloc or calloc.
92 int MBData::TransferValueFrom(uint8_t* &data, int dlen)
93 {
94     if(data == NULL)
95         return MBError::INVALID_ARG;
96
97     if(free_buffer && buff != NULL)
98         free(buff);
99     buff = data;
100     buff_len = dlen;
101     data_len = dlen;
102     free_buffer = true;
103
104     data = NULL;
105     return MBError::SUCCESS;
106 }
107
108 MBData::~MBData()
109 {
110     if(free_buffer)
111         free(buff);
112 }
113
114 // This function is for prefix match only.
115 void MBData::Clear()
116 {
117     match_len = 0;
118     data_len = 0;
119     next = false;
120 }
121
122 int MBData::Resize(int size)
123 {
124     if(size > buff_len)
125     {
126         buff_len = size;
127         if(free_buffer)
128         {
129             free(buff);
130         }
131         else
132         {
133             free_buffer = true;
134         }
135
136         buff = static_cast<uint8_t*>(malloc(buff_len + 1));
137         if(buff == NULL)
138         {
139             buff_len = 0;
140             free_buffer = false;
141             return MBError::NO_MEMORY;
142         }
143
144     }
145     return MBError::SUCCESS;
146 }
147
148 }