Add methods for converting between Collections (asBag, asSet, etc)
[panda.git] / src / st-system.c
blobc23ca77d8ec1022f21aad2f81aa24a59bf789421
3 #include "st-system.h"
4 #include "st-utils.h"
6 #include <unistd.h>
7 #include <sys/mman.h>
8 #include <errno.h>
9 #include <stdio.h>
10 #include <string.h>
12 static st_pointer
13 st_mmap_anon (st_pointer address, st_uint length,
14 int protect, int flags)
16 /* Wrapper for mmap(), with the default flags as MAP_PRIVATE | MAP_ANONYMOUS.
17 * Returns NULL on failure.
19 st_pointer result;
21 result = mmap (address, length, protect,
22 flags | MAP_PRIVATE | MAP_ANONYMOUS,
23 0, 0);
25 if (result == (st_pointer) -1) {
26 fprintf (stderr, "panda: error: %s\n", strerror (errno));
27 return NULL;
30 return result;
33 st_pointer
34 st_system_reserve_memory (st_pointer addr, st_uint size)
36 /* Reserves a virtual memory region without actually allocating any
37 * storage in physical memory or swap space.
39 int flags = 0;
41 flags = MAP_NORESERVE;
42 if (addr)
43 flags |= MAP_FIXED;
45 return st_mmap_anon (addr, size, PROT_NONE, flags);
48 st_pointer
49 st_system_commit_memory (st_pointer addr, st_uint size)
51 /* Allocates storage in physical memory or swap space.
53 int flags = 0;
55 if (munmap (addr, size) < 0) {
56 fprintf (stderr, "panda: error: %s\n", strerror (errno));
57 return false;
60 if (addr)
61 flags |= MAP_FIXED;
63 return st_mmap_anon (addr, size, PROT_READ | PROT_WRITE, flags);
66 st_pointer
67 st_system_decommit_memory (st_pointer addr, st_uint size)
69 /* Deallocates any storage but ensures that the given region is still reserved
71 int flags = 0;
73 st_assert (addr != NULL);
74 if (munmap (addr, size) < 0) {
75 fprintf (stderr, "panda: error: %s\n", strerror (errno));
76 return false;
79 flags = MAP_NORESERVE;
80 if (addr)
81 flags |= MAP_FIXED;
83 return st_mmap_anon (addr, size, PROT_NONE, flags);
86 void
87 st_system_release_memory (st_pointer addr, st_uint size)
89 /* destroys any virtual memory mappings within the given region
91 st_assert (addr != NULL);
92 if (munmap (addr, size) < 0) {
93 fprintf (stderr, "panda: error: %s\n", strerror (errno));
94 st_assert_not_reached ();
98 st_uint
99 st_system_pagesize (void)
101 return getpagesize ();