Issue #25318: cleanup code _PyBytesWriter

Rename "stack buffer" to "small buffer".

Add also an assertion in _PyBytesWriter_GetPos().
This commit is contained in:
Victor Stinner 2015-10-09 03:38:24 +02:00
parent 3fa36ff5e4
commit b3653a3458
2 changed files with 19 additions and 19 deletions

View File

@ -141,8 +141,8 @@ typedef struct {
int overallocate; int overallocate;
/* Stack buffer */ /* Stack buffer */
int use_stack_buffer; int use_small_buffer;
char stack_buffer[512]; char small_buffer[512];
} _PyBytesWriter; } _PyBytesWriter;
/* Initialize a bytes writer /* Initialize a bytes writer

View File

@ -3754,9 +3754,9 @@ _PyBytesWriter_Init(_PyBytesWriter *writer)
writer->allocated = 0; writer->allocated = 0;
writer->size = 0; writer->size = 0;
writer->overallocate = 0; writer->overallocate = 0;
writer->use_stack_buffer = 0; writer->use_small_buffer = 0;
#ifdef Py_DEBUG #ifdef Py_DEBUG
memset(writer->stack_buffer, 0xCB, sizeof(writer->stack_buffer)); memset(writer->small_buffer, 0xCB, sizeof(writer->small_buffer));
#endif #endif
} }
@ -3769,13 +3769,13 @@ _PyBytesWriter_Dealloc(_PyBytesWriter *writer)
Py_LOCAL_INLINE(char*) Py_LOCAL_INLINE(char*)
_PyBytesWriter_AsString(_PyBytesWriter *writer) _PyBytesWriter_AsString(_PyBytesWriter *writer)
{ {
if (!writer->use_stack_buffer) { if (!writer->use_small_buffer) {
assert(writer->buffer != NULL); assert(writer->buffer != NULL);
return PyBytes_AS_STRING(writer->buffer); return PyBytes_AS_STRING(writer->buffer);
} }
else { else {
assert(writer->buffer == NULL); assert(writer->buffer == NULL);
return writer->stack_buffer; return writer->small_buffer;
} }
} }
@ -3785,6 +3785,7 @@ _PyBytesWriter_GetPos(_PyBytesWriter *writer, char *str)
char *start = _PyBytesWriter_AsString(writer); char *start = _PyBytesWriter_AsString(writer);
assert(str != NULL); assert(str != NULL);
assert(str >= start); assert(str >= start);
assert(str - start <= writer->allocated);
return str - start; return str - start;
} }
@ -3794,7 +3795,7 @@ _PyBytesWriter_CheckConsistency(_PyBytesWriter *writer, char *str)
#ifdef Py_DEBUG #ifdef Py_DEBUG
char *start, *end; char *start, *end;
if (!writer->use_stack_buffer) { if (!writer->use_small_buffer) {
assert(writer->buffer != NULL); assert(writer->buffer != NULL);
assert(PyBytes_CheckExact(writer->buffer)); assert(PyBytes_CheckExact(writer->buffer));
assert(Py_REFCNT(writer->buffer) == 1); assert(Py_REFCNT(writer->buffer) == 1);
@ -3846,7 +3847,7 @@ _PyBytesWriter_Prepare(_PyBytesWriter *writer, char *str, Py_ssize_t size)
} }
pos = _PyBytesWriter_GetPos(writer, str); pos = _PyBytesWriter_GetPos(writer, str);
if (!writer->use_stack_buffer) { if (!writer->use_small_buffer) {
/* Note: Don't use a bytearray object because the conversion from /* Note: Don't use a bytearray object because the conversion from
byterray to bytes requires to copy all bytes. */ byterray to bytes requires to copy all bytes. */
if (_PyBytes_Resize(&writer->buffer, allocated)) { if (_PyBytes_Resize(&writer->buffer, allocated)) {
@ -3864,15 +3865,14 @@ _PyBytesWriter_Prepare(_PyBytesWriter *writer, char *str, Py_ssize_t size)
if (pos != 0) { if (pos != 0) {
Py_MEMCPY(PyBytes_AS_STRING(writer->buffer), Py_MEMCPY(PyBytes_AS_STRING(writer->buffer),
writer->stack_buffer, writer->small_buffer,
pos); pos);
} }
writer->use_small_buffer = 0;
#ifdef Py_DEBUG #ifdef Py_DEBUG
memset(writer->stack_buffer, 0xDB, sizeof(writer->stack_buffer)); memset(writer->small_buffer, 0xDB, sizeof(writer->small_buffer));
#endif #endif
writer->use_stack_buffer = 0;
} }
writer->allocated = allocated; writer->allocated = allocated;
@ -3891,15 +3891,15 @@ _PyBytesWriter_Alloc(_PyBytesWriter *writer, Py_ssize_t size)
assert(writer->size == 0 && writer->buffer == NULL); assert(writer->size == 0 && writer->buffer == NULL);
assert(size >= 0); assert(size >= 0);
writer->use_stack_buffer = 1; writer->use_small_buffer = 1;
#ifdef Py_DEBUG #ifdef Py_DEBUG
/* the last byte is reserved, it must be '\0' */ /* the last byte is reserved, it must be '\0' */
writer->stack_buffer[sizeof(writer->stack_buffer) - 1] = 0; writer->allocated = sizeof(writer->small_buffer) - 1;
writer->allocated = sizeof(writer->stack_buffer) - 1; writer->small_buffer[writer->allocated] = 0;
#else #else
writer->allocated = sizeof(writer->stack_buffer); writer->allocated = sizeof(writer->small_buffer);
#endif #endif
return _PyBytesWriter_Prepare(writer, writer->stack_buffer, size); return _PyBytesWriter_Prepare(writer, writer->small_buffer, size);
} }
PyObject * PyObject *
@ -3911,7 +3911,7 @@ _PyBytesWriter_Finish(_PyBytesWriter *writer, char *str)
_PyBytesWriter_CheckConsistency(writer, str); _PyBytesWriter_CheckConsistency(writer, str);
pos = _PyBytesWriter_GetPos(writer, str); pos = _PyBytesWriter_GetPos(writer, str);
if (!writer->use_stack_buffer) { if (!writer->use_small_buffer) {
if (pos != writer->allocated) { if (pos != writer->allocated) {
if (_PyBytes_Resize(&writer->buffer, pos)) { if (_PyBytes_Resize(&writer->buffer, pos)) {
assert(writer->buffer == NULL); assert(writer->buffer == NULL);
@ -3923,7 +3923,7 @@ _PyBytesWriter_Finish(_PyBytesWriter *writer, char *str)
writer->buffer = NULL; writer->buffer = NULL;
} }
else { else {
result = PyBytes_FromStringAndSize(writer->stack_buffer, pos); result = PyBytes_FromStringAndSize(writer->small_buffer, pos);
} }
return result; return result;