[libcxx-commits] [libcxx] [libc++] Update the status for lwg-3120 (PR #116772)
via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Nov 19 01:47:13 PST 2024
love1angel wrote:
the two example provided in https://cplusplus.github.io/LWG/issue3120 is also tested.
## example 1 provided
``` cpp
#include <memory_resource>
int main()
{
char buffer[100];
{
std::pmr::monotonic_buffer_resource mr(buffer, 100, std::pmr::null_memory_resource());
mr.release();
mr.allocate(60); // A
}
{
std::pmr::monotonic_buffer_resource mr(buffer, 100, std::pmr::null_memory_resource());
mr.allocate(60); // B
mr.release();
mr.allocate(60); // C
}
}
```
below is the test result
``` text
[4/5] Building CXX object CMakeFiles/main.dir/main.cpp.o
/Users/heli/github/cpp/main.cpp:9:5: warning: ignoring return value of function declared with 'nodiscard' attribute [-Wunused-result]
9 | mr.allocate(60); // A
| ^~~~~~~~~~~
/Users/heli/github/cpp/main.cpp:13:5: warning: ignoring return value of function declared with 'nodiscard' attribute [-Wunused-result]
13 | mr.allocate(60); // B
| ^~~~~~~~~~~
/Users/heli/github/cpp/main.cpp:15:5: warning: ignoring return value of function declared with 'nodiscard' attribute [-Wunused-result]
15 | mr.allocate(60); // C
| ^~~~~~~~~~~
3 warnings generated.
[5/5] Linking CXX executable main
heli at helis-MacBook-Pro ~/github/cpp cmake --build build --parallel 3 && ./build/main
```
## example 2 provided
``` cpp
std::pmr::monotonic_buffer_resource mr(std::pmr::new_delete_resource());
for (int i=0; i < 100; ++i) {
mr.allocate(1); // D
mr.release();
}
```
i tested it through this, A, B, C is succeess.
``` cpp
#include <memory_resource>
#include <print>
class MyPmr : public std::pmr::memory_resource {
public:
explicit MyPmr(std::pmr::memory_resource* upstream)
: m_upstream(upstream)
{
}
private:
void* do_allocate(size_t bytes, size_t align) override
{
std::println("allocate: {}, {}", bytes, align);
void* p = m_upstream->allocate(bytes, align);
return p;
}
void do_deallocate(void* p, size_t bytes, size_t align) override
{
std::println("deallocate: {}, {}, {}", p, bytes, align);
m_upstream->deallocate(p, bytes, align);
}
bool do_is_equal(memory_resource const& other) const noexcept override
{
return other.is_equal(*m_upstream);
}
private:
std::pmr::memory_resource* m_upstream;
};
int main()
{
MyPmr s { std::pmr::new_delete_resource() };
std::pmr::monotonic_buffer_resource mr { &s };
for (int i = 0; i < 100; ++i) {
mr.allocate(1); // D
mr.release();
}
}
```
which output always
``` text
deallocate: 0x122809200, 2080, 8
allocate: 2080, 8
deallocate: 0x122809200, 2080, 8
allocate: 2080, 8
deallocate: 0x122809200, 2080, 8
allocate: 2080, 8
deallocate: 0x122809200, 2080, 8
allocate: 2080, 8
deallocate: 0x122809200, 2080, 8
allocate: 2080, 8
deallocate: 0x122809200, 2080, 8
```
means we don't increase the next buffer size.
https://github.com/llvm/llvm-project/pull/116772
More information about the libcxx-commits
mailing list