C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Fwd: set_new_handler extension

From: Phil Bouchard <boost_at_[hidden]>
Date: Mon, 20 Feb 2023 18:33:46 -0500
On 2/20/23 17:16, Phil Bouchard via Std-Proposals wrote:
> Yeah or you could postpone the deallocations when the CPU idles, just
> like a garbage collector.
>
> Whatever the reason I think it's imperative to override application-wide
> default allocators.

Mix that with thread-local, allocation rate-based and type-based and
you'll have a blinking fast application.


> On 2/20/23 17:09, Frederick Virchanza Gotham via Std-Proposals wrote:
>> On Mon, Feb 20, 2023 at 7:51 PM Julien Villemure-Fréchette via
>> Std-Proposals <std-proposals_at_[hidden]> wrote:
>>>
>>> global operator new and delete can be replaced by a non inline, non
>>> static global operator new with equivalent signature
>>
>>
>> I wrote a program one time and I wanted it to be lightning fast, and
>> so I overloaded 'new' so that it allocated massive chunks at a time,
>> and 'delete' didn't do anything. I of course was wasting memory but it
>> was fine as I had enough RAM. Here's what I did:
>>
>> #include <cstddef> // size_t
>> #include <cstdlib> // malloc
>> #include <new> // required otherwise we get a compiler error
>> for the 'noexcept'
>>
>> using std::size_t;
>>
>> size_t g_total_allocation = 0u;
>>
>> inline void *Implementation_Global_New(size_t size) noexcept
>> {
>> size += 8u - (size % 8u);
>>
>> static size_t constexpr bytes_at_a_time = 10485760u; /* We
>> allocate 10 megabytes at a time */
>>
>> if ( size > bytes_at_a_time ) return nullptr;
>>
>> static void *p = nullptr;
>>
>> static size_t bytes_allocated_so_far = 0u;
>>
>> for (; /* ever */ ;)
>> {
>> if ( nullptr == p )
>> {
>> p = std::malloc(bytes_at_a_time);
>>
>> if ( nullptr == p ) return nullptr;
>>
>> g_total_allocation += bytes_at_a_time;
>> }
>>
>> if ( (bytes_allocated_so_far + size) > bytes_at_a_time )
>> {
>> p = nullptr;
>>
>> bytes_allocated_so_far = 0u;
>> }
>> else
>> {
>> break;
>> }
>> }
>>
>> void *const retval = static_cast<char*>(p) + bytes_allocated_so_far;
>>
>> bytes_allocated_so_far += size;
>>
>> return retval;
>> }
>>
>> void *operator new (size_t const size) noexcept { return
>> Implementation_Global_New(size); }
>> void *operator new[](size_t const size) noexcept { return
>> Implementation_Global_New(size); }
>> void operator delete (void *const p) noexcept { /* Do Nothing */ }
>> void operator delete[](void *const p) noexcept { /* Do Nothing */ }
>

-- 
Logo <https://www.fornux.com/>  
*Phil Bouchard*  facebook icon
<https://www.linkedin.com/in/phil-bouchard-5723a910/> 
CTO
T: (819) 328-4743
E: phil_at_[hidden]| www.fornux.com <http://www.fornux.com>
8 rue de la Baie| Gatineau (Qc), J8T 3H3 Canada
Banner <https://goglobalawards.org/> Le message ci-dessus, ainsi que les
documents l'accompagnant, sont destinés uniquement aux personnes
identifiées et peuvent contenir des informations privilégiées,
confidentielles ou ne pouvant être divulguées. Si vous avez reçu ce
message par erreur, veuillez le détruire.
This communication (and/or the attachments) is intended for named
recipients only and may contain privileged or confidential information
which is not to be disclosed. If you received this communication by
mistake please destroy all copies.

Received on 2023-02-20 23:33:48