<commit_before>// This file is part of libfringe, a low-level green threading library.
// Copyright (c) 2015, edef <<EMAIL>>
// See the LICENSE file included in this distribution.
use core::mem::{size_of, align_of};
use core::cmp::max;
use core::ptr;

use void::{self, Void};

use super::imp::STACK_ALIGN;

pub unsafe extern "C" fn rust_trampoline<F>(f: *const F) -> !
  where F: FnOnce() -> Void {
  void::unreachable(ptr::read(f)())
}

pub unsafe fn push<T>(spp: &mut *mut usize, value: T) -> *mut T {
  let mut sp = *spp as *mut T;
  sp = offset_mut(sp, -1);
  sp = align_down_mut(sp, max(align_of::<T>(), STACK_ALIGN));
  *sp = value;
  *spp = sp as *mut usize;
  sp
}

pub fn align_down_mut<T>(sp: *mut T, n: usize) -> *mut T {
  let sp = (sp as usize) & !(n - 1);
  sp as *mut T
}

// ptr::offset_mut is positive ints only
pub fn offset_mut<T>(ptr: *mut T, count: isize) -> *mut T {
  (ptr as isize + count * (size_of::<T>() as isize)) as *mut T
}<commit_msg>don't drop uninitialised memory when pushing to stack<commit_after>// This file is part of libfringe, a low-level green threading library.
// Copyright (c) 2015, edef <<EMAIL>>
// See the LICENSE file included in this distribution.
use core::mem::{size_of, align_of};
use core::cmp::max;
use core::ptr;

use void::{self, Void};

use super::imp::STACK_ALIGN;

pub unsafe extern "C" fn rust_trampoline<F>(f: *const F) -> !
  where F: FnOnce() -> Void {
  void::unreachable(ptr::read(f)())
}

pub unsafe fn push<T>(spp: &mut *mut usize, value: T) -> *mut T {
  let mut sp = *spp as *mut T;
  sp = offset_mut(sp, -1);
  sp = align_down_mut(sp, max(align_of::<T>(), STACK_ALIGN));
  ptr::write(sp, value); // does not attempt to drop old value
  *spp = sp as *mut usize;
  sp
}

pub fn align_down_mut<T>(sp: *mut T, n: usize) -> *mut T {
  let sp = (sp as usize) & !(n - 1);
  sp as *mut T
}

// ptr::offset_mut is positive ints only
pub fn offset_mut<T>(ptr: *mut T, count: isize) -> *mut T {
  (ptr as isize + count * (size_of::<T>() as isize)) as *mut T
}
{'fraction_non_alphanumeric': 0.17443012884043607, 'fraction_numerical': 0.005946481665014866, 'mean_word_length': 3.8650602409638553, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7615648', 'n_tokens_mistral': 789, 'n_tokens_neox': 792, 'n_words': 268}