Skip to content

Commit 694a40d

Browse files
committed
Implement memory space lifecycle management
Add functions to create and destroy memory spaces, which serve as containers for flexpages. A memory space can be dedicated to a single task or shared across multiple tasks, supporting both isolated and shared memory models.
1 parent 2a7a554 commit 694a40d

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

include/sys/memprot.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,17 @@ fpage_t *mo_fpage_create(uint32_t base,
9191
* @fpage : Pointer to flexpage to destroy
9292
*/
9393
void mo_fpage_destroy(fpage_t *fpage);
94+
95+
/* Memory Space Management Functions */
96+
97+
/* Creates and initializes a memory space.
98+
* @as_id : Memory space identifier
99+
* @shared : Whether this space can be shared across tasks
100+
* Returns pointer to created memory space, or NULL on failure.
101+
*/
102+
memspace_t *mo_memspace_create(uint32_t as_id, uint32_t shared);
103+
104+
/* Destroys a memory space and all its flexpages.
105+
* @mspace : Pointer to memory space to destroy
106+
*/
107+
void mo_memspace_destroy(memspace_t *mspace);

kernel/memprot.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,44 @@ int32_t pmp_evict_fpage(fpage_t *fpage)
127127

128128
return ret;
129129
}
130+
131+
/* Creates and initializes a memory space.
132+
*
133+
* @as_id : Memory space identifier
134+
* @shared : Whether this space can be shared across tasks
135+
* Returns pointer to created memory space, or NULL on failure.
136+
*/
137+
memspace_t *mo_memspace_create(uint32_t as_id, uint32_t shared)
138+
{
139+
memspace_t *mspace = malloc(sizeof(memspace_t));
140+
if (!mspace)
141+
return NULL;
142+
143+
mspace->as_id = as_id;
144+
mspace->first = NULL;
145+
mspace->pmp_first = NULL;
146+
mspace->pmp_stack = NULL;
147+
mspace->shared = shared;
148+
149+
return mspace;
150+
}
151+
152+
/* Destroys a memory space and all its flexpages.
153+
*
154+
* @mspace : Pointer to memory space to destroy
155+
*/
156+
void mo_memspace_destroy(memspace_t *mspace)
157+
{
158+
if (!mspace)
159+
return;
160+
161+
/* Free all flexpages in the list */
162+
fpage_t *fp = mspace->first;
163+
while (fp) {
164+
fpage_t *next = fp->as_next;
165+
mo_fpage_destroy(fp);
166+
fp = next;
167+
}
168+
169+
free(mspace);
170+
}

0 commit comments

Comments
 (0)