Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
U
uniset2
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
1
Issues
1
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
UniSet project repositories
uniset2
Commits
b4113aa4
Commit
b4113aa4
authored
Jul 24, 2009
by
Ivan Donchevskiy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
malloc changed to new in TableBlockStorage and CycleStorage, freeing memory where it's needed
parent
5811033c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
75 additions
and
43 deletions
+75
-43
CycleStorage.cc
src/Various/CycleStorage.cc
+43
-20
TableBlockStorage.cc
src/Various/TableBlockStorage.cc
+32
-23
No files found.
src/Various/CycleStorage.cc
View file @
b4113aa4
...
...
@@ -33,14 +33,11 @@
CycleStorage
::
CycleStorage
()
{
file
=
NULL
;
size
=
0
;
inf_size
=
0
;
seekpos
=
0
;
head
=
tail
=-
1
;
}
CycleStorage
::
CycleStorage
(
const
char
*
name
,
int
inf_sz
,
int
sz
,
int
seek
)
{
file
=
NULL
;
if
(
!
Open
(
name
,
inf_sz
,
sz
,
seek
))
Create
(
name
,
inf_sz
,
sz
,
seek
);
}
...
...
@@ -66,15 +63,24 @@ bool CycleStorage::Open(const char* name, int inf_sz, int sz, int seek)
seekpos
=
seek
;
CycleStorageAttr
*
csa
=
new
CycleStorageAttr
();
if
(
fseek
(
file
,
seekpos
,
0
)
==
0
)
fread
(
csa
,
sizeof
(
CycleStorageAttr
),
1
,
file
);
else
return
false
;
else
{
delete
csa
;
return
false
;
}
if
((
csa
->
size
!=
((
sz
-
sizeof
(
CycleStorageAttr
))
/
(
sizeof
(
CycleStorageElem
)
+
inf_sz
)))
||
(
csa
->
inf_size
!=
inf_sz
)
||
(
csa
->
seekpos
!=
seek
))
return
false
;
if
((
csa
->
size
!=
((
sz
-
sizeof
(
CycleStorageAttr
))
/
(
sizeof
(
CycleStorageElem
)
+
inf_sz
)))
||
(
csa
->
inf_size
!=
inf_sz
)
||
(
csa
->
seekpos
!=
seek
))
{
delete
csa
;
return
false
;
}
delete
csa
;
size
=
(
sz
-
sizeof
(
CycleStorageAttr
))
/
(
sizeof
(
CycleStorageElem
)
+
inf_sz
);
inf_size
=
inf_sz
;
seekpos
=
seek
;
CycleStorageElem
*
jrn
=
(
CycleStorageElem
*
)
malloc
(
sizeof
(
CycleStorageElem
)
+
inf_sz
)
;
CycleStorageElem
*
jrn
=
(
CycleStorageElem
*
)
new
char
[(
sizeof
(
CycleStorageElem
)
+
inf_sz
)]
;
int
l
=-
1
,
r
=
size
,
mid
;
iter
=
0
;
seekpos
+=
sizeof
(
CycleStorageAttr
);
...
...
@@ -153,6 +159,7 @@ bool CycleStorage::Open(const char* name, int inf_sz, int sz, int seek)
tail
=
head
-
1
;
if
(
tail
<
0
)
tail
=
size
-
1
;
}
delete
jrn
;
return
true
;
}
...
...
@@ -167,7 +174,9 @@ bool CycleStorage::Create(const char* name, int inf_sz, int sz, int seek)
file
=
fopen
(
name
,
"r+"
);
}
CycleStorageElem
*
jrn
=
(
CycleStorageElem
*
)
malloc
(
sizeof
(
CycleStorageElem
)
+
inf_sz
);
CycleStorageElem
*
jrn
=
(
CycleStorageElem
*
)
new
char
[(
sizeof
(
CycleStorageElem
)
+
inf_sz
)];
jrn
->
status
=
0
;
size
=
(
sz
-
sizeof
(
CycleStorageAttr
))
/
(
sizeof
(
CycleStorageElem
)
+
inf_sz
);
iter
=
0
;
inf_size
=
inf_sz
;
...
...
@@ -195,17 +204,20 @@ bool CycleStorage::Create(const char* name, int inf_sz, int sz, int seek)
char
*
empty
=
new
char
[
emp
];
fwrite
(
empty
,
emp
,
1
,
file
);
fflush
(
file
);
delete
empty
;
}
head
=
tail
=-
1
;
delete
csa
;
delete
jrn
;
return
true
;
}
bool
CycleStorage
::
AddRow
(
char
*
str
)
{
if
(
file
==
NULL
)
return
false
;
CycleStorageElem
*
jrn
=
(
CycleStorageElem
*
)
malloc
(
sizeof
(
CycleStorageElem
)
+
inf_size
)
;
int
i
,
k
;
CycleStorageElem
*
jrn
=
(
CycleStorageElem
*
)
new
char
[(
sizeof
(
CycleStorageElem
)
+
inf_size
)]
;
int
i
=
0
,
k
;
if
(
head
==-
1
)
{
jrn
->
status
=
1
;
...
...
@@ -213,6 +225,7 @@ bool CycleStorage::AddRow(char* str)
filewrite
(
jrn
,
0
);
head
=
0
;
tail
=
0
;
delete
jrn
;
return
true
;
}
if
(
head
==
tail
)
...
...
@@ -221,6 +234,7 @@ bool CycleStorage::AddRow(char* str)
memcpy
((
void
*
)((
char
*
)
jrn
+
sizeof
(
CycleStorageElem
)),(
void
*
)
str
,
inf_size
);
filewrite
(
jrn
,
1
);
tail
=
1
;
delete
jrn
;
return
true
;
}
fseek
(
file
,
seekpos
+
tail
*
(
sizeof
(
CycleStorageElem
)
+
inf_size
),
0
);
...
...
@@ -242,7 +256,6 @@ bool CycleStorage::AddRow(char* str)
jrn
->
status
=
2
;
memcpy
((
void
*
)((
char
*
)
jrn
+
sizeof
(
CycleStorageElem
)),(
void
*
)
str
,
inf_size
);
filewrite
(
jrn
,
tail
);
return
true
;
}
else
{
...
...
@@ -256,36 +269,43 @@ bool CycleStorage::AddRow(char* str)
if
((
jrn
->
status
==
3
)
||
(
jrn
->
status
==
5
))
jrn
->
status
=
6
;
else
jrn
->
status
=
1
;
filewrite
(
jrn
,
head
);
return
true
;
}
delete
jrn
;
return
true
;
}
bool
CycleStorage
::
DelRow
(
int
row
)
{
CycleStorageElem
*
jrn
=
(
CycleStorageElem
*
)
malloc
(
sizeof
(
CycleStorageElem
)
+
inf_size
);
int
i
=
(
head
+
row
)
%
size
,
j
;
if
(
row
>=
size
)
return
false
;
if
(
file
==
NULL
)
return
false
;
CycleStorageElem
*
jrn
=
(
CycleStorageElem
*
)
new
char
[(
sizeof
(
CycleStorageElem
)
+
inf_size
)];
fseek
(
file
,
seekpos
+
i
*
(
sizeof
(
CycleStorageElem
)
+
inf_size
),
0
);
fread
(
jrn
,(
sizeof
(
CycleStorageElem
)
+
inf_size
),
1
,
file
);
if
(
jrn
->
status
==
1
)
{
jrn
->
status
=
6
;
filewrite
(
jrn
,
i
);
delete
jrn
;
return
true
;
}
if
(
jrn
->
status
==
2
)
j
=
3
;
else
if
(
jrn
->
status
==
4
)
j
=
5
;
else
return
false
;
else
{
delete
jrn
;
return
false
;
}
jrn
->
status
=
j
;
filewrite
(
jrn
,
i
);
delete
jrn
;
return
true
;
}
bool
CycleStorage
::
DelAllRows
()
{
if
(
file
==
NULL
)
return
false
;
CycleStorageElem
*
jrn
=
(
CycleStorageElem
*
)
malloc
(
sizeof
(
CycleStorageElem
)
+
inf_size
)
;
CycleStorageElem
*
jrn
=
(
CycleStorageElem
*
)
new
char
[(
sizeof
(
CycleStorageElem
)
+
inf_size
)]
;
int
i
,
j
;
fseek
(
file
,
seekpos
,
0
);
...
...
@@ -300,16 +320,17 @@ bool CycleStorage::DelAllRows()
}
fflush
(
file
);
head
=
tail
=-
1
;
delete
jrn
;
return
true
;
}
bool
CycleStorage
::
ViewRows
(
int
beg
,
int
num
)
{
CycleStorageElem
*
jrn
=
(
CycleStorageElem
*
)
malloc
(
sizeof
(
CycleStorageElem
)
+
inf_size
);
int
i
,
j
=
(
head
+
beg
)
%
size
,
n
=
num
,
k
;
if
(
num
==
0
)
n
=
size
;
if
(
num
>
size
)
n
=
size
;
if
(
file
==
NULL
)
return
false
;
CycleStorageElem
*
jrn
=
(
CycleStorageElem
*
)
new
char
[(
sizeof
(
CycleStorageElem
)
+
inf_size
)];
fseek
(
file
,
seekpos
+
j
*
(
sizeof
(
CycleStorageElem
)
+
inf_size
),
0
);
for
(
i
=
0
;
i
<
n
;
i
++
)
{
...
...
@@ -319,22 +340,22 @@ bool CycleStorage::ViewRows(int beg, int num)
fseek
(
file
,
seekpos
,
0
);
}
fread
(
jrn
,(
sizeof
(
CycleStorageElem
)
+
inf_size
),
1
,
file
);
if
((
jrn
->
status
!=
0
)
&&
(
jrn
->
status
!=
3
)
&&
(
jrn
->
status
!=
5
)
&&
(
jrn
->
status
!=
6
))
if
((
jrn
->
status
==
1
)
||
(
jrn
->
status
==
2
)
||
(
jrn
->
status
==
4
))
{
for
(
k
=
0
;
k
<
inf_size
;
k
++
)
printf
(
"%c"
,((
char
*
)(
jrn
)
+
sizeof
(
CycleStorageElem
))[
k
]);
printf
(
"%s"
,((
char
*
)(
jrn
)
+
sizeof
(
CycleStorageElem
)));
printf
(
"
\n
"
);
}
j
++
;
}
delete
jrn
;
return
true
;
}
bool
CycleStorage
::
ExportToXML
(
const
char
*
name
)
{
CycleStorageElem
*
jrn
=
(
CycleStorageElem
*
)
malloc
(
sizeof
(
CycleStorageElem
)
+
inf_size
);
int
i
,
j
=
head
;
if
(
file
==
NULL
)
return
false
;
CycleStorageElem
*
jrn
=
(
CycleStorageElem
*
)
new
char
[(
sizeof
(
CycleStorageElem
)
+
inf_size
)];
UniXML
*
f
=
new
UniXML
();
f
->
newDoc
(
"!"
);
fseek
(
file
,
seekpos
+
j
*
(
sizeof
(
CycleStorageElem
)
+
inf_size
),
0
);
...
...
@@ -353,5 +374,6 @@ bool CycleStorage::ExportToXML(const char* name)
j
++
;
}
f
->
save
(
name
);
delete
jrn
;
return
true
;
}
\ No newline at end of file
src/Various/TableBlockStorage.cc
View file @
b4113aa4
...
...
@@ -46,28 +46,25 @@ bool KeyCompare(char* key1, char* key2, int cnt)
TableBlockStorage
::
TableBlockStorage
()
{
size
=
0
;
k_size
=
0
;
inf_size
=
0
;
max
=-
1
;
lim
=
0
;
cur_block
=
0
;
block_number
=
0
;
block_size
=
0
;
seekpos
=
0
;
file
=
NULL
;
}
TableBlockStorage
::
TableBlockStorage
(
const
char
*
name
,
int
key_sz
,
int
inf_sz
,
int
sz
,
int
block_num
,
int
block_lim
,
int
seek
)
{
if
(
file
!=
NULL
)
fclose
(
file
)
;
file
=
NULL
;
if
(
!
Open
(
name
,
key_sz
,
inf_sz
,
sz
,
block_num
,
block_lim
,
seek
))
Create
(
name
,
key_sz
,
inf_sz
,
sz
,
block_num
,
block_lim
,
seek
);
}
TableBlockStorage
::~
TableBlockStorage
()
{
fclose
(
file
);
for
(
int
i
=
0
;
i
<
block_size
;
i
++
)
{
delete
mem
[
i
];
}
delete
mem
;
if
(
file
!=
NULL
)
fclose
(
file
);
}
void
TableBlockStorage
::
filewrite
(
TableBlockStorageElem
*
tbl
,
int
seek
,
bool
needflush
)
...
...
@@ -79,8 +76,8 @@ void TableBlockStorage::filewrite(TableBlockStorageElem* tbl,int seek, bool need
bool
TableBlockStorage
::
CopyToNextBlock
(
void
)
{
int
i
,
j
,
full_size
=
sizeof
(
TableBlockStorageElem
)
+
k_size
+
inf_size
;
TableBlockStorageElem
*
tbl
=
(
TableBlockStorageElem
*
)
malloc
(
full_size
)
;
int
i
,
j
;
TableBlockStorageElem
*
tbl
=
(
TableBlockStorageElem
*
)
new
char
[
full_size
]
;
if
(
max
==
lim
-
1
)
{
...
...
@@ -102,6 +99,7 @@ bool TableBlockStorage::CopyToNextBlock(void)
cur_block
++
;
}
}
delete
tbl
;
return
true
;
}
...
...
@@ -115,15 +113,24 @@ bool TableBlockStorage::Open(const char* name, int key_sz, int inf_sz, int sz, i
seekpos
=
seek
;
StorageAttr
*
sa
=
new
StorageAttr
();
if
(
fseek
(
file
,
seekpos
,
0
)
==
0
)
fread
(
sa
,
sizeof
(
StorageAttr
),
1
,
file
);
else
return
false
;
else
{
delete
sa
;
return
false
;
}
int
full_size
=
sizeof
(
TableBlockStorageElem
)
+
key_sz
+
inf_sz
;
full_size
=
sizeof
(
TableBlockStorageElem
)
+
key_sz
+
inf_sz
;
int
tmpsize
=
(
sz
-
sizeof
(
StorageAttr
))
/
(
full_size
);
int
tmpblock
=
tmpsize
/
block_num
;
tmpsize
=
tmpblock
*
block_num
;
if
((
sa
->
k_size
!=
key_sz
)
||
(
sa
->
inf_size
!=
inf_sz
)
||
(
sa
->
size
!=
tmpsize
)
||
(
sa
->
block_number
!=
block_num
)
||
(
sa
->
lim
!=
block_lim
)
||
(
sa
->
seekpos
!=
seek
))
return
false
;
if
((
sa
->
k_size
!=
key_sz
)
||
(
sa
->
inf_size
!=
inf_sz
)
||
(
sa
->
size
!=
tmpsize
)
||
(
sa
->
block_number
!=
block_num
)
||
(
sa
->
lim
!=
block_lim
)
||
(
sa
->
seekpos
!=
seek
))
{
delete
sa
;
return
false
;
}
delete
sa
;
k_size
=
key_sz
;
inf_size
=
inf_sz
;
...
...
@@ -139,10 +146,10 @@ bool TableBlockStorage::Open(const char* name, int key_sz, int inf_sz, int sz, i
mem
=
new
TableBlockStorageElem
*
[
block_size
];
for
(
i
=
0
;
i
<
block_size
;
i
++
)
{
mem
[
i
]
=
(
TableBlockStorageElem
*
)
malloc
(
full_size
)
;
mem
[
i
]
=
(
TableBlockStorageElem
*
)
new
char
[
full_size
]
;
}
TableBlockStorageElem
*
t
=
(
TableBlockStorageElem
*
)
malloc
(
full_size
)
;
TableBlockStorageElem
*
t
=
(
TableBlockStorageElem
*
)
new
char
[
full_size
]
;
seekpos
+=
sizeof
(
StorageAttr
);
for
(
i
=
0
;
i
<
block_num
;
i
++
)
...
...
@@ -161,6 +168,7 @@ bool TableBlockStorage::Open(const char* name, int key_sz, int inf_sz, int sz, i
fread
(
mem
[
i
],(
full_size
),
1
,
file
);
if
(
mem
[
i
]
->
count
>
max
)
max
=
mem
[
i
]
->
count
;
}
delete
t
;
return
true
;
}
...
...
@@ -178,8 +186,9 @@ bool TableBlockStorage::Create(const char* name, int key_sz, int inf_sz, int sz,
inf_size
=
inf_sz
;
seekpos
=
seek
;
lim
=
block_lim
;
int
i
,
full_size
=
sizeof
(
TableBlockStorageElem
)
+
k_size
+
inf_size
;
TableBlockStorageElem
*
t
=
(
TableBlockStorageElem
*
)
malloc
(
full_size
);
full_size
=
sizeof
(
TableBlockStorageElem
)
+
k_size
+
inf_size
;
int
i
;
TableBlockStorageElem
*
t
=
(
TableBlockStorageElem
*
)
new
char
[
full_size
];
size
=
(
sz
-
sizeof
(
StorageAttr
))
/
(
full_size
);
block_number
=
block_num
;
...
...
@@ -190,7 +199,7 @@ bool TableBlockStorage::Create(const char* name, int key_sz, int inf_sz, int sz,
mem
=
new
TableBlockStorageElem
*
[
block_size
];
for
(
i
=
0
;
i
<
block_size
;
i
++
)
{
mem
[
i
]
=
(
TableBlockStorageElem
*
)
malloc
(
full_size
)
;
mem
[
i
]
=
(
TableBlockStorageElem
*
)
new
char
[
full_size
]
;
}
StorageAttr
*
sa
=
new
StorageAttr
();
...
...
@@ -226,12 +235,13 @@ bool TableBlockStorage::Create(const char* name, int key_sz, int inf_sz, int sz,
mem
[
0
]
->
count
=-
5
;
for
(
i
=
1
;
i
<
block_size
;
i
++
)
mem
[
i
]
->
count
=-
1
;
delete
sa
;
delete
t
;
return
true
;
}
bool
TableBlockStorage
::
AddRow
(
char
*
key
,
char
*
value
)
{
int
full_size
=
sizeof
(
TableBlockStorageElem
)
+
k_size
+
inf_size
;
int
i
=
0
,
pos
=-
1
,
empty
=-
1
,
k
;
if
(
file
==
NULL
)
return
false
;
...
...
@@ -259,7 +269,6 @@ bool TableBlockStorage::AddRow(char* key, char* value)
bool
TableBlockStorage
::
DelRow
(
char
*
key
)
{
int
full_size
=
sizeof
(
TableBlockStorageElem
)
+
k_size
+
inf_size
;
int
i
;
if
(
file
==
NULL
)
return
false
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment