Source

pypy-c4gc / azm_mem_test / utils.h

Full commit
/*
 * Copyright (c) 2012, Greg Bowyer. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * This file contains functions taken directly from the OpenJDK project with a
 * view as to porting out the Azul pauseless MRI interfaces such that they
 * can be examined as toy programs separate to the complexities of both C4 and
 * hotspot
 *
 */

#ifndef __UTILS_H
#define __UTILS_H

// I suspect that stdint.h is c99, which raises questions about the
// linking of the code for azmm, technically if I can link it under
// g++ I *should* be good as that is both a c0x compiler (which legally
// supersets c99) as well as a compiler that breaks the rules.

#include <stdint.h>
#include <stdio.h> // For printf
#include <stdlib.h> // For abort

// globalDefinitions.{cpp,hpp} nonsence stolen from openjdk {{{
extern "C" void breakpoint() {
  // use debugger to set breakpoint here
}

#define BREAKPOINT ::breakpoint() 
// }}}

#undef assert
#define assert(cond, msg) { if (!(cond)) { fprintf(stderr, "assert fails %s %d: %s\n", __FILE__, __LINE__, msg); abort(); }}
#define assertfalse(cond, msg) { if ((cond)) { fprintf(stderr, "assert fails %s %d: %s\n", __FILE__, __LINE__, msg); abort(); }}

// }}}

// intx and uintx are the 'extended' int and 'extended' unsigned int types;
// they are 32bit wide on a 32-bit platform, and 64bit wide on a 64bit platform.
typedef intptr_t  intx;
typedef uintptr_t uintx;

inline intptr_t round_to(intptr_t x, uintx s);

const intptr_t AllBits    = ~0; // all bits set in a word
const intptr_t NoBits     =  0; // no bits set in a word
const int64_t  NoLongBits =  0; // no bits set in a long
const intptr_t OneBit     =  1; // only right_most bit set in a word

// bit-operations using a mask m
inline void set_bits (intptr_t& x, intptr_t m) { x |= m; }
inline void clear_bits (intptr_t& x, intptr_t m) { x &= ~m; }
inline intptr_t mask_bits (intptr_t x, intptr_t m) { return x & m; }
inline int64_t mask_long_bits (int64_t x, int64_t m) { return x & m; }
inline bool mask_bits_are_true (intptr_t flags, intptr_t mask) { return (flags & mask) == mask; }

// true if x is a power of 2, false otherwise
inline bool is_power_of_2(intptr_t x) {
  return ((x != NoBits) && (mask_bits(x, x - 1) == NoBits));
}

// long version of is_power_of_2
inline bool is_power_of_2_long(int64_t x) {
  return ((x != NoLongBits) && (mask_long_bits(x, x - 1) == NoLongBits));
}

inline intptr_t round_to(intptr_t x, uintx s) {
    printf("Size of s : %d\n", s);
    //assert(!is_power_of_2(s), "s must be a power of 2");
    const uintx m = s - 1;
    return mask_bits(x + m, ~m);
}

#endif

/* vim: set et fenc=utf-8 ff=unix sts=4 sw=4 ts=4 foldmethod=marker foldlevel=0 : */