1- from typing import Callable , Optional , TypeVar , Any
1+ from typing import Any , Callable , Optional , TypeVar
22
33from _pointers import run_stack_callback
44
1313
1414
1515class StackAllocatedPointer (IterDereferencable [T ], BaseAllocatedPointer [T ]):
16- """Pointer to allocated memory ."""
16+ """Pointer to memory allocated on the stack ."""
1717
1818 def __init__ (
1919 self ,
@@ -36,6 +36,10 @@ def __init__(
3636 def freed (self ) -> bool :
3737 return self ._freed
3838
39+ @freed .setter
40+ def freed (self , value : bool ) -> None :
41+ self ._freed = value
42+
3943 @property
4044 def address (self ) -> Optional [int ]:
4145 return self ._address
@@ -45,9 +49,7 @@ def address(self, value: int) -> None:
4549 self ._address = value
4650
4751 def __repr__ (self ) -> str :
48- return (
49- f"StackAllocatedPointer(address={ self .address } , size={ self .size } )" # noqa
50- )
52+ return f"StackAllocatedPointer(address={ self .address } , size={ self .size } )" # noqa
5153
5254 def __add__ (self , amount : int ):
5355 return StackAllocatedPointer (
@@ -70,11 +72,21 @@ def free(self) -> None:
7072
7173
7274@handle
73- def stack_alloc (size : int ):
74- """Get a callback with a pointer to stack allocated memory."""
75+ def stack_alloc (
76+ size : int ,
77+ ) -> Callable [
78+ [Callable [[StackAllocatedPointer [Any ]], T ]], Callable [[], T ]
79+ ]: # noqa
80+ """Get a callback with a pointer to stack allocated memory.
81+ This function **is not** run automatically.
82+ For that purpose, use `acquire_stack_alloc`.
83+
84+ Args:
85+ size: Size of the allocation
86+ """
7587
7688 def decorator (
77- func : Callable [[StackAllocatedPointer ], T ],
89+ func : Callable [[StackAllocatedPointer [ Any ] ], T ],
7890 ) -> Callable [[], T ]:
7991 def wrapper ():
8092 return run_stack_callback (size , StackAllocatedPointer , func )
@@ -84,8 +96,14 @@ def wrapper():
8496 return decorator
8597
8698
87- def acquire_stack_alloc (size : int ):
88- """Execute a callback with a pointer to stack allocated memory."""
99+ def acquire_stack_alloc (
100+ size : int ,
101+ ) -> Callable [[Callable [[StackAllocatedPointer [Any ]], T ]], T ]:
102+ """Execute a callback with a pointer to stack allocated memory.
103+
104+ Args:
105+ size: Size of the allocation
106+ """
89107
90108 def decorator (func : Callable [[StackAllocatedPointer [Any ]], T ]) -> T :
91109 def wrapper ():
0 commit comments