Refactor compilation process around the new concept of packages.
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/.
28 ToProtBits(OS::Protection prot)
31 case OS::Prot_ReadWrite:
32 return PROT_READ | PROT_WRITE;
33 case OS::Prot_ReadWriteExecute:
34 return PROT_READ | PROT_WRITE | PROT_EXEC;
36 assert(prot == OS::Prot_ReadExecute);
37 return PROT_READ | PROT_EXEC;
42 OS::MapMemory(size_t amount, MapMode mode, Protection prot_)
44 int prot = ToProtBits(prot_);
45 void *addr = mmap(NULL, amount, prot, MAP_ANON | MAP_PRIVATE, -1, 0);
46 if (addr == MAP_FAILED)
53 OS::CommitMemory(Address address, size_t amount, Protection prot_)
59 OS::UnmapMemory(Address address, size_t size)
61 munmap(address, size);
65 OS::MapAlignedMemory(size_t amount, size_t alignment, MapMode mode)
67 assert(amount >= alignment);
68 assert(IsPowerOfTwo(alignment));
70 if (alignment == kPageAlignment) {
71 Address address = MapMemory(amount, mode, Prot_ReadWrite);
73 return AlignableMapping();
74 return AlignableMapping(address, alignment, amount);
77 size_t requested = Min(amount * 2, amount + 2 * alignment);
78 Address address = MapMemory(requested, mode, Prot_ReadWrite);
80 return AlignableMapping();
83 Address aligned = AlignedBase(address, alignment);
84 if (!IsAligned(address, alignment)) {
85 // Shave off the first part of the unaligned region.
87 begin = size_t(aligned - address);
88 UnmapAlignedMemory(address, begin);
91 if (requested - begin > amount) {
92 // Shave off the last part of the unaligned region.
93 UnmapAlignedMemory(aligned + amount, requested - amount - begin);
96 return AlignableMapping(aligned, alignment, amount);
100 OS::UnmapAlignedMemory(Address address, size_t size)
102 OS::UnmapMemory(address, size);
106 OS::GetAbsolutePath(const char *path, AutoFree<char> &chars)
108 chars = (char *)calloc(PATH_MAX, sizeof(char));
112 char *ptr = realpath(path, *chars);
116 return ptr == *chars;
120 OS::SplitPath(const char *path, AutoFree<char> &base, AutoFree<char> &name)
122 // dirname() and basename() pretty much suck.
123 size_t len = strlen(path);
125 // Skip trailing slashes.
126 while (len > 1 && (path[len - 1] == '/'))
129 for (size_t i = len - 1; i < len; i--) {
130 if (path[i] == '/') {
131 name = (char *)calloc(len - i, sizeof(char));
135 strcpy(*name, &path[i + 1]);
137 base = (char *)calloc(i + 1, sizeof(char));
141 memcpy(*base, path, i);
147 // The path should have been absolute, which contains at least one separator.