Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
c67aec25
Commit
c67aec25
authored
Mar 29, 2013
by
Masaki Muranaka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reduce temporary memory allocations. They are redundant.
parent
87423130
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
14 additions
and
58 deletions
+14
-58
src/dump.c
src/dump.c
+14
-58
No files found.
src/dump.c
View file @
c67aec25
...
@@ -107,19 +107,12 @@ get_pool_block_size(mrb_state *mrb, mrb_irep *irep)
...
@@ -107,19 +107,12 @@ get_pool_block_size(mrb_state *mrb, mrb_irep *irep)
static
int
static
int
write_pool_block
(
mrb_state
*
mrb
,
mrb_irep
*
irep
,
uint8_t
*
buf
)
write_pool_block
(
mrb_state
*
mrb
,
mrb_irep
*
irep
,
uint8_t
*
buf
)
{
{
int
result
;
size_t
pool_no
;
size_t
pool_no
;
uint8_t
*
cur
=
buf
;
uint8_t
*
cur
=
buf
;
size_t
buf_size
,
len
;
size_t
len
;
mrb_value
str
;
mrb_value
str
;
char
*
char_buf
=
NULL
;
const
char
*
char_ptr
;
char
char_buf
[
30
];
buf_size
=
MRB_DUMP_DEFAULT_STR_LEN
;
char_buf
=
(
char
*
)
mrb_malloc
(
mrb
,
buf_size
);
if
(
char_buf
==
NULL
)
{
result
=
MRB_DUMP_GENERAL_FAILURE
;
goto
error_exit
;
}
cur
+=
uint32_to_bin
(
irep
->
plen
,
cur
);
/* number of pool */
cur
+=
uint32_to_bin
(
irep
->
plen
,
cur
);
/* number of pool */
...
@@ -127,51 +120,37 @@ write_pool_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf)
...
@@ -127,51 +120,37 @@ write_pool_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf)
int
ai
=
mrb_gc_arena_save
(
mrb
);
int
ai
=
mrb_gc_arena_save
(
mrb
);
cur
+=
uint8_to_bin
(
mrb_type
(
irep
->
pool
[
pool_no
]),
cur
);
/* data type */
cur
+=
uint8_to_bin
(
mrb_type
(
irep
->
pool
[
pool_no
]),
cur
);
/* data type */
memset
(
char_buf
,
0
,
buf_size
);
switch
(
mrb_type
(
irep
->
pool
[
pool_no
]))
{
switch
(
mrb_type
(
irep
->
pool
[
pool_no
]))
{
case
MRB_TT_FIXNUM
:
case
MRB_TT_FIXNUM
:
str
=
mrb_fix2str
(
mrb
,
irep
->
pool
[
pool_no
],
10
);
str
=
mrb_fix2str
(
mrb
,
irep
->
pool
[
pool_no
],
10
);
memcpy
(
char_buf
,
RSTRING_PTR
(
str
),
RSTRING_LEN
(
str
)
);
char_ptr
=
RSTRING_PTR
(
str
);
len
=
RSTRING_LEN
(
str
);
len
=
RSTRING_LEN
(
str
);
break
;
break
;
case
MRB_TT_FLOAT
:
case
MRB_TT_FLOAT
:
len
=
mrb_float_to_str
(
char_buf
,
mrb_float
(
irep
->
pool
[
pool_no
]));
len
=
mrb_float_to_str
(
char_buf
,
mrb_float
(
irep
->
pool
[
pool_no
]));
char_ptr
=
&
char_buf
[
0
];
break
;
break
;
case
MRB_TT_STRING
:
case
MRB_TT_STRING
:
str
=
irep
->
pool
[
pool_no
];
str
=
irep
->
pool
[
pool_no
];
char_ptr
=
RSTRING_PTR
(
str
);
len
=
RSTRING_LEN
(
str
);
len
=
RSTRING_LEN
(
str
);
if
(
len
>
buf_size
-
1
)
{
buf_size
=
len
+
1
;
char_buf
=
(
char
*
)
mrb_realloc
(
mrb
,
char_buf
,
buf_size
);
if
(
char_buf
==
NULL
)
{
mrb_gc_arena_restore
(
mrb
,
ai
);
result
=
MRB_DUMP_GENERAL_FAILURE
;
goto
error_exit
;
}
memset
(
char_buf
,
0
,
buf_size
);
}
memcpy
(
char_buf
,
RSTRING_PTR
(
str
),
RSTRING_LEN
(
str
));
break
;
break
;
default:
default:
len
=
0
;
continue
;
continue
;
}
}
cur
+=
uint16_to_bin
(
len
,
cur
);
/* data length */
cur
+=
uint16_to_bin
(
len
,
cur
);
/* data length */
memcpy
(
cur
,
char_
buf
,
len
);
memcpy
(
cur
,
char_
ptr
,
len
);
cur
+=
len
;
cur
+=
len
;
mrb_gc_arena_restore
(
mrb
,
ai
);
mrb_gc_arena_restore
(
mrb
,
ai
);
}
}
result
=
(
int
)(
cur
-
buf
);
error_exit:
return
(
int
)(
cur
-
buf
);
mrb_free
(
mrb
,
char_buf
);
return
result
;
}
}
...
@@ -197,21 +176,10 @@ get_syms_block_size(mrb_state *mrb, mrb_irep *irep)
...
@@ -197,21 +176,10 @@ get_syms_block_size(mrb_state *mrb, mrb_irep *irep)
static
int
static
int
write_syms_block
(
mrb_state
*
mrb
,
mrb_irep
*
irep
,
uint8_t
*
buf
)
write_syms_block
(
mrb_state
*
mrb
,
mrb_irep
*
irep
,
uint8_t
*
buf
)
{
{
int
result
;
size_t
sym_no
;
size_t
sym_no
;
size_t
buf_size
;
uint8_t
*
cur
=
buf
;
uint8_t
*
cur
=
buf
;
uint16_t
nlen
;
char
*
char_buf
=
NULL
;
const
char
*
name
;
const
char
*
name
;
buf_size
=
MRB_DUMP_DEFAULT_STR_LEN
;
char_buf
=
(
char
*
)
mrb_malloc
(
mrb
,
buf_size
);
if
(
char_buf
==
NULL
)
{
result
=
MRB_DUMP_GENERAL_FAILURE
;
goto
error_exit
;
}
cur
+=
uint32_to_bin
(
irep
->
slen
,
cur
);
/* number of symbol */
cur
+=
uint32_to_bin
(
irep
->
slen
,
cur
);
/* number of symbol */
for
(
sym_no
=
0
;
sym_no
<
irep
->
slen
;
sym_no
++
)
{
for
(
sym_no
=
0
;
sym_no
<
irep
->
slen
;
sym_no
++
)
{
...
@@ -219,32 +187,20 @@ write_syms_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf)
...
@@ -219,32 +187,20 @@ write_syms_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf)
size_t
len
;
size_t
len
;
name
=
mrb_sym2name_len
(
mrb
,
irep
->
syms
[
sym_no
],
&
len
);
name
=
mrb_sym2name_len
(
mrb
,
irep
->
syms
[
sym_no
],
&
len
);
if
(
len
>
UINT16_MAX
)
goto
error_exit
;
if
(
len
>
UINT16_MAX
)
{
nlen
=
(
uint16_t
)
len
;
return
MRB_DUMP_GENERAL_FAILURE
;
if
(
nlen
>
buf_size
-
1
)
{
buf_size
=
nlen
+
1
;
char_buf
=
(
char
*
)
mrb_realloc
(
mrb
,
char_buf
,
buf_size
);
if
(
char_buf
==
NULL
)
{
result
=
MRB_DUMP_GENERAL_FAILURE
;
goto
error_exit
;
}
}
}
memset
(
char_buf
,
0
,
buf_size
);
memcpy
(
char_buf
,
name
,
len
);
cur
+=
uint16_to_bin
(
n
len
,
cur
);
/* length of symbol name */
cur
+=
uint16_to_bin
(
(
uint16_t
)
len
,
cur
);
/* length of symbol name */
memcpy
(
cur
,
char_buf
,
n
len
);
/* symbol name */
memcpy
(
cur
,
name
,
len
);
/* symbol name */
cur
+=
n
len
;
cur
+=
(
uint16_t
)
len
;
}
}
else
{
else
{
cur
+=
uint16_to_bin
(
MRB_DUMP_NULL_SYM_LEN
,
cur
);
/* length of symbol name */
cur
+=
uint16_to_bin
(
MRB_DUMP_NULL_SYM_LEN
,
cur
);
/* length of symbol name */
}
}
}
}
result
=
(
int
)(
cur
-
buf
);
error_exit:
return
(
int
)(
cur
-
buf
);
mrb_free
(
mrb
,
char_buf
);
return
result
;
}
}
...
...
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