Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
StackBuffer.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Memory_StackBuffer_h_
5#define _Stroika_Foundation_Memory_StackBuffer_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
10
11/**
12 * \file
13 *
14 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
15 */
16
17namespace Stroika::Foundation::Memory {
18
19 /**
20 * On Windows, there is _chkstk which shows up in alot of profiles. Perhaps something similar for UNIX? Or just kernel does this automatically?
21 * Anyhow - target number we try - for performance reasons - to avoid more than this much in a stack frame.
22 *
23 * https://www.codeguru.com/visual-studio/adventures-with-_chkstk/
24 */
26 qStroika_Foundation_Common_Platform_Windows ? (sizeof (int) == 4 ? 4 : 8) * 1024 : 16 * 1024;
27
28 /**
29 * \note good to keep this small (around 2k) for Windows, cuz else _chkstack calls end up litering profiles in alot of functions
30 * even if along paths not actually used. COULD optimize those paths with specific value in usages, but seems reasonable to keep
31 * to 2k for now --LGP 2023-09-12
32 */
33 constexpr size_t kStackBuffer_TargetInlineByteBufferSize = qStroika_Foundation_Common_Platform_Windows ? 2 * 1024 : 4 * 1024;
34
35 /**
36 */
37 template <typename T = byte>
38 constexpr size_t StackBuffer_DefaultInlineSize ()
39 {
40 // note must be defined here, not in inl file, due to use as default template argument
41 auto r = ((kStackBuffer_TargetInlineByteBufferSize / sizeof (T)) == 0 ? 1 : (kStackBuffer_TargetInlineByteBufferSize / sizeof (T)));
42 Ensure (r >= 1);
43 return r;
44 }
45
46 /**
47 * \brief Store variable sized (BUF_SIZE elements) array on the stack (\see also InlineBuffer<T,BUF_SIZE>), and on heap if it grows if needed
48 *
49 * Typically, StackBuffer<> combines the performance of using a stack buffer (inline array on stack) to store arrays with
50 * the safety and flexability of using the free store (malloc).
51 *
52 * \note we used to have separate InlineBuffer and StackBuffer, but they did exactly the same thing. The only difference was
53 * the IDEA that StackBuffer might someday be re-implemented using alloca. I dont think thats plausible any longer, but something akin
54 * to it might be possible, so maintain the API difference for now.
55 *
56 *
57 */
58 template <typename T = byte, size_t BUF_SIZE = StackBuffer_DefaultInlineSize<T> ()>
60
61}
62
63/*
64 ********************************************************************************
65 ***************************** Implementation Details ***************************
66 ********************************************************************************
67 */
68
69#endif /*_Stroika_Foundation_Memory_StackBuffer_h_*/
constexpr size_t kStackBuffer_SizeIfLargerStackGuardCalled
Definition StackBuffer.h:25
constexpr size_t kStackBuffer_TargetInlineByteBufferSize
Definition StackBuffer.h:33
Logically halfway between std::array and std::vector; Smart 'direct memory array' - which when needed...