Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
node/lib/memory/managed.cpp at python-wrapper · VILLASframework/node · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
VILLASframework
/
node
Public
Notifications
You must be signed in to change notification settings
Fork
22
Star
21
Code
Issues
62
Pull requests
15
Discussions
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Security and quality
Insights
Files
Expand file tree
python-wrapper
Breadcrumbs
node
/
lib
/
memory
/
managed.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
176 lines (146 loc) · 5.16 KB
python-wrapper
Breadcrumbs
node
/
lib
/
memory
/
managed.cpp
Copy path
Top
File metadata and controls
Code
Blame
176 lines (146 loc) · 5.16 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
Managed memory allocator.
*
* Author: Steffen Vogel <post@steffenvogel.de>
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
* SPDX-License-Identifier: Apache-2.0
*/
#
include
<
cstdlib
>
#
include
<
strings.h
>
#
include
<
sys/mman.h
>
#
include
<
sys/resource.h
>
#
include
<
sys/time.h
>
#
include
<
sys/types.h
>
#
include
<
unistd.h
>
#
include
<
villas/exceptions.hpp
>
#
include
<
villas/log.hpp
>
#
include
<
villas/node/memory.hpp
>
#
include
<
villas/utils.hpp
>
using
namespace
villas
;
using
namespace
villas
::node
;
using
namespace
villas
::utils
;
using
namespace
villas
::node::memory
;
static
struct
Allocation
*
managed_alloc
(
size_t
len,
size_t
alignment,
struct
Type
*m) {
//
Simple first-fit allocation
struct
Block
*first = (
struct
Block
*)m->
_vd
;
struct
Block
*block;
for
(block = first; block !=
nullptr
; block = block->
next
) {
if
(block->
used
)
continue
;
char
*cptr =
reinterpret_cast
<
char
*>(block) +
sizeof
(
struct
Block
);
size_t
avail = block->
length
;
uintptr_t
uptr = (
uintptr_t
)cptr;
/*
Check alignment first; leave a gap at start of block to assure
* alignment if necessary
*/
uintptr_t
rem = uptr % alignment;
uintptr_t
gap =
0
;
if
(rem !=
0
) {
gap = alignment - rem;
if
(gap > avail)
continue
;
//
Next aligned address isn't in this block anymore
cptr += gap;
avail -= gap;
}
if
(avail >= len) {
if
(gap >
sizeof
(
struct
Block
)) {
/*
The alignment gap is big enough to fit another block.
* The original block descriptor is already at the correct
* position, so we just change its len and create a new block
* descriptor for the actual block we're handling.
*/
block->
length
= gap -
sizeof
(
struct
Block
);
struct
Block
*newblock =
reinterpret_cast
<
struct
Block
*>(cptr -
sizeof
(
struct
Block
));
newblock->
prev
= block;
newblock->
next
= block->
next
;
block->
next
= newblock;
newblock->
used
=
false
;
newblock->
length
= len;
block = newblock;
}
else
{
/*
The gap is too small to fit another block descriptor, so we
* must account for the gap length in the block length.
*/
block->
length
= len + gap;
}
if
(avail > len +
sizeof
(
struct
Block
)) {
//
Imperfect fit, so create another block for the remaining part
struct
Block
*newblock =
reinterpret_cast
<
struct
Block
*>(cptr + len);
newblock->
prev
= block;
newblock->
next
= block->
next
;
block->
next
= newblock;
if
(newblock->
next
)
newblock->
next
->
prev
= newblock;
newblock->
used
=
false
;
newblock->
length
= avail - len -
sizeof
(
struct
Block
);
}
else
{
/*
If this block was larger than the requested length, but only
* by less than sizeof(struct Block), we may have wasted
* memory by previous assignments to block->length.
*/
block->
length
= avail;
}
block->
used
=
true
;
auto
*ma =
new
struct
Allocation
;
if
(!ma)
throw
MemoryAllocationError
();
ma->
address
= cptr;
ma->
type
= m;
ma->
alignment
= alignment;
ma->
length
= len;
ma->
managed
.
block
= block;
return
ma;
}
}
//
No suitable block found
return
nullptr
;
}
static
int
managed_free
(
struct
Allocation
*ma,
struct
Type
*m) {
struct
Block
*block = ma->
managed
.
block
;
//
Try to merge it with neighbouring free blocks
if
(block->
prev
&& !block->
prev
->
used
&& block->
next
&& !block->
next
->
used
) {
//
Special case first: both previous and next block are unused
block->
prev
->
length
+=
block->
length
+ block->
next
->
length
+
2
*
sizeof
(
struct
Block
);
block->
prev
->
next
= block->
next
->
next
;
if
(block->
next
->
next
)
block->
next
->
next
->
prev
= block->
prev
;
}
else
if
(block->
prev
&& !block->
prev
->
used
) {
block->
prev
->
length
+= block->
length
+
sizeof
(
struct
Block
);
block->
prev
->
next
= block->
next
;
if
(block->
next
)
block->
next
->
prev
= block->
prev
;
}
else
if
(block->
next
&& !block->
next
->
used
) {
block->
length
+= block->
next
->
length
+
sizeof
(
struct
Block
);
block->
next
= block->
next
->
next
;
if
(block->
next
)
block->
next
->
prev
= block;
}
else
{
//
no neighbouring free block, so just mark it as free
block->
used
=
false
;
}
return
0
;
}
struct
Type
*
villas::node::memory::managed
(
void
*ptr,
size_t
len) {
struct
Type
*mt = (
struct
Type
*)ptr;
struct
Block
*mb;
char
*cptr = (
char
*)ptr;
if
(len <
sizeof
(
struct
Type
) +
sizeof
(
struct
Block
)) {
auto
logger =
Log::get
(
"
memory:managed
"
);
logger->
info
(
"
Passed region is too small
"
);
return
nullptr
;
}
//
Initialize type
mt->
name
=
"
managed
"
;
mt->
flags
=
0
;
mt->
alloc
= managed_alloc;
mt->
free
= managed_free;
mt->
alignment
=
1
;
cptr +=
ALIGN
(
sizeof
(
struct
Type
),
sizeof
(
void
*));
//
Initialize first free memory block
mb =
reinterpret_cast
<
struct
Block
*>(cptr);
mb->
prev
=
nullptr
;
mb->
next
=
nullptr
;
mb->
used
=
false
;
cptr +=
ALIGN
(
sizeof
(
struct
Block
),
sizeof
(
void
*));
mb->
length
= len - (cptr - (
char
*)ptr);
mt->
_vd
= (
void
*)mb;
return
mt;
}
You can’t perform that action at this time.