Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
micropython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
card10
micropython
Commits
37378f8a
Commit
37378f8a
authored
10 years ago
by
Damien George
Browse files
Options
Downloads
Patches
Plain Diff
py: Clean up edge cases of malloc/realloc/free.
parent
f5d69794
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
py/gc.c
+6
-0
6 additions, 0 deletions
py/gc.c
py/malloc.c
+14
-28
14 additions, 28 deletions
py/malloc.c
with
20 additions
and
28 deletions
py/gc.c
+
6
−
0
View file @
37378f8a
...
...
@@ -546,6 +546,12 @@ void *gc_realloc(void *ptr_in, mp_uint_t n_bytes) {
return
gc_alloc
(
n_bytes
,
false
);
}
// check for pure free
if
(
n_bytes
==
0
)
{
gc_free
(
ptr_in
);
return
NULL
;
}
mp_uint_t
ptr
=
(
mp_uint_t
)
ptr_in
;
// sanity check the ptr
...
...
This diff is collapsed.
Click to expand it.
py/malloc.c
+
14
−
28
View file @
37378f8a
...
...
@@ -63,11 +63,8 @@ STATIC size_t peak_bytes_allocated = 0;
#endif // MICROPY_ENABLE_GC
void
*
m_malloc
(
size_t
num_bytes
)
{
if
(
num_bytes
==
0
)
{
return
NULL
;
}
void
*
ptr
=
malloc
(
num_bytes
);
if
(
ptr
==
NULL
)
{
if
(
ptr
==
NULL
&&
num_bytes
!=
0
)
{
return
m_malloc_fail
(
num_bytes
);
}
#if MICROPY_MEM_STATS
...
...
@@ -81,9 +78,6 @@ void *m_malloc(size_t num_bytes) {
void
*
m_malloc_maybe
(
size_t
num_bytes
)
{
void
*
ptr
=
malloc
(
num_bytes
);
if
(
ptr
==
NULL
)
{
return
NULL
;
}
#if MICROPY_MEM_STATS
total_bytes_allocated
+=
num_bytes
;
current_bytes_allocated
+=
num_bytes
;
...
...
@@ -95,11 +89,8 @@ void *m_malloc_maybe(size_t num_bytes) {
#if MICROPY_ENABLE_FINALISER
void
*
m_malloc_with_finaliser
(
size_t
num_bytes
)
{
if
(
num_bytes
==
0
)
{
return
NULL
;
}
void
*
ptr
=
malloc_with_finaliser
(
num_bytes
);
if
(
ptr
==
NULL
)
{
if
(
ptr
==
NULL
&&
num_bytes
!=
0
)
{
return
m_malloc_fail
(
num_bytes
);
}
#if MICROPY_MEM_STATS
...
...
@@ -114,19 +105,16 @@ void *m_malloc_with_finaliser(size_t num_bytes) {
void
*
m_malloc0
(
size_t
num_bytes
)
{
void
*
ptr
=
m_malloc
(
num_bytes
);
if
(
ptr
!
=
NULL
)
{
memset
(
ptr
,
0
,
num_bytes
);
if
(
ptr
=
=
NULL
&&
num_bytes
!=
0
)
{
return
m_malloc_fail
(
num_bytes
);
}
memset
(
ptr
,
0
,
num_bytes
);
return
ptr
;
}
void
*
m_realloc
(
void
*
ptr
,
size_t
old_num_bytes
,
size_t
new_num_bytes
)
{
if
(
new_num_bytes
==
0
)
{
free
(
ptr
);
return
NULL
;
}
void
*
new_ptr
=
realloc
(
ptr
,
new_num_bytes
);
if
(
new_ptr
==
NULL
)
{
if
(
new_ptr
==
NULL
&&
new_num_bytes
!=
0
)
{
return
m_malloc_fail
(
new_num_bytes
);
}
#if MICROPY_MEM_STATS
...
...
@@ -146,28 +134,26 @@ void *m_realloc(void *ptr, size_t old_num_bytes, size_t new_num_bytes) {
void
*
m_realloc_maybe
(
void
*
ptr
,
size_t
old_num_bytes
,
size_t
new_num_bytes
)
{
void
*
new_ptr
=
realloc
(
ptr
,
new_num_bytes
);
if
(
new_ptr
==
NULL
)
{
return
NULL
;
}
#if MICROPY_MEM_STATS
// At first thought, "Total bytes allocated" should only grow,
// after all, it's *total*. But consider for example 2K block
// shrunk to 1K and then grown to 2K again. It's still 2K
// allocated total. If we process only positive increments,
// we'll count 3K.
size_t
diff
=
new_num_bytes
-
old_num_bytes
;
total_bytes_allocated
+=
diff
;
current_bytes_allocated
+=
diff
;
UPDATE_PEAK
();
// Also, don't count failed reallocs.
if
(
!
(
new_ptr
==
NULL
&&
new_num_bytes
!=
0
))
{
size_t
diff
=
new_num_bytes
-
old_num_bytes
;
total_bytes_allocated
+=
diff
;
current_bytes_allocated
+=
diff
;
UPDATE_PEAK
();
}
#endif
DEBUG_printf
(
"realloc %p, %d, %d : %p
\n
"
,
ptr
,
old_num_bytes
,
new_num_bytes
,
new_ptr
);
return
new_ptr
;
}
void
m_free
(
void
*
ptr
,
size_t
num_bytes
)
{
if
(
ptr
!=
NULL
)
{
free
(
ptr
);
}
free
(
ptr
);
#if MICROPY_MEM_STATS
current_bytes_allocated
-=
num_bytes
;
#endif
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment