1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/Platform.h Tue Jul 24 23:53:34 2012 -0700
1.3 @@ -0,0 +1,91 @@
1.4 +/* vim: set ts=4 sw=4 tw=99 et:
1.5 + *
1.6 + * Copyright (C) 2012 David Anderson
1.7 + *
1.8 + * This file is part of JITCraft.
1.9 + *
1.10 + * JITCraft is free software: you can redistribute it and/or modify it under
1.11 + * the terms of the GNU General Public License as published by the Free
1.12 + * Software Foundation, either version 3 of the License, or (at your option)
1.13 + * any later version.
1.14 + *
1.15 + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
1.16 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License along with
1.20 + * JITCraft. If not, see http://www.gnu.org/licenses/.
1.21 + */
1.22 +#ifndef _include_jitcraft_platform_h_
1.23 +#define _include_jitcraft_platform_h_
1.24 +
1.25 +#include "Utility.h"
1.26 +
1.27 +namespace ke {
1.28 +
1.29 +namespace OS {
1.30 + enum Protection {
1.31 + Prot_ReadWrite, // rw-, for the heap
1.32 + Prot_ReadExecute, // r-x, for code
1.33 + Prot_ReadWriteExecute // rwx, if disabling r-x for code.
1.34 + };
1.35 +
1.36 + enum MapMode {
1.37 + Map_Reserve, // don't ask for actual pages yet
1.38 + Map_Commit // ask for pages right away
1.39 + };
1.40 +
1.41 + Address MapMemory(size_t amount, MapMode mode, Protection prot);
1.42 + bool CommitMemory(Address address, size_t amount, Protection prot);
1.43 + void UnmapMemory(Address address, size_t size);
1.44 +
1.45 + class AlignableMapping {
1.46 + Address base_;
1.47 + Address aligned_;
1.48 + size_t size_;
1.49 +
1.50 + public:
1.51 + AlignableMapping(Address ptr, size_t alignment, size_t size)
1.52 + : base_(ptr),
1.53 + size_(size)
1.54 + {
1.55 + aligned_ = base_;
1.56 + if (!IsAligned(aligned_, alignment))
1.57 + aligned_ = AlignedBase(aligned_, alignment) + alignment;
1.58 + }
1.59 +
1.60 + AlignableMapping() : base_(NULL), aligned_(NULL), size_(0)
1.61 + {
1.62 + }
1.63 +
1.64 + Address base() const {
1.65 + return base_;
1.66 + }
1.67 + bool isValid() const {
1.68 + return !!base_;
1.69 + }
1.70 + size_t size() const {
1.71 + return size_;
1.72 + }
1.73 + Address aligned() const {
1.74 + return aligned_;
1.75 + }
1.76 + };
1.77 +
1.78 + // MapAlignedMemory is guaranteed to return zeroed memory.
1.79 + AlignableMapping MapAlignedMemory(size_t amount, size_t alignment, MapMode mode);
1.80 + void UnmapAlignedMemory(Address address, size_t size);
1.81 +
1.82 + // The default alignment of memory returned via MapMemory().
1.83 + // :TODO: better ifdefs
1.84 +#ifdef _MSC_VER
1.85 + static const size_t kPageAlignment = 64 * kKB;
1.86 +#else
1.87 + static const size_t kPageAlignment = 4 * kKB;
1.88 +#endif
1.89 +
1.90 +}
1.91 +
1.92 +}
1.93 +
1.94 +#endif // _include_jitcraft_platform_h_