Skip to content

Commit b96516a

Browse files
Move binary stamps from void* to std::vector<char>
1 parent 0e32185 commit b96516a

File tree

10 files changed

+82
-60
lines changed

10 files changed

+82
-60
lines changed

blobstamper/blob.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Blob::Size()
5353
return end - begin + 1;
5454
}
5555

56-
void *
56+
std::vector<char>
5757
Blob::ShiftSingleStampBin(StampBase& stmp)
5858
{
5959
return stmp.ExtractBin(*this);
@@ -73,3 +73,13 @@ Blob::DataDup(char *& data_out, size_t& size_out)
7373
//FIXME add out of memory check here!!!!
7474
memcpy(data_out, data + begin, size_out);
7575
}
76+
77+
std::vector<char>
78+
Blob::asVector()
79+
{
80+
std::vector<char> res(Size());
81+
82+
memcpy(&res[0], data + begin, Size());
83+
return res;
84+
}
85+

blobstamper/blob.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include <string>
66
#include <list>
7+
#include <vector>
8+
79

810
class StampBase;
911

@@ -21,8 +23,9 @@ class Blob
2123
void Dump();
2224
Blob ShiftBytes(size_t n);
2325
void DataDup(char *& data_out, size_t& size_out);
26+
std::vector<char> asVector();
2427

25-
void * ShiftSingleStampBin(StampBase &stmp);
28+
std::vector<char> ShiftSingleStampBin(StampBase &stmp);
2629
std::string ShiftSingleStampStr(StampBase &stmp);
2730
};
2831

blobstamper/galley.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ GalleySeries::ExtractStr(Blob &blob)
4444
return res;
4545
}
4646

47-
std::list<void *>
47+
std::list<std::vector<char>>
4848
GalleySeries::ExtractBin(Blob &blob)
4949
{
50-
std::list<void *> res;
50+
std::list<std::vector<char>> res;
5151
std::list<Blob> blobs = extract_internal(blob);
5252
for(Blob blob : blobs)
5353
{
54-
void * data= blob.ShiftSingleStampBin(stamp);
54+
std::vector<char> data = blob.ShiftSingleStampBin(stamp);
5555
res.push_back(data);
5656
}
5757
return res;
@@ -97,7 +97,9 @@ GalleySeries::extract_internal(Blob &blob)
9797
size_t count_max = (blob.Size() - ORACLE_SIZE) / (stamp.minSize() + ORACLE_SIZE); //First oracle - for number of items, and second one is oracle for each item size
9898
ORACLE_STAMP stamp_oracle;
9999
ORACLE_TYPE *count_oracle;
100-
count_oracle = (ORACLE_TYPE *) blob.ShiftSingleStampBin(stamp_oracle);
100+
101+
std::vector<char> v = blob.ShiftSingleStampBin(stamp_oracle);
102+
count_oracle = (ORACLE_TYPE *) &v[0];
101103

102104
ORACLE_TYPE count_target = count_max * (*count_oracle) / ORACLE_MAX + 1; /* +1 -- это грубая эмуляция округления вверх. oracle == ORACLE_MAX-1 == 65534 должен дать count_max*/
103105
if (count_target > count_max) count_target = count_max; // В случае если oracle оказался рваен ORACLE_MAX
@@ -107,10 +109,10 @@ GalleySeries::extract_internal(Blob &blob)
107109
int size_oracle_total = 0;
108110
for(int i = 0; i<count_target; i++)
109111
{
110-
ORACLE_TYPE *o = (ORACLE_TYPE *) blob.ShiftSingleStampBin(stamp_oracle);
112+
std::vector<char> v = blob.ShiftSingleStampBin(stamp_oracle);
113+
ORACLE_TYPE *o = (ORACLE_TYPE *) &v[0];
111114
size_oracles.push_back(*o);
112115
size_oracle_total += *o;
113-
free(o);
114116
}
115117

116118
/* Calculating available vairable size, that will be destributed between parts according to size oracles */
@@ -129,7 +131,6 @@ GalleySeries::extract_internal(Blob &blob)
129131
Blob blob2 = blob.ShiftBytes(el_size);
130132
res.push_back(blob2);
131133
}
132-
free(count_oracle);
133134
}
134135
else
135136
{
@@ -141,13 +142,13 @@ GalleySeries::extract_internal(Blob &blob)
141142
{
142143
if(stamp.minSize() + stamp_oracle.minSize() > blob.Size())
143144
break;
144-
ORACLE_TYPE *oracle = (ORACLE_TYPE *) blob.ShiftSingleStampBin(stamp_oracle);
145+
std::vector<char> v = blob.ShiftSingleStampBin(stamp_oracle);
146+
ORACLE_TYPE *oracle = (ORACLE_TYPE *) &v[0];
145147
int size = (double) *oracle / ORACLE_MAX * (var_size + 1); /* +1 -- это грубая эмуляция округления вверх. oracle == ORACLE_MAX-1 == 65534 должен дать count_max*/
146148
if (size > var_size) size = var_size; // In case we've hit oracle == ORACLE_MAX boundary
147149
size += fixed_size;
148150
Blob blob2 = blob.ShiftBytes(size);
149151
res.push_back(blob2);
150-
free(oracle);
151152
}
152153
}
153154
}
@@ -215,9 +216,10 @@ GalleyVector::extract_internal(Blob &blob)
215216
/* try do devide available data between variated and unbounded stamps */
216217
/* if predicted variated size is smaller than varited_total_size_limit we will decrice that limit */
217218

