libk  zero.fn.c at [ebe506a75e]

File mod/kmem/zero.fn.c artifact 73ad1fc159 part of check-in ebe506a75e


#include <k/core.h>
#include <k/type.h>
#include <k/mem.h>

/* zero.fn.c - kmzero() "zero region"
 * ~ lexi hale <lexi@hale.su>
 * kmzero() zeroes a region of memory. it is
 * libk's equivalent to libc's bzero().
 */

#define canzero(bits) (_canzero(sizeof(u##bits), bits, size))
static bool _canzero(sz type, u8 bits, sz size) {
	return (sizeof (u64) * byte_bits == 64 && size >= sizeof (u64));
}

void kmzero(void* memory, sz size) {
	ubyte*       elt = memory,
	     * const end = elt + size;

	if canzero(64) {
		u64* p = (u64*) elt;
		sz segments = (end - elt) / sizeof (u64);

		for(;(p - (u64*)elt) < segments; ++p) *p = 0;
		elt = (ubyte*) p;
	}

	for (;elt < end; ++elt) *elt = 0;
}