Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
Pyston
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
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
Pyston
Commits
261d33c0
Commit
261d33c0
authored
May 18, 2016
by
Kevin Modzelewski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Revert to CPython's _PyUnicode_New
parent
6466fee0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
71 additions
and
52 deletions
+71
-52
from_cpython/Objects/unicodeobject.c
from_cpython/Objects/unicodeobject.c
+71
-3
src/runtime/types.cpp
src/runtime/types.cpp
+0
-49
No files found.
from_cpython/Objects/unicodeobject.c
View file @
261d33c0
...
...
@@ -108,9 +108,8 @@ PyUnicodeObject *unicode_empty = NULL;
Py_INCREF(unicode_empty); \
else { \
unicode_empty = _PyUnicode_New(0); \
if (unicode_empty != NULL)
{
\
if (unicode_empty != NULL)
\
Py_INCREF(unicode_empty); \
} \
} \
return (PyObject *)unicode_empty; \
} while (0)
...
...
@@ -316,7 +315,76 @@ int unicode_resize(register PyUnicodeObject *unicode,
*/
extern
PyUnicodeObject
*
_PyUnicode_New
(
Py_ssize_t
length
);
static
PyUnicodeObject
*
_PyUnicode_New
(
Py_ssize_t
length
)
{
register
PyUnicodeObject
*
unicode
;
/* Optimization for empty strings */
if
(
length
==
0
&&
unicode_empty
!=
NULL
)
{
Py_INCREF
(
unicode_empty
);
return
unicode_empty
;
}
/* Ensure we won't overflow the size. */
if
(
length
>
((
PY_SSIZE_T_MAX
/
sizeof
(
Py_UNICODE
))
-
1
))
{
return
(
PyUnicodeObject
*
)
PyErr_NoMemory
();
}
/* Unicode freelist & memory allocation */
if
(
free_list
)
{
unicode
=
free_list
;
free_list
=
*
(
PyUnicodeObject
**
)
unicode
;
numfree
--
;
if
(
unicode
->
str
)
{
/* Keep-Alive optimization: we only upsize the buffer,
never downsize it. */
if
((
unicode
->
length
<
length
)
&&
unicode_resize
(
unicode
,
length
)
<
0
)
{
PyObject_DEL
(
unicode
->
str
);
unicode
->
str
=
NULL
;
}
}
else
{
size_t
new_size
=
sizeof
(
Py_UNICODE
)
*
((
size_t
)
length
+
1
);
unicode
->
str
=
(
Py_UNICODE
*
)
PyObject_MALLOC
(
new_size
);
}
PyObject_INIT
(
unicode
,
&
PyUnicode_Type
);
}
else
{
size_t
new_size
;
unicode
=
PyObject_New
(
PyUnicodeObject
,
&
PyUnicode_Type
);
if
(
unicode
==
NULL
)
return
NULL
;
new_size
=
sizeof
(
Py_UNICODE
)
*
((
size_t
)
length
+
1
);
unicode
->
str
=
(
Py_UNICODE
*
)
PyObject_MALLOC
(
new_size
);
}
if
(
!
unicode
->
str
)
{
PyErr_NoMemory
();
goto
onError
;
}
/* Initialize the first element to guard against cases where
* the caller fails before initializing str -- unicode_resize()
* reads str[0], and the Keep-Alive optimization can keep memory
* allocated for str alive across a call to unicode_dealloc(unicode).
* We don't want unicode_resize to read uninitialized memory in
* that case.
*/
unicode
->
str
[
0
]
=
0
;
unicode
->
str
[
length
]
=
0
;
unicode
->
length
=
length
;
unicode
->
hash
=
-
1
;
unicode
->
defenc
=
NULL
;
return
unicode
;
onError:
/* XXX UNREF/NEWREF interface should be more symmetrical */
_Py_DEC_REFTOTAL
;
_Py_ForgetReference
((
PyObject
*
)
unicode
);
PyObject_Del
(
unicode
);
return
NULL
;
}
static
void
unicode_dealloc
(
register
PyUnicodeObject
*
unicode
)
...
...
src/runtime/types.cpp
View file @
261d33c0
...
...
@@ -3632,55 +3632,6 @@ out:
return
result
;
}
extern
"C"
PyUnicodeObject
*
unicode_empty
;
extern
"C"
PyUnicodeObject
*
_PyUnicode_New
(
Py_ssize_t
length
)
noexcept
{
PyUnicodeObject
*
unicode
;
/* Optimization for empty strings */
if
(
length
==
0
&&
unicode_empty
!=
NULL
)
{
Py_INCREF
(
unicode_empty
);
return
unicode_empty
;
}
/* Ensure we won't overflow the size. */
if
(
length
>
((
PY_SSIZE_T_MAX
/
sizeof
(
Py_UNICODE
))
-
1
))
{
return
(
PyUnicodeObject
*
)
PyErr_NoMemory
();
}
// Pyston change: allocate ->str first, so that if this allocation
// causes a collection, we don't see a half-created unicode object:
size_t
new_size
=
sizeof
(
Py_UNICODE
)
*
((
size_t
)
length
+
1
);
unicode
=
PyObject_New
(
PyUnicodeObject
,
&
PyUnicode_Type
);
if
(
unicode
==
NULL
)
return
NULL
;
unicode
->
str
=
(
Py_UNICODE
*
)
PyObject_MALLOC
(
new_size
);
if
(
!
unicode
->
str
)
{
Py_DECREF
(
unicode
);
return
(
PyUnicodeObject
*
)
PyErr_NoMemory
();
}
#if STAT_ALLOCATIONS
{
size_t
size
=
sizeof
(
PyUnicodeObject
);
ALLOC_STATS
(
unicode_cls
);
}
#endif
/* Initialize the first element to guard against cases where
* the caller fails before initializing str -- unicode_resize()
* reads str[0], and the Keep-Alive optimization can keep memory
* allocated for str alive across a call to unicode_dealloc(unicode).
* We don't want unicode_resize to read uninitialized memory in
* that case.
*/
unicode
->
str
[
0
]
=
0
;
unicode
->
str
[
length
]
=
0
;
unicode
->
length
=
length
;
unicode
->
hash
=
-
1
;
unicode
->
defenc
=
NULL
;
return
unicode
;
}
void
dealloc_null
(
Box
*
box
)
{
assert
(
box
->
cls
->
tp_del
==
NULL
);
}
...
...
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