Ctrl K
ComunidadGuías…

vlib/dlmalloc/global.v

Rama master · réplica de lectura del repositorio original.

Archivo original ↗Preguntar a KiBot
@[has_globals]
module dlmalloc

__global global = new(get_system_allocator())

// malloc allocates `size` bytes.
// It returns a null pointer if allocation fails.
// It returns a valid pointer otherwise.
@[unsafe]
pub fn malloc(size uint) voidptr {
	unsafe {
		return global.malloc(size)
	}
}

// free deallocates a `ptr`.
@[unsafe]
pub fn free(ptr voidptr) {
	unsafe {
		global.free_(ptr)
	}
}

// Same as `malloc`, except if the allocation succeeds it's guaranteed to
// point to `size` bytes of zeros.
@[unsafe]
pub fn calloc(size uint) voidptr {
	unsafe {
		return global.calloc(size)
	}
}

// realloc reallocates `ptr`, a previous allocation with `old_size` and
// to have `new_size`.
//
//
// Returns a null pointer if the memory couldn't be reallocated, but `ptr`
// is still valid. Returns a valid pointer and frees `ptr` if the request
// is satisfied.
@[unsafe]
pub fn realloc(ptr voidptr, oldsize uint, newsize uint) voidptr {
	unsafe {
		_ := oldsize

		return global.realloc(ptr, newsize)
	}
}

// memalign allocates `size` bytes with `align` align.
//
//
// Returns a null pointer if allocation fails. Returns a valid pointer otherwise.
@[unsafe]
pub fn memalign(size uint, align uint) voidptr {
	unsafe {
		if align <= malloc_alignment() {
			return global.malloc(size)
		} else {
			return global.memalign(align, size)
		}
	}
}