218-
ORACLE_TYPE * oracle = (ORACLE_TYPE *) blob.ShiftSingleStampBin(oracle_stamp);
219+
std::vector<char> v = blob.ShiftSingleStampBin(oracle_stamp);
220+
221+
ORACLE_TYPE * oracle = (ORACLE_TYPE *) &v[0];
219222
int predicted_variated_limit = round ((double) *oracle / (double) ORACLE_MAX * (double) (avaliable_nonfixed_size));
220-
free(oracle);
221223

222224
if (varited_total_size_limit > predicted_variated_limit)
223225
varited_total_size_limit = predicted_variated_limit;
@@ -240,9 +242,9 @@ GalleyVector::extract_internal(Blob &blob)
240242
modifier = 1; //Nothing to predict, it will use all space
241243
} else
242244
{
243-
ORACLE_TYPE * oracle = (ORACLE_TYPE *) blob.ShiftSingleStampBin(oracle_stamp);
245+
std::vector<char> v = blob.ShiftSingleStampBin(oracle_stamp);
246+
ORACLE_TYPE * oracle = (ORACLE_TYPE *) &v[0];
244247
o_value = * oracle;
245-
free(oracle);
246248
modifier = (double) o_value / (double) ORACLE_MAX;
247249
}
248250
if (s.isUnbounded())

blobstamper/galley.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
#define GALLEY_H
44

55

6-
#include <limits.h>
6+
#include <limits>
77
#include <vector>
88
#include <functional> // for std::reference_wrapper
99

1010
#define ORACLE_TYPE unsigned short int
1111
#define ORACLE_STAMP StampArithm<ORACLE_TYPE>
1212
#define ORACLE_SIZE sizeof(ORACLE_TYPE)
13-
#define ORACLE_MAX USHRT_MAX
13+
#define ORACLE_MAX std::numeric_limits<ORACLE_TYPE>::max()
1414

