<commit_before>#include <metodo/metodo.h>

unsigned int placement = 0;

/* malloc(size, flags)
 * Simple placement based allocator. This allocates off the end of the kernel.
 * Potential bugs: This has no bound, and does not check with the final heap allocator
 */
void *kmalloc_int(unsigned int size, unsigned int flags)
{
	// FIXME: It seems "tmp" (and not "placement") may be what was meant to be returned, but that results in a triple fault. What gives?
	unsigned int tmp;

	// Initialize if needed.
	if (placement == 0)
		placement = (unsigned int)end;

	// Align on a page if needed.
/* The following line was this, but meteger said to invert the bitmask in the check:
 *	if ((flags & MALLOC_ALIGN) && (placement & 0xfffff000)) {
*/
	if ((flags & MALLOC_ALIGN) && ((placement) & 0x0fff)) {
		placement &= 0xfffff000;
		placement += 0x1000;
	}

	// Return the address of allocation and increment the next placement address to whatever.
	tmp = placement;
	placement += size;
	//return (void*) placement;
	return (void*) tmp;
}

typedef long Align;

union header {
	struct {
		union header *ptr;
		unsigned size;
	} s;
	Align x;
};

typedef union header Header;

static Header base;
static Header *freep = NULL;

#define NALLOC 4096-sizeof(Header)

void free(void *ap)
{
	Header *bp, *p;

	bp = (Header *)ap - 1; /* point to block header */
	for (p = freep; !(bp > p && bp < p->s.ptr); p = p->s.ptr)
		if (p >= p->s.ptr && (bp > p || bp < p->s.ptr))
			break; /* freed block at start or end of arena */

	if (bp + bp->s.size == p->s.ptr) { /* join to upper nbr */
		bp->s.size += p->s.ptr->s.size;
		bp->s.ptr = p->s.ptr->s.ptr;
	} else
		bp->s.ptr = p->s.ptr;
	if (p + p->s.size == bp) { /* join to lower nbr */
		p->s.size += bp->s.size;
		p->s.ptr = bp->s.ptr;
	} else
		p->s.ptr = bp;
	freep = p;
}

/* Maybe it would be better to allocate in more than units of pages? */
static Header *morecore(unsigned nu)
{
	Header *cp;
	Header *up;

	if (nu <= NALLOC)
		nu = NALLOC;
	else
		return NULL;

	cp = (void*) first_frame();
	/* first_frame() can't fail. */
	up = cp;
	up->s.size = nu;
	free((void *)(up+1));
	return freep;
}

void *kmalloc(unsigned int nbytes)
{
	Header *p, *prevp;
	unsigned nunits;

	nunits = (nbytes+sizeof(Header)-1)/sizeof(Header) +1;
	if ((prevp = freep) == NULL) {
		base.s.ptr = freep = prevp = &base;
		base.s.size = 0;
	}
	for (p = prevp->s.ptr; ; prevp = p, p = p->s.ptr) {
		if (p->s.size >= nunits) { /* big enough */
			if (p->s.size == nunits) /* exactly */
				prevp->s.ptr = p->s.ptr;
			else { /* allocate tail end */
				p->s.size -= nunits;
				p += p->s.size;<commit_msg>Cleaned up one warning in memory.c<commit_after>#include <metodo/metodo.h>

unsigned int placement = 0;

/* malloc(size, flags)
 * Simple placement based allocator. This allocates off the end of the kernel.
 * Potential bugs: This has no bound, and does not check with the final heap allocator
 */
void *kmalloc_int(unsigned int size, unsigned int flags)
{
	// FIXME: It seems "tmp" (and not "placement") may be what was meant to be returned, but that results in a triple fault. What gives?
	unsigned int tmp;

	// Initialize if needed.
	if (placement == 0)
		placement = (unsigned int)end;

	// Align on a page if needed.
/* The following line was this, but meteger said to invert the bitmask in the check:
 *	if ((flags & MALLOC_ALIGN) && (placement & 0xfffff000)) {
*/
	if ((flags & MALLOC_ALIGN) && ((placement) & 0x0fff)) {
		placement &= 0xfffff000;
		placement += 0x1000;
	}

	// Return the address of allocation and increment the next placement address to whatever.
	tmp = placement;
	placement += size;
	//return (void*) placement;
	return (void*) tmp;
}

typedef long Align;

union header {
	struct {
		union header *ptr;
		unsigned size;
	} s;
	Align x;
};

typedef union header Header;

static Header base;
static Header *freep = NULL;

#define NALLOC 4096-sizeof(Header)

void free(void *ap)
{
	Header *bp, *p;

	bp = (Header *)ap - 1; /* point to block header */
	for (p = freep; !(bp > p && bp < p->s.ptr); p = p->s.ptr)
		if (p >= p->s.ptr && (bp > p || bp < p->s.ptr))
			break; /* freed block at start or end of arena */

	if (bp + bp->s.size == p->s.ptr) { /* join to upper nbr */
		bp->s.size += p->s.ptr->s.size;
		bp->s.ptr = p->s.ptr->s.ptr;
	} else
		bp->s.ptr = p->s.ptr;
	if (p + p->s.size == bp) { /* join to lower nbr */
		p->s.size += bp->s.size;
		p->s.ptr = bp->s.ptr;
	} else
		p->s.ptr = bp;
	freep = p;
}

/* Maybe it would be better to allocate in more than units of pages? */
static Header *morecore(unsigned nu)
{
	Header *cp;
	Header *up;
	unsigned int tmp;

	if (nu <= NALLOC)
		nu = NALLOC;
	else
		return NULL;

	tmp = first_frame();
	cp = (void*)tmp;
	/* first_frame() can't fail. */
	up = cp;
	up->s.size = nu;
	free((void *)(up+1));
	return freep;
}

void *kmalloc(unsigned int nbytes)
{
	Header *p, *prevp;
	unsigned nunits;

	nunits = (nbytes+sizeof(Header)-1)/sizeof(Header) +1;
	if ((prevp = freep) == NULL) {
		base.s.ptr = freep = prevp = &base;
		base.s.size = 0;
	}
	for (p = prevp->s.ptr; ; prevp = p, p = p->s.ptr) {
		if (p->s.size >= nunits) { /* big enough */
			if (p->s.size == nunits) /* exactly */
				prevp->s.ptr = p->s.ptr;
			else { /* allocate tail end */
				p->s.size -= nunits;
				p += p->s.size;
{'fraction_non_alphanumeric': 0.16587767468282522, 'fraction_numerical': 0.009846619958341223, 'mean_word_length': 2.9654654654654653, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 71, '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': '2338458', 'n_tokens_mistral': 2105, 'n_tokens_neox': 1963, 'n_words': 660}