aux: fix for empty_foo() returning non-NULL ptr

Updates #135
This commit is contained in:
Nigel Tao committed 2023-10-25 16:06:10 +11:00
1 parent b900be0315
commit 8cfde80759
4 files changed
+33 -8

No files matched your search

+1
View File
@@ -26,6 +26,7 @@ The LICENSE has changed from a single license (Apache 2) to a dual license
- Changed `lzw.set_literal_width` to `lzw.set_quirk`.
- Changed `set_quirk_enabled!(quirk: u32, enabled: bool)` to `set_quirk!(key:
u32, value: u64) status`.
- Changed `wuffs_base__empty_foo` to return non-NULL ptr.
- Deprecate `std/lzw.decoder.flush`.
- Generated constants now default to unsigned.
- Halve the sizeof `wuffs_foo__bar::unique_ptr`.
+14 -4
View File
@@ -25,14 +25,14 @@ DynIOBuffer::DynIOBuffer(uint64_t max_incl)
: m_buf(wuffs_base__empty_io_buffer()), m_max_incl(max_incl) {}
DynIOBuffer::~DynIOBuffer() {
if (m_buf.data.ptr) {
if (allocated()) {
free(m_buf.data.ptr);
}
}
void //
DynIOBuffer::drop() {
if (m_buf.data.ptr) {
if (allocated()) {
free(m_buf.data.ptr);
}
m_buf = wuffs_base__empty_io_buffer();
@@ -45,17 +45,27 @@ DynIOBuffer::grow(uint64_t min_incl) {
return ((min_incl == 0) && (m_max_incl == 0))
? DynIOBuffer::GrowResult::OK
: DynIOBuffer::GrowResult::FailedMaxInclExceeded;
} else if (n > SIZE_MAX) {
return DynIOBuffer::GrowResult::FailedOutOfMemory;
} else if (n > m_buf.data.len) {
uint8_t* ptr = static_cast<uint8_t*>(realloc(m_buf.data.ptr, n));
uint8_t* ptr = static_cast<uint8_t*>(
allocated() ? realloc(m_buf.data.ptr, static_cast<size_t>(n))
: malloc(static_cast<size_t>(n)));
if (!ptr) {
return DynIOBuffer::GrowResult::FailedOutOfMemory;
}
m_buf.data.ptr = ptr;
m_buf.data.len = n;
m_buf.data.len = static_cast<size_t>(n);
}
return DynIOBuffer::GrowResult::OK;
}
bool //
DynIOBuffer::allocated() {
IOBuffer buf = wuffs_base__empty_io_buffer();
return buf.data.ptr != m_buf.data.ptr;
}
// round_up rounds min_incl up, returning the smallest value x satisfying
// (min_incl <= x) and (x <= max_incl) and some other constraints. It returns 0
// if there is no such x.
+2
View File
@@ -84,6 +84,8 @@ class DynIOBuffer {
DynIOBuffer(const DynIOBuffer&) = delete;
DynIOBuffer& operator=(const DynIOBuffer&) = delete;
bool allocated();
static uint64_t round_up(uint64_t min_incl, uint64_t max_incl);
};
+16 -4
View File
@@ -13626,6 +13626,8 @@ class DynIOBuffer {
DynIOBuffer(const DynIOBuffer&) = delete;
DynIOBuffer& operator=(const DynIOBuffer&) = delete;
bool allocated();
static uint64_t round_up(uint64_t min_incl, uint64_t max_incl);
};
@@ -64220,14 +64222,14 @@ DynIOBuffer::DynIOBuffer(uint64_t max_incl)
: m_buf(wuffs_base__empty_io_buffer()), m_max_incl(max_incl) {}
DynIOBuffer::~DynIOBuffer() {
if (m_buf.data.ptr) {
if (allocated()) {
free(m_buf.data.ptr);
}
}
void //
DynIOBuffer::drop() {
if (m_buf.data.ptr) {
if (allocated()) {
free(m_buf.data.ptr);
}
m_buf = wuffs_base__empty_io_buffer();
@@ -64240,17 +64242,27 @@ DynIOBuffer::grow(uint64_t min_incl) {
return ((min_incl == 0) && (m_max_incl == 0))
? DynIOBuffer::GrowResult::OK
: DynIOBuffer::GrowResult::FailedMaxInclExceeded;
} else if (n > SIZE_MAX) {
return DynIOBuffer::GrowResult::FailedOutOfMemory;
} else if (n > m_buf.data.len) {
uint8_t* ptr = static_cast<uint8_t*>(realloc(m_buf.data.ptr, n));
uint8_t* ptr = static_cast<uint8_t*>(
allocated() ? realloc(m_buf.data.ptr, static_cast<size_t>(n))
: malloc(static_cast<size_t>(n)));
if (!ptr) {
return DynIOBuffer::GrowResult::FailedOutOfMemory;
}
m_buf.data.ptr = ptr;
m_buf.data.len = n;
m_buf.data.len = static_cast<size_t>(n);
}
return DynIOBuffer::GrowResult::OK;
}
bool //
DynIOBuffer::allocated() {
IOBuffer buf = wuffs_base__empty_io_buffer();
return buf.data.ptr != m_buf.data.ptr;
}
// round_up rounds min_incl up, returning the smallest value x satisfying
// (min_incl <= x) and (x <= max_incl) and some other constraints. It returns 0
// if there is no such x.