-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShareBlock.cpp
More file actions
148 lines (126 loc) · 3.48 KB
/
ShareBlock.cpp
File metadata and controls
148 lines (126 loc) · 3.48 KB
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
/*
* ShareBlock.cpp
*
* Created on: 30 Jul 2013
* Author: lester
*/
#include "ShareBlock.h"
#include "string.h"
ShareBlock::ShareBlock()
{
}
ShareBlock::~ShareBlock()
{
}
void ShareBlock::descFill(struct _block_desc *pdesc, unsigned int data_ID, unsigned short size, unsigned short type, void* val, unsigned short max, unsigned short* count)
{
memset(pdesc, 0, sizeof(*pdesc));
pdesc->id = data_ID;
pdesc->size = size;
pdesc->type = type;
pdesc->val = val;
pdesc->max_count = max;
pdesc->pcount = count;
}
/*!
Create a block of data using descriptors,
check how long block will be
loop into descriptor and put data into block buffer.
*/
void ShareBlock::blockFromStruct(struct _block_desc *pdesc, unsigned short count, struct _block* block, unsigned int max_size)
{
unsigned short i;
unsigned short val_size; // real size in bytes value store in data
unsigned int tot_size;
struct _data* data;
struct _block_desc *desc = pdesc;
/*!
Get all necessary size to store data from structure
Use size * element count for pointer
size * ( count OR max if count is not supplied )
*/
tot_size = sizeof(*block) - sizeof(block->data);
for (i = 0, pdesc = desc; i < count; i++, pdesc++)
{
tot_size += (sizeof(block->data) - sizeof(data->val));
// Array type
if (pdesc->type & ARRAY)
{
if (!pdesc->pcount)
tot_size += pdesc->size * pdesc->max_count;
else
tot_size += pdesc->size * *pdesc->pcount;
} else if (pdesc->type & POINTER)
// Pointer type
{
if (!pdesc->pcount)
return; //!< Error, NULL is no valid for
tot_size += pdesc->size * *pdesc->pcount;
} else if (pdesc->type & PRIMITIVE)
{
tot_size += (pdesc->size);
} else
{
// unknown type
}
if (tot_size & (sizeof(int) - 1))
tot_size = (tot_size + sizeof(int)) & (~(sizeof(int) - 1));
}
if (max_size < tot_size)
return;
memset(block, 0, sizeof(*block));
block->size = OFFSETOF(struct _block,data);
for (i = 0, pdesc = desc; i < count; i++, pdesc++)
{
data = (struct _data*) (((unsigned char*) block) + block->size);
data->type = pdesc->type;
data->size = pdesc->size;
val_size = data->size;
// pointer or array
if (pdesc->type & ARRAY)
{
if (!pdesc->pcount)
val_size = data->size * pdesc->max_count;
else
val_size = data->size * *pdesc->pcount;
} else if (pdesc->type & POINTER)
{
if (!pdesc->pcount)
return; //!< Error, NULL is no valid for
val_size = data->size * *pdesc->pcount;
block->asize += val_size;
}
// smaller data size most be same that an integer, because memory alignment is necessary
if (data->size <= sizeof(int))
{
data->val = *((int *) (pdesc->val));
} else
{
//Unknown data type
memcpy(&data->val, pdesc->val, val_size);
}
// another data with size > integer
block->size += sizeof(struct _data) - sizeof(data->val) + val_size;
if (block->size & (sizeof(int) - 1))
block->size = (block->size + sizeof(int)) & (~(sizeof(int) - 1));
}
}
/*
Starting with first data position until reach the size of the block
Read data from block, look for data id on description structure (more than one data with same description is allowed)
*/
void ShareBlock::structFromBlock(struct _block_desc *pdesc, unsigned short count, struct _block* block)
{
void *max = (char*) block + block->size;
/*
unsigned short i;
unsigned short val_size; // real size in bytes value store in data
unsigned int tot_size;
*/
struct _data* data;
data = block->data;
while (data < max)
{
// look at descriptor using id
}
}