-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
memory_rt.spice
121 lines (111 loc) · 3.76 KB
/
memory_rt.spice
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#![core.compiler.alwaysKeepOnNameCollision = true]
// Link external functions
ext f<heap byte*> malloc(unsigned long);
ext f<heap byte*> realloc(heap byte*, unsigned long);
ext p free(heap byte*);
ext p memcpy(heap byte*, heap byte*, unsigned long);
// Generic type defs
type T dyn;
/**
* Allocates a new block of memory of the given size.
*
* @param size The size of the block to allocate.
* @return A pointer to the allocated block, or an error if the allocation failed.
*/
public f<Result<heap byte*>> sAlloc(unsigned long size) {
heap byte* ptr = malloc(size);
return ptr != nil<heap byte*> ? ok(ptr) : err<heap byte*>("Out of memory occurred!");
}
/**
* Reallocates a block of memory to the given size.
*
* @param ptr The pointer to the block to reallocate.
* @param size The new size of the block.
* @return A pointer to the reallocated block, or an error if the reallocation failed.
*/
public f<Result<heap byte*>> sRealloc(heap byte* ptr, unsigned long size) {
heap byte* newPtr = realloc(ptr, size);
return newPtr != nil<heap byte*> ? ok(newPtr) : err<heap byte*>("Out of memory occurred!");
}
/**
* Copies a block of memory to a new block of memory.
*
* @param oldPtr The pointer to the block to copy.
* @param newPtr The pointer to the new block to copy to.
* @param size The size of the block to copy.
* @return A pointer to the copied block, or an error if the copy failed.
*/
public f<Result<heap byte*>> sCopy(heap byte* oldPtr, heap byte* newPtr, unsigned long size) {
if oldPtr == nil<heap byte*> | newPtr == nil<heap byte*> {
return err<heap byte*>("Cannot copy from or to nil pointer!");
}
memcpy(newPtr, oldPtr, size);
return ok(newPtr);
}
/**
* Frees a block of memory.
* The pointer is zeroed out after freeing the memory to prevent accidental double frees.
*
* @param ptr The pointer to the block to free.
*/
public p sDealloc(heap byte*& ptr) {
if ptr == nil<heap byte*> { return; }
free(ptr);
ptr = nil<heap byte*>; // Zero out to prevent accidental double frees
}
/**
* Allocates memory for a new instance of the given type on the heap.
* Note: This function panics if the allocation fails.
*
* @return A pointer to the heap-allocated instance
*/
public f<heap T*> sNew<T>() {
unsafe {
// Allocate memory for the instance
Result<heap byte*> res = sAlloc(sizeof(type T));
return (heap T*) res.unwrap();
}
}
/**
* Allocates memory for a new instance of the given type on the heap.
* Note: This function panics if the allocation fails.
*
* @param val The value to put into the heap-allocated memory
* @return A pointer to the heap-allocated instance
*/
public f<heap T*> sNew<T>(const T& val) {
unsafe {
// Allocate memory for the instance
Result<heap byte*> res = sAlloc(sizeof(type T));
result = (heap T*) res.unwrap();
// Copy the value into the heap-allocated memory
sCopy((heap byte*) &val, (heap byte*) result, sizeof(type T));
}
}
/**
* Allocates memory for a new instance of the given type on the heap.
*
* @param ptr The pointer to the heap-allocated memory
* @param val The value to put into the heap-allocated memory
* @return A pointer to the heap-allocated instance
*/
public f<heap T*> sPlacementNew<T>(heap byte* ptr, const T& val) {
unsafe {
// Copy the value into the heap-allocated memory
sCopy((heap byte*) &val, (heap byte*) ptr, sizeof(type T));
}
return ptr;
}
/**
* Destroys the given heap-allocated instance and frees the memory.
*
* @param ptr The pointer to the heap-allocated instance
*/
public p sDelete<T>(heap T*& ptr) {
unsafe {
// Destruct the object
ptr.dtor();
// Deallocate the memory
sDealloc((heap byte*&) ptr);
}
}