1515
class GalleyBase
1616
{
@@ -30,7 +30,7 @@ class GalleySeries : public GalleyBase
3030
GalleySeries(StampBase & stamp_arg) : stamp(stamp_arg) {};
3131
std::list<Blob> extract_internal(Blob &blob);
3232
std::list<std::string> ExtractStr(Blob &blob);
33-
std::list<void *> ExtractBin(Blob &blob);
33+
std::list<std::vector<char>> ExtractBin(Blob &blob);
3434

3535
int minSize() override;
3636
int maxSize() override {return -1;}; /* Sereies always takes as much data as it can take */
@@ -44,7 +44,6 @@ class GalleyVector : public GalleyBase
4444
GalleyVector(std::vector<std::reference_wrapper<StampBase>> arg) : stamps(arg) {};
4545
std::vector<Blob> extract_internal(Blob &blob);
4646
std::vector<std::string> ExtractStr(Blob &blob);
47-
// std::list<void *> ExtractBin(Blob &blob);
4847

4948
int minSize() override;
5049
int maxSize() override;

blobstamper/stamp.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,17 @@
1111
/*************************************************************************************/
1212

1313
/* Generic Exrtact Bin function for fixed size stamp. In some cases we need just a chunk of raw blob data. */
14-
/* Use this method in such a case */
15-
void *
14+
/* Use this method in those cases */
15+
std::vector<char>
1616
StampFixed::ExtractBin(Blob &blob)
1717
{
1818
Blob blob2 = blob.ShiftBytes(size);
1919

2020
if (blob2.isEmpty()) /* original blob does not have enought data */
21-
return NULL;
22-
23-
size_t res_size;
24-
char *res;
25-
blob2.DataDup(res,res_size);
26-
return (void *) res;
21+
{
22+
std::vector<char> empty(0);
23+
return empty;
24+
}
25+
return blob2.asVector();
2726
}
2827

blobstamper/stamp.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include <string>
66
#include <list>
7+
#include <vector>
8+
79

810
class StampBase
911
{
@@ -15,7 +17,7 @@ class StampBase
1517
bool isVariated() {return ! isFixedSize() && ! isUnbounded();}
1618
bool isUnbounded() {return maxSize() == -1;}
1719

18-
virtual void * ExtractBin(Blob &blob) {printf ("Not implemented"); exit(1);}
20+
virtual std::vector<char> ExtractBin(Blob &blob) {printf ("Not implemented"); exit(1);}
1921
virtual std::string ExtractStr(Blob &blob) {printf ("Not implemented"); exit(1);}
2022
};
2123

@@ -28,7 +30,7 @@ class StampFixed : public StampBase
2830
virtual int minSize() {return size;}
2931
virtual int maxSize() {return size;}
3032

31-
void * ExtractBin(Blob &blob) override;
33+
std::vector<char> ExtractBin(Blob &blob) override;
3234
};
3335

3436
class StampVariated : public StampBase

blobstamper/stamp_arithm.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ template<class T> std::string
1515
StampArithm<T>::ExtractStr(Blob &blob)
1616
{
1717
std::string res;
18-
T *pT = (T *)this->ExtractBin(blob);
19-
if (! pT)
18+
std::vector<char> bin = this->ExtractBin(blob);
19+
20+
if (bin.size() == 0)
2021
return "";
21-
res = to_string_precise(*pT);
22-
free(pT);
23-
return res;
22+
23+
T *pT = (T *) &bin[0];
24+
25+
return to_string_precise(*pT);
2426
}
2527

2628
#endif /* STAMP_ATOMIC_H */

