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
GitLab 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
f0954e3f
Commit
f0954e3f
authored
11 years ago
by
Damien George
Browse files
Options
Downloads
Patches
Plain Diff
py: Add emergency exception object for when heap allocation fails.
parent
6f355fd3
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
py/malloc.c
+14
-0
14 additions, 0 deletions
py/malloc.c
py/misc.h
+1
-0
1 addition, 0 deletions
py/misc.h
py/objexcept.c
+39
-20
39 additions, 20 deletions
py/objexcept.c
with
54 additions
and
20 deletions
py/malloc.c
+
14
−
0
View file @
f0954e3f
...
...
@@ -53,6 +53,20 @@ void *m_malloc(int num_bytes) {
return
ptr
;
}
void
*
m_malloc_maybe
(
int
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
;
UPDATE_PEAK
();
#endif
DEBUG_printf
(
"malloc %d : %p
\n
"
,
num_bytes
,
ptr
);
return
ptr
;
}
#if MICROPY_ENABLE_FINALISER
void
*
m_malloc_with_finaliser
(
int
num_bytes
)
{
if
(
num_bytes
==
0
)
{
...
...
This diff is collapsed.
Click to expand it.
py/misc.h
+
1
−
0
View file @
f0954e3f
...
...
@@ -38,6 +38,7 @@ typedef unsigned int uint;
#define m_del_var(obj_type, var_type, var_num, ptr) (m_free(ptr, sizeof(obj_type) + sizeof(var_type) * (var_num)))
void
*
m_malloc
(
int
num_bytes
);
void
*
m_malloc_maybe
(
int
num_bytes
);
void
*
m_malloc_with_finaliser
(
int
num_bytes
);
void
*
m_malloc0
(
int
num_bytes
);
void
*
m_realloc
(
void
*
ptr
,
int
old_num_bytes
,
int
new_num_bytes
);
...
...
This diff is collapsed.
Click to expand it.
py/objexcept.c
+
39
−
20
View file @
f0954e3f
...
...
@@ -20,6 +20,9 @@ typedef struct _mp_obj_exception_t {
// Instance of MemoryError exception - needed by mp_malloc_fail
const
mp_obj_exception_t
mp_const_MemoryError_obj
=
{{
&
mp_type_MemoryError
},
MP_OBJ_NULL
,
{{
&
mp_type_tuple
},
0
}};
// Local non-heap memory for allocating an exception when we run out of RAM
STATIC
mp_obj_exception_t
mp_emergency_exception_obj
;
// Instance of GeneratorExit exception - needed by generator.close()
// This would belong to objgenerator.c, but to keep mp_obj_exception_t
// definition module-private so far, have it here.
...
...
@@ -51,7 +54,13 @@ STATIC mp_obj_t mp_obj_exception_make_new(mp_obj_t type_in, uint n_args, uint n_
nlr_raise
(
mp_obj_new_exception_msg_varg
(
&
mp_type_TypeError
,
"%s does not take keyword arguments"
,
mp_obj_get_type_str
(
type_in
)));
}
mp_obj_exception_t
*
o
=
m_new_obj_var
(
mp_obj_exception_t
,
mp_obj_t
,
n_args
);
mp_obj_exception_t
*
o
=
m_malloc_maybe
(
sizeof
(
mp_obj_exception_t
)
+
n_args
*
sizeof
(
mp_obj_t
));
if
(
o
==
NULL
)
{
// Couldn't allocate heap memory; use local data instead.
o
=
&
mp_emergency_exception_obj
;
// We can't store any args.
n_args
=
0
;
}
o
->
base
.
type
=
type
;
o
->
traceback
=
MP_OBJ_NULL
;
o
->
args
.
base
.
type
=
&
mp_type_tuple
;
...
...
@@ -196,7 +205,16 @@ mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char
assert
(
exc_type
->
make_new
==
mp_obj_exception_make_new
);
// make exception object
mp_obj_exception_t
*
o
=
m_new_obj_var
(
mp_obj_exception_t
,
mp_obj_t
,
1
);
mp_obj_exception_t
*
o
=
m_malloc_maybe
(
sizeof
(
mp_obj_exception_t
)
+
1
*
sizeof
(
mp_obj_t
));
if
(
o
==
NULL
)
{
// Couldn't allocate heap memory; use local data instead.
// Unfortunately, we won't be able to format the string...
o
=
&
mp_emergency_exception_obj
;
o
->
base
.
type
=
exc_type
;
o
->
traceback
=
MP_OBJ_NULL
;
o
->
args
.
base
.
type
=
&
mp_type_tuple
;
o
->
args
.
len
=
0
;
}
else
{
o
->
base
.
type
=
exc_type
;
o
->
traceback
=
MP_OBJ_NULL
;
o
->
args
.
base
.
type
=
&
mp_type_tuple
;
...
...
@@ -216,6 +234,7 @@ mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char
o
->
args
.
items
[
0
]
=
mp_obj_new_str
((
byte
*
)
vstr
->
buf
,
vstr
->
len
,
false
);
vstr_free
(
vstr
);
}
}
return
o
;
}
...
...
@@ -259,7 +278,7 @@ void mp_obj_exception_clear_traceback(mp_obj_t self_in) {
void
mp_obj_exception_add_traceback
(
mp_obj_t
self_in
,
qstr
file
,
machine_uint_t
line
,
qstr
block
)
{
// make sure self_in is an exception instance
// TODO add traceback information to user-defined exceptions (need proper builtin subclassing for that)
if
(
mp_obj_get_type
(
self_in
)
->
make_new
==
mp_obj_exception_make_new
)
{
if
(
mp_obj_get_type
(
self_in
)
->
make_new
==
mp_obj_exception_make_new
&&
self_in
!=
&
mp_emergency_exception_obj
)
{
mp_obj_exception_t
*
self
=
self_in
;
// for traceback, we are just using the list object for convenience, it's not really a list of Python objects
...
...
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