Fix warnings when building with clang.
1 /* vim: set ts=4 sw=4 tw=99 et:
3 * Copyright (C) 2012 David Anderson
5 * This file is part of JITCraft.
7 * JITCraft is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License as published by the Free
9 * Software Foundation, either version 3 of the License, or (at your option)
12 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with
17 * JITCraft. If not, see http://www.gnu.org/licenses/.
29 ToProtBits(OS::Protection prot)
32 case OS::Prot_ReadWrite:
33 return PROT_READ | PROT_WRITE;
34 case OS::Prot_ReadWriteExecute:
35 return PROT_READ | PROT_WRITE | PROT_EXEC;
37 assert(prot == OS::Prot_ReadExecute);
38 return PROT_READ | PROT_EXEC;
43 OS::MapMemory(size_t amount, MapMode mode, Protection prot_)
45 int prot = ToProtBits(prot_);
46 void *addr = mmap(NULL, amount, prot, MAP_ANON | MAP_PRIVATE, -1, 0);
47 if (addr == MAP_FAILED)
54 OS::CommitMemory(Address address, size_t amount, Protection prot_)
60 OS::UnmapMemory(Address address, size_t size)
62 munmap(address, size);
66 OS::MapAlignedMemory(size_t amount, size_t alignment, MapMode mode)
68 assert(amount >= alignment);
69 assert(IsPowerOfTwo(alignment));
71 if (alignment == kPageAlignment) {
72 Address address = MapMemory(amount, mode, Prot_ReadWrite);
74 return AlignableMapping();
75 return AlignableMapping(address, alignment, amount);
78 size_t requested = Min(amount * 2, amount + 2 * alignment);
79 Address address = MapMemory(requested, mode, Prot_ReadWrite);
81 return AlignableMapping();
84 Address aligned = AlignedBase(address, alignment);
85 if (!IsAligned(address, alignment)) {
86 // Shave off the first part of the unaligned region.
88 begin = size_t(aligned - address);
89 UnmapAlignedMemory(address, begin);
92 if (requested - begin > amount) {
93 // Shave off the last part of the unaligned region.
94 UnmapAlignedMemory(aligned + amount, requested - amount - begin);
97 return AlignableMapping(aligned, alignment, amount);
101 OS::UnmapAlignedMemory(Address address, size_t size)
103 OS::UnmapMemory(address, size);
107 OS::GetAbsolutePath(const char *path, AutoFree<char> &chars)
109 chars = (char *)calloc(PATH_MAX, sizeof(char));
113 char *ptr = realpath(path, *chars);
117 return ptr == *chars;
121 OS::SplitPath(const char *path, AutoFree<char> &base, AutoFree<char> &name)
123 // dirname() and basename() pretty much suck.
124 size_t len = strlen(path);
126 // Skip trailing slashes.
127 while (len > 1 && (path[len - 1] == '/'))
130 for (size_t i = len - 1; i < len; i--) {
131 if (path[i] == '/') {
132 name = (char *)calloc(len - i, sizeof(char));
136 strcpy(*name, &path[i + 1]);
138 base = (char *)calloc(i + 1, sizeof(char));
142 memcpy(*base, path, i);
148 // The path should have been absolute, which contains at least one separator.
159 OS::InspectPath(const char *path)
162 int result = stat(path, &s);
164 return OS::Path_Invalid;
166 if (s.st_mode & S_IFDIR)
167 return OS::Path_Folder;
168 if (s.st_mode & S_IFREG)
169 return OS::Path_File;
171 return OS::Path_Invalid;