blobstamper/stamp_dict.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,30 @@ StampDict::ExtractStr(Blob &blob)
3636
{
3737
case 1:
3838
{
39-
unsigned char * i = (unsigned char *) blob.ShiftSingleStampBin(stamp);
39+
std::vector<char> v = blob.ShiftSingleStampBin(stamp);
40+
unsigned char * i = (unsigned char *) &v[0];
4041
index_oracle = * i;
41-
free(i);
4242
break;
4343
}
4444
case 2:
4545
{
46-
unsigned short int * i = (unsigned short int *) blob.ShiftSingleStampBin(stamp);
46+
std::vector<char> v = blob.ShiftSingleStampBin(stamp);
47+
unsigned short int * i = (unsigned short int *) &v[0];
4748
index_oracle = * i;
48-
free(i);
4949
break;
5050
}
5151
case 4:
5252
{
53-
unsigned int * i = ( unsigned int *) blob.ShiftSingleStampBin(stamp);
53+
std::vector<char> v = blob.ShiftSingleStampBin(stamp);
54+
unsigned int * i = ( unsigned int *) &v[0];
5455
index_oracle = * i;
55-
free(i);
5656
break;
5757
}
58-
5958
case 8:
6059
{
61-
unsigned long long * i = ( unsigned long long *) blob.ShiftSingleStampBin(stamp);
60+
std::vector<char> v = blob.ShiftSingleStampBin(stamp);
61+
unsigned long long * i = ( unsigned long long *) &v[0];
6262
index_oracle = * i;
63-
free(i);
6463
break;
6564
}
6665
default:

t/110-stamp-atomic.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ main()
3636
{ /* 1, 2 */
3737
Blob blob(sample_data_char, strlen(sample_data_char));
3838
StampArithm<char> stamp;
39-
char * c = (char *) blob.ShiftSingleStampBin(stamp);
39+
std::vector<char> v = blob.ShiftSingleStampBin(stamp);
40+
char * c = (char *) &v[0];
4041
is(*c, 'S' , "Bin Char stamp works well");
41-
free(c);
4242

4343
// StampStrUInt8 stamp2;
4444
StampArithm<char> stamp2;
@@ -50,9 +50,9 @@ main()
5050
{ /* 3, 4, 5 */
5151
Blob blob((char *)sample_data_int16, sample_data_int16_size);
5252
StampArithm<short int> stamp;
53-
short int * i = (short int *) blob.ShiftSingleStampBin(stamp);
53+
std::vector<char> v = blob.ShiftSingleStampBin(stamp);
54+
short int * i = (short int *) &v[0];
5455
is(*i, 1 , "Bin Int16 stamp works well");
55-
free(i);
5656

5757
StampArithm<unsigned short int> stamp2;
5858
std::string s = blob.ShiftSingleStampStr(stamp2);
@@ -67,9 +67,11 @@ main()
6767
{ /* 6, 7, 8 */
6868
Blob blob((char *)sample_data_int32, sample_data_int32_size);
6969
StampArithm<int> stamp;
70-
int * i = (int *) blob.ShiftSingleStampBin(stamp);
70+
71+
std::vector<char> v = blob.ShiftSingleStampBin(stamp);
72+
73+
int * i = (int *) &v[0];
7174
is(*i, 10 , "Bin Int32 stamp works well");
72-
free(i);
7375

7476
StampArithm<unsigned int> stamp2;
7577
std::string s = blob.ShiftSingleStampStr(stamp2);
@@ -85,9 +87,11 @@ main()
8587
{ /* 9, 10, 11 */
8688
Blob blob((char *)sample_data_int64, sample_data_int64_size);
8789
StampArithm<long long> stamp;
88-
long long * i = (long long *) blob.ShiftSingleStampBin(stamp);
90+
91+
std::vector<char> v = blob.ShiftSingleStampBin(stamp);
92+
93+
long long * i = (long long *) &v[0];
8994
is(*i, 100 , "Bin Int64 stamp works well");
90-
free(i);
9195

9296
StampArithm<unsigned long long> stamp2;
9397
std::string s = blob.ShiftSingleStampStr(stamp2);
@@ -104,9 +108,9 @@ main()
104108
{ /* 12 */
105109
Blob blob((char *)sample_data_double, sample_data_double_size);
106110
StampArithm<double> stamp;
107-
double *d = (double *) blob.ShiftSingleStampBin(stamp);
111+
std::vector<char> v = blob.ShiftSingleStampBin(stamp);
112+
double *d = (double *) &v[0];
108113
is(*d, 1.4142, "Bin Double stamp works well");
109-
free(d);
110114
}
111115

112116
/* Check that Str Double stamp works well */

t/300-galley.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <exception>
44
#include <string>
55
#include <cstdlib>
6+
#include <vector>
67
#define WANT_TEST_EXTRAS
78
#include <tap++/tap++.h>
89

@@ -91,24 +92,25 @@ main()
9192
StampArithm<unsigned short int> stamp;
9293
GalleySeries galley(stamp);
9394
Blob blob(short_sample, strlen(short_sample));
94-
std::list<void * > res = galley.ExtractBin(blob);
95+
std::list<std::vector<char>> res = galley.ExtractBin(blob);
9596

97+
std::vector<char> v;
9698
unsigned short int * data;
9799

98-
data = (unsigned short int *) res.front();
100+
v = res.front();
101+
data = (unsigned short int *) &v[0];
99102
is(*data, expected1, "GalleySeries, fixed size binary stamp: First element of shifted list is ok");
100103
res.pop_front();
101-
free(data);
102104

103-
data = (unsigned short int *) res.front();
105+
v = res.front();
106+
data = (unsigned short int *) &v[0];
104107
is(*data, expected2, "GalleySeries, fixed size binary stamp: Second element of shifted list is ok");
105108
res.pop_front();
106-
free(data);
107109

108-
data = (unsigned short int *) res.front();
110+
v = res.front();
111+
data = (unsigned short int *) &v[0];
109112
is(*data, expected3, "GalleySeries, fixed size binary stamp: Third element of shifted list is ok");
110113
res.pop_front();
111-
free(data);
112114

113115
ok(res.empty(), "GalleySeries, fixed size binary stamp: The rest of the list is empty");
114116
}

0 commit comments

Comments
 (0)