[CRIU] [PATCH 03/28] rst: helpers for shared memory allocation
introduced
Kinsbursky Stanislav
skinsbursky at openvz.org
Thu Mar 22 13:57:50 EDT 2012
From: Stanislav Kinsbursky <skinsbursky at openvz.org>
Memory pool is 1 MB for now.
Signed-off-by: Stanislav Kinsbursky <skinsbursky at openvz.org>
---
cr-restore.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++---
files.c | 12 ++++++------
include/util.h | 3 +++
3 files changed, 61 insertions(+), 9 deletions(-)
-------------- next part --------------
diff --git a/cr-restore.c b/cr-restore.c
index 6ede2b7..e95b0f6 100644
--- a/cr-restore.c
+++ b/cr-restore.c
@@ -374,12 +374,61 @@ static inline void pstree_skip(int fd, struct pstree_entry *e)
lseek(fd, e->nr_children * sizeof(u32) + e->nr_threads * sizeof(u32), SEEK_CUR);
}
+char *shm_pool_start, *shm_pool_end, *shm_pool_cur, *shm_pool_prev;
+
+void *xrealloc_shm(void *addr, int bytes)
+{
+ int curr_size;
+ int new_size = round_up(bytes, sizeof(long));
+
+ if (addr != shm_pool_prev)
+ return NULL;
+ curr_size = shm_pool_cur - shm_pool_prev;
+ if (new_size < curr_size)
+ shm_pool_cur = shm_pool_prev + new_size;
+ else {
+ int delta = new_size - curr_size;
+
+ if (shm_pool_cur + delta > shm_pool_end)
+ return NULL;
+ shm_pool_cur += delta;
+ }
+ return shm_pool_prev;
+}
+
+void *xmalloc_shm(int bytes)
+{
+ int size = round_up(bytes, sizeof(long));
+
+ if (shm_pool_cur + size > shm_pool_end)
+ return NULL;
+ shm_pool_prev = shm_pool_cur;
+ shm_pool_cur += size;
+ return shm_pool_prev;
+}
+
+static int init_shm_pool(int size)
+{
+ shm_pool_start = mmap(NULL, size, PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_ANON, 0, 0);
+ if (shm_pool_start == MAP_FAILED) {
+ pr_perror("Can't init shared memory pool");
+ return -1;
+ }
+ shm_pool_cur = shm_pool_prev = shm_pool_start;
+ shm_pool_end = shm_pool_start + size;
+ return 0;
+}
+
static int prepare_shared(int ps_fd)
{
int ret = 0;
pr_info("Preparing info about shared resources\n");
+ if (init_shm_pool(1 << 20))
+ return -1;
+
task_pids = xmalloc(PAGE_SIZE);
if (task_pids == NULL)
return -1;
@@ -401,9 +450,9 @@ static int prepare_shared(int ps_fd)
task_entries->nr = 0;
task_entries->start = CR_STATE_RESTORE;
- pipes = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, 0, 0);
- if (pipes == MAP_FAILED) {
- pr_perror("Can't map pipes");
+ pipes = xmalloc_shm(4096);
+ if (pipes == NULL) {
+ pr_perror("Can't alloc pipes");
return -1;
}
diff --git a/files.c b/files.c
index e4a85e3..e9b7496 100644
--- a/files.c
+++ b/files.c
@@ -30,15 +30,15 @@ static struct fmap_fd *fmap_fds;
int prepare_shared_fdinfo(void)
{
- fdinfo_descs = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, 0, 0);
- if (fdinfo_descs == MAP_FAILED) {
- pr_perror("Can't map fdinfo_descs");
+ fdinfo_descs = xmalloc_shm(4096);
+ if (fdinfo_descs == NULL) {
+ pr_perror("Can't alloc fdinfo_descs");
return -1;
}
- fdinfo_list = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, 0, 0);
- if (fdinfo_list == MAP_FAILED) {
- pr_perror("Can't map fdinfo_list");
+ fdinfo_list = xmalloc_shm(4096);
+ if (fdinfo_list == NULL) {
+ pr_perror("Can't alloc fdinfo_list");
return -1;
}
return 0;
diff --git a/include/util.h b/include/util.h
index 9899cca..f9c2403 100644
--- a/include/util.h
+++ b/include/util.h
@@ -242,6 +242,9 @@ int do_open_proc(pid_t pid, int flags, const char *fmt, ...);
#define xzalloc(size) __xalloc(calloc, size, 1, size)
#define xrealloc(p, size) __xalloc(realloc, size, p, size)
+extern void *xmalloc_shm(int size);
+extern void *xrealloc_shm(void *addr, int new_size);
+
#define xfree(p) if (p) free(p)
#define xrealloc_safe(pptr, size) \
More information about the CRIU
mailing list