Skip to content

core.base.libc

This module provides low-level C library operations for direct memory manipulation.

TIP

To use this module, you need to import it first: import("core.base.libc")

This module wraps standard C library functions for memory allocation, copying, and manipulation. It provides consistent interfaces that work with both standard Lua and LuaJIT with FFI.

WARNING

These functions operate at a low level and should be used with caution. Improper use can lead to memory leaks, crashes, or security vulnerabilities.

libc.malloc

  • Allocate memory

Function Prototype

API

lua
libc.malloc(size: <number>, opt?: <table>)

Parameter Description

ParameterDescription
sizeRequired. Size of memory to allocate in bytes
optOptional. Options table, supports:
- gc - If true, memory will be automatically garbage collected

Return Value

TypeDescription
userdataReturns a pointer to the allocated memory

Usage

lua
import("core.base.libc")

-- Allocate memory with garbage collection
local data = libc.malloc(1024, {gc = true})

-- Allocate memory without auto GC
local data = libc.malloc(1024)

libc.free

  • Free allocated memory

Function Prototype

API

lua
libc.free(data: <userdata>)

Parameter Description

ParameterDescription
dataRequired. Pointer to memory to free

Return Value

No return value

Usage

lua
import("core.base.libc")

local data = libc.malloc(1024)
-- ... use data ...
libc.free(data)

libc.memcpy

  • Copy memory

Function Prototype

API

lua
libc.memcpy(dst: <userdata>, src: <userdata>, size: <number>)

Parameter Description

ParameterDescription
dstRequired. Destination memory pointer
srcRequired. Source memory pointer
sizeRequired. Number of bytes to copy

Return Value

No return value

Usage

lua
import("core.base.libc")

local src = libc.malloc(100)
local dst = libc.malloc(100)
libc.memcpy(dst, src, 100)

libc.memmov

  • Move memory (handles overlapping regions)

Function Prototype

API

lua
libc.memmov(dst: <userdata>, src: <userdata>, size: <number>)

Parameter Description

ParameterDescription
dstRequired. Destination memory pointer
srcRequired. Source memory pointer
sizeRequired. Number of bytes to move

Return Value

No return value

Usage

lua
import("core.base.libc")

local data = libc.malloc(100)
libc.memmov(data, src, 100)

libc.memset

  • Set memory to a specific value

Function Prototype

API

lua
libc.memset(data: <userdata>, ch: <number>, size: <number>)

Parameter Description

ParameterDescription
dataRequired. Memory pointer to fill
chRequired. Byte value to set
sizeRequired. Number of bytes to set

Return Value

No return value

Usage

lua
import("core.base.libc")

local data = libc.malloc(1024)
libc.memset(data, 0, 1024)  -- Zero-initialize memory

libc.strndup

  • Duplicate a string with specified length

Function Prototype

API

lua
libc.strndup(s: <string>, n: <number>)

Parameter Description

ParameterDescription
sRequired. Source string
nRequired. Maximum number of characters to duplicate

Return Value

TypeDescription
stringReturns the duplicated string

Usage

lua
import("core.base.libc")

local str = "hello world"
local dup = libc.strndup(str, 5)  -- Returns "hello"

libc.byteof

  • Get a byte at a specific offset

Function Prototype

API

lua
libc.byteof(data: <userdata>, offset: <number>)

Parameter Description

ParameterDescription
dataRequired. Memory pointer
offsetRequired. Byte offset from start of memory

Return Value

TypeDescription
numberReturns the byte value at the offset

Usage

lua
import("core.base.libc")

local data = libc.malloc(100)
local byte = libc.byteof(data, 10)

libc.setbyte

  • Set a byte at a specific offset

Function Prototype

API

lua
libc.setbyte(data: <userdata>, offset: <number>, value: <number>)

Parameter Description

ParameterDescription
dataRequired. Memory pointer
offsetRequired. Byte offset from start of memory
valueRequired. Byte value to set

Return Value

No return value

Usage

lua
import("core.base.libc")

local data = libc.malloc(100)
libc.setbyte(data, 10, 255)

libc.dataptr

  • Get data pointer

Function Prototype

API

lua
libc.dataptr(data: <any>, opt?: <table>)

Parameter Description

ParameterDescription
dataRequired. Data to get pointer for
optOptional. Options table, supports:
- ffi - Use FFI if available (default: true)
- gc - Enable garbage collection

Return Value

TypeDescription
userdataReturns a pointer to the data

Usage

lua
import("core.base.libc")

local ptr = libc.dataptr(data, {ffi = true, gc = true})

libc.ptraddr

  • Get pointer address as a number

Function Prototype

API

lua
libc.ptraddr(data: <userdata>, opt?: <table>)

Parameter Description

ParameterDescription
dataRequired. Pointer or data
optOptional. Options table, supports:
- ffi - Use FFI if available (default: true)

Return Value

TypeDescription
numberReturns the pointer address as a number

Usage

lua
import("core.base.libc")

local data = libc.malloc(1024)
local addr = libc.ptraddr(data)
print("Pointer address:", addr)