Skip to content

Commit 598821c

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 9447bc9 commit 598821c

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, uint32_t size, uint32_t rwx,
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
@@ -125,3 +125,44 @@ int32_t pmp_evict_fpage(fpage_t *fpage)
125125

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

0 commit comments

Comments
 